blob: 7615312ac31a3447f3a2521fe5bb9f295f11e5b0 [file] [log] [blame]
Filip Varga18f1e412020-12-03 15:27:40 +01001#!/usr/bin/env python3
2
3import unittest
4from io import BytesIO
Klement Sekeraad3187f2022-02-18 10:34:35 +00005from random import randint, choice
Filip Varga18f1e412020-12-03 15:27:40 +01006
Dmitry Valter6b97c432022-12-09 19:34:22 +00007import re
Filip Varga18f1e412020-12-03 15:27:40 +01008import scapy.compat
Dave Wallace8800f732023-08-31 00:47:44 -04009from framework import VppTestCase, VppLoInterface
10from asfframework import VppTestRunner, tag_fixme_ubuntu2204, is_distro_ubuntu2204
Filip Varga18f1e412020-12-03 15:27:40 +010011from scapy.data import IP_PROTOS
12from scapy.layers.inet import IP, TCP, UDP, ICMP, GRE
13from scapy.layers.inet import IPerror, TCPerror
14from scapy.layers.l2 import Ether
15from scapy.packet import Raw
Vladislav Grishenko579a6fb2023-03-16 19:31:00 +050016from statistics import variance
Filip Varga18f1e412020-12-03 15:27:40 +010017from syslog_rfc5424_parser import SyslogMessage, ParseError
18from syslog_rfc5424_parser.constants import SyslogSeverity
Klement Sekerac2feb652022-03-08 20:13:57 +010019from util import ppp, pr, ip4_range
Filip Varga18f1e412020-12-03 15:27:40 +010020from vpp_acl import AclRule, VppAcl, VppAclInterface
21from vpp_ip_route import VppIpRoute, VppRoutePath
22from vpp_papi import VppEnum
Klement Sekeraad3187f2022-02-18 10:34:35 +000023from util import StatsDiff
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000024from config import config
Filip Varga18f1e412020-12-03 15:27:40 +010025
26
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000027@unittest.skipIf("nat" in config.excluded_plugins, "Exclude NAT plugin tests")
Klement Sekeraff334db2021-05-26 13:02:35 +020028class TestNAT44ED(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020029 """NAT44ED Test Case"""
Filip Varga18f1e412020-12-03 15:27:40 +010030
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020031 nat_addr = "10.0.10.3"
Filip Varga18f1e412020-12-03 15:27:40 +010032
33 tcp_port_in = 6303
34 tcp_port_out = 6303
35
36 udp_port_in = 6304
37 udp_port_out = 6304
38
39 icmp_id_in = 6305
40 icmp_id_out = 6305
41
42 tcp_external_port = 80
43
44 max_sessions = 100
45
46 def setUp(self):
Klement Sekeraff334db2021-05-26 13:02:35 +020047 super().setUp()
Filip Varga18f1e412020-12-03 15:27:40 +010048 self.plugin_enable()
49
50 def tearDown(self):
Klement Sekeraff334db2021-05-26 13:02:35 +020051 super().tearDown()
Filip Varga18f1e412020-12-03 15:27:40 +010052 if not self.vpp_dead:
53 self.plugin_disable()
54
Dmitry Valter6b97c432022-12-09 19:34:22 +000055 def plugin_enable(self, max_sessions=None):
56 max_sessions = max_sessions or self.max_sessions
57 self.vapi.nat44_ed_plugin_enable_disable(sessions=max_sessions, enable=1)
Filip Varga18f1e412020-12-03 15:27:40 +010058
59 def plugin_disable(self):
Filip Vargae7a80a92021-02-26 09:31:21 +010060 self.vapi.nat44_ed_plugin_enable_disable(enable=0)
Filip Varga18f1e412020-12-03 15:27:40 +010061
62 @property
63 def config_flags(self):
64 return VppEnum.vl_api_nat_config_flags_t
65
66 @property
67 def nat44_config_flags(self):
68 return VppEnum.vl_api_nat44_config_flags_t
69
70 @property
71 def syslog_severity(self):
72 return VppEnum.vl_api_syslog_severity_t
73
74 @property
75 def server_addr(self):
76 return self.pg1.remote_hosts[0].ip4
77
78 @staticmethod
79 def random_port():
Vladislav Grishenko5f694d12022-08-19 20:42:22 +050080 return randint(1024, 65535)
Filip Varga18f1e412020-12-03 15:27:40 +010081
82 @staticmethod
83 def proto2layer(proto):
84 if proto == IP_PROTOS.tcp:
85 return TCP
86 elif proto == IP_PROTOS.udp:
87 return UDP
88 elif proto == IP_PROTOS.icmp:
89 return ICMP
90 else:
91 raise Exception("Unsupported protocol")
92
93 @classmethod
94 def create_and_add_ip4_table(cls, i, table_id=0):
BenoƮt Ganneff570d32024-04-16 09:36:05 +020095 cls.vapi.ip_table_add_del_v2(is_add=1, table={"table_id": table_id})
Filip Varga18f1e412020-12-03 15:27:40 +010096 i.set_table_ip4(table_id)
97
98 @classmethod
99 def configure_ip4_interface(cls, i, hosts=0, table_id=None):
100 if table_id:
101 cls.create_and_add_ip4_table(i, table_id)
102
103 i.admin_up()
Filip Varga18f1e412020-12-03 15:27:40 +0100104 i.config_ip4()
105 i.resolve_arp()
106
107 if hosts:
108 i.generate_remote_hosts(hosts)
109 i.configure_ipv4_neighbors()
110
111 @classmethod
112 def nat_add_interface_address(cls, i):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200113 cls.vapi.nat44_add_del_interface_addr(sw_if_index=i.sw_if_index, is_add=1)
Filip Varga18f1e412020-12-03 15:27:40 +0100114
115 def nat_add_inside_interface(self, i):
116 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200117 flags=self.config_flags.NAT_IS_INSIDE, sw_if_index=i.sw_if_index, is_add=1
118 )
Filip Varga18f1e412020-12-03 15:27:40 +0100119
120 def nat_add_outside_interface(self, i):
121 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200122 flags=self.config_flags.NAT_IS_OUTSIDE, sw_if_index=i.sw_if_index, is_add=1
123 )
Filip Varga18f1e412020-12-03 15:27:40 +0100124
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200125 def nat_add_address(self, address, twice_nat=0, vrf_id=0xFFFFFFFF, is_add=1):
Filip Varga18f1e412020-12-03 15:27:40 +0100126 flags = self.config_flags.NAT_IS_TWICE_NAT if twice_nat else 0
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200127 self.vapi.nat44_add_del_address_range(
128 first_ip_address=address,
129 last_ip_address=address,
130 vrf_id=vrf_id,
131 is_add=is_add,
132 flags=flags,
133 )
Filip Varga18f1e412020-12-03 15:27:40 +0100134
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200135 def nat_add_static_mapping(
136 self,
137 local_ip,
138 external_ip="0.0.0.0",
139 local_port=0,
140 external_port=0,
141 vrf_id=0,
142 is_add=1,
143 external_sw_if_index=0xFFFFFFFF,
144 proto=0,
145 tag="",
146 flags=0,
147 ):
Filip Varga18f1e412020-12-03 15:27:40 +0100148 if not (local_port and external_port):
149 flags |= self.config_flags.NAT_IS_ADDR_ONLY
150
151 self.vapi.nat44_add_del_static_mapping(
152 is_add=is_add,
153 local_ip_address=local_ip,
154 external_ip_address=external_ip,
155 external_sw_if_index=external_sw_if_index,
156 local_port=local_port,
157 external_port=external_port,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200158 vrf_id=vrf_id,
159 protocol=proto,
Filip Varga18f1e412020-12-03 15:27:40 +0100160 flags=flags,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200161 tag=tag,
162 )
Filip Varga18f1e412020-12-03 15:27:40 +0100163
164 @classmethod
165 def setUpClass(cls):
Klement Sekeraff334db2021-05-26 13:02:35 +0200166 super().setUpClass()
Filip Varga18f1e412020-12-03 15:27:40 +0100167
168 cls.create_pg_interfaces(range(12))
169 cls.interfaces = list(cls.pg_interfaces[:4])
170
171 cls.create_and_add_ip4_table(cls.pg2, 10)
172
173 for i in cls.interfaces:
174 cls.configure_ip4_interface(i, hosts=3)
175
176 # test specific (test-multiple-vrf)
BenoƮt Ganneff570d32024-04-16 09:36:05 +0200177 cls.vapi.ip_table_add_del_v2(is_add=1, table={"table_id": 1})
Filip Varga18f1e412020-12-03 15:27:40 +0100178
179 # test specific (test-one-armed-nat44-static)
180 cls.pg4.generate_remote_hosts(2)
181 cls.pg4.config_ip4()
182 cls.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200183 sw_if_index=cls.pg4.sw_if_index, prefix="10.0.0.1/24"
184 )
Filip Varga18f1e412020-12-03 15:27:40 +0100185 cls.pg4.admin_up()
186 cls.pg4.resolve_arp()
187 cls.pg4._remote_hosts[1]._ip4 = cls.pg4._remote_hosts[0]._ip4
188 cls.pg4.resolve_arp()
189
190 # test specific interface (pg5)
191 cls.pg5._local_ip4 = "10.1.1.1"
192 cls.pg5._remote_hosts[0]._ip4 = "10.1.1.2"
193 cls.pg5.set_table_ip4(1)
194 cls.pg5.config_ip4()
195 cls.pg5.admin_up()
196 cls.pg5.resolve_arp()
197
198 # test specific interface (pg6)
199 cls.pg6._local_ip4 = "10.1.2.1"
200 cls.pg6._remote_hosts[0]._ip4 = "10.1.2.2"
201 cls.pg6.set_table_ip4(1)
202 cls.pg6.config_ip4()
203 cls.pg6.admin_up()
204 cls.pg6.resolve_arp()
205
206 rl = list()
207
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200208 rl.append(
209 VppIpRoute(
210 cls,
211 "0.0.0.0",
212 0,
213 [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=0)],
214 register=False,
215 table_id=1,
216 )
217 )
218 rl.append(
219 VppIpRoute(
220 cls,
221 "0.0.0.0",
222 0,
223 [VppRoutePath(cls.pg1.local_ip4, cls.pg1.sw_if_index)],
224 register=False,
225 )
226 )
227 rl.append(
228 VppIpRoute(
229 cls,
230 cls.pg5.remote_ip4,
231 32,
232 [VppRoutePath("0.0.0.0", cls.pg5.sw_if_index)],
233 register=False,
234 table_id=1,
235 )
236 )
237 rl.append(
238 VppIpRoute(
239 cls,
240 cls.pg6.remote_ip4,
241 32,
242 [VppRoutePath("0.0.0.0", cls.pg6.sw_if_index)],
243 register=False,
244 table_id=1,
245 )
246 )
247 rl.append(
248 VppIpRoute(
249 cls,
250 cls.pg6.remote_ip4,
251 16,
252 [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=1)],
253 register=False,
254 table_id=0,
255 )
256 )
Filip Varga18f1e412020-12-03 15:27:40 +0100257
258 for r in rl:
259 r.add_vpp_config()
260
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200261 cls.no_diff = StatsDiff(
262 {
263 pg.sw_if_index: {
264 "/nat44-ed/in2out/fastpath/tcp": 0,
265 "/nat44-ed/in2out/fastpath/udp": 0,
266 "/nat44-ed/in2out/fastpath/icmp": 0,
267 "/nat44-ed/in2out/fastpath/drops": 0,
268 "/nat44-ed/in2out/slowpath/tcp": 0,
269 "/nat44-ed/in2out/slowpath/udp": 0,
270 "/nat44-ed/in2out/slowpath/icmp": 0,
271 "/nat44-ed/in2out/slowpath/drops": 0,
272 "/nat44-ed/in2out/fastpath/tcp": 0,
273 "/nat44-ed/in2out/fastpath/udp": 0,
274 "/nat44-ed/in2out/fastpath/icmp": 0,
275 "/nat44-ed/in2out/fastpath/drops": 0,
276 "/nat44-ed/in2out/slowpath/tcp": 0,
277 "/nat44-ed/in2out/slowpath/udp": 0,
278 "/nat44-ed/in2out/slowpath/icmp": 0,
279 "/nat44-ed/in2out/slowpath/drops": 0,
280 }
281 for pg in cls.pg_interfaces
Klement Sekeraad3187f2022-02-18 10:34:35 +0000282 }
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200283 )
Klement Sekeraad3187f2022-02-18 10:34:35 +0000284
Filip Varga18f1e412020-12-03 15:27:40 +0100285 def get_err_counter(self, path):
286 return self.statistics.get_err_counter(path)
287
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200288 def reass_hairpinning(
289 self,
290 server_addr,
291 server_in_port,
292 server_out_port,
293 host_in_port,
294 proto=IP_PROTOS.tcp,
295 ignore_port=False,
296 ):
Filip Varga18f1e412020-12-03 15:27:40 +0100297 layer = self.proto2layer(proto)
298
299 if proto == IP_PROTOS.tcp:
300 data = b"A" * 4 + b"B" * 16 + b"C" * 3
301 else:
302 data = b"A" * 16 + b"B" * 16 + b"C" * 3
303
304 # send packet from host to server
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200305 pkts = self.create_stream_frag(
306 self.pg0, self.nat_addr, host_in_port, server_out_port, data, proto
307 )
Filip Varga18f1e412020-12-03 15:27:40 +0100308 self.pg0.add_stream(pkts)
309 self.pg_enable_capture(self.pg_interfaces)
310 self.pg_start()
311 frags = self.pg0.get_capture(len(pkts))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200312 p = self.reass_frags_and_verify(frags, self.nat_addr, server_addr)
Filip Varga18f1e412020-12-03 15:27:40 +0100313 if proto != IP_PROTOS.icmp:
314 if not ignore_port:
315 self.assertNotEqual(p[layer].sport, host_in_port)
316 self.assertEqual(p[layer].dport, server_in_port)
317 else:
318 if not ignore_port:
319 self.assertNotEqual(p[layer].id, host_in_port)
320 self.assertEqual(data, p[Raw].load)
321
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200322 def frag_out_of_order(
323 self, proto=IP_PROTOS.tcp, dont_translate=False, ignore_port=False
324 ):
Filip Varga18f1e412020-12-03 15:27:40 +0100325 layer = self.proto2layer(proto)
326
327 if proto == IP_PROTOS.tcp:
328 data = b"A" * 4 + b"B" * 16 + b"C" * 3
329 else:
330 data = b"A" * 16 + b"B" * 16 + b"C" * 3
331 self.port_in = self.random_port()
332
333 for i in range(2):
334 # in2out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200335 pkts = self.create_stream_frag(
336 self.pg0, self.pg1.remote_ip4, self.port_in, 20, data, proto
337 )
Filip Varga18f1e412020-12-03 15:27:40 +0100338 pkts.reverse()
339 self.pg0.add_stream(pkts)
340 self.pg_enable_capture(self.pg_interfaces)
341 self.pg_start()
342 frags = self.pg1.get_capture(len(pkts))
343 if not dont_translate:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200344 p = self.reass_frags_and_verify(
345 frags, self.nat_addr, self.pg1.remote_ip4
346 )
Filip Varga18f1e412020-12-03 15:27:40 +0100347 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200348 p = self.reass_frags_and_verify(
349 frags, self.pg0.remote_ip4, self.pg1.remote_ip4
350 )
Filip Varga18f1e412020-12-03 15:27:40 +0100351 if proto != IP_PROTOS.icmp:
352 if not dont_translate:
353 self.assertEqual(p[layer].dport, 20)
354 if not ignore_port:
355 self.assertNotEqual(p[layer].sport, self.port_in)
356 else:
357 self.assertEqual(p[layer].sport, self.port_in)
358 else:
359 if not ignore_port:
360 if not dont_translate:
361 self.assertNotEqual(p[layer].id, self.port_in)
362 else:
363 self.assertEqual(p[layer].id, self.port_in)
364 self.assertEqual(data, p[Raw].load)
365
366 # out2in
367 if not dont_translate:
368 dst_addr = self.nat_addr
369 else:
370 dst_addr = self.pg0.remote_ip4
371 if proto != IP_PROTOS.icmp:
372 sport = 20
373 dport = p[layer].sport
374 else:
375 sport = p[layer].id
376 dport = 0
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200377 pkts = self.create_stream_frag(
378 self.pg1, dst_addr, sport, dport, data, proto, echo_reply=True
379 )
Filip Varga18f1e412020-12-03 15:27:40 +0100380 pkts.reverse()
381 self.pg1.add_stream(pkts)
382 self.pg_enable_capture(self.pg_interfaces)
Klement Sekerae79bbe92021-03-04 18:41:02 +0100383 self.logger.info(self.vapi.cli("show trace"))
Filip Varga18f1e412020-12-03 15:27:40 +0100384 self.pg_start()
385 frags = self.pg0.get_capture(len(pkts))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200386 p = self.reass_frags_and_verify(
387 frags, self.pg1.remote_ip4, self.pg0.remote_ip4
388 )
Filip Varga18f1e412020-12-03 15:27:40 +0100389 if proto != IP_PROTOS.icmp:
390 self.assertEqual(p[layer].sport, 20)
391 self.assertEqual(p[layer].dport, self.port_in)
392 else:
393 self.assertEqual(p[layer].id, self.port_in)
394 self.assertEqual(data, p[Raw].load)
395
396 def reass_frags_and_verify(self, frags, src, dst):
397 buffer = BytesIO()
398 for p in frags:
399 self.assertEqual(p[IP].src, src)
400 self.assertEqual(p[IP].dst, dst)
401 self.assert_ip_checksum_valid(p)
402 buffer.seek(p[IP].frag * 8)
403 buffer.write(bytes(p[IP].payload))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200404 ip = IP(src=frags[0][IP].src, dst=frags[0][IP].dst, proto=frags[0][IP].proto)
Filip Varga18f1e412020-12-03 15:27:40 +0100405 if ip.proto == IP_PROTOS.tcp:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200406 p = ip / TCP(buffer.getvalue())
Filip Varga18f1e412020-12-03 15:27:40 +0100407 self.logger.debug(ppp("Reassembled:", p))
408 self.assert_tcp_checksum_valid(p)
409 elif ip.proto == IP_PROTOS.udp:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200410 p = ip / UDP(buffer.getvalue()[:8]) / Raw(buffer.getvalue()[8:])
Filip Varga18f1e412020-12-03 15:27:40 +0100411 elif ip.proto == IP_PROTOS.icmp:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200412 p = ip / ICMP(buffer.getvalue())
Filip Varga18f1e412020-12-03 15:27:40 +0100413 return p
414
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200415 def frag_in_order(
416 self, proto=IP_PROTOS.tcp, dont_translate=False, ignore_port=False
417 ):
Filip Varga18f1e412020-12-03 15:27:40 +0100418 layer = self.proto2layer(proto)
419
420 if proto == IP_PROTOS.tcp:
421 data = b"A" * 4 + b"B" * 16 + b"C" * 3
422 else:
423 data = b"A" * 16 + b"B" * 16 + b"C" * 3
424 self.port_in = self.random_port()
425
426 # in2out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200427 pkts = self.create_stream_frag(
428 self.pg0, self.pg1.remote_ip4, self.port_in, 20, data, proto
429 )
Filip Varga18f1e412020-12-03 15:27:40 +0100430 self.pg0.add_stream(pkts)
431 self.pg_enable_capture(self.pg_interfaces)
432 self.pg_start()
433 frags = self.pg1.get_capture(len(pkts))
434 if not dont_translate:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200435 p = self.reass_frags_and_verify(frags, self.nat_addr, self.pg1.remote_ip4)
Filip Varga18f1e412020-12-03 15:27:40 +0100436 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200437 p = self.reass_frags_and_verify(
438 frags, self.pg0.remote_ip4, self.pg1.remote_ip4
439 )
Filip Varga18f1e412020-12-03 15:27:40 +0100440 if proto != IP_PROTOS.icmp:
441 if not dont_translate:
442 self.assertEqual(p[layer].dport, 20)
443 if not ignore_port:
444 self.assertNotEqual(p[layer].sport, self.port_in)
445 else:
446 self.assertEqual(p[layer].sport, self.port_in)
447 else:
448 if not ignore_port:
449 if not dont_translate:
450 self.assertNotEqual(p[layer].id, self.port_in)
451 else:
452 self.assertEqual(p[layer].id, self.port_in)
453 self.assertEqual(data, p[Raw].load)
454
455 # out2in
456 if not dont_translate:
457 dst_addr = self.nat_addr
458 else:
459 dst_addr = self.pg0.remote_ip4
460 if proto != IP_PROTOS.icmp:
461 sport = 20
462 dport = p[layer].sport
463 else:
464 sport = p[layer].id
465 dport = 0
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200466 pkts = self.create_stream_frag(
467 self.pg1, dst_addr, sport, dport, data, proto, echo_reply=True
468 )
Filip Varga18f1e412020-12-03 15:27:40 +0100469 self.pg1.add_stream(pkts)
470 self.pg_enable_capture(self.pg_interfaces)
471 self.pg_start()
472 frags = self.pg0.get_capture(len(pkts))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200473 p = self.reass_frags_and_verify(frags, self.pg1.remote_ip4, self.pg0.remote_ip4)
Filip Varga18f1e412020-12-03 15:27:40 +0100474 if proto != IP_PROTOS.icmp:
475 self.assertEqual(p[layer].sport, 20)
476 self.assertEqual(p[layer].dport, self.port_in)
477 else:
478 self.assertEqual(p[layer].id, self.port_in)
479 self.assertEqual(data, p[Raw].load)
480
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200481 def verify_capture_out(
482 self, capture, nat_ip=None, same_port=False, dst_ip=None, ignore_port=False
483 ):
Filip Varga18f1e412020-12-03 15:27:40 +0100484 if nat_ip is None:
485 nat_ip = self.nat_addr
486 for packet in capture:
487 try:
488 self.assert_packet_checksums_valid(packet)
489 self.assertEqual(packet[IP].src, nat_ip)
490 if dst_ip is not None:
491 self.assertEqual(packet[IP].dst, dst_ip)
492 if packet.haslayer(TCP):
493 if not ignore_port:
494 if same_port:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200495 self.assertEqual(packet[TCP].sport, self.tcp_port_in)
Filip Varga18f1e412020-12-03 15:27:40 +0100496 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200497 self.assertNotEqual(packet[TCP].sport, self.tcp_port_in)
Filip Varga18f1e412020-12-03 15:27:40 +0100498 self.tcp_port_out = packet[TCP].sport
499 self.assert_packet_checksums_valid(packet)
500 elif packet.haslayer(UDP):
501 if not ignore_port:
502 if same_port:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200503 self.assertEqual(packet[UDP].sport, self.udp_port_in)
Filip Varga18f1e412020-12-03 15:27:40 +0100504 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200505 self.assertNotEqual(packet[UDP].sport, self.udp_port_in)
Filip Varga18f1e412020-12-03 15:27:40 +0100506 self.udp_port_out = packet[UDP].sport
507 else:
508 if not ignore_port:
509 if same_port:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200510 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
Filip Varga18f1e412020-12-03 15:27:40 +0100511 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200512 self.assertNotEqual(packet[ICMP].id, self.icmp_id_in)
Filip Varga18f1e412020-12-03 15:27:40 +0100513 self.icmp_id_out = packet[ICMP].id
514 self.assert_packet_checksums_valid(packet)
515 except:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200516 self.logger.error(
517 ppp("Unexpected or invalid packet (outside network):", packet)
518 )
Filip Varga18f1e412020-12-03 15:27:40 +0100519 raise
520
521 def verify_capture_in(self, capture, in_if):
522 for packet in capture:
523 try:
524 self.assert_packet_checksums_valid(packet)
525 self.assertEqual(packet[IP].dst, in_if.remote_ip4)
526 if packet.haslayer(TCP):
527 self.assertEqual(packet[TCP].dport, self.tcp_port_in)
528 elif packet.haslayer(UDP):
529 self.assertEqual(packet[UDP].dport, self.udp_port_in)
530 else:
531 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
532 except:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200533 self.logger.error(
534 ppp("Unexpected or invalid packet (inside network):", packet)
535 )
Filip Varga18f1e412020-12-03 15:27:40 +0100536 raise
537
538 def create_stream_in(self, in_if, out_if, dst_ip=None, ttl=64):
539 if dst_ip is None:
540 dst_ip = out_if.remote_ip4
541
542 pkts = []
543 # TCP
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200544 p = (
545 Ether(dst=in_if.local_mac, src=in_if.remote_mac)
546 / IP(src=in_if.remote_ip4, dst=dst_ip, ttl=ttl)
547 / TCP(sport=self.tcp_port_in, dport=20)
548 )
Filip Varga18f1e412020-12-03 15:27:40 +0100549 pkts.extend([p, p])
550
551 # UDP
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200552 p = (
553 Ether(dst=in_if.local_mac, src=in_if.remote_mac)
554 / IP(src=in_if.remote_ip4, dst=dst_ip, ttl=ttl)
555 / UDP(sport=self.udp_port_in, dport=20)
556 )
Filip Varga18f1e412020-12-03 15:27:40 +0100557 pkts.append(p)
558
559 # ICMP
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200560 p = (
561 Ether(dst=in_if.local_mac, src=in_if.remote_mac)
562 / IP(src=in_if.remote_ip4, dst=dst_ip, ttl=ttl)
563 / ICMP(id=self.icmp_id_in, type="echo-request")
564 )
Filip Varga18f1e412020-12-03 15:27:40 +0100565 pkts.append(p)
566
567 return pkts
568
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200569 def create_stream_out(self, out_if, dst_ip=None, ttl=64, use_inside_ports=False):
Filip Varga18f1e412020-12-03 15:27:40 +0100570 if dst_ip is None:
571 dst_ip = self.nat_addr
572 if not use_inside_ports:
573 tcp_port = self.tcp_port_out
574 udp_port = self.udp_port_out
575 icmp_id = self.icmp_id_out
576 else:
577 tcp_port = self.tcp_port_in
578 udp_port = self.udp_port_in
579 icmp_id = self.icmp_id_in
580 pkts = []
581 # TCP
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200582 p = (
583 Ether(dst=out_if.local_mac, src=out_if.remote_mac)
584 / IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl)
585 / TCP(dport=tcp_port, sport=20)
586 )
Filip Varga18f1e412020-12-03 15:27:40 +0100587 pkts.extend([p, p])
588
589 # UDP
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200590 p = (
591 Ether(dst=out_if.local_mac, src=out_if.remote_mac)
592 / IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl)
593 / UDP(dport=udp_port, sport=20)
594 )
Filip Varga18f1e412020-12-03 15:27:40 +0100595 pkts.append(p)
596
597 # ICMP
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200598 p = (
599 Ether(dst=out_if.local_mac, src=out_if.remote_mac)
600 / IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl)
601 / ICMP(id=icmp_id, type="echo-reply")
602 )
Filip Varga18f1e412020-12-03 15:27:40 +0100603 pkts.append(p)
604
605 return pkts
606
607 def create_tcp_stream(self, in_if, out_if, count):
608 pkts = []
609 port = 6303
610
611 for i in range(count):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200612 p = (
613 Ether(dst=in_if.local_mac, src=in_if.remote_mac)
614 / IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=64)
615 / TCP(sport=port + i, dport=20)
616 )
Filip Varga18f1e412020-12-03 15:27:40 +0100617 pkts.append(p)
618
619 return pkts
620
Dmitry Valter6b97c432022-12-09 19:34:22 +0000621 def create_udp_stream(self, in_if, out_if, count, base_port=6303):
622 return [
623 (
624 Ether(dst=in_if.local_mac, src=in_if.remote_mac)
625 / IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=64)
626 / UDP(sport=base_port + i, dport=20)
627 )
628 for i in range(count)
629 ]
630
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200631 def create_stream_frag(
632 self, src_if, dst, sport, dport, data, proto=IP_PROTOS.tcp, echo_reply=False
633 ):
Filip Varga18f1e412020-12-03 15:27:40 +0100634 if proto == IP_PROTOS.tcp:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200635 p = (
636 IP(src=src_if.remote_ip4, dst=dst)
637 / TCP(sport=sport, dport=dport)
638 / Raw(data)
639 )
Filip Varga18f1e412020-12-03 15:27:40 +0100640 p = p.__class__(scapy.compat.raw(p))
641 chksum = p[TCP].chksum
642 proto_header = TCP(sport=sport, dport=dport, chksum=chksum)
643 elif proto == IP_PROTOS.udp:
644 proto_header = UDP(sport=sport, dport=dport)
645 elif proto == IP_PROTOS.icmp:
646 if not echo_reply:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200647 proto_header = ICMP(id=sport, type="echo-request")
Filip Varga18f1e412020-12-03 15:27:40 +0100648 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200649 proto_header = ICMP(id=sport, type="echo-reply")
Filip Varga18f1e412020-12-03 15:27:40 +0100650 else:
651 raise Exception("Unsupported protocol")
652 id = self.random_port()
653 pkts = []
654 if proto == IP_PROTOS.tcp:
655 raw = Raw(data[0:4])
656 else:
657 raw = Raw(data[0:16])
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200658 p = (
659 Ether(src=src_if.remote_mac, dst=src_if.local_mac)
660 / IP(src=src_if.remote_ip4, dst=dst, flags="MF", frag=0, id=id)
661 / proto_header
662 / raw
663 )
Filip Varga18f1e412020-12-03 15:27:40 +0100664 pkts.append(p)
665 if proto == IP_PROTOS.tcp:
666 raw = Raw(data[4:20])
667 else:
668 raw = Raw(data[16:32])
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200669 p = (
670 Ether(src=src_if.remote_mac, dst=src_if.local_mac)
671 / IP(src=src_if.remote_ip4, dst=dst, flags="MF", frag=3, id=id, proto=proto)
672 / raw
673 )
Filip Varga18f1e412020-12-03 15:27:40 +0100674 pkts.append(p)
675 if proto == IP_PROTOS.tcp:
676 raw = Raw(data[20:])
677 else:
678 raw = Raw(data[32:])
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200679 p = (
680 Ether(src=src_if.remote_mac, dst=src_if.local_mac)
681 / IP(src=src_if.remote_ip4, dst=dst, frag=5, proto=proto, id=id)
682 / raw
683 )
Filip Varga18f1e412020-12-03 15:27:40 +0100684 pkts.append(p)
685 return pkts
686
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200687 def frag_in_order_in_plus_out(
688 self, in_addr, out_addr, in_port, out_port, proto=IP_PROTOS.tcp
689 ):
Filip Varga18f1e412020-12-03 15:27:40 +0100690 layer = self.proto2layer(proto)
691
692 if proto == IP_PROTOS.tcp:
693 data = b"A" * 4 + b"B" * 16 + b"C" * 3
694 else:
695 data = b"A" * 16 + b"B" * 16 + b"C" * 3
696 port_in = self.random_port()
697
698 for i in range(2):
699 # out2in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200700 pkts = self.create_stream_frag(
701 self.pg0, out_addr, port_in, out_port, data, proto
702 )
Filip Varga18f1e412020-12-03 15:27:40 +0100703 self.pg0.add_stream(pkts)
704 self.pg_enable_capture(self.pg_interfaces)
705 self.pg_start()
706 frags = self.pg1.get_capture(len(pkts))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200707 p = self.reass_frags_and_verify(frags, self.pg0.remote_ip4, in_addr)
Filip Varga18f1e412020-12-03 15:27:40 +0100708 if proto != IP_PROTOS.icmp:
709 self.assertEqual(p[layer].sport, port_in)
710 self.assertEqual(p[layer].dport, in_port)
711 else:
712 self.assertEqual(p[layer].id, port_in)
713 self.assertEqual(data, p[Raw].load)
714
715 # in2out
716 if proto != IP_PROTOS.icmp:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200717 pkts = self.create_stream_frag(
718 self.pg1, self.pg0.remote_ip4, in_port, p[layer].sport, data, proto
719 )
Filip Varga18f1e412020-12-03 15:27:40 +0100720 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200721 pkts = self.create_stream_frag(
722 self.pg1,
723 self.pg0.remote_ip4,
724 p[layer].id,
725 0,
726 data,
727 proto,
728 echo_reply=True,
729 )
Filip Varga18f1e412020-12-03 15:27:40 +0100730 self.pg1.add_stream(pkts)
731 self.pg_enable_capture(self.pg_interfaces)
732 self.pg_start()
733 frags = self.pg0.get_capture(len(pkts))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200734 p = self.reass_frags_and_verify(frags, out_addr, self.pg0.remote_ip4)
Filip Varga18f1e412020-12-03 15:27:40 +0100735 if proto != IP_PROTOS.icmp:
736 self.assertEqual(p[layer].sport, out_port)
737 self.assertEqual(p[layer].dport, port_in)
738 else:
739 self.assertEqual(p[layer].id, port_in)
740 self.assertEqual(data, p[Raw].load)
741
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200742 def frag_out_of_order_in_plus_out(
743 self, in_addr, out_addr, in_port, out_port, proto=IP_PROTOS.tcp
744 ):
Filip Varga18f1e412020-12-03 15:27:40 +0100745 layer = self.proto2layer(proto)
746
747 if proto == IP_PROTOS.tcp:
748 data = b"A" * 4 + b"B" * 16 + b"C" * 3
749 else:
750 data = b"A" * 16 + b"B" * 16 + b"C" * 3
751 port_in = self.random_port()
752
753 for i in range(2):
754 # out2in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200755 pkts = self.create_stream_frag(
756 self.pg0, out_addr, port_in, out_port, data, proto
757 )
Filip Varga18f1e412020-12-03 15:27:40 +0100758 pkts.reverse()
759 self.pg0.add_stream(pkts)
760 self.pg_enable_capture(self.pg_interfaces)
761 self.pg_start()
762 frags = self.pg1.get_capture(len(pkts))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200763 p = self.reass_frags_and_verify(frags, self.pg0.remote_ip4, in_addr)
Filip Varga18f1e412020-12-03 15:27:40 +0100764 if proto != IP_PROTOS.icmp:
765 self.assertEqual(p[layer].dport, in_port)
766 self.assertEqual(p[layer].sport, port_in)
767 self.assertEqual(p[layer].dport, in_port)
768 else:
769 self.assertEqual(p[layer].id, port_in)
770 self.assertEqual(data, p[Raw].load)
771
772 # in2out
773 if proto != IP_PROTOS.icmp:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200774 pkts = self.create_stream_frag(
775 self.pg1, self.pg0.remote_ip4, in_port, p[layer].sport, data, proto
776 )
Filip Varga18f1e412020-12-03 15:27:40 +0100777 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200778 pkts = self.create_stream_frag(
779 self.pg1,
780 self.pg0.remote_ip4,
781 p[layer].id,
782 0,
783 data,
784 proto,
785 echo_reply=True,
786 )
Filip Varga18f1e412020-12-03 15:27:40 +0100787 pkts.reverse()
788 self.pg1.add_stream(pkts)
789 self.pg_enable_capture(self.pg_interfaces)
790 self.pg_start()
791 frags = self.pg0.get_capture(len(pkts))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200792 p = self.reass_frags_and_verify(frags, out_addr, self.pg0.remote_ip4)
Filip Varga18f1e412020-12-03 15:27:40 +0100793 if proto != IP_PROTOS.icmp:
794 self.assertEqual(p[layer].sport, out_port)
795 self.assertEqual(p[layer].dport, port_in)
796 else:
797 self.assertEqual(p[layer].id, port_in)
798 self.assertEqual(data, p[Raw].load)
799
800 def init_tcp_session(self, in_if, out_if, in_port, ext_port):
801 # SYN packet in->out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200802 p = (
803 Ether(src=in_if.remote_mac, dst=in_if.local_mac)
804 / IP(src=in_if.remote_ip4, dst=out_if.remote_ip4)
805 / TCP(sport=in_port, dport=ext_port, flags="S")
806 )
Filip Varga18f1e412020-12-03 15:27:40 +0100807 in_if.add_stream(p)
808 self.pg_enable_capture(self.pg_interfaces)
809 self.pg_start()
810 capture = out_if.get_capture(1)
811 p = capture[0]
812 out_port = p[TCP].sport
813
814 # SYN + ACK packet out->in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200815 p = (
816 Ether(src=out_if.remote_mac, dst=out_if.local_mac)
817 / IP(src=out_if.remote_ip4, dst=self.nat_addr)
818 / TCP(sport=ext_port, dport=out_port, flags="SA")
819 )
Filip Varga18f1e412020-12-03 15:27:40 +0100820 out_if.add_stream(p)
821 self.pg_enable_capture(self.pg_interfaces)
822 self.pg_start()
823 in_if.get_capture(1)
824
825 # ACK packet in->out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200826 p = (
827 Ether(src=in_if.remote_mac, dst=in_if.local_mac)
828 / IP(src=in_if.remote_ip4, dst=out_if.remote_ip4)
829 / TCP(sport=in_port, dport=ext_port, flags="A")
830 )
Filip Varga18f1e412020-12-03 15:27:40 +0100831 in_if.add_stream(p)
832 self.pg_enable_capture(self.pg_interfaces)
833 self.pg_start()
834 out_if.get_capture(1)
835
836 return out_port
837
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200838 def twice_nat_common(
839 self, self_twice_nat=False, same_pg=False, lb=False, client_id=None
840 ):
841 twice_nat_addr = "10.0.1.3"
Filip Varga18f1e412020-12-03 15:27:40 +0100842
843 port_in = 8080
844 if lb:
845 if not same_pg:
846 port_in1 = port_in
847 port_in2 = port_in
848 else:
849 port_in1 = port_in + 1
850 port_in2 = port_in + 2
851
852 port_out = 80
853 eh_port_out = 4567
854
855 server1 = self.pg0.remote_hosts[0]
856 server2 = self.pg0.remote_hosts[1]
857 if lb and same_pg:
858 server2 = server1
859 if not lb:
860 server = server1
861
862 pg0 = self.pg0
863 if same_pg:
864 pg1 = self.pg0
865 else:
866 pg1 = self.pg1
867
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200868 eh_translate = (not self_twice_nat) or (not lb and same_pg) or client_id == 1
Filip Varga18f1e412020-12-03 15:27:40 +0100869
870 self.nat_add_address(self.nat_addr)
871 self.nat_add_address(twice_nat_addr, twice_nat=1)
872
873 flags = 0
874 if self_twice_nat:
875 flags |= self.config_flags.NAT_IS_SELF_TWICE_NAT
876 else:
877 flags |= self.config_flags.NAT_IS_TWICE_NAT
878
879 if not lb:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200880 self.nat_add_static_mapping(
881 pg0.remote_ip4,
882 self.nat_addr,
883 port_in,
884 port_out,
885 proto=IP_PROTOS.tcp,
886 flags=flags,
887 )
Filip Varga18f1e412020-12-03 15:27:40 +0100888 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200889 locals = [
890 {"addr": server1.ip4, "port": port_in1, "probability": 50, "vrf_id": 0},
891 {"addr": server2.ip4, "port": port_in2, "probability": 50, "vrf_id": 0},
892 ]
Filip Varga18f1e412020-12-03 15:27:40 +0100893 out_addr = self.nat_addr
894
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200895 self.vapi.nat44_add_del_lb_static_mapping(
896 is_add=1,
897 flags=flags,
898 external_addr=out_addr,
899 external_port=port_out,
900 protocol=IP_PROTOS.tcp,
901 local_num=len(locals),
902 locals=locals,
903 )
Filip Varga18f1e412020-12-03 15:27:40 +0100904 self.nat_add_inside_interface(pg0)
905 self.nat_add_outside_interface(pg1)
906
907 if same_pg:
908 if not lb:
909 client = server
910 else:
911 assert client_id is not None
912 if client_id == 1:
913 client = self.pg0.remote_hosts[0]
914 elif client_id == 2:
915 client = self.pg0.remote_hosts[1]
916 else:
917 client = pg1.remote_hosts[0]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200918 p = (
919 Ether(src=pg1.remote_mac, dst=pg1.local_mac)
920 / IP(src=client.ip4, dst=self.nat_addr)
921 / TCP(sport=eh_port_out, dport=port_out)
922 )
Filip Varga18f1e412020-12-03 15:27:40 +0100923 pg1.add_stream(p)
924 self.pg_enable_capture(self.pg_interfaces)
925 self.pg_start()
926 capture = pg0.get_capture(1)
927 p = capture[0]
928 try:
929 ip = p[IP]
930 tcp = p[TCP]
931 if lb:
932 if ip.dst == server1.ip4:
933 server = server1
934 port_in = port_in1
935 else:
936 server = server2
937 port_in = port_in2
938 self.assertEqual(ip.dst, server.ip4)
939 if lb and same_pg:
940 self.assertIn(tcp.dport, [port_in1, port_in2])
941 else:
942 self.assertEqual(tcp.dport, port_in)
943 if eh_translate:
944 self.assertEqual(ip.src, twice_nat_addr)
945 self.assertNotEqual(tcp.sport, eh_port_out)
946 else:
947 self.assertEqual(ip.src, client.ip4)
948 self.assertEqual(tcp.sport, eh_port_out)
949 eh_addr_in = ip.src
950 eh_port_in = tcp.sport
951 saved_port_in = tcp.dport
952 self.assert_packet_checksums_valid(p)
953 except:
954 self.logger.error(ppp("Unexpected or invalid packet:", p))
955 raise
956
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200957 p = (
958 Ether(src=server.mac, dst=pg0.local_mac)
959 / IP(src=server.ip4, dst=eh_addr_in)
960 / TCP(sport=saved_port_in, dport=eh_port_in)
961 )
Filip Varga18f1e412020-12-03 15:27:40 +0100962 pg0.add_stream(p)
963 self.pg_enable_capture(self.pg_interfaces)
964 self.pg_start()
965 capture = pg1.get_capture(1)
966 p = capture[0]
967 try:
968 ip = p[IP]
969 tcp = p[TCP]
970 self.assertEqual(ip.dst, client.ip4)
971 self.assertEqual(ip.src, self.nat_addr)
972 self.assertEqual(tcp.dport, eh_port_out)
973 self.assertEqual(tcp.sport, port_out)
974 self.assert_packet_checksums_valid(p)
975 except:
976 self.logger.error(ppp("Unexpected or invalid packet:", p))
977 raise
978
979 if eh_translate:
980 sessions = self.vapi.nat44_user_session_dump(server.ip4, 0)
981 self.assertEqual(len(sessions), 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200982 self.assertTrue(sessions[0].flags & self.config_flags.NAT_IS_EXT_HOST_VALID)
983 self.assertTrue(sessions[0].flags & self.config_flags.NAT_IS_TWICE_NAT)
Filip Varga0eaf4e62021-02-17 14:34:54 +0100984 self.logger.info(self.vapi.cli("show nat44 sessions"))
Filip Varga18f1e412020-12-03 15:27:40 +0100985 self.vapi.nat44_del_session(
986 address=sessions[0].inside_ip_address,
987 port=sessions[0].inside_port,
988 protocol=sessions[0].protocol,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200989 flags=(
990 self.config_flags.NAT_IS_INSIDE
991 | self.config_flags.NAT_IS_EXT_HOST_VALID
992 ),
Filip Varga18f1e412020-12-03 15:27:40 +0100993 ext_host_address=sessions[0].ext_host_nat_address,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200994 ext_host_port=sessions[0].ext_host_nat_port,
995 )
Filip Varga18f1e412020-12-03 15:27:40 +0100996 sessions = self.vapi.nat44_user_session_dump(server.ip4, 0)
997 self.assertEqual(len(sessions), 0)
998
Klement Sekera56c492a2022-01-10 21:57:27 +0000999 def verify_syslog_sess(self, data, msgid, is_ip6=False):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001000 message = data.decode("utf-8")
Filip Varga18f1e412020-12-03 15:27:40 +01001001 try:
1002 message = SyslogMessage.parse(message)
1003 except ParseError as e:
1004 self.logger.error(e)
1005 raise
1006 else:
1007 self.assertEqual(message.severity, SyslogSeverity.info)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001008 self.assertEqual(message.appname, "NAT")
Klement Sekera56c492a2022-01-10 21:57:27 +00001009 self.assertEqual(message.msgid, msgid)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001010 sd_params = message.sd.get("nsess")
Filip Varga18f1e412020-12-03 15:27:40 +01001011 self.assertTrue(sd_params is not None)
1012 if is_ip6:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001013 self.assertEqual(sd_params.get("IATYP"), "IPv6")
1014 self.assertEqual(sd_params.get("ISADDR"), self.pg0.remote_ip6)
Filip Varga18f1e412020-12-03 15:27:40 +01001015 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001016 self.assertEqual(sd_params.get("IATYP"), "IPv4")
1017 self.assertEqual(sd_params.get("ISADDR"), self.pg0.remote_ip4)
1018 self.assertTrue(sd_params.get("SSUBIX") is not None)
1019 self.assertEqual(sd_params.get("ISPORT"), "%d" % self.tcp_port_in)
1020 self.assertEqual(sd_params.get("XATYP"), "IPv4")
1021 self.assertEqual(sd_params.get("XSADDR"), self.nat_addr)
1022 self.assertEqual(sd_params.get("XSPORT"), "%d" % self.tcp_port_out)
1023 self.assertEqual(sd_params.get("PROTO"), "%d" % IP_PROTOS.tcp)
1024 self.assertEqual(sd_params.get("SVLAN"), "0")
1025 self.assertEqual(sd_params.get("XDADDR"), self.pg1.remote_ip4)
1026 self.assertEqual(sd_params.get("XDPORT"), "%d" % self.tcp_external_port)
Filip Varga18f1e412020-12-03 15:27:40 +01001027
Filip Vargaa0648b62021-06-21 12:59:41 +02001028 def test_icmp_error(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001029 """NAT44ED test ICMP error message with inner header"""
Filip Vargaa0648b62021-06-21 12:59:41 +02001030
1031 payload = "H" * 10
1032
1033 self.nat_add_address(self.nat_addr)
1034 self.nat_add_inside_interface(self.pg0)
1035 self.nat_add_outside_interface(self.pg1)
1036
1037 # in2out (initiate connection)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001038 p1 = [
1039 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1040 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
1041 / UDP(sport=21, dport=20)
1042 / payload,
1043 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1044 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
1045 / TCP(sport=21, dport=20, flags="S")
1046 / payload,
1047 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1048 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
1049 / ICMP(type="echo-request", id=7777)
1050 / payload,
1051 ]
Filip Vargaa0648b62021-06-21 12:59:41 +02001052
Klement Sekerac2feb652022-03-08 20:13:57 +01001053 capture = self.send_and_expect(self.pg0, p1, self.pg1)
Filip Vargaa0648b62021-06-21 12:59:41 +02001054
1055 # out2in (send error message)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001056 p2 = [
1057 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1058 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
1059 / ICMP(type="dest-unreach", code="port-unreachable")
1060 / c[IP:]
1061 for c in capture
1062 ]
Filip Vargaa0648b62021-06-21 12:59:41 +02001063
Klement Sekerac2feb652022-03-08 20:13:57 +01001064 capture = self.send_and_expect(self.pg1, p2, self.pg0)
Filip Vargaa0648b62021-06-21 12:59:41 +02001065
Klement Sekerac2feb652022-03-08 20:13:57 +01001066 for c in capture:
1067 try:
1068 assert c[IP].dst == self.pg0.remote_ip4
1069 assert c[IPerror].src == self.pg0.remote_ip4
1070 except AssertionError as a:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001071 raise AssertionError(f"Packet {pr(c)} not translated properly") from a
Filip Vargaa0648b62021-06-21 12:59:41 +02001072
Klement Sekera254c8032021-07-27 13:33:51 +02001073 def test_icmp_echo_reply_trailer(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001074 """ICMP echo reply with ethernet trailer"""
Klement Sekera254c8032021-07-27 13:33:51 +02001075
1076 self.nat_add_address(self.nat_addr)
1077 self.nat_add_inside_interface(self.pg0)
1078 self.nat_add_outside_interface(self.pg1)
1079
1080 # in2out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001081 p1 = (
1082 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1083 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
1084 / ICMP(type=8, id=0xABCD, seq=0)
1085 )
Klement Sekera254c8032021-07-27 13:33:51 +02001086
1087 self.pg0.add_stream(p1)
1088 self.pg_enable_capture(self.pg_interfaces)
1089 self.pg_start()
1090 c = self.pg1.get_capture(1)[0]
1091
1092 self.logger.debug(self.vapi.cli("show trace"))
1093
1094 # out2in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001095 p2 = (
1096 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1097 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr, id=0xEE59)
1098 / ICMP(type=0, id=c[ICMP].id, seq=0)
1099 )
Klement Sekera254c8032021-07-27 13:33:51 +02001100
1101 # force checksum calculation
1102 p2 = p2.__class__(bytes(p2))
1103
1104 self.logger.debug(ppp("Packet before modification:", p2))
1105
1106 # hex representation of vss monitoring ethernet trailer
1107 # this seems to be just added to end of packet without modifying
1108 # IP or ICMP lengths / checksums
1109 p2 = p2 / Raw("\x00\x00\x52\x54\x00\x46\xab\x04\x84\x18")
1110 # change it so that IP/ICMP is unaffected
1111 p2[IP].len = 28
1112
1113 self.logger.debug(ppp("Packet with added trailer:", p2))
1114
1115 self.pg1.add_stream(p2)
1116 self.pg_enable_capture(self.pg_interfaces)
1117 self.pg_start()
1118
1119 self.pg0.get_capture(1)
1120
Filip Varga18f1e412020-12-03 15:27:40 +01001121 def test_users_dump(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001122 """NAT44ED API test - nat44_user_dump"""
Filip Varga18f1e412020-12-03 15:27:40 +01001123
1124 self.nat_add_address(self.nat_addr)
1125 self.nat_add_inside_interface(self.pg0)
1126 self.nat_add_outside_interface(self.pg1)
1127
1128 self.vapi.nat44_forwarding_enable_disable(enable=1)
1129
1130 local_ip = self.pg0.remote_ip4
1131 external_ip = self.nat_addr
1132 self.nat_add_static_mapping(local_ip, external_ip)
1133
1134 users = self.vapi.nat44_user_dump()
1135 self.assertEqual(len(users), 0)
1136
1137 # in2out - static mapping match
1138
1139 pkts = self.create_stream_out(self.pg1)
1140 self.pg1.add_stream(pkts)
1141 self.pg_enable_capture(self.pg_interfaces)
1142 self.pg_start()
1143 capture = self.pg0.get_capture(len(pkts))
1144 self.verify_capture_in(capture, self.pg0)
1145
1146 pkts = self.create_stream_in(self.pg0, self.pg1)
1147 self.pg0.add_stream(pkts)
1148 self.pg_enable_capture(self.pg_interfaces)
1149 self.pg_start()
1150 capture = self.pg1.get_capture(len(pkts))
1151 self.verify_capture_out(capture, same_port=True)
1152
1153 users = self.vapi.nat44_user_dump()
1154 self.assertEqual(len(users), 1)
1155 static_user = users[0]
1156 self.assertEqual(static_user.nstaticsessions, 3)
1157 self.assertEqual(static_user.nsessions, 0)
1158
1159 # in2out - no static mapping match (forwarding test)
1160
1161 host0 = self.pg0.remote_hosts[0]
1162 self.pg0.remote_hosts[0] = self.pg0.remote_hosts[1]
1163 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001164 pkts = self.create_stream_out(
1165 self.pg1, dst_ip=self.pg0.remote_ip4, use_inside_ports=True
1166 )
Filip Varga18f1e412020-12-03 15:27:40 +01001167 self.pg1.add_stream(pkts)
1168 self.pg_enable_capture(self.pg_interfaces)
1169 self.pg_start()
1170 capture = self.pg0.get_capture(len(pkts))
1171 self.verify_capture_in(capture, self.pg0)
1172
1173 pkts = self.create_stream_in(self.pg0, self.pg1)
1174 self.pg0.add_stream(pkts)
1175 self.pg_enable_capture(self.pg_interfaces)
1176 self.pg_start()
1177 capture = self.pg1.get_capture(len(pkts))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001178 self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4, same_port=True)
Filip Varga18f1e412020-12-03 15:27:40 +01001179 finally:
1180 self.pg0.remote_hosts[0] = host0
1181
1182 users = self.vapi.nat44_user_dump()
1183 self.assertEqual(len(users), 2)
1184 if str(users[0].ip_address) == self.pg0.remote_hosts[0].ip4:
1185 non_static_user = users[1]
1186 static_user = users[0]
1187 else:
1188 non_static_user = users[0]
1189 static_user = users[1]
1190 self.assertEqual(static_user.nstaticsessions, 3)
1191 self.assertEqual(static_user.nsessions, 0)
1192 self.assertEqual(non_static_user.nstaticsessions, 0)
1193 self.assertEqual(non_static_user.nsessions, 3)
1194
1195 users = self.vapi.nat44_user_dump()
1196 self.assertEqual(len(users), 2)
1197 if str(users[0].ip_address) == self.pg0.remote_hosts[0].ip4:
1198 non_static_user = users[1]
1199 static_user = users[0]
1200 else:
1201 non_static_user = users[0]
1202 static_user = users[1]
1203 self.assertEqual(static_user.nstaticsessions, 3)
1204 self.assertEqual(static_user.nsessions, 0)
1205 self.assertEqual(non_static_user.nstaticsessions, 0)
1206 self.assertEqual(non_static_user.nsessions, 3)
1207
1208 def test_frag_out_of_order_do_not_translate(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001209 """NAT44ED don't translate fragments arriving out of order"""
Filip Varga18f1e412020-12-03 15:27:40 +01001210 self.nat_add_inside_interface(self.pg0)
1211 self.nat_add_outside_interface(self.pg1)
1212 self.vapi.nat44_forwarding_enable_disable(enable=True)
1213 self.frag_out_of_order(proto=IP_PROTOS.tcp, dont_translate=True)
1214
1215 def test_forwarding(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001216 """NAT44ED forwarding test"""
Filip Varga18f1e412020-12-03 15:27:40 +01001217
1218 self.nat_add_inside_interface(self.pg0)
1219 self.nat_add_outside_interface(self.pg1)
1220 self.vapi.nat44_forwarding_enable_disable(enable=1)
1221
1222 real_ip = self.pg0.remote_ip4
1223 alias_ip = self.nat_addr
1224 flags = self.config_flags.NAT_IS_ADDR_ONLY
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001225 self.vapi.nat44_add_del_static_mapping(
1226 is_add=1,
1227 local_ip_address=real_ip,
1228 external_ip_address=alias_ip,
1229 external_sw_if_index=0xFFFFFFFF,
1230 flags=flags,
1231 )
Filip Varga18f1e412020-12-03 15:27:40 +01001232
1233 try:
1234 # in2out - static mapping match
1235
1236 pkts = self.create_stream_out(self.pg1)
1237 self.pg1.add_stream(pkts)
1238 self.pg_enable_capture(self.pg_interfaces)
1239 self.pg_start()
1240 capture = self.pg0.get_capture(len(pkts))
1241 self.verify_capture_in(capture, self.pg0)
1242
1243 pkts = self.create_stream_in(self.pg0, self.pg1)
1244 self.pg0.add_stream(pkts)
1245 self.pg_enable_capture(self.pg_interfaces)
1246 self.pg_start()
1247 capture = self.pg1.get_capture(len(pkts))
1248 self.verify_capture_out(capture, same_port=True)
1249
1250 # in2out - no static mapping match
1251
1252 host0 = self.pg0.remote_hosts[0]
1253 self.pg0.remote_hosts[0] = self.pg0.remote_hosts[1]
1254 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001255 pkts = self.create_stream_out(
1256 self.pg1, dst_ip=self.pg0.remote_ip4, use_inside_ports=True
1257 )
Filip Varga18f1e412020-12-03 15:27:40 +01001258 self.pg1.add_stream(pkts)
1259 self.pg_enable_capture(self.pg_interfaces)
1260 self.pg_start()
1261 capture = self.pg0.get_capture(len(pkts))
1262 self.verify_capture_in(capture, self.pg0)
1263
1264 pkts = self.create_stream_in(self.pg0, self.pg1)
1265 self.pg0.add_stream(pkts)
1266 self.pg_enable_capture(self.pg_interfaces)
1267 self.pg_start()
1268 capture = self.pg1.get_capture(len(pkts))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001269 self.verify_capture_out(
1270 capture, nat_ip=self.pg0.remote_ip4, same_port=True
1271 )
Filip Varga18f1e412020-12-03 15:27:40 +01001272 finally:
1273 self.pg0.remote_hosts[0] = host0
1274
1275 user = self.pg0.remote_hosts[1]
1276 sessions = self.vapi.nat44_user_session_dump(user.ip4, 0)
1277 self.assertEqual(len(sessions), 3)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001278 self.assertTrue(sessions[0].flags & self.config_flags.NAT_IS_EXT_HOST_VALID)
Filip Varga18f1e412020-12-03 15:27:40 +01001279 self.vapi.nat44_del_session(
1280 address=sessions[0].inside_ip_address,
1281 port=sessions[0].inside_port,
1282 protocol=sessions[0].protocol,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001283 flags=(
1284 self.config_flags.NAT_IS_INSIDE
1285 | self.config_flags.NAT_IS_EXT_HOST_VALID
1286 ),
Filip Varga18f1e412020-12-03 15:27:40 +01001287 ext_host_address=sessions[0].ext_host_address,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001288 ext_host_port=sessions[0].ext_host_port,
1289 )
Filip Varga18f1e412020-12-03 15:27:40 +01001290 sessions = self.vapi.nat44_user_session_dump(user.ip4, 0)
1291 self.assertEqual(len(sessions), 2)
1292
1293 finally:
1294 self.vapi.nat44_forwarding_enable_disable(enable=0)
1295 flags = self.config_flags.NAT_IS_ADDR_ONLY
1296 self.vapi.nat44_add_del_static_mapping(
1297 is_add=0,
1298 local_ip_address=real_ip,
1299 external_ip_address=alias_ip,
1300 external_sw_if_index=0xFFFFFFFF,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001301 flags=flags,
1302 )
Filip Varga18f1e412020-12-03 15:27:40 +01001303
1304 def test_output_feature_and_service2(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001305 """NAT44ED interface output feature and service host direct access"""
Filip Varga18f1e412020-12-03 15:27:40 +01001306 self.vapi.nat44_forwarding_enable_disable(enable=1)
1307 self.nat_add_address(self.nat_addr)
1308
Filip Vargab6810822022-02-15 11:56:07 -08001309 self.vapi.nat44_ed_add_del_output_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001310 sw_if_index=self.pg1.sw_if_index, is_add=1
1311 )
Filip Varga18f1e412020-12-03 15:27:40 +01001312
1313 # session initiated from service host - translate
1314 pkts = self.create_stream_in(self.pg0, self.pg1)
1315 self.pg0.add_stream(pkts)
1316 self.pg_enable_capture(self.pg_interfaces)
1317 self.pg_start()
1318 capture = self.pg1.get_capture(len(pkts))
1319 self.verify_capture_out(capture, ignore_port=True)
1320
1321 pkts = self.create_stream_out(self.pg1)
1322 self.pg1.add_stream(pkts)
1323 self.pg_enable_capture(self.pg_interfaces)
1324 self.pg_start()
1325 capture = self.pg0.get_capture(len(pkts))
1326 self.verify_capture_in(capture, self.pg0)
1327
1328 # session initiated from remote host - do not translate
1329 tcp_port_in = self.tcp_port_in
1330 udp_port_in = self.udp_port_in
1331 icmp_id_in = self.icmp_id_in
1332
1333 self.tcp_port_in = 60303
1334 self.udp_port_in = 60304
1335 self.icmp_id_in = 60305
1336
1337 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001338 pkts = self.create_stream_out(
1339 self.pg1, self.pg0.remote_ip4, use_inside_ports=True
1340 )
Filip Varga18f1e412020-12-03 15:27:40 +01001341 self.pg1.add_stream(pkts)
1342 self.pg_enable_capture(self.pg_interfaces)
1343 self.pg_start()
1344 capture = self.pg0.get_capture(len(pkts))
1345 self.verify_capture_in(capture, self.pg0)
1346
1347 pkts = self.create_stream_in(self.pg0, self.pg1)
1348 self.pg0.add_stream(pkts)
1349 self.pg_enable_capture(self.pg_interfaces)
1350 self.pg_start()
1351 capture = self.pg1.get_capture(len(pkts))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001352 self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4, same_port=True)
Filip Varga18f1e412020-12-03 15:27:40 +01001353 finally:
1354 self.tcp_port_in = tcp_port_in
1355 self.udp_port_in = udp_port_in
1356 self.icmp_id_in = icmp_id_in
1357
1358 def test_twice_nat(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001359 """NAT44ED Twice NAT"""
Filip Varga18f1e412020-12-03 15:27:40 +01001360 self.twice_nat_common()
1361
1362 def test_self_twice_nat_positive(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001363 """NAT44ED Self Twice NAT (positive test)"""
Filip Varga18f1e412020-12-03 15:27:40 +01001364 self.twice_nat_common(self_twice_nat=True, same_pg=True)
1365
1366 def test_self_twice_nat_lb_positive(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001367 """NAT44ED Self Twice NAT local service load balancing (positive test)"""
1368 self.twice_nat_common(lb=True, self_twice_nat=True, same_pg=True, client_id=1)
Filip Varga18f1e412020-12-03 15:27:40 +01001369
1370 def test_twice_nat_lb(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001371 """NAT44ED Twice NAT local service load balancing"""
Filip Varga18f1e412020-12-03 15:27:40 +01001372 self.twice_nat_common(lb=True)
1373
1374 def test_output_feature(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001375 """NAT44ED interface output feature (in2out postrouting)"""
Filip Varga18f1e412020-12-03 15:27:40 +01001376 self.vapi.nat44_forwarding_enable_disable(enable=1)
1377 self.nat_add_address(self.nat_addr)
1378
1379 self.nat_add_outside_interface(self.pg0)
Filip Vargab6810822022-02-15 11:56:07 -08001380 self.vapi.nat44_ed_add_del_output_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001381 sw_if_index=self.pg1.sw_if_index, is_add=1
1382 )
Filip Varga18f1e412020-12-03 15:27:40 +01001383
1384 # in2out
1385 pkts = self.create_stream_in(self.pg0, self.pg1)
1386 self.pg0.add_stream(pkts)
1387 self.pg_enable_capture(self.pg_interfaces)
1388 self.pg_start()
1389 capture = self.pg1.get_capture(len(pkts))
1390 self.verify_capture_out(capture, ignore_port=True)
Klement Sekera05b5a5b2021-06-28 13:40:40 +02001391 self.logger.debug(self.vapi.cli("show trace"))
Filip Varga18f1e412020-12-03 15:27:40 +01001392
1393 # out2in
1394 pkts = self.create_stream_out(self.pg1)
1395 self.pg1.add_stream(pkts)
1396 self.pg_enable_capture(self.pg_interfaces)
1397 self.pg_start()
1398 capture = self.pg0.get_capture(len(pkts))
1399 self.verify_capture_in(capture, self.pg0)
Klement Sekera05b5a5b2021-06-28 13:40:40 +02001400 self.logger.debug(self.vapi.cli("show trace"))
Filip Varga18f1e412020-12-03 15:27:40 +01001401
Klement Sekera79699b02021-06-21 16:04:40 +02001402 # in2out
1403 pkts = self.create_stream_in(self.pg0, self.pg1, ttl=2)
1404 self.pg0.add_stream(pkts)
1405 self.pg_enable_capture(self.pg_interfaces)
1406 self.pg_start()
1407 capture = self.pg1.get_capture(len(pkts))
1408 self.verify_capture_out(capture, ignore_port=True)
Klement Sekera05b5a5b2021-06-28 13:40:40 +02001409 self.logger.debug(self.vapi.cli("show trace"))
Klement Sekera79699b02021-06-21 16:04:40 +02001410
1411 # out2in
1412 pkts = self.create_stream_out(self.pg1, ttl=2)
1413 self.pg1.add_stream(pkts)
1414 self.pg_enable_capture(self.pg_interfaces)
1415 self.pg_start()
1416 capture = self.pg0.get_capture(len(pkts))
1417 self.verify_capture_in(capture, self.pg0)
Klement Sekera05b5a5b2021-06-28 13:40:40 +02001418 self.logger.debug(self.vapi.cli("show trace"))
Klement Sekera79699b02021-06-21 16:04:40 +02001419
1420 # in2out
1421 pkts = self.create_stream_in(self.pg0, self.pg1, ttl=1)
Neale Ranns5c6dd172022-02-17 09:08:47 +00001422 capture = self.send_and_expect_some(self.pg0, pkts, self.pg0)
Klement Sekera79699b02021-06-21 16:04:40 +02001423 for p in capture:
1424 self.assertIn(ICMP, p)
1425 self.assertEqual(p[ICMP].type, 11) # 11 == time-exceeded
1426
Filip Varga18f1e412020-12-03 15:27:40 +01001427 def test_static_with_port_out2(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001428 """NAT44ED 1:1 NAPT asymmetrical rule"""
Filip Varga18f1e412020-12-03 15:27:40 +01001429
1430 external_port = 80
1431 local_port = 8080
1432
1433 self.vapi.nat44_forwarding_enable_disable(enable=1)
1434 flags = self.config_flags.NAT_IS_OUT2IN_ONLY
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001435 self.nat_add_static_mapping(
1436 self.pg0.remote_ip4,
1437 self.nat_addr,
1438 local_port,
1439 external_port,
1440 proto=IP_PROTOS.tcp,
1441 flags=flags,
1442 )
Filip Varga18f1e412020-12-03 15:27:40 +01001443
1444 self.nat_add_inside_interface(self.pg0)
1445 self.nat_add_outside_interface(self.pg1)
1446
1447 # from client to service
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001448 p = (
1449 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1450 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
1451 / TCP(sport=12345, dport=external_port)
1452 )
Filip Varga18f1e412020-12-03 15:27:40 +01001453 self.pg1.add_stream(p)
1454 self.pg_enable_capture(self.pg_interfaces)
1455 self.pg_start()
1456 capture = self.pg0.get_capture(1)
1457 p = capture[0]
1458 try:
1459 ip = p[IP]
1460 tcp = p[TCP]
1461 self.assertEqual(ip.dst, self.pg0.remote_ip4)
1462 self.assertEqual(tcp.dport, local_port)
1463 self.assert_packet_checksums_valid(p)
1464 except:
1465 self.logger.error(ppp("Unexpected or invalid packet:", p))
1466 raise
1467
1468 # ICMP error
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001469 p = (
1470 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
1471 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
1472 / ICMP(type=11)
1473 / capture[0][IP]
1474 )
Filip Varga18f1e412020-12-03 15:27:40 +01001475 self.pg0.add_stream(p)
1476 self.pg_enable_capture(self.pg_interfaces)
1477 self.pg_start()
1478 capture = self.pg1.get_capture(1)
1479 p = capture[0]
1480 try:
1481 self.assertEqual(p[IP].src, self.nat_addr)
1482 inner = p[IPerror]
1483 self.assertEqual(inner.dst, self.nat_addr)
1484 self.assertEqual(inner[TCPerror].dport, external_port)
1485 except:
1486 self.logger.error(ppp("Unexpected or invalid packet:", p))
1487 raise
1488
1489 # from service back to client
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001490 p = (
1491 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1492 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
1493 / TCP(sport=local_port, dport=12345)
1494 )
Filip Varga18f1e412020-12-03 15:27:40 +01001495 self.pg0.add_stream(p)
1496 self.pg_enable_capture(self.pg_interfaces)
1497 self.pg_start()
1498 capture = self.pg1.get_capture(1)
1499 p = capture[0]
1500 try:
1501 ip = p[IP]
1502 tcp = p[TCP]
1503 self.assertEqual(ip.src, self.nat_addr)
1504 self.assertEqual(tcp.sport, external_port)
1505 self.assert_packet_checksums_valid(p)
1506 except:
1507 self.logger.error(ppp("Unexpected or invalid packet:", p))
1508 raise
1509
1510 # ICMP error
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001511 p = (
1512 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
1513 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
1514 / ICMP(type=11)
1515 / capture[0][IP]
1516 )
Filip Varga18f1e412020-12-03 15:27:40 +01001517 self.pg1.add_stream(p)
1518 self.pg_enable_capture(self.pg_interfaces)
1519 self.pg_start()
1520 capture = self.pg0.get_capture(1)
1521 p = capture[0]
1522 try:
1523 self.assertEqual(p[IP].dst, self.pg0.remote_ip4)
1524 inner = p[IPerror]
1525 self.assertEqual(inner.src, self.pg0.remote_ip4)
1526 self.assertEqual(inner[TCPerror].sport, local_port)
1527 except:
1528 self.logger.error(ppp("Unexpected or invalid packet:", p))
1529 raise
1530
1531 # from client to server (no translation)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001532 p = (
1533 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1534 / IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4)
1535 / TCP(sport=12346, dport=local_port)
1536 )
Filip Varga18f1e412020-12-03 15:27:40 +01001537 self.pg1.add_stream(p)
1538 self.pg_enable_capture(self.pg_interfaces)
1539 self.pg_start()
1540 capture = self.pg0.get_capture(1)
1541 p = capture[0]
1542 try:
1543 ip = p[IP]
1544 tcp = p[TCP]
1545 self.assertEqual(ip.dst, self.pg0.remote_ip4)
1546 self.assertEqual(tcp.dport, local_port)
1547 self.assert_packet_checksums_valid(p)
1548 except:
1549 self.logger.error(ppp("Unexpected or invalid packet:", p))
1550 raise
1551
1552 # from service back to client (no translation)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001553 p = (
1554 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1555 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
1556 / TCP(sport=local_port, dport=12346)
1557 )
Filip Varga18f1e412020-12-03 15:27:40 +01001558 self.pg0.add_stream(p)
1559 self.pg_enable_capture(self.pg_interfaces)
1560 self.pg_start()
1561 capture = self.pg1.get_capture(1)
1562 p = capture[0]
1563 try:
1564 ip = p[IP]
1565 tcp = p[TCP]
1566 self.assertEqual(ip.src, self.pg0.remote_ip4)
1567 self.assertEqual(tcp.sport, local_port)
1568 self.assert_packet_checksums_valid(p)
1569 except:
1570 self.logger.error(ppp("Unexpected or invalid packet:", p))
1571 raise
1572
1573 def test_static_lb(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001574 """NAT44ED local service load balancing"""
Filip Varga18f1e412020-12-03 15:27:40 +01001575 external_addr_n = self.nat_addr
1576 external_port = 80
1577 local_port = 8080
1578 server1 = self.pg0.remote_hosts[0]
1579 server2 = self.pg0.remote_hosts[1]
1580
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001581 locals = [
1582 {"addr": server1.ip4, "port": local_port, "probability": 70, "vrf_id": 0},
1583 {"addr": server2.ip4, "port": local_port, "probability": 30, "vrf_id": 0},
1584 ]
Filip Varga18f1e412020-12-03 15:27:40 +01001585
1586 self.nat_add_address(self.nat_addr)
1587 self.vapi.nat44_add_del_lb_static_mapping(
1588 is_add=1,
1589 external_addr=external_addr_n,
1590 external_port=external_port,
1591 protocol=IP_PROTOS.tcp,
1592 local_num=len(locals),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001593 locals=locals,
1594 )
Filip Varga18f1e412020-12-03 15:27:40 +01001595 flags = self.config_flags.NAT_IS_INSIDE
1596 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001597 sw_if_index=self.pg0.sw_if_index, flags=flags, is_add=1
1598 )
Filip Varga18f1e412020-12-03 15:27:40 +01001599 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001600 sw_if_index=self.pg1.sw_if_index, is_add=1
1601 )
Filip Varga18f1e412020-12-03 15:27:40 +01001602
1603 # from client to service
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001604 p = (
1605 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1606 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
1607 / TCP(sport=12345, dport=external_port)
1608 )
Filip Varga18f1e412020-12-03 15:27:40 +01001609 self.pg1.add_stream(p)
1610 self.pg_enable_capture(self.pg_interfaces)
1611 self.pg_start()
1612 capture = self.pg0.get_capture(1)
1613 p = capture[0]
1614 server = None
1615 try:
1616 ip = p[IP]
1617 tcp = p[TCP]
1618 self.assertIn(ip.dst, [server1.ip4, server2.ip4])
1619 if ip.dst == server1.ip4:
1620 server = server1
1621 else:
1622 server = server2
1623 self.assertEqual(tcp.dport, local_port)
1624 self.assert_packet_checksums_valid(p)
1625 except:
1626 self.logger.error(ppp("Unexpected or invalid packet:", p))
1627 raise
1628
1629 # from service back to client
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001630 p = (
1631 Ether(src=server.mac, dst=self.pg0.local_mac)
1632 / IP(src=server.ip4, dst=self.pg1.remote_ip4)
1633 / TCP(sport=local_port, dport=12345)
1634 )
Filip Varga18f1e412020-12-03 15:27:40 +01001635 self.pg0.add_stream(p)
1636 self.pg_enable_capture(self.pg_interfaces)
1637 self.pg_start()
1638 capture = self.pg1.get_capture(1)
1639 p = capture[0]
1640 try:
1641 ip = p[IP]
1642 tcp = p[TCP]
1643 self.assertEqual(ip.src, self.nat_addr)
1644 self.assertEqual(tcp.sport, external_port)
1645 self.assert_packet_checksums_valid(p)
1646 except:
1647 self.logger.error(ppp("Unexpected or invalid packet:", p))
1648 raise
1649
1650 sessions = self.vapi.nat44_user_session_dump(server.ip4, 0)
1651 self.assertEqual(len(sessions), 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001652 self.assertTrue(sessions[0].flags & self.config_flags.NAT_IS_EXT_HOST_VALID)
Filip Varga18f1e412020-12-03 15:27:40 +01001653 self.vapi.nat44_del_session(
1654 address=sessions[0].inside_ip_address,
1655 port=sessions[0].inside_port,
1656 protocol=sessions[0].protocol,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001657 flags=(
1658 self.config_flags.NAT_IS_INSIDE
1659 | self.config_flags.NAT_IS_EXT_HOST_VALID
1660 ),
Filip Varga18f1e412020-12-03 15:27:40 +01001661 ext_host_address=sessions[0].ext_host_address,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001662 ext_host_port=sessions[0].ext_host_port,
1663 )
Filip Varga18f1e412020-12-03 15:27:40 +01001664 sessions = self.vapi.nat44_user_session_dump(server.ip4, 0)
1665 self.assertEqual(len(sessions), 0)
1666
1667 def test_static_lb_2(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001668 """NAT44ED local service load balancing (asymmetrical rule)"""
Filip Varga18f1e412020-12-03 15:27:40 +01001669 external_addr = self.nat_addr
1670 external_port = 80
1671 local_port = 8080
1672 server1 = self.pg0.remote_hosts[0]
1673 server2 = self.pg0.remote_hosts[1]
1674
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001675 locals = [
1676 {"addr": server1.ip4, "port": local_port, "probability": 70, "vrf_id": 0},
1677 {"addr": server2.ip4, "port": local_port, "probability": 30, "vrf_id": 0},
1678 ]
Filip Varga18f1e412020-12-03 15:27:40 +01001679
1680 self.vapi.nat44_forwarding_enable_disable(enable=1)
1681 flags = self.config_flags.NAT_IS_OUT2IN_ONLY
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001682 self.vapi.nat44_add_del_lb_static_mapping(
1683 is_add=1,
1684 flags=flags,
1685 external_addr=external_addr,
1686 external_port=external_port,
1687 protocol=IP_PROTOS.tcp,
1688 local_num=len(locals),
1689 locals=locals,
1690 )
Filip Varga18f1e412020-12-03 15:27:40 +01001691 flags = self.config_flags.NAT_IS_INSIDE
1692 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001693 sw_if_index=self.pg0.sw_if_index, flags=flags, is_add=1
1694 )
Filip Varga18f1e412020-12-03 15:27:40 +01001695 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001696 sw_if_index=self.pg1.sw_if_index, is_add=1
1697 )
Filip Varga18f1e412020-12-03 15:27:40 +01001698
1699 # from client to service
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001700 p = (
1701 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1702 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
1703 / TCP(sport=12345, dport=external_port)
1704 )
Filip Varga18f1e412020-12-03 15:27:40 +01001705 self.pg1.add_stream(p)
1706 self.pg_enable_capture(self.pg_interfaces)
1707 self.pg_start()
1708 capture = self.pg0.get_capture(1)
1709 p = capture[0]
1710 server = None
1711 try:
1712 ip = p[IP]
1713 tcp = p[TCP]
1714 self.assertIn(ip.dst, [server1.ip4, server2.ip4])
1715 if ip.dst == server1.ip4:
1716 server = server1
1717 else:
1718 server = server2
1719 self.assertEqual(tcp.dport, local_port)
1720 self.assert_packet_checksums_valid(p)
1721 except:
1722 self.logger.error(ppp("Unexpected or invalid packet:", p))
1723 raise
1724
1725 # from service back to client
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001726 p = (
1727 Ether(src=server.mac, dst=self.pg0.local_mac)
1728 / IP(src=server.ip4, dst=self.pg1.remote_ip4)
1729 / TCP(sport=local_port, dport=12345)
1730 )
Filip Varga18f1e412020-12-03 15:27:40 +01001731 self.pg0.add_stream(p)
1732 self.pg_enable_capture(self.pg_interfaces)
1733 self.pg_start()
1734 capture = self.pg1.get_capture(1)
1735 p = capture[0]
1736 try:
1737 ip = p[IP]
1738 tcp = p[TCP]
1739 self.assertEqual(ip.src, self.nat_addr)
1740 self.assertEqual(tcp.sport, external_port)
1741 self.assert_packet_checksums_valid(p)
1742 except:
1743 self.logger.error(ppp("Unexpected or invalid packet:", p))
1744 raise
1745
1746 # from client to server (no translation)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001747 p = (
1748 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1749 / IP(src=self.pg1.remote_ip4, dst=server1.ip4)
1750 / TCP(sport=12346, dport=local_port)
1751 )
Filip Varga18f1e412020-12-03 15:27:40 +01001752 self.pg1.add_stream(p)
1753 self.pg_enable_capture(self.pg_interfaces)
1754 self.pg_start()
1755 capture = self.pg0.get_capture(1)
1756 p = capture[0]
1757 server = None
1758 try:
1759 ip = p[IP]
1760 tcp = p[TCP]
1761 self.assertEqual(ip.dst, server1.ip4)
1762 self.assertEqual(tcp.dport, local_port)
1763 self.assert_packet_checksums_valid(p)
1764 except:
1765 self.logger.error(ppp("Unexpected or invalid packet:", p))
1766 raise
1767
1768 # from service back to client (no translation)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001769 p = (
1770 Ether(src=server1.mac, dst=self.pg0.local_mac)
1771 / IP(src=server1.ip4, dst=self.pg1.remote_ip4)
1772 / TCP(sport=local_port, dport=12346)
1773 )
Filip Varga18f1e412020-12-03 15:27:40 +01001774 self.pg0.add_stream(p)
1775 self.pg_enable_capture(self.pg_interfaces)
1776 self.pg_start()
1777 capture = self.pg1.get_capture(1)
1778 p = capture[0]
1779 try:
1780 ip = p[IP]
1781 tcp = p[TCP]
1782 self.assertEqual(ip.src, server1.ip4)
1783 self.assertEqual(tcp.sport, local_port)
1784 self.assert_packet_checksums_valid(p)
1785 except:
1786 self.logger.error(ppp("Unexpected or invalid packet:", p))
1787 raise
1788
1789 def test_lb_affinity(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001790 """NAT44ED local service load balancing affinity"""
Filip Varga18f1e412020-12-03 15:27:40 +01001791 external_addr = self.nat_addr
1792 external_port = 80
1793 local_port = 8080
1794 server1 = self.pg0.remote_hosts[0]
1795 server2 = self.pg0.remote_hosts[1]
1796
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001797 locals = [
1798 {"addr": server1.ip4, "port": local_port, "probability": 50, "vrf_id": 0},
1799 {"addr": server2.ip4, "port": local_port, "probability": 50, "vrf_id": 0},
1800 ]
Filip Varga18f1e412020-12-03 15:27:40 +01001801
1802 self.nat_add_address(self.nat_addr)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001803 self.vapi.nat44_add_del_lb_static_mapping(
1804 is_add=1,
1805 external_addr=external_addr,
1806 external_port=external_port,
1807 protocol=IP_PROTOS.tcp,
1808 affinity=10800,
1809 local_num=len(locals),
1810 locals=locals,
1811 )
Filip Varga18f1e412020-12-03 15:27:40 +01001812 flags = self.config_flags.NAT_IS_INSIDE
1813 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001814 sw_if_index=self.pg0.sw_if_index, flags=flags, is_add=1
1815 )
Filip Varga18f1e412020-12-03 15:27:40 +01001816 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001817 sw_if_index=self.pg1.sw_if_index, is_add=1
1818 )
Filip Varga18f1e412020-12-03 15:27:40 +01001819
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001820 p = (
1821 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
1822 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
1823 / TCP(sport=1025, dport=external_port)
1824 )
Filip Varga18f1e412020-12-03 15:27:40 +01001825 self.pg1.add_stream(p)
1826 self.pg_enable_capture(self.pg_interfaces)
1827 self.pg_start()
1828 capture = self.pg0.get_capture(1)
1829 backend = capture[0][IP].dst
1830
1831 sessions = self.vapi.nat44_user_session_dump(backend, 0)
1832 self.assertEqual(len(sessions), 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001833 self.assertTrue(sessions[0].flags & self.config_flags.NAT_IS_EXT_HOST_VALID)
Filip Varga18f1e412020-12-03 15:27:40 +01001834 self.vapi.nat44_del_session(
1835 address=sessions[0].inside_ip_address,
1836 port=sessions[0].inside_port,
1837 protocol=sessions[0].protocol,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001838 flags=(
1839 self.config_flags.NAT_IS_INSIDE
1840 | self.config_flags.NAT_IS_EXT_HOST_VALID
1841 ),
Filip Varga18f1e412020-12-03 15:27:40 +01001842 ext_host_address=sessions[0].ext_host_address,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001843 ext_host_port=sessions[0].ext_host_port,
1844 )
Filip Varga18f1e412020-12-03 15:27:40 +01001845
1846 pkts = []
1847 for port in range(1030, 1100):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001848 p = (
1849 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
1850 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
1851 / TCP(sport=port, dport=external_port)
1852 )
Filip Varga18f1e412020-12-03 15:27:40 +01001853 pkts.append(p)
1854 self.pg1.add_stream(pkts)
1855 self.pg_enable_capture(self.pg_interfaces)
1856 self.pg_start()
1857 capture = self.pg0.get_capture(len(pkts))
1858 for p in capture:
1859 self.assertEqual(p[IP].dst, backend)
1860
Klement Sekeraa920af72021-05-17 13:17:56 +02001861 def test_multiple_vrf_1(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001862 """Multiple VRF - both client & service in VRF1"""
Filip Varga18f1e412020-12-03 15:27:40 +01001863
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001864 external_addr = "1.2.3.4"
Filip Varga18f1e412020-12-03 15:27:40 +01001865 external_port = 80
1866 local_port = 8080
1867 port = 0
1868
Filip Varga18f1e412020-12-03 15:27:40 +01001869 flags = self.config_flags.NAT_IS_INSIDE
1870 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001871 sw_if_index=self.pg5.sw_if_index, is_add=1
1872 )
Filip Varga18f1e412020-12-03 15:27:40 +01001873 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001874 sw_if_index=self.pg5.sw_if_index, is_add=1, flags=flags
1875 )
Filip Varga18f1e412020-12-03 15:27:40 +01001876 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001877 sw_if_index=self.pg6.sw_if_index, is_add=1
1878 )
Filip Varga18f1e412020-12-03 15:27:40 +01001879 flags = self.config_flags.NAT_IS_OUT2IN_ONLY
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001880 self.nat_add_static_mapping(
1881 self.pg5.remote_ip4,
1882 external_addr,
1883 local_port,
1884 external_port,
1885 vrf_id=1,
1886 proto=IP_PROTOS.tcp,
1887 flags=flags,
1888 )
Filip Varga18f1e412020-12-03 15:27:40 +01001889
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001890 p = (
1891 Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac)
1892 / IP(src=self.pg6.remote_ip4, dst=external_addr)
1893 / TCP(sport=12345, dport=external_port)
1894 )
Filip Varga18f1e412020-12-03 15:27:40 +01001895 self.pg6.add_stream(p)
1896 self.pg_enable_capture(self.pg_interfaces)
1897 self.pg_start()
1898 capture = self.pg5.get_capture(1)
1899 p = capture[0]
1900 try:
1901 ip = p[IP]
1902 tcp = p[TCP]
1903 self.assertEqual(ip.dst, self.pg5.remote_ip4)
1904 self.assertEqual(tcp.dport, local_port)
1905 self.assert_packet_checksums_valid(p)
1906 except:
1907 self.logger.error(ppp("Unexpected or invalid packet:", p))
1908 raise
1909
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001910 p = (
1911 Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac)
1912 / IP(src=self.pg5.remote_ip4, dst=self.pg6.remote_ip4)
1913 / TCP(sport=local_port, dport=12345)
1914 )
Filip Varga18f1e412020-12-03 15:27:40 +01001915 self.pg5.add_stream(p)
1916 self.pg_enable_capture(self.pg_interfaces)
1917 self.pg_start()
1918 capture = self.pg6.get_capture(1)
1919 p = capture[0]
1920 try:
1921 ip = p[IP]
1922 tcp = p[TCP]
1923 self.assertEqual(ip.src, external_addr)
1924 self.assertEqual(tcp.sport, external_port)
1925 self.assert_packet_checksums_valid(p)
1926 except:
1927 self.logger.error(ppp("Unexpected or invalid packet:", p))
1928 raise
1929
Klement Sekeraa920af72021-05-17 13:17:56 +02001930 def test_multiple_vrf_2(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001931 """Multiple VRF - dynamic NAT from VRF1 to VRF0 (output-feature)"""
Klement Sekeraa920af72021-05-17 13:17:56 +02001932
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001933 external_addr = "1.2.3.4"
Klement Sekeraa920af72021-05-17 13:17:56 +02001934 external_port = 80
1935 local_port = 8080
1936 port = 0
1937
1938 self.nat_add_address(self.nat_addr)
1939 flags = self.config_flags.NAT_IS_INSIDE
Filip Vargab6810822022-02-15 11:56:07 -08001940 self.vapi.nat44_ed_add_del_output_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001941 sw_if_index=self.pg1.sw_if_index, is_add=1
1942 )
Klement Sekeraa920af72021-05-17 13:17:56 +02001943 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001944 sw_if_index=self.pg5.sw_if_index, is_add=1
1945 )
Klement Sekeraa920af72021-05-17 13:17:56 +02001946 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001947 sw_if_index=self.pg5.sw_if_index, is_add=1, flags=flags
1948 )
Klement Sekeraa920af72021-05-17 13:17:56 +02001949 flags = self.config_flags.NAT_IS_OUT2IN_ONLY
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001950 self.nat_add_static_mapping(
1951 self.pg5.remote_ip4,
1952 external_addr,
1953 local_port,
1954 external_port,
1955 vrf_id=1,
1956 proto=IP_PROTOS.tcp,
1957 flags=flags,
1958 )
Klement Sekeraa920af72021-05-17 13:17:56 +02001959
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001960 p = (
1961 Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac)
1962 / IP(src=self.pg5.remote_ip4, dst=self.pg1.remote_ip4)
1963 / TCP(sport=2345, dport=22)
1964 )
Filip Varga18f1e412020-12-03 15:27:40 +01001965 self.pg5.add_stream(p)
1966 self.pg_enable_capture(self.pg_interfaces)
1967 self.pg_start()
1968 capture = self.pg1.get_capture(1)
1969 p = capture[0]
1970 try:
1971 ip = p[IP]
1972 tcp = p[TCP]
1973 self.assertEqual(ip.src, self.nat_addr)
1974 self.assert_packet_checksums_valid(p)
1975 port = tcp.sport
1976 except:
1977 self.logger.error(ppp("Unexpected or invalid packet:", p))
1978 raise
1979
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001980 p = (
1981 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1982 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
1983 / TCP(sport=22, dport=port)
1984 )
Filip Varga18f1e412020-12-03 15:27:40 +01001985 self.pg1.add_stream(p)
1986 self.pg_enable_capture(self.pg_interfaces)
1987 self.pg_start()
1988 capture = self.pg5.get_capture(1)
1989 p = capture[0]
1990 try:
1991 ip = p[IP]
1992 tcp = p[TCP]
1993 self.assertEqual(ip.dst, self.pg5.remote_ip4)
1994 self.assertEqual(tcp.dport, 2345)
1995 self.assert_packet_checksums_valid(p)
1996 except:
1997 self.logger.error(ppp("Unexpected or invalid packet:", p))
1998 raise
1999
Klement Sekeraa920af72021-05-17 13:17:56 +02002000 def test_multiple_vrf_3(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002001 """Multiple VRF - client in VRF1, service in VRF0"""
Klement Sekeraa920af72021-05-17 13:17:56 +02002002
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002003 external_addr = "1.2.3.4"
Klement Sekeraa920af72021-05-17 13:17:56 +02002004 external_port = 80
2005 local_port = 8080
2006 port = 0
2007
2008 flags = self.config_flags.NAT_IS_INSIDE
2009 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002010 sw_if_index=self.pg0.sw_if_index, is_add=1
2011 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002012 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002013 sw_if_index=self.pg0.sw_if_index, is_add=1, flags=flags
2014 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002015 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002016 sw_if_index=self.pg6.sw_if_index, is_add=1
2017 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002018 flags = self.config_flags.NAT_IS_OUT2IN_ONLY
2019 self.nat_add_static_mapping(
2020 self.pg0.remote_ip4,
2021 external_sw_if_index=self.pg0.sw_if_index,
2022 local_port=local_port,
2023 vrf_id=0,
2024 external_port=external_port,
2025 proto=IP_PROTOS.tcp,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002026 flags=flags,
Klement Sekeraa920af72021-05-17 13:17:56 +02002027 )
2028
Filip Varga18f1e412020-12-03 15:27:40 +01002029 # from client VRF1 to service VRF0
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002030 p = (
2031 Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac)
2032 / IP(src=self.pg6.remote_ip4, dst=self.pg0.local_ip4)
2033 / TCP(sport=12346, dport=external_port)
2034 )
Filip Varga18f1e412020-12-03 15:27:40 +01002035 self.pg6.add_stream(p)
2036 self.pg_enable_capture(self.pg_interfaces)
2037 self.pg_start()
2038 capture = self.pg0.get_capture(1)
2039 p = capture[0]
2040 try:
2041 ip = p[IP]
2042 tcp = p[TCP]
2043 self.assertEqual(ip.dst, self.pg0.remote_ip4)
2044 self.assertEqual(tcp.dport, local_port)
2045 self.assert_packet_checksums_valid(p)
2046 except:
2047 self.logger.error(ppp("Unexpected or invalid packet:", p))
2048 raise
2049
2050 # from service VRF0 back to client VRF1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002051 p = (
2052 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2053 / IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4)
2054 / TCP(sport=local_port, dport=12346)
2055 )
Filip Varga18f1e412020-12-03 15:27:40 +01002056 self.pg0.add_stream(p)
2057 self.pg_enable_capture(self.pg_interfaces)
2058 self.pg_start()
2059 capture = self.pg6.get_capture(1)
2060 p = capture[0]
2061 try:
2062 ip = p[IP]
2063 tcp = p[TCP]
2064 self.assertEqual(ip.src, self.pg0.local_ip4)
2065 self.assertEqual(tcp.sport, external_port)
2066 self.assert_packet_checksums_valid(p)
2067 except:
2068 self.logger.error(ppp("Unexpected or invalid packet:", p))
2069 raise
2070
Klement Sekeraa920af72021-05-17 13:17:56 +02002071 def test_multiple_vrf_4(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002072 """Multiple VRF - client in VRF0, service in VRF1"""
Klement Sekeraa920af72021-05-17 13:17:56 +02002073
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002074 external_addr = "1.2.3.4"
Klement Sekeraa920af72021-05-17 13:17:56 +02002075 external_port = 80
2076 local_port = 8080
2077 port = 0
2078
2079 flags = self.config_flags.NAT_IS_INSIDE
2080 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002081 sw_if_index=self.pg0.sw_if_index, is_add=1
2082 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002083 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002084 sw_if_index=self.pg0.sw_if_index, is_add=1, flags=flags
2085 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002086 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002087 sw_if_index=self.pg5.sw_if_index, is_add=1
2088 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002089 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002090 sw_if_index=self.pg5.sw_if_index, is_add=1, flags=flags
2091 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002092 flags = self.config_flags.NAT_IS_OUT2IN_ONLY
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002093 self.nat_add_static_mapping(
2094 self.pg5.remote_ip4,
2095 external_addr,
2096 local_port,
2097 external_port,
2098 vrf_id=1,
2099 proto=IP_PROTOS.tcp,
2100 flags=flags,
2101 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002102
Filip Varga18f1e412020-12-03 15:27:40 +01002103 # from client VRF0 to service VRF1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002104 p = (
2105 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2106 / IP(src=self.pg0.remote_ip4, dst=external_addr)
2107 / TCP(sport=12347, dport=external_port)
2108 )
Filip Varga18f1e412020-12-03 15:27:40 +01002109 self.pg0.add_stream(p)
2110 self.pg_enable_capture(self.pg_interfaces)
2111 self.pg_start()
2112 capture = self.pg5.get_capture(1)
2113 p = capture[0]
2114 try:
2115 ip = p[IP]
2116 tcp = p[TCP]
2117 self.assertEqual(ip.dst, self.pg5.remote_ip4)
2118 self.assertEqual(tcp.dport, local_port)
2119 self.assert_packet_checksums_valid(p)
2120 except:
2121 self.logger.error(ppp("Unexpected or invalid packet:", p))
2122 raise
2123
2124 # from service VRF1 back to client VRF0
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002125 p = (
2126 Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac)
2127 / IP(src=self.pg5.remote_ip4, dst=self.pg0.remote_ip4)
2128 / TCP(sport=local_port, dport=12347)
2129 )
Filip Varga18f1e412020-12-03 15:27:40 +01002130 self.pg5.add_stream(p)
2131 self.pg_enable_capture(self.pg_interfaces)
2132 self.pg_start()
2133 capture = self.pg0.get_capture(1)
2134 p = capture[0]
2135 try:
2136 ip = p[IP]
2137 tcp = p[TCP]
2138 self.assertEqual(ip.src, external_addr)
2139 self.assertEqual(tcp.sport, external_port)
2140 self.assert_packet_checksums_valid(p)
2141 except:
2142 self.logger.error(ppp("Unexpected or invalid packet:", p))
2143 raise
2144
Klement Sekeraa920af72021-05-17 13:17:56 +02002145 def test_multiple_vrf_5(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002146 """Multiple VRF - forwarding - no translation"""
Klement Sekeraa920af72021-05-17 13:17:56 +02002147
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002148 external_addr = "1.2.3.4"
Klement Sekeraa920af72021-05-17 13:17:56 +02002149 external_port = 80
2150 local_port = 8080
2151 port = 0
2152
2153 self.vapi.nat44_forwarding_enable_disable(enable=1)
2154 flags = self.config_flags.NAT_IS_INSIDE
2155 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002156 sw_if_index=self.pg0.sw_if_index, is_add=1
2157 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002158 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002159 sw_if_index=self.pg0.sw_if_index, is_add=1, flags=flags
2160 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002161 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002162 sw_if_index=self.pg5.sw_if_index, is_add=1
2163 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002164 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002165 sw_if_index=self.pg5.sw_if_index, is_add=1, flags=flags
2166 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002167 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002168 sw_if_index=self.pg6.sw_if_index, is_add=1
2169 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002170 flags = self.config_flags.NAT_IS_OUT2IN_ONLY
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002171 self.nat_add_static_mapping(
2172 self.pg5.remote_ip4,
2173 external_addr,
2174 local_port,
2175 external_port,
2176 vrf_id=1,
2177 proto=IP_PROTOS.tcp,
2178 flags=flags,
2179 )
Klement Sekeraa920af72021-05-17 13:17:56 +02002180 self.nat_add_static_mapping(
2181 self.pg0.remote_ip4,
2182 external_sw_if_index=self.pg0.sw_if_index,
2183 local_port=local_port,
2184 vrf_id=0,
2185 external_port=external_port,
2186 proto=IP_PROTOS.tcp,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002187 flags=flags,
Klement Sekeraa920af72021-05-17 13:17:56 +02002188 )
2189
Filip Varga18f1e412020-12-03 15:27:40 +01002190 # from client to server (both VRF1, no translation)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002191 p = (
2192 Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac)
2193 / IP(src=self.pg6.remote_ip4, dst=self.pg5.remote_ip4)
2194 / TCP(sport=12348, dport=local_port)
2195 )
Filip Varga18f1e412020-12-03 15:27:40 +01002196 self.pg6.add_stream(p)
2197 self.pg_enable_capture(self.pg_interfaces)
2198 self.pg_start()
2199 capture = self.pg5.get_capture(1)
2200 p = capture[0]
2201 try:
2202 ip = p[IP]
2203 tcp = p[TCP]
2204 self.assertEqual(ip.dst, self.pg5.remote_ip4)
2205 self.assertEqual(tcp.dport, local_port)
2206 self.assert_packet_checksums_valid(p)
2207 except:
2208 self.logger.error(ppp("Unexpected or invalid packet:", p))
2209 raise
2210
2211 # from server back to client (both VRF1, no translation)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002212 p = (
2213 Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac)
2214 / IP(src=self.pg5.remote_ip4, dst=self.pg6.remote_ip4)
2215 / TCP(sport=local_port, dport=12348)
2216 )
Filip Varga18f1e412020-12-03 15:27:40 +01002217 self.pg5.add_stream(p)
2218 self.pg_enable_capture(self.pg_interfaces)
2219 self.pg_start()
2220 capture = self.pg6.get_capture(1)
2221 p = capture[0]
2222 try:
2223 ip = p[IP]
2224 tcp = p[TCP]
2225 self.assertEqual(ip.src, self.pg5.remote_ip4)
2226 self.assertEqual(tcp.sport, local_port)
2227 self.assert_packet_checksums_valid(p)
2228 except:
2229 self.logger.error(ppp("Unexpected or invalid packet:", p))
2230 raise
2231
2232 # from client VRF1 to server VRF0 (no translation)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002233 p = (
2234 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2235 / IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4)
2236 / TCP(sport=local_port, dport=12349)
2237 )
Filip Varga18f1e412020-12-03 15:27:40 +01002238 self.pg0.add_stream(p)
2239 self.pg_enable_capture(self.pg_interfaces)
2240 self.pg_start()
2241 capture = self.pg6.get_capture(1)
2242 p = capture[0]
2243 try:
2244 ip = p[IP]
2245 tcp = p[TCP]
2246 self.assertEqual(ip.src, self.pg0.remote_ip4)
2247 self.assertEqual(tcp.sport, local_port)
2248 self.assert_packet_checksums_valid(p)
2249 except:
2250 self.logger.error(ppp("Unexpected or invalid packet:", p))
2251 raise
2252
2253 # from server VRF0 back to client VRF1 (no translation)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002254 p = (
2255 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2256 / IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4)
2257 / TCP(sport=local_port, dport=12349)
2258 )
Filip Varga18f1e412020-12-03 15:27:40 +01002259 self.pg0.add_stream(p)
2260 self.pg_enable_capture(self.pg_interfaces)
2261 self.pg_start()
2262 capture = self.pg6.get_capture(1)
2263 p = capture[0]
2264 try:
2265 ip = p[IP]
2266 tcp = p[TCP]
2267 self.assertEqual(ip.src, self.pg0.remote_ip4)
2268 self.assertEqual(tcp.sport, local_port)
2269 self.assert_packet_checksums_valid(p)
2270 except:
2271 self.logger.error(ppp("Unexpected or invalid packet:", p))
2272 raise
2273
2274 # from client VRF0 to server VRF1 (no translation)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002275 p = (
2276 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2277 / IP(src=self.pg0.remote_ip4, dst=self.pg5.remote_ip4)
2278 / TCP(sport=12344, dport=local_port)
2279 )
Filip Varga18f1e412020-12-03 15:27:40 +01002280 self.pg0.add_stream(p)
2281 self.pg_enable_capture(self.pg_interfaces)
2282 self.pg_start()
2283 capture = self.pg5.get_capture(1)
2284 p = capture[0]
2285 try:
2286 ip = p[IP]
2287 tcp = p[TCP]
2288 self.assertEqual(ip.dst, self.pg5.remote_ip4)
2289 self.assertEqual(tcp.dport, local_port)
2290 self.assert_packet_checksums_valid(p)
2291 except:
2292 self.logger.error(ppp("Unexpected or invalid packet:", p))
2293 raise
2294
2295 # from server VRF1 back to client VRF0 (no translation)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002296 p = (
2297 Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac)
2298 / IP(src=self.pg5.remote_ip4, dst=self.pg0.remote_ip4)
2299 / TCP(sport=local_port, dport=12344)
2300 )
Filip Varga18f1e412020-12-03 15:27:40 +01002301 self.pg5.add_stream(p)
2302 self.pg_enable_capture(self.pg_interfaces)
2303 self.pg_start()
2304 capture = self.pg0.get_capture(1)
2305 p = capture[0]
2306 try:
2307 ip = p[IP]
2308 tcp = p[TCP]
2309 self.assertEqual(ip.src, self.pg5.remote_ip4)
2310 self.assertEqual(tcp.sport, local_port)
2311 self.assert_packet_checksums_valid(p)
2312 except:
2313 self.logger.error(ppp("Unexpected or invalid packet:", p))
2314 raise
2315
Klement Sekeradc243ee2021-02-25 16:47:23 +01002316 def test_outside_address_distribution(self):
Vladislav Grishenko579a6fb2023-03-16 19:31:00 +05002317 """NAT44ED outside address distribution based on source address"""
Klement Sekeradc243ee2021-02-25 16:47:23 +01002318
Vladislav Grishenko579a6fb2023-03-16 19:31:00 +05002319 addresses = 65
Klement Sekeradc243ee2021-02-25 16:47:23 +01002320 x = 100
Klement Sekeradc243ee2021-02-25 16:47:23 +01002321
Vladislav Grishenko579a6fb2023-03-16 19:31:00 +05002322 nat_addresses = []
2323 nat_distribution = {}
2324 for i in range(1, addresses):
Klement Sekeradc243ee2021-02-25 16:47:23 +01002325 a = "10.0.0.%d" % i
2326 nat_addresses.append(a)
Vladislav Grishenko579a6fb2023-03-16 19:31:00 +05002327 nat_distribution[a] = set()
Klement Sekeradc243ee2021-02-25 16:47:23 +01002328
2329 self.nat_add_inside_interface(self.pg0)
2330 self.nat_add_outside_interface(self.pg1)
2331
2332 self.vapi.nat44_add_del_address_range(
2333 first_ip_address=nat_addresses[0],
2334 last_ip_address=nat_addresses[-1],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002335 vrf_id=0xFFFFFFFF,
2336 is_add=1,
2337 flags=0,
2338 )
Klement Sekeradc243ee2021-02-25 16:47:23 +01002339
2340 self.pg0.generate_remote_hosts(x)
2341
2342 pkts = []
2343 for i in range(x):
Klement Sekerad2b69972021-03-09 17:53:47 +01002344 info = self.create_packet_info(self.pg0, self.pg1)
2345 payload = self.info_to_payload(info)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002346 p = (
2347 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
2348 / IP(src=self.pg0.remote_hosts[i].ip4, dst=self.pg1.remote_ip4)
2349 / UDP(sport=7000 + i, dport=8000 + i)
2350 / Raw(payload)
2351 )
Klement Sekerad2b69972021-03-09 17:53:47 +01002352 info.data = p
Klement Sekeradc243ee2021-02-25 16:47:23 +01002353 pkts.append(p)
2354
2355 self.pg0.add_stream(pkts)
2356 self.pg_enable_capture(self.pg_interfaces)
2357 self.pg_start()
2358 recvd = self.pg1.get_capture(len(pkts))
Klement Sekerad2b69972021-03-09 17:53:47 +01002359 for p_recvd in recvd:
2360 payload_info = self.payload_to_info(p_recvd[Raw])
2361 packet_index = payload_info.index
2362 info = self._packet_infos[packet_index]
2363 self.assertTrue(info is not None)
2364 self.assertEqual(packet_index, info.index)
2365 p_sent = info.data
Vladislav Grishenko579a6fb2023-03-16 19:31:00 +05002366 self.assertIn(p_recvd[IP].src, nat_distribution)
2367 nat_distribution[p_recvd[IP].src].add(p_sent[IP].src)
2368
2369 var = variance(map(len, nat_distribution.values()), x / addresses)
2370 self.assertLess(var, 0.33, msg="Bad outside address distribution")
Klement Sekeradc243ee2021-02-25 16:47:23 +01002371
Vladislav Grishenko5f694d12022-08-19 20:42:22 +05002372 def test_dynamic_edge_ports(self):
2373 """NAT44ED dynamic translation test: edge ports"""
2374
2375 worker_count = self.vpp_worker_count or 1
2376 port_offset = 1024
2377 port_per_thread = (65536 - port_offset) // worker_count
2378 port_count = port_per_thread * worker_count
2379
2380 # worker thread edge ports
2381 thread_edge_ports = {0, port_offset - 1, 65535}
2382 for i in range(0, worker_count):
2383 port_thread_offset = (port_per_thread * i) + port_offset
2384 for port_range_offset in [0, port_per_thread - 1]:
2385 port = port_thread_offset + port_range_offset
2386 thread_edge_ports.add(port)
2387 thread_drop_ports = set(
2388 filter(
2389 lambda x: x not in range(port_offset, port_offset + port_count),
2390 thread_edge_ports,
2391 )
2392 )
2393
2394 in_if = self.pg7
2395 out_if = self.pg8
2396
2397 self.nat_add_address(self.nat_addr)
2398
2399 try:
2400 self.configure_ip4_interface(in_if, hosts=worker_count)
2401 self.configure_ip4_interface(out_if)
2402
2403 self.nat_add_inside_interface(in_if)
2404 self.nat_add_outside_interface(out_if)
2405
2406 # in2out
2407 tc1 = self.statistics["/nat44-ed/in2out/slowpath/tcp"]
2408 uc1 = self.statistics["/nat44-ed/in2out/slowpath/udp"]
2409 ic1 = self.statistics["/nat44-ed/in2out/slowpath/icmp"]
2410 dc1 = self.statistics["/nat44-ed/in2out/slowpath/drops"]
2411
2412 pkt_count = worker_count * len(thread_edge_ports)
2413
2414 i2o_pkts = [[] for x in range(0, worker_count)]
2415 for i in range(0, worker_count):
2416 remote_host = in_if.remote_hosts[i]
2417 for port in thread_edge_ports:
2418 p = (
2419 Ether(dst=in_if.local_mac, src=in_if.remote_mac)
2420 / IP(src=remote_host.ip4, dst=out_if.remote_ip4)
2421 / TCP(sport=port, dport=port)
2422 )
2423 i2o_pkts[i].append(p)
2424
2425 p = (
2426 Ether(dst=in_if.local_mac, src=in_if.remote_mac)
2427 / IP(src=remote_host.ip4, dst=out_if.remote_ip4)
2428 / UDP(sport=port, dport=port)
2429 )
2430 i2o_pkts[i].append(p)
2431
2432 p = (
2433 Ether(dst=in_if.local_mac, src=in_if.remote_mac)
2434 / IP(src=remote_host.ip4, dst=out_if.remote_ip4)
2435 / ICMP(id=port, seq=port, type="echo-request")
2436 )
2437 i2o_pkts[i].append(p)
2438
2439 for i in range(0, worker_count):
2440 if len(i2o_pkts[i]) > 0:
2441 in_if.add_stream(i2o_pkts[i], worker=i)
2442
2443 self.pg_enable_capture(self.pg_interfaces)
2444 self.pg_start()
2445 capture = out_if.get_capture(pkt_count * 3)
2446 for packet in capture:
2447 self.assert_packet_checksums_valid(packet)
2448 if packet.haslayer(TCP):
2449 self.assert_in_range(
2450 packet[TCP].sport,
2451 port_offset,
2452 port_offset + port_count,
2453 "src TCP port",
2454 )
2455 elif packet.haslayer(UDP):
2456 self.assert_in_range(
2457 packet[UDP].sport,
2458 port_offset,
2459 port_offset + port_count,
2460 "src UDP port",
2461 )
2462 elif packet.haslayer(ICMP):
2463 self.assert_in_range(
2464 packet[ICMP].id,
2465 port_offset,
2466 port_offset + port_count,
2467 "ICMP id",
2468 )
2469 else:
2470 self.fail(
2471 ppp("Unexpected or invalid packet (outside network):", packet)
2472 )
2473
2474 if_idx = in_if.sw_if_index
2475 tc2 = self.statistics["/nat44-ed/in2out/slowpath/tcp"]
2476 uc2 = self.statistics["/nat44-ed/in2out/slowpath/udp"]
2477 ic2 = self.statistics["/nat44-ed/in2out/slowpath/icmp"]
2478 dc2 = self.statistics["/nat44-ed/in2out/slowpath/drops"]
2479
2480 self.assertEqual(tc2[:, if_idx].sum() - tc1[:, if_idx].sum(), pkt_count)
2481 self.assertEqual(uc2[:, if_idx].sum() - uc1[:, if_idx].sum(), pkt_count)
2482 self.assertEqual(ic2[:, if_idx].sum() - ic1[:, if_idx].sum(), pkt_count)
2483 self.assertEqual(dc2[:, if_idx].sum() - dc1[:, if_idx].sum(), 0)
2484
2485 # out2in
2486 tc1 = self.statistics["/nat44-ed/out2in/fastpath/tcp"]
2487 uc1 = self.statistics["/nat44-ed/out2in/fastpath/udp"]
2488 ic1 = self.statistics["/nat44-ed/out2in/fastpath/icmp"]
2489 dc1 = self.statistics["/nat44-ed/out2in/fastpath/drops"]
2490 dc3 = self.statistics["/nat44-ed/out2in/slowpath/drops"]
2491
2492 # replies to unchanged thread ports should pass on each worker,
2493 # excluding packets outside dynamic port range
2494 drop_count = worker_count * len(thread_drop_ports)
2495 pass_count = worker_count * len(thread_edge_ports) - drop_count
2496
2497 o2i_pkts = [[] for x in range(0, worker_count)]
2498 for i in range(0, worker_count):
2499 for port in thread_edge_ports:
2500 p = (
2501 Ether(dst=out_if.local_mac, src=out_if.remote_mac)
2502 / IP(src=out_if.remote_ip4, dst=self.nat_addr)
2503 / TCP(sport=port, dport=port)
2504 )
2505 o2i_pkts[i].append(p)
2506
2507 p = (
2508 Ether(dst=out_if.local_mac, src=out_if.remote_mac)
2509 / IP(src=out_if.remote_ip4, dst=self.nat_addr)
2510 / UDP(sport=port, dport=port)
2511 )
2512 o2i_pkts[i].append(p)
2513
2514 p = (
2515 Ether(dst=out_if.local_mac, src=out_if.remote_mac)
2516 / IP(src=out_if.remote_ip4, dst=self.nat_addr)
2517 / ICMP(id=port, seq=port, type="echo-reply")
2518 )
2519 o2i_pkts[i].append(p)
2520
2521 for i in range(0, worker_count):
2522 if len(o2i_pkts[i]) > 0:
2523 out_if.add_stream(o2i_pkts[i], worker=i)
2524
2525 self.pg_enable_capture(self.pg_interfaces)
2526 self.pg_start()
2527 capture = in_if.get_capture(pass_count * 3)
2528 for packet in capture:
2529 self.assert_packet_checksums_valid(packet)
2530 if packet.haslayer(TCP):
2531 self.assertIn(packet[TCP].dport, thread_edge_ports, "dst TCP port")
2532 self.assertEqual(packet[TCP].dport, packet[TCP].sport, "TCP ports")
2533 elif packet.haslayer(UDP):
2534 self.assertIn(packet[UDP].dport, thread_edge_ports, "dst UDP port")
2535 self.assertEqual(packet[UDP].dport, packet[UDP].sport, "UDP ports")
2536 elif packet.haslayer(ICMP):
2537 self.assertIn(packet[ICMP].id, thread_edge_ports, "ICMP id")
2538 self.assertEqual(packet[ICMP].id, packet[ICMP].seq, "ICMP id & seq")
2539 else:
2540 self.fail(
2541 ppp("Unexpected or invalid packet (inside network):", packet)
2542 )
2543
2544 if_idx = out_if.sw_if_index
2545 tc2 = self.statistics["/nat44-ed/out2in/fastpath/tcp"]
2546 uc2 = self.statistics["/nat44-ed/out2in/fastpath/udp"]
2547 ic2 = self.statistics["/nat44-ed/out2in/fastpath/icmp"]
2548 dc2 = self.statistics["/nat44-ed/out2in/fastpath/drops"]
2549 dc4 = self.statistics["/nat44-ed/out2in/slowpath/drops"]
2550
2551 self.assertEqual(tc2[:, if_idx].sum() - tc1[:, if_idx].sum(), pass_count)
2552 self.assertEqual(uc2[:, if_idx].sum() - uc1[:, if_idx].sum(), pass_count)
2553 self.assertEqual(ic2[:, if_idx].sum() - ic1[:, if_idx].sum(), pass_count)
2554 self.assertEqual(dc2[:, if_idx].sum() - dc1[:, if_idx].sum(), 0)
2555 self.assertEqual(
2556 dc4[:, if_idx].sum() - dc3[:, if_idx].sum(), drop_count * 3
2557 )
2558
2559 finally:
2560 in_if.unconfig()
2561 out_if.unconfig()
2562
Vladislav Grishenko3abb32c2022-12-04 15:02:03 +05002563 def test_delete_interface(self):
2564 """NAT44ED delete nat interface"""
2565
2566 self.nat_add_address(self.nat_addr)
2567
2568 interfaces = self.create_loopback_interfaces(4)
2569 self.nat_add_outside_interface(interfaces[0])
2570 self.nat_add_inside_interface(interfaces[1])
2571 self.nat_add_outside_interface(interfaces[2])
2572 self.nat_add_inside_interface(interfaces[2])
2573 self.vapi.nat44_ed_add_del_output_interface(
2574 sw_if_index=interfaces[3].sw_if_index, is_add=1
2575 )
2576
2577 nat_sw_if_indices = [
2578 i.sw_if_index
2579 for i in self.vapi.nat44_interface_dump()
2580 + list(self.vapi.vpp.details_iter(self.vapi.nat44_ed_output_interface_get))
2581 ]
2582 self.assertEqual(len(nat_sw_if_indices), len(interfaces))
2583
2584 loopbacks = []
2585 for i in interfaces:
2586 # delete nat-enabled interface
2587 self.assertIn(i.sw_if_index, nat_sw_if_indices)
2588 i.remove_vpp_config()
2589
2590 # create interface with the same index
2591 lo = VppLoInterface(self)
2592 loopbacks.append(lo)
2593 self.assertEqual(lo.sw_if_index, i.sw_if_index)
2594
2595 # check interface is not nat-enabled
2596 nat_sw_if_indices = [
2597 i.sw_if_index
2598 for i in self.vapi.nat44_interface_dump()
2599 + list(
2600 self.vapi.vpp.details_iter(self.vapi.nat44_ed_output_interface_get)
2601 )
2602 ]
2603 self.assertNotIn(lo.sw_if_index, nat_sw_if_indices)
2604
2605 for i in loopbacks:
2606 i.remove_vpp_config()
2607
Filip Varga18f1e412020-12-03 15:27:40 +01002608
Dave Wallace670724c2022-09-20 21:52:18 -04002609@tag_fixme_ubuntu2204
Dmitry Valter34fa0ce2024-03-11 10:38:46 +00002610@unittest.skipIf("nat" in config.excluded_plugins, "Exclude NAT plugin tests")
Filip Varga18f1e412020-12-03 15:27:40 +01002611class TestNAT44EDMW(TestNAT44ED):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002612 """NAT44ED MW Test Case"""
2613
Klement Sekera1fbf0342021-03-31 13:38:09 +02002614 vpp_worker_count = 4
2615 max_sessions = 5000
Filip Varga18f1e412020-12-03 15:27:40 +01002616
Filip Varga18f1e412020-12-03 15:27:40 +01002617 def test_dynamic(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002618 """NAT44ED dynamic translation test"""
Klement Sekera1fbf0342021-03-31 13:38:09 +02002619 pkt_count = 1500
2620 tcp_port_offset = 20
2621 udp_port_offset = 20
2622 icmp_id_offset = 20
Filip Varga18f1e412020-12-03 15:27:40 +01002623
2624 self.nat_add_address(self.nat_addr)
2625 self.nat_add_inside_interface(self.pg0)
2626 self.nat_add_outside_interface(self.pg1)
2627
2628 # in2out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002629 tc1 = self.statistics["/nat44-ed/in2out/slowpath/tcp"]
2630 uc1 = self.statistics["/nat44-ed/in2out/slowpath/udp"]
2631 ic1 = self.statistics["/nat44-ed/in2out/slowpath/icmp"]
2632 dc1 = self.statistics["/nat44-ed/in2out/slowpath/drops"]
Filip Varga18f1e412020-12-03 15:27:40 +01002633
Klement Sekera1fbf0342021-03-31 13:38:09 +02002634 i2o_pkts = [[] for x in range(0, self.vpp_worker_count)]
2635
2636 for i in range(pkt_count):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002637 p = (
2638 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
2639 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
2640 / TCP(sport=tcp_port_offset + i, dport=20)
2641 )
Klement Sekera1fbf0342021-03-31 13:38:09 +02002642 i2o_pkts[p[TCP].sport % self.vpp_worker_count].append(p)
2643
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002644 p = (
2645 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
2646 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
2647 / UDP(sport=udp_port_offset + i, dport=20)
2648 )
Klement Sekera1fbf0342021-03-31 13:38:09 +02002649 i2o_pkts[p[UDP].sport % self.vpp_worker_count].append(p)
2650
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002651 p = (
2652 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
2653 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
2654 / ICMP(id=icmp_id_offset + i, type="echo-request")
2655 )
Klement Sekera1fbf0342021-03-31 13:38:09 +02002656 i2o_pkts[p[ICMP].id % self.vpp_worker_count].append(p)
2657
2658 for i in range(0, self.vpp_worker_count):
2659 if len(i2o_pkts[i]) > 0:
2660 self.pg0.add_stream(i2o_pkts[i], worker=i)
2661
Filip Varga18f1e412020-12-03 15:27:40 +01002662 self.pg_enable_capture(self.pg_interfaces)
2663 self.pg_start()
Klement Sekeraff334db2021-05-26 13:02:35 +02002664 capture = self.pg1.get_capture(pkt_count * 3, timeout=5)
Filip Varga18f1e412020-12-03 15:27:40 +01002665
2666 if_idx = self.pg0.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002667 tc2 = self.statistics["/nat44-ed/in2out/slowpath/tcp"]
2668 uc2 = self.statistics["/nat44-ed/in2out/slowpath/udp"]
2669 ic2 = self.statistics["/nat44-ed/in2out/slowpath/icmp"]
2670 dc2 = self.statistics["/nat44-ed/in2out/slowpath/drops"]
Filip Varga18f1e412020-12-03 15:27:40 +01002671
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002672 self.assertEqual(tc2[:, if_idx].sum() - tc1[:, if_idx].sum(), pkt_count)
2673 self.assertEqual(uc2[:, if_idx].sum() - uc1[:, if_idx].sum(), pkt_count)
2674 self.assertEqual(ic2[:, if_idx].sum() - ic1[:, if_idx].sum(), pkt_count)
Klement Sekera3887be72021-03-30 20:29:05 +02002675 self.assertEqual(dc2[:, if_idx].sum() - dc1[:, if_idx].sum(), 0)
Filip Varga18f1e412020-12-03 15:27:40 +01002676
Klement Sekera1fbf0342021-03-31 13:38:09 +02002677 self.logger.info(self.vapi.cli("show trace"))
2678
Filip Varga18f1e412020-12-03 15:27:40 +01002679 # out2in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002680 tc1 = self.statistics["/nat44-ed/out2in/fastpath/tcp"]
2681 uc1 = self.statistics["/nat44-ed/out2in/fastpath/udp"]
2682 ic1 = self.statistics["/nat44-ed/out2in/fastpath/icmp"]
2683 dc1 = self.statistics["/nat44-ed/out2in/fastpath/drops"]
Filip Varga18f1e412020-12-03 15:27:40 +01002684
Klement Sekera1fbf0342021-03-31 13:38:09 +02002685 recvd_tcp_ports = set()
2686 recvd_udp_ports = set()
2687 recvd_icmp_ids = set()
2688
2689 for p in capture:
2690 if TCP in p:
2691 recvd_tcp_ports.add(p[TCP].sport)
2692 if UDP in p:
2693 recvd_udp_ports.add(p[UDP].sport)
2694 if ICMP in p:
2695 recvd_icmp_ids.add(p[ICMP].id)
2696
2697 recvd_tcp_ports = list(recvd_tcp_ports)
2698 recvd_udp_ports = list(recvd_udp_ports)
2699 recvd_icmp_ids = list(recvd_icmp_ids)
2700
2701 o2i_pkts = [[] for x in range(0, self.vpp_worker_count)]
2702 for i in range(pkt_count):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002703 p = (
2704 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
2705 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
2706 / TCP(dport=choice(recvd_tcp_ports), sport=20)
2707 )
Klement Sekera1fbf0342021-03-31 13:38:09 +02002708 o2i_pkts[p[TCP].dport % self.vpp_worker_count].append(p)
2709
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002710 p = (
2711 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
2712 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
2713 / UDP(dport=choice(recvd_udp_ports), sport=20)
2714 )
Klement Sekera1fbf0342021-03-31 13:38:09 +02002715 o2i_pkts[p[UDP].dport % self.vpp_worker_count].append(p)
2716
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002717 p = (
2718 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
2719 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
2720 / ICMP(id=choice(recvd_icmp_ids), type="echo-reply")
2721 )
Klement Sekera1fbf0342021-03-31 13:38:09 +02002722 o2i_pkts[p[ICMP].id % self.vpp_worker_count].append(p)
2723
2724 for i in range(0, self.vpp_worker_count):
2725 if len(o2i_pkts[i]) > 0:
2726 self.pg1.add_stream(o2i_pkts[i], worker=i)
2727
Filip Varga18f1e412020-12-03 15:27:40 +01002728 self.pg_enable_capture(self.pg_interfaces)
2729 self.pg_start()
Klement Sekera1fbf0342021-03-31 13:38:09 +02002730 capture = self.pg0.get_capture(pkt_count * 3)
2731 for packet in capture:
2732 try:
2733 self.assert_packet_checksums_valid(packet)
2734 self.assertEqual(packet[IP].dst, self.pg0.remote_ip4)
2735 if packet.haslayer(TCP):
2736 self.assert_in_range(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002737 packet[TCP].dport,
2738 tcp_port_offset,
2739 tcp_port_offset + pkt_count,
2740 "dst TCP port",
2741 )
Klement Sekera1fbf0342021-03-31 13:38:09 +02002742 elif packet.haslayer(UDP):
2743 self.assert_in_range(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002744 packet[UDP].dport,
2745 udp_port_offset,
2746 udp_port_offset + pkt_count,
2747 "dst UDP port",
2748 )
Klement Sekera1fbf0342021-03-31 13:38:09 +02002749 else:
2750 self.assert_in_range(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002751 packet[ICMP].id,
2752 icmp_id_offset,
2753 icmp_id_offset + pkt_count,
2754 "ICMP id",
2755 )
Klement Sekera1fbf0342021-03-31 13:38:09 +02002756 except:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002757 self.logger.error(
2758 ppp("Unexpected or invalid packet (inside network):", packet)
2759 )
Klement Sekera1fbf0342021-03-31 13:38:09 +02002760 raise
Filip Varga18f1e412020-12-03 15:27:40 +01002761
2762 if_idx = self.pg1.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002763 tc2 = self.statistics["/nat44-ed/out2in/fastpath/tcp"]
2764 uc2 = self.statistics["/nat44-ed/out2in/fastpath/udp"]
2765 ic2 = self.statistics["/nat44-ed/out2in/fastpath/icmp"]
2766 dc2 = self.statistics["/nat44-ed/out2in/fastpath/drops"]
Filip Varga18f1e412020-12-03 15:27:40 +01002767
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002768 self.assertEqual(tc2[:, if_idx].sum() - tc1[:, if_idx].sum(), pkt_count)
2769 self.assertEqual(uc2[:, if_idx].sum() - uc1[:, if_idx].sum(), pkt_count)
2770 self.assertEqual(ic2[:, if_idx].sum() - ic1[:, if_idx].sum(), pkt_count)
Klement Sekera3887be72021-03-30 20:29:05 +02002771 self.assertEqual(dc2[:, if_idx].sum() - dc1[:, if_idx].sum(), 0)
Filip Varga18f1e412020-12-03 15:27:40 +01002772
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002773 sc = self.statistics["/nat44-ed/total-sessions"]
2774 self.assertEqual(
2775 sc[:, 0].sum(),
2776 len(recvd_tcp_ports) + len(recvd_udp_ports) + len(recvd_icmp_ids),
2777 )
Filip Varga18f1e412020-12-03 15:27:40 +01002778
2779 def test_frag_in_order(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002780 """NAT44ED translate fragments arriving in order"""
Filip Varga18f1e412020-12-03 15:27:40 +01002781
2782 self.nat_add_address(self.nat_addr)
2783 self.nat_add_inside_interface(self.pg0)
2784 self.nat_add_outside_interface(self.pg1)
2785
2786 self.frag_in_order(proto=IP_PROTOS.tcp, ignore_port=True)
2787 self.frag_in_order(proto=IP_PROTOS.udp, ignore_port=True)
2788 self.frag_in_order(proto=IP_PROTOS.icmp, ignore_port=True)
2789
2790 def test_frag_in_order_do_not_translate(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002791 """NAT44ED don't translate fragments arriving in order"""
Filip Varga18f1e412020-12-03 15:27:40 +01002792
2793 self.nat_add_address(self.nat_addr)
2794 self.nat_add_inside_interface(self.pg0)
2795 self.nat_add_outside_interface(self.pg1)
2796 self.vapi.nat44_forwarding_enable_disable(enable=True)
2797
2798 self.frag_in_order(proto=IP_PROTOS.tcp, dont_translate=True)
2799
2800 def test_frag_out_of_order(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002801 """NAT44ED translate fragments arriving out of order"""
Filip Varga18f1e412020-12-03 15:27:40 +01002802
2803 self.nat_add_address(self.nat_addr)
2804 self.nat_add_inside_interface(self.pg0)
2805 self.nat_add_outside_interface(self.pg1)
2806
2807 self.frag_out_of_order(proto=IP_PROTOS.tcp, ignore_port=True)
2808 self.frag_out_of_order(proto=IP_PROTOS.udp, ignore_port=True)
2809 self.frag_out_of_order(proto=IP_PROTOS.icmp, ignore_port=True)
2810
2811 def test_frag_in_order_in_plus_out(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002812 """NAT44ED in+out interface fragments in order"""
Filip Varga18f1e412020-12-03 15:27:40 +01002813
2814 in_port = self.random_port()
2815 out_port = self.random_port()
2816
2817 self.nat_add_address(self.nat_addr)
2818 self.nat_add_inside_interface(self.pg0)
2819 self.nat_add_outside_interface(self.pg0)
2820 self.nat_add_inside_interface(self.pg1)
2821 self.nat_add_outside_interface(self.pg1)
2822
2823 # add static mappings for server
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002824 self.nat_add_static_mapping(
2825 self.server_addr, self.nat_addr, in_port, out_port, proto=IP_PROTOS.tcp
2826 )
2827 self.nat_add_static_mapping(
2828 self.server_addr, self.nat_addr, in_port, out_port, proto=IP_PROTOS.udp
2829 )
2830 self.nat_add_static_mapping(
2831 self.server_addr, self.nat_addr, proto=IP_PROTOS.icmp
2832 )
Filip Varga18f1e412020-12-03 15:27:40 +01002833
2834 # run tests for each protocol
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002835 self.frag_in_order_in_plus_out(
2836 self.server_addr, self.nat_addr, in_port, out_port, IP_PROTOS.tcp
2837 )
2838 self.frag_in_order_in_plus_out(
2839 self.server_addr, self.nat_addr, in_port, out_port, IP_PROTOS.udp
2840 )
2841 self.frag_in_order_in_plus_out(
2842 self.server_addr, self.nat_addr, in_port, out_port, IP_PROTOS.icmp
2843 )
Filip Varga18f1e412020-12-03 15:27:40 +01002844
2845 def test_frag_out_of_order_in_plus_out(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002846 """NAT44ED in+out interface fragments out of order"""
Filip Varga18f1e412020-12-03 15:27:40 +01002847
2848 in_port = self.random_port()
2849 out_port = self.random_port()
2850
2851 self.nat_add_address(self.nat_addr)
2852 self.nat_add_inside_interface(self.pg0)
2853 self.nat_add_outside_interface(self.pg0)
2854 self.nat_add_inside_interface(self.pg1)
2855 self.nat_add_outside_interface(self.pg1)
2856
2857 # add static mappings for server
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002858 self.nat_add_static_mapping(
2859 self.server_addr, self.nat_addr, in_port, out_port, proto=IP_PROTOS.tcp
2860 )
2861 self.nat_add_static_mapping(
2862 self.server_addr, self.nat_addr, in_port, out_port, proto=IP_PROTOS.udp
2863 )
2864 self.nat_add_static_mapping(
2865 self.server_addr, self.nat_addr, proto=IP_PROTOS.icmp
2866 )
Filip Varga18f1e412020-12-03 15:27:40 +01002867
2868 # run tests for each protocol
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002869 self.frag_out_of_order_in_plus_out(
2870 self.server_addr, self.nat_addr, in_port, out_port, IP_PROTOS.tcp
2871 )
2872 self.frag_out_of_order_in_plus_out(
2873 self.server_addr, self.nat_addr, in_port, out_port, IP_PROTOS.udp
2874 )
2875 self.frag_out_of_order_in_plus_out(
2876 self.server_addr, self.nat_addr, in_port, out_port, IP_PROTOS.icmp
2877 )
Filip Varga18f1e412020-12-03 15:27:40 +01002878
2879 def test_reass_hairpinning(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002880 """NAT44ED fragments hairpinning"""
Filip Varga18f1e412020-12-03 15:27:40 +01002881
2882 server_addr = self.pg0.remote_hosts[1].ip4
2883
2884 host_in_port = self.random_port()
2885 server_in_port = self.random_port()
2886 server_out_port = self.random_port()
2887
2888 self.nat_add_address(self.nat_addr)
2889 self.nat_add_inside_interface(self.pg0)
2890 self.nat_add_outside_interface(self.pg1)
2891
2892 # add static mapping for server
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002893 self.nat_add_static_mapping(
2894 server_addr,
2895 self.nat_addr,
2896 server_in_port,
2897 server_out_port,
2898 proto=IP_PROTOS.tcp,
2899 )
2900 self.nat_add_static_mapping(
2901 server_addr,
2902 self.nat_addr,
2903 server_in_port,
2904 server_out_port,
2905 proto=IP_PROTOS.udp,
2906 )
Filip Varga18f1e412020-12-03 15:27:40 +01002907 self.nat_add_static_mapping(server_addr, self.nat_addr)
2908
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002909 self.reass_hairpinning(
2910 server_addr,
2911 server_in_port,
2912 server_out_port,
2913 host_in_port,
2914 proto=IP_PROTOS.tcp,
2915 ignore_port=True,
2916 )
2917 self.reass_hairpinning(
2918 server_addr,
2919 server_in_port,
2920 server_out_port,
2921 host_in_port,
2922 proto=IP_PROTOS.udp,
2923 ignore_port=True,
2924 )
2925 self.reass_hairpinning(
2926 server_addr,
2927 server_in_port,
2928 server_out_port,
2929 host_in_port,
2930 proto=IP_PROTOS.icmp,
2931 ignore_port=True,
2932 )
Filip Varga18f1e412020-12-03 15:27:40 +01002933
2934 def test_session_limit_per_vrf(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002935 """NAT44ED per vrf session limit"""
Filip Varga18f1e412020-12-03 15:27:40 +01002936
2937 inside = self.pg0
2938 inside_vrf10 = self.pg2
2939 outside = self.pg1
2940
2941 limit = 5
2942
Vladislav Grishenko5b3e04c2022-09-05 10:32:46 +05002943 # 2 interfaces pg0, pg1 (vrf10, limit 5 tcp sessions)
Filip Varga18f1e412020-12-03 15:27:40 +01002944 self.vapi.nat44_set_session_limit(session_limit=limit, vrf_id=10)
2945
Vladislav Grishenko5b3e04c2022-09-05 10:32:46 +05002946 # expect error when bad is specified
2947 with self.vapi.assert_negative_api_retval():
2948 self.vapi.nat44_set_session_limit(session_limit=limit, vrf_id=20)
2949
Filip Varga18f1e412020-12-03 15:27:40 +01002950 self.nat_add_inside_interface(inside)
2951 self.nat_add_inside_interface(inside_vrf10)
2952 self.nat_add_outside_interface(outside)
2953
2954 # vrf independent
2955 self.nat_add_interface_address(outside)
2956
2957 # BUG: causing core dump - when bad vrf_id is specified
2958 # self.nat_add_address(outside.local_ip4, vrf_id=20)
2959
2960 stream = self.create_tcp_stream(inside_vrf10, outside, limit * 2)
2961 inside_vrf10.add_stream(stream)
2962
2963 self.pg_enable_capture(self.pg_interfaces)
2964 self.pg_start()
2965
2966 capture = outside.get_capture(limit)
2967
2968 stream = self.create_tcp_stream(inside, outside, limit * 2)
2969 inside.add_stream(stream)
2970
2971 self.pg_enable_capture(self.pg_interfaces)
2972 self.pg_start()
2973
2974 capture = outside.get_capture(len(stream))
2975
Filip Varga18f1e412020-12-03 15:27:40 +01002976 def test_show_max_translations(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002977 """NAT44ED API test - max translations per thread"""
Filip Vargab6810822022-02-15 11:56:07 -08002978 config = self.vapi.nat44_show_running_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002979 self.assertEqual(self.max_sessions, config.sessions)
Filip Varga18f1e412020-12-03 15:27:40 +01002980
2981 def test_lru_cleanup(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002982 """NAT44ED LRU cleanup algorithm"""
Filip Varga18f1e412020-12-03 15:27:40 +01002983
2984 self.nat_add_address(self.nat_addr)
2985 self.nat_add_inside_interface(self.pg0)
2986 self.nat_add_outside_interface(self.pg1)
2987
2988 self.vapi.nat_set_timeouts(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002989 udp=1, tcp_established=7440, tcp_transitory=30, icmp=1
2990 )
Filip Varga18f1e412020-12-03 15:27:40 +01002991
2992 tcp_port_out = self.init_tcp_session(self.pg0, self.pg1, 2000, 80)
2993 pkts = []
2994 for i in range(0, self.max_sessions - 1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002995 p = (
2996 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
2997 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, ttl=64)
2998 / UDP(sport=7000 + i, dport=80)
2999 )
Filip Varga18f1e412020-12-03 15:27:40 +01003000 pkts.append(p)
3001
3002 self.pg0.add_stream(pkts)
3003 self.pg_enable_capture(self.pg_interfaces)
3004 self.pg_start()
3005 self.pg1.get_capture(len(pkts))
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02003006 self.virtual_sleep(1.5, "wait for timeouts")
Filip Varga18f1e412020-12-03 15:27:40 +01003007
3008 pkts = []
3009 for i in range(0, self.max_sessions - 1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003010 p = (
3011 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3012 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, ttl=64)
3013 / ICMP(id=8000 + i, type="echo-request")
3014 )
Filip Varga18f1e412020-12-03 15:27:40 +01003015 pkts.append(p)
3016
3017 self.pg0.add_stream(pkts)
3018 self.pg_enable_capture(self.pg_interfaces)
3019 self.pg_start()
3020 self.pg1.get_capture(len(pkts))
3021
3022 def test_session_rst_timeout(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003023 """NAT44ED session RST timeouts"""
Filip Varga18f1e412020-12-03 15:27:40 +01003024
3025 self.nat_add_address(self.nat_addr)
3026 self.nat_add_inside_interface(self.pg0)
3027 self.nat_add_outside_interface(self.pg1)
3028
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003029 self.vapi.nat_set_timeouts(
3030 udp=300, tcp_established=7440, tcp_transitory=5, icmp=60
3031 )
Filip Varga18f1e412020-12-03 15:27:40 +01003032
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003033 self.init_tcp_session(
3034 self.pg0, self.pg1, self.tcp_port_in, self.tcp_external_port
3035 )
3036 p = (
3037 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3038 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3039 / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, flags="R")
3040 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003041 self.send_and_expect(self.pg0, p, self.pg1)
Filip Varga18f1e412020-12-03 15:27:40 +01003042
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02003043 self.virtual_sleep(6)
Filip Varga18f1e412020-12-03 15:27:40 +01003044
Klement Sekera56c492a2022-01-10 21:57:27 +00003045 # The session is already closed
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003046 p = (
3047 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3048 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3049 / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, flags="P")
3050 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003051 self.send_and_assert_no_replies(self.pg0, p, self.pg1)
3052
3053 # The session can be re-opened
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003054 p = (
3055 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3056 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3057 / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, flags="S")
3058 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003059 self.send_and_expect(self.pg0, p, self.pg1)
3060
3061 def test_session_rst_established_timeout(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003062 """NAT44ED session RST timeouts"""
Klement Sekera56c492a2022-01-10 21:57:27 +00003063
3064 self.nat_add_address(self.nat_addr)
3065 self.nat_add_inside_interface(self.pg0)
3066 self.nat_add_outside_interface(self.pg1)
3067
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003068 self.vapi.nat_set_timeouts(
3069 udp=300, tcp_established=7440, tcp_transitory=5, icmp=60
3070 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003071
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003072 self.init_tcp_session(
3073 self.pg0, self.pg1, self.tcp_port_in, self.tcp_external_port
3074 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003075
3076 # Wait at least the transitory time, the session is in established
Ole Troan52974472022-03-17 11:58:38 +01003077 # state anyway. RST followed by a data packet should move it to
3078 # transitory state.
Klement Sekera56c492a2022-01-10 21:57:27 +00003079 self.virtual_sleep(6)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003080 p = (
3081 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3082 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3083 / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, flags="R")
3084 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003085 self.send_and_expect(self.pg0, p, self.pg1)
3086
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003087 p = (
3088 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3089 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3090 / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, flags="P")
3091 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003092 self.send_and_expect(self.pg0, p, self.pg1)
3093
Klement Sekera56c492a2022-01-10 21:57:27 +00003094 # State is transitory, session should be closed after 6 seconds
3095 self.virtual_sleep(6)
3096
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003097 p = (
3098 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3099 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3100 / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, flags="P")
3101 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003102 self.send_and_assert_no_replies(self.pg0, p, self.pg1)
Filip Varga18f1e412020-12-03 15:27:40 +01003103
3104 def test_dynamic_out_of_ports(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003105 """NAT44ED dynamic translation test: out of ports"""
Filip Varga18f1e412020-12-03 15:27:40 +01003106
3107 self.nat_add_inside_interface(self.pg0)
3108 self.nat_add_outside_interface(self.pg1)
3109
3110 # in2out and no NAT addresses added
Filip Varga18f1e412020-12-03 15:27:40 +01003111 pkts = self.create_stream_in(self.pg0, self.pg1)
Filip Varga18f1e412020-12-03 15:27:40 +01003112
Klement Sekeraad3187f2022-02-18 10:34:35 +00003113 self.send_and_assert_no_replies(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003114 self.pg0,
3115 pkts,
3116 msg="i2o pkts",
3117 stats_diff=self.no_diff
3118 | {
Klement Sekeraad3187f2022-02-18 10:34:35 +00003119 "err": {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003120 "/err/nat44-ed-in2out-slowpath/out of ports": len(pkts),
Klement Sekeraad3187f2022-02-18 10:34:35 +00003121 },
3122 self.pg0.sw_if_index: {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003123 "/nat44-ed/in2out/slowpath/drops": len(pkts),
Klement Sekeraad3187f2022-02-18 10:34:35 +00003124 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003125 },
Klement Sekeraad3187f2022-02-18 10:34:35 +00003126 )
Filip Varga18f1e412020-12-03 15:27:40 +01003127
3128 # in2out after NAT addresses added
3129 self.nat_add_address(self.nat_addr)
3130
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003131 tcpn, udpn, icmpn = (
3132 sum(x) for x in zip(*((TCP in p, UDP in p, ICMP in p) for p in pkts))
3133 )
Filip Varga18f1e412020-12-03 15:27:40 +01003134
Klement Sekeraad3187f2022-02-18 10:34:35 +00003135 self.send_and_expect(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003136 self.pg0,
3137 pkts,
3138 self.pg1,
3139 msg="i2o pkts",
3140 stats_diff=self.no_diff
3141 | {
Klement Sekeraad3187f2022-02-18 10:34:35 +00003142 "err": {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003143 "/err/nat44-ed-in2out-slowpath/out of ports": 0,
Klement Sekeraad3187f2022-02-18 10:34:35 +00003144 },
3145 self.pg0.sw_if_index: {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003146 "/nat44-ed/in2out/slowpath/drops": 0,
3147 "/nat44-ed/in2out/slowpath/tcp": tcpn,
3148 "/nat44-ed/in2out/slowpath/udp": udpn,
3149 "/nat44-ed/in2out/slowpath/icmp": icmpn,
Klement Sekeraad3187f2022-02-18 10:34:35 +00003150 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003151 },
Klement Sekeraad3187f2022-02-18 10:34:35 +00003152 )
Filip Varga18f1e412020-12-03 15:27:40 +01003153
3154 def test_unknown_proto(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003155 """NAT44ED translate packet with unknown protocol"""
Filip Varga18f1e412020-12-03 15:27:40 +01003156
3157 self.nat_add_address(self.nat_addr)
3158 self.nat_add_inside_interface(self.pg0)
3159 self.nat_add_outside_interface(self.pg1)
3160
3161 # in2out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003162 p = (
3163 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3164 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3165 / TCP(sport=self.tcp_port_in, dport=20)
3166 )
Filip Varga18f1e412020-12-03 15:27:40 +01003167 self.pg0.add_stream(p)
3168 self.pg_enable_capture(self.pg_interfaces)
3169 self.pg_start()
3170 p = self.pg1.get_capture(1)
3171
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003172 p = (
3173 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3174 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3175 / GRE()
3176 / IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4)
3177 / TCP(sport=1234, dport=1234)
3178 )
Filip Varga18f1e412020-12-03 15:27:40 +01003179 self.pg0.add_stream(p)
3180 self.pg_enable_capture(self.pg_interfaces)
3181 self.pg_start()
3182 p = self.pg1.get_capture(1)
3183 packet = p[0]
3184 try:
3185 self.assertEqual(packet[IP].src, self.nat_addr)
3186 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
3187 self.assertEqual(packet.haslayer(GRE), 1)
3188 self.assert_packet_checksums_valid(packet)
3189 except:
3190 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3191 raise
3192
3193 # out2in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003194 p = (
3195 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
3196 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
3197 / GRE()
3198 / IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4)
3199 / TCP(sport=1234, dport=1234)
3200 )
Filip Varga18f1e412020-12-03 15:27:40 +01003201 self.pg1.add_stream(p)
3202 self.pg_enable_capture(self.pg_interfaces)
3203 self.pg_start()
3204 p = self.pg0.get_capture(1)
3205 packet = p[0]
3206 try:
3207 self.assertEqual(packet[IP].src, self.pg1.remote_ip4)
3208 self.assertEqual(packet[IP].dst, self.pg0.remote_ip4)
3209 self.assertEqual(packet.haslayer(GRE), 1)
3210 self.assert_packet_checksums_valid(packet)
3211 except:
3212 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3213 raise
3214
3215 def test_hairpinning_unknown_proto(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003216 """NAT44ED translate packet with unknown protocol - hairpinning"""
Filip Varga18f1e412020-12-03 15:27:40 +01003217 host = self.pg0.remote_hosts[0]
3218 server = self.pg0.remote_hosts[1]
3219 host_in_port = 1234
3220 server_out_port = 8765
3221 server_nat_ip = "10.0.0.11"
3222
3223 self.nat_add_address(self.nat_addr)
3224 self.nat_add_inside_interface(self.pg0)
3225 self.nat_add_outside_interface(self.pg1)
3226
3227 # add static mapping for server
3228 self.nat_add_static_mapping(server.ip4, server_nat_ip)
3229
3230 # host to server
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003231 p = (
3232 Ether(src=host.mac, dst=self.pg0.local_mac)
3233 / IP(src=host.ip4, dst=server_nat_ip)
3234 / TCP(sport=host_in_port, dport=server_out_port)
3235 )
Filip Varga18f1e412020-12-03 15:27:40 +01003236 self.pg0.add_stream(p)
3237 self.pg_enable_capture(self.pg_interfaces)
3238 self.pg_start()
3239 self.pg0.get_capture(1)
3240
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003241 p = (
3242 Ether(dst=self.pg0.local_mac, src=host.mac)
3243 / IP(src=host.ip4, dst=server_nat_ip)
3244 / GRE()
3245 / IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4)
3246 / TCP(sport=1234, dport=1234)
3247 )
Filip Varga18f1e412020-12-03 15:27:40 +01003248 self.pg0.add_stream(p)
3249 self.pg_enable_capture(self.pg_interfaces)
3250 self.pg_start()
3251 p = self.pg0.get_capture(1)
3252 packet = p[0]
3253 try:
3254 self.assertEqual(packet[IP].src, self.nat_addr)
3255 self.assertEqual(packet[IP].dst, server.ip4)
3256 self.assertEqual(packet.haslayer(GRE), 1)
3257 self.assert_packet_checksums_valid(packet)
3258 except:
3259 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3260 raise
3261
3262 # server to host
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003263 p = (
3264 Ether(dst=self.pg0.local_mac, src=server.mac)
3265 / IP(src=server.ip4, dst=self.nat_addr)
3266 / GRE()
3267 / IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4)
3268 / TCP(sport=1234, dport=1234)
3269 )
Filip Varga18f1e412020-12-03 15:27:40 +01003270 self.pg0.add_stream(p)
3271 self.pg_enable_capture(self.pg_interfaces)
3272 self.pg_start()
3273 p = self.pg0.get_capture(1)
3274 packet = p[0]
3275 try:
3276 self.assertEqual(packet[IP].src, server_nat_ip)
3277 self.assertEqual(packet[IP].dst, host.ip4)
3278 self.assertEqual(packet.haslayer(GRE), 1)
3279 self.assert_packet_checksums_valid(packet)
3280 except:
3281 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3282 raise
3283
3284 def test_output_feature_and_service(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003285 """NAT44ED interface output feature and services"""
3286 external_addr = "1.2.3.4"
Filip Varga18f1e412020-12-03 15:27:40 +01003287 external_port = 80
3288 local_port = 8080
3289
3290 self.vapi.nat44_forwarding_enable_disable(enable=1)
3291 self.nat_add_address(self.nat_addr)
3292 flags = self.config_flags.NAT_IS_ADDR_ONLY
3293 self.vapi.nat44_add_del_identity_mapping(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003294 ip_address=self.pg1.remote_ip4,
3295 sw_if_index=0xFFFFFFFF,
3296 flags=flags,
3297 is_add=1,
3298 )
Filip Varga18f1e412020-12-03 15:27:40 +01003299 flags = self.config_flags.NAT_IS_OUT2IN_ONLY
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003300 self.nat_add_static_mapping(
3301 self.pg0.remote_ip4,
3302 external_addr,
3303 local_port,
3304 external_port,
3305 proto=IP_PROTOS.tcp,
3306 flags=flags,
3307 )
Filip Varga18f1e412020-12-03 15:27:40 +01003308
3309 self.nat_add_inside_interface(self.pg0)
3310 self.nat_add_outside_interface(self.pg0)
Filip Varga9c25eb12021-10-21 13:00:27 +02003311 self.vapi.nat44_ed_add_del_output_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003312 sw_if_index=self.pg1.sw_if_index, is_add=1
3313 )
Filip Varga18f1e412020-12-03 15:27:40 +01003314
3315 # from client to service
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003316 p = (
3317 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3318 / IP(src=self.pg1.remote_ip4, dst=external_addr)
3319 / TCP(sport=12345, dport=external_port)
3320 )
Filip Varga18f1e412020-12-03 15:27:40 +01003321 self.pg1.add_stream(p)
3322 self.pg_enable_capture(self.pg_interfaces)
3323 self.pg_start()
3324 capture = self.pg0.get_capture(1)
3325 p = capture[0]
3326 try:
3327 ip = p[IP]
3328 tcp = p[TCP]
3329 self.assertEqual(ip.dst, self.pg0.remote_ip4)
3330 self.assertEqual(tcp.dport, local_port)
3331 self.assert_packet_checksums_valid(p)
3332 except:
3333 self.logger.error(ppp("Unexpected or invalid packet:", p))
3334 raise
3335
3336 # from service back to client
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003337 p = (
3338 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3339 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3340 / TCP(sport=local_port, dport=12345)
3341 )
Filip Varga18f1e412020-12-03 15:27:40 +01003342 self.pg0.add_stream(p)
3343 self.pg_enable_capture(self.pg_interfaces)
3344 self.pg_start()
3345 capture = self.pg1.get_capture(1)
3346 p = capture[0]
3347 try:
3348 ip = p[IP]
3349 tcp = p[TCP]
3350 self.assertEqual(ip.src, external_addr)
3351 self.assertEqual(tcp.sport, external_port)
3352 self.assert_packet_checksums_valid(p)
3353 except:
3354 self.logger.error(ppp("Unexpected or invalid packet:", p))
3355 raise
3356
3357 # from local network host to external network
3358 pkts = self.create_stream_in(self.pg0, self.pg1)
3359 self.pg0.add_stream(pkts)
3360 self.pg_enable_capture(self.pg_interfaces)
3361 self.pg_start()
3362 capture = self.pg1.get_capture(len(pkts))
3363 self.verify_capture_out(capture, ignore_port=True)
3364 pkts = self.create_stream_in(self.pg0, self.pg1)
3365 self.pg0.add_stream(pkts)
3366 self.pg_enable_capture(self.pg_interfaces)
3367 self.pg_start()
3368 capture = self.pg1.get_capture(len(pkts))
3369 self.verify_capture_out(capture, ignore_port=True)
3370
3371 # from external network back to local network host
3372 pkts = self.create_stream_out(self.pg1)
3373 self.pg1.add_stream(pkts)
3374 self.pg_enable_capture(self.pg_interfaces)
3375 self.pg_start()
3376 capture = self.pg0.get_capture(len(pkts))
3377 self.verify_capture_in(capture, self.pg0)
3378
3379 def test_output_feature_and_service3(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003380 """NAT44ED interface output feature and DST NAT"""
3381 external_addr = "1.2.3.4"
Filip Varga18f1e412020-12-03 15:27:40 +01003382 external_port = 80
3383 local_port = 8080
3384
3385 self.vapi.nat44_forwarding_enable_disable(enable=1)
3386 self.nat_add_address(self.nat_addr)
3387 flags = self.config_flags.NAT_IS_OUT2IN_ONLY
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003388 self.nat_add_static_mapping(
3389 self.pg1.remote_ip4,
3390 external_addr,
3391 local_port,
3392 external_port,
3393 proto=IP_PROTOS.tcp,
3394 flags=flags,
3395 )
Filip Varga18f1e412020-12-03 15:27:40 +01003396
3397 self.nat_add_inside_interface(self.pg0)
3398 self.nat_add_outside_interface(self.pg0)
Filip Varga9c25eb12021-10-21 13:00:27 +02003399 self.vapi.nat44_ed_add_del_output_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003400 sw_if_index=self.pg1.sw_if_index, is_add=1
3401 )
Filip Varga18f1e412020-12-03 15:27:40 +01003402
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003403 p = (
3404 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3405 / IP(src=self.pg0.remote_ip4, dst=external_addr)
3406 / TCP(sport=12345, dport=external_port)
3407 )
Filip Varga18f1e412020-12-03 15:27:40 +01003408 self.pg0.add_stream(p)
3409 self.pg_enable_capture(self.pg_interfaces)
3410 self.pg_start()
3411 capture = self.pg1.get_capture(1)
3412 p = capture[0]
3413 try:
3414 ip = p[IP]
3415 tcp = p[TCP]
3416 self.assertEqual(ip.src, self.pg0.remote_ip4)
3417 self.assertEqual(tcp.sport, 12345)
3418 self.assertEqual(ip.dst, self.pg1.remote_ip4)
3419 self.assertEqual(tcp.dport, local_port)
3420 self.assert_packet_checksums_valid(p)
3421 except:
3422 self.logger.error(ppp("Unexpected or invalid packet:", p))
3423 raise
3424
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003425 p = (
3426 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3427 / IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4)
3428 / TCP(sport=local_port, dport=12345)
3429 )
Filip Varga18f1e412020-12-03 15:27:40 +01003430 self.pg1.add_stream(p)
3431 self.pg_enable_capture(self.pg_interfaces)
3432 self.pg_start()
3433 capture = self.pg0.get_capture(1)
3434 p = capture[0]
3435 try:
3436 ip = p[IP]
3437 tcp = p[TCP]
3438 self.assertEqual(ip.src, external_addr)
3439 self.assertEqual(tcp.sport, external_port)
3440 self.assertEqual(ip.dst, self.pg0.remote_ip4)
3441 self.assertEqual(tcp.dport, 12345)
3442 self.assert_packet_checksums_valid(p)
3443 except:
3444 self.logger.error(ppp("Unexpected or invalid packet:", p))
3445 raise
3446
3447 def test_self_twice_nat_lb_negative(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003448 """NAT44ED Self Twice NAT local service load balancing (negative test)"""
3449 self.twice_nat_common(lb=True, self_twice_nat=True, same_pg=True, client_id=2)
Filip Varga18f1e412020-12-03 15:27:40 +01003450
3451 def test_self_twice_nat_negative(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003452 """NAT44ED Self Twice NAT (negative test)"""
Filip Varga18f1e412020-12-03 15:27:40 +01003453 self.twice_nat_common(self_twice_nat=True)
3454
3455 def test_static_lb_multi_clients(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003456 """NAT44ED local service load balancing - multiple clients"""
Filip Varga18f1e412020-12-03 15:27:40 +01003457
3458 external_addr = self.nat_addr
3459 external_port = 80
3460 local_port = 8080
3461 server1 = self.pg0.remote_hosts[0]
3462 server2 = self.pg0.remote_hosts[1]
3463 server3 = self.pg0.remote_hosts[2]
3464
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003465 locals = [
3466 {"addr": server1.ip4, "port": local_port, "probability": 90, "vrf_id": 0},
3467 {"addr": server2.ip4, "port": local_port, "probability": 10, "vrf_id": 0},
3468 ]
Filip Varga18f1e412020-12-03 15:27:40 +01003469
3470 flags = self.config_flags.NAT_IS_INSIDE
3471 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003472 sw_if_index=self.pg0.sw_if_index, flags=flags, is_add=1
3473 )
Filip Varga18f1e412020-12-03 15:27:40 +01003474 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003475 sw_if_index=self.pg1.sw_if_index, is_add=1
3476 )
Filip Varga18f1e412020-12-03 15:27:40 +01003477
3478 self.nat_add_address(self.nat_addr)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003479 self.vapi.nat44_add_del_lb_static_mapping(
3480 is_add=1,
3481 external_addr=external_addr,
3482 external_port=external_port,
3483 protocol=IP_PROTOS.tcp,
3484 local_num=len(locals),
3485 locals=locals,
3486 )
Filip Varga18f1e412020-12-03 15:27:40 +01003487
3488 server1_n = 0
3489 server2_n = 0
3490 clients = ip4_range(self.pg1.remote_ip4, 10, 50)
3491 pkts = []
3492 for client in clients:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003493 p = (
3494 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3495 / IP(src=client, dst=self.nat_addr)
3496 / TCP(sport=12345, dport=external_port)
3497 )
Filip Varga18f1e412020-12-03 15:27:40 +01003498 pkts.append(p)
3499 self.pg1.add_stream(pkts)
3500 self.pg_enable_capture(self.pg_interfaces)
3501 self.pg_start()
3502 capture = self.pg0.get_capture(len(pkts))
3503 for p in capture:
3504 if p[IP].dst == server1.ip4:
3505 server1_n += 1
3506 else:
3507 server2_n += 1
Klement Sekera1fbf0342021-03-31 13:38:09 +02003508 self.assertGreaterEqual(server1_n, server2_n)
Filip Varga18f1e412020-12-03 15:27:40 +01003509
3510 local = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003511 "addr": server3.ip4,
3512 "port": local_port,
3513 "probability": 20,
3514 "vrf_id": 0,
Filip Varga18f1e412020-12-03 15:27:40 +01003515 }
3516
3517 # add new back-end
3518 self.vapi.nat44_lb_static_mapping_add_del_local(
3519 is_add=1,
3520 external_addr=external_addr,
3521 external_port=external_port,
3522 local=local,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003523 protocol=IP_PROTOS.tcp,
3524 )
Filip Varga18f1e412020-12-03 15:27:40 +01003525 server1_n = 0
3526 server2_n = 0
3527 server3_n = 0
3528 clients = ip4_range(self.pg1.remote_ip4, 60, 110)
3529 pkts = []
3530 for client in clients:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003531 p = (
3532 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3533 / IP(src=client, dst=self.nat_addr)
3534 / TCP(sport=12346, dport=external_port)
3535 )
Filip Varga18f1e412020-12-03 15:27:40 +01003536 pkts.append(p)
3537 self.assertGreater(len(pkts), 0)
3538 self.pg1.add_stream(pkts)
3539 self.pg_enable_capture(self.pg_interfaces)
3540 self.pg_start()
3541 capture = self.pg0.get_capture(len(pkts))
3542 for p in capture:
3543 if p[IP].dst == server1.ip4:
3544 server1_n += 1
3545 elif p[IP].dst == server2.ip4:
3546 server2_n += 1
3547 else:
3548 server3_n += 1
3549 self.assertGreater(server1_n, 0)
3550 self.assertGreater(server2_n, 0)
3551 self.assertGreater(server3_n, 0)
3552
3553 local = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003554 "addr": server2.ip4,
3555 "port": local_port,
3556 "probability": 10,
3557 "vrf_id": 0,
Filip Varga18f1e412020-12-03 15:27:40 +01003558 }
3559
3560 # remove one back-end
3561 self.vapi.nat44_lb_static_mapping_add_del_local(
3562 is_add=0,
3563 external_addr=external_addr,
3564 external_port=external_port,
3565 local=local,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003566 protocol=IP_PROTOS.tcp,
3567 )
Filip Varga18f1e412020-12-03 15:27:40 +01003568 server1_n = 0
3569 server2_n = 0
3570 server3_n = 0
3571 self.pg1.add_stream(pkts)
3572 self.pg_enable_capture(self.pg_interfaces)
3573 self.pg_start()
3574 capture = self.pg0.get_capture(len(pkts))
3575 for p in capture:
3576 if p[IP].dst == server1.ip4:
3577 server1_n += 1
3578 elif p[IP].dst == server2.ip4:
3579 server2_n += 1
3580 else:
3581 server3_n += 1
3582 self.assertGreater(server1_n, 0)
3583 self.assertEqual(server2_n, 0)
3584 self.assertGreater(server3_n, 0)
3585
Klement Sekera56c492a2022-01-10 21:57:27 +00003586 # put zzz in front of syslog test name so that it runs as a last test
3587 # setting syslog sender cannot be undone and if it is set, it messes
3588 # with self.send_and_assert_no_replies functionality
3589 def test_zzz_syslog_sess(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003590 """NAT44ED Test syslog session creation and deletion"""
3591 self.vapi.syslog_set_filter(self.syslog_severity.SYSLOG_API_SEVERITY_INFO)
Filip Varga18f1e412020-12-03 15:27:40 +01003592 self.vapi.syslog_set_sender(self.pg3.local_ip4, self.pg3.remote_ip4)
3593
3594 self.nat_add_address(self.nat_addr)
3595 self.nat_add_inside_interface(self.pg0)
3596 self.nat_add_outside_interface(self.pg1)
3597
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003598 p = (
3599 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3600 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3601 / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port)
3602 )
Filip Varga18f1e412020-12-03 15:27:40 +01003603 self.pg0.add_stream(p)
3604 self.pg_enable_capture(self.pg_interfaces)
3605 self.pg_start()
3606 capture = self.pg1.get_capture(1)
3607 self.tcp_port_out = capture[0][TCP].sport
3608 capture = self.pg3.get_capture(1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003609 self.verify_syslog_sess(capture[0][Raw].load, "SADD")
Filip Varga18f1e412020-12-03 15:27:40 +01003610
3611 self.pg_enable_capture(self.pg_interfaces)
3612 self.pg_start()
3613 self.nat_add_address(self.nat_addr, is_add=0)
3614 capture = self.pg3.get_capture(1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003615 self.verify_syslog_sess(capture[0][Raw].load, "SDEL")
Klement Sekera56c492a2022-01-10 21:57:27 +00003616
3617 # put zzz in front of syslog test name so that it runs as a last test
3618 # setting syslog sender cannot be undone and if it is set, it messes
3619 # with self.send_and_assert_no_replies functionality
3620 def test_zzz_syslog_sess_reopen(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003621 """Syslog events for session reopen"""
3622 self.vapi.syslog_set_filter(self.syslog_severity.SYSLOG_API_SEVERITY_INFO)
Klement Sekera56c492a2022-01-10 21:57:27 +00003623 self.vapi.syslog_set_sender(self.pg3.local_ip4, self.pg3.remote_ip4)
3624
3625 self.nat_add_address(self.nat_addr)
3626 self.nat_add_inside_interface(self.pg0)
3627 self.nat_add_outside_interface(self.pg1)
3628
3629 # SYN in2out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003630 p = (
3631 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3632 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3633 / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port)
3634 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003635 capture = self.send_and_expect(self.pg0, p, self.pg1)[0]
3636 self.tcp_port_out = capture[0][TCP].sport
3637 capture = self.pg3.get_capture(1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003638 self.verify_syslog_sess(capture[0][Raw].load, "SADD")
Klement Sekera56c492a2022-01-10 21:57:27 +00003639
3640 # SYN out2in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003641 p = (
3642 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
3643 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
3644 / TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, flags="SA")
3645 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003646 self.send_and_expect(self.pg1, p, self.pg0)
3647
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003648 p = (
3649 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3650 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3651 / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, flags="A")
3652 )
Ole Troan52974472022-03-17 11:58:38 +01003653 self.send_and_expect(self.pg0, p, self.pg1)
3654
Klement Sekera56c492a2022-01-10 21:57:27 +00003655 # FIN in2out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003656 p = (
3657 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3658 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3659 / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, flags="F")
3660 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003661 self.send_and_expect(self.pg0, p, self.pg1)
3662
3663 # FIN out2in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003664 p = (
3665 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
3666 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
3667 / TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, flags="F")
3668 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003669 self.send_and_expect(self.pg1, p, self.pg0)
3670
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003671 self.init_tcp_session(
3672 self.pg0, self.pg1, self.tcp_port_in, self.tcp_external_port
3673 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003674
3675 # 2 records should be produced - first one del & add
3676 capture = self.pg3.get_capture(2)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003677 self.verify_syslog_sess(capture[0][Raw].load, "SDEL")
3678 self.verify_syslog_sess(capture[1][Raw].load, "SADD")
Filip Varga18f1e412020-12-03 15:27:40 +01003679
3680 def test_twice_nat_interface_addr(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003681 """NAT44ED Acquire twice NAT addresses from interface"""
Filip Varga18f1e412020-12-03 15:27:40 +01003682 flags = self.config_flags.NAT_IS_TWICE_NAT
3683 self.vapi.nat44_add_del_interface_addr(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003684 sw_if_index=self.pg11.sw_if_index, flags=flags, is_add=1
3685 )
Filip Varga18f1e412020-12-03 15:27:40 +01003686
3687 # no address in NAT pool
3688 adresses = self.vapi.nat44_address_dump()
3689 self.assertEqual(0, len(adresses))
3690
3691 # configure interface address and check NAT address pool
3692 self.pg11.config_ip4()
3693 adresses = self.vapi.nat44_address_dump()
3694 self.assertEqual(1, len(adresses))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003695 self.assertEqual(str(adresses[0].ip_address), self.pg11.local_ip4)
Filip Varga18f1e412020-12-03 15:27:40 +01003696 self.assertEqual(adresses[0].flags, flags)
3697
3698 # remove interface address and check NAT address pool
3699 self.pg11.unconfig_ip4()
3700 adresses = self.vapi.nat44_address_dump()
3701 self.assertEqual(0, len(adresses))
3702
3703 def test_output_feature_stateful_acl(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003704 """NAT44ED output feature works with stateful ACL"""
Filip Varga18f1e412020-12-03 15:27:40 +01003705
3706 self.nat_add_address(self.nat_addr)
Filip Varga9c25eb12021-10-21 13:00:27 +02003707 self.vapi.nat44_ed_add_del_output_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003708 sw_if_index=self.pg1.sw_if_index, is_add=1
3709 )
Filip Varga18f1e412020-12-03 15:27:40 +01003710
3711 # First ensure that the NAT is working sans ACL
3712
3713 # send packets out2in, no sessions yet so packets should drop
3714 pkts_out2in = self.create_stream_out(self.pg1)
3715 self.send_and_assert_no_replies(self.pg1, pkts_out2in)
3716
3717 # send packets into inside intf, ensure received via outside intf
3718 pkts_in2out = self.create_stream_in(self.pg0, self.pg1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003719 capture = self.send_and_expect(
3720 self.pg0, pkts_in2out, self.pg1, len(pkts_in2out)
3721 )
Filip Varga18f1e412020-12-03 15:27:40 +01003722 self.verify_capture_out(capture, ignore_port=True)
3723
3724 # send out2in again, with sessions created it should work now
3725 pkts_out2in = self.create_stream_out(self.pg1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003726 capture = self.send_and_expect(
3727 self.pg1, pkts_out2in, self.pg0, len(pkts_out2in)
3728 )
Filip Varga18f1e412020-12-03 15:27:40 +01003729 self.verify_capture_in(capture, self.pg0)
3730
3731 # Create an ACL blocking everything
3732 out2in_deny_rule = AclRule(is_permit=0)
3733 out2in_acl = VppAcl(self, rules=[out2in_deny_rule])
3734 out2in_acl.add_vpp_config()
3735
3736 # create an ACL to permit/reflect everything
3737 in2out_reflect_rule = AclRule(is_permit=2)
3738 in2out_acl = VppAcl(self, rules=[in2out_reflect_rule])
3739 in2out_acl.add_vpp_config()
3740
3741 # apply as input acl on interface and confirm it blocks everything
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003742 acl_if = VppAclInterface(
3743 self, sw_if_index=self.pg1.sw_if_index, n_input=1, acls=[out2in_acl]
3744 )
Filip Varga18f1e412020-12-03 15:27:40 +01003745 acl_if.add_vpp_config()
3746 self.send_and_assert_no_replies(self.pg1, pkts_out2in)
3747
3748 # apply output acl
3749 acl_if.acls = [out2in_acl, in2out_acl]
3750 acl_if.add_vpp_config()
3751 # send in2out to generate ACL state (NAT state was created earlier)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003752 capture = self.send_and_expect(
3753 self.pg0, pkts_in2out, self.pg1, len(pkts_in2out)
3754 )
Filip Varga18f1e412020-12-03 15:27:40 +01003755 self.verify_capture_out(capture, ignore_port=True)
3756
3757 # send out2in again. ACL state exists so it should work now.
3758 # TCP packets with the syn flag set also need the ack flag
3759 for p in pkts_out2in:
3760 if p.haslayer(TCP) and p[TCP].flags & 0x02:
3761 p[TCP].flags |= 0x10
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003762 capture = self.send_and_expect(
3763 self.pg1, pkts_out2in, self.pg0, len(pkts_out2in)
3764 )
Filip Varga18f1e412020-12-03 15:27:40 +01003765 self.verify_capture_in(capture, self.pg0)
3766 self.logger.info(self.vapi.cli("show trace"))
3767
3768 def test_tcp_close(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003769 """NAT44ED Close TCP session from inside network - output feature"""
Filip Vargab6810822022-02-15 11:56:07 -08003770 config = self.vapi.nat44_show_running_config()
3771 old_timeouts = config.timeouts
Filip Varga18f1e412020-12-03 15:27:40 +01003772 new_transitory = 2
3773 self.vapi.nat_set_timeouts(
3774 udp=old_timeouts.udp,
3775 tcp_established=old_timeouts.tcp_established,
3776 icmp=old_timeouts.icmp,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003777 tcp_transitory=new_transitory,
3778 )
Filip Varga18f1e412020-12-03 15:27:40 +01003779
3780 self.vapi.nat44_forwarding_enable_disable(enable=1)
3781 self.nat_add_address(self.pg1.local_ip4)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003782 twice_nat_addr = "10.0.1.3"
3783 service_ip = "192.168.16.150"
Filip Varga18f1e412020-12-03 15:27:40 +01003784 self.nat_add_address(twice_nat_addr, twice_nat=1)
3785
3786 flags = self.config_flags.NAT_IS_INSIDE
3787 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003788 sw_if_index=self.pg0.sw_if_index, is_add=1
3789 )
Filip Varga18f1e412020-12-03 15:27:40 +01003790 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003791 sw_if_index=self.pg0.sw_if_index, flags=flags, is_add=1
3792 )
Filip Varga9c25eb12021-10-21 13:00:27 +02003793 self.vapi.nat44_ed_add_del_output_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003794 is_add=1, sw_if_index=self.pg1.sw_if_index
3795 )
Filip Varga18f1e412020-12-03 15:27:40 +01003796
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003797 flags = (
3798 self.config_flags.NAT_IS_OUT2IN_ONLY | self.config_flags.NAT_IS_TWICE_NAT
3799 )
3800 self.nat_add_static_mapping(
3801 self.pg0.remote_ip4, service_ip, 80, 80, proto=IP_PROTOS.tcp, flags=flags
3802 )
Filip Varga18f1e412020-12-03 15:27:40 +01003803 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
3804 start_sessnum = len(sessions)
3805
3806 # SYN packet out->in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003807 p = (
3808 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3809 / IP(src=self.pg1.remote_ip4, dst=service_ip)
3810 / TCP(sport=33898, dport=80, flags="S")
3811 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003812 capture = self.send_and_expect(self.pg1, p, self.pg0, n_rx=1)
Filip Varga18f1e412020-12-03 15:27:40 +01003813 p = capture[0]
3814 tcp_port = p[TCP].sport
3815
3816 # SYN + ACK packet in->out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003817 p = (
3818 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3819 / IP(src=self.pg0.remote_ip4, dst=twice_nat_addr)
3820 / TCP(sport=80, dport=tcp_port, flags="SA")
3821 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003822 self.send_and_expect(self.pg0, p, self.pg1, n_rx=1)
Filip Varga18f1e412020-12-03 15:27:40 +01003823
3824 # ACK packet out->in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003825 p = (
3826 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3827 / IP(src=self.pg1.remote_ip4, dst=service_ip)
3828 / TCP(sport=33898, dport=80, flags="A")
3829 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003830 self.send_and_expect(self.pg1, p, self.pg0, n_rx=1)
Filip Varga18f1e412020-12-03 15:27:40 +01003831
3832 # FIN packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003833 p = (
3834 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3835 / IP(src=self.pg0.remote_ip4, dst=twice_nat_addr)
3836 / TCP(sport=80, dport=tcp_port, flags="FA", seq=100, ack=300)
3837 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003838 self.send_and_expect(self.pg0, p, self.pg1, n_rx=1)
Filip Varga18f1e412020-12-03 15:27:40 +01003839
3840 # FIN+ACK packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003841 p = (
3842 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3843 / IP(src=self.pg1.remote_ip4, dst=service_ip)
3844 / TCP(sport=33898, dport=80, flags="FA", seq=300, ack=101)
3845 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003846 self.send_and_expect(self.pg1, p, self.pg0, n_rx=1)
Filip Varga18f1e412020-12-03 15:27:40 +01003847
3848 # ACK packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003849 p = (
3850 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3851 / IP(src=self.pg0.remote_ip4, dst=twice_nat_addr)
3852 / TCP(sport=80, dport=tcp_port, flags="A", seq=101, ack=301)
3853 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003854 self.send_and_expect(self.pg0, p, self.pg1, n_rx=1)
Filip Varga18f1e412020-12-03 15:27:40 +01003855
Klement Sekera56c492a2022-01-10 21:57:27 +00003856 # session now in transitory timeout, but traffic still flows
3857 # try FIN packet out->in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003858 p = (
3859 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3860 / IP(src=self.pg1.remote_ip4, dst=service_ip)
3861 / TCP(sport=33898, dport=80, flags="F")
3862 )
Filip Varga18f1e412020-12-03 15:27:40 +01003863 self.pg1.add_stream(p)
3864 self.pg_enable_capture(self.pg_interfaces)
3865 self.pg_start()
3866
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02003867 self.virtual_sleep(new_transitory, "wait for transitory timeout")
Klement Sekera56c492a2022-01-10 21:57:27 +00003868 self.pg0.get_capture(1)
Filip Varga18f1e412020-12-03 15:27:40 +01003869
3870 # session should still exist
3871 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
3872 self.assertEqual(len(sessions) - start_sessnum, 1)
3873
3874 # send FIN+ACK packet out -> in - will cause session to be wiped
3875 # but won't create a new session
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003876 p = (
3877 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3878 / IP(src=self.pg1.remote_ip4, dst=service_ip)
3879 / TCP(sport=33898, dport=80, flags="FA", seq=300, ack=101)
3880 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003881 self.send_and_assert_no_replies(self.pg1, p)
Filip Varga18f1e412020-12-03 15:27:40 +01003882 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
3883 self.assertEqual(len(sessions) - start_sessnum, 0)
Filip Varga18f1e412020-12-03 15:27:40 +01003884
3885 def test_tcp_session_close_in(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003886 """NAT44ED Close TCP session from inside network"""
Filip Varga18f1e412020-12-03 15:27:40 +01003887
3888 in_port = self.tcp_port_in
3889 out_port = 10505
3890 ext_port = self.tcp_external_port
3891
3892 self.nat_add_address(self.nat_addr)
3893 self.nat_add_inside_interface(self.pg0)
3894 self.nat_add_outside_interface(self.pg1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003895 self.nat_add_static_mapping(
3896 self.pg0.remote_ip4,
3897 self.nat_addr,
3898 in_port,
3899 out_port,
3900 proto=IP_PROTOS.tcp,
3901 flags=self.config_flags.NAT_IS_TWICE_NAT,
3902 )
Filip Varga18f1e412020-12-03 15:27:40 +01003903
3904 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
3905 session_n = len(sessions)
3906
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003907 self.vapi.nat_set_timeouts(
3908 udp=300, tcp_established=7440, tcp_transitory=2, icmp=5
3909 )
Filip Varga18f1e412020-12-03 15:27:40 +01003910
3911 self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port)
3912
3913 # FIN packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003914 p = (
3915 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3916 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3917 / TCP(sport=in_port, dport=ext_port, flags="FA", seq=100, ack=300)
3918 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003919 self.send_and_expect(self.pg0, p, self.pg1)
Filip Varga18f1e412020-12-03 15:27:40 +01003920 pkts = []
3921
3922 # ACK packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003923 p = (
3924 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3925 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
3926 / TCP(sport=ext_port, dport=out_port, flags="A", seq=300, ack=101)
3927 )
Filip Varga18f1e412020-12-03 15:27:40 +01003928 pkts.append(p)
3929
3930 # FIN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003931 p = (
3932 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3933 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
3934 / TCP(sport=ext_port, dport=out_port, flags="FA", seq=300, ack=101)
3935 )
Filip Varga18f1e412020-12-03 15:27:40 +01003936 pkts.append(p)
3937
Klement Sekera56c492a2022-01-10 21:57:27 +00003938 self.send_and_expect(self.pg1, pkts, self.pg0)
Filip Varga18f1e412020-12-03 15:27:40 +01003939
3940 # ACK packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003941 p = (
3942 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3943 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3944 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
3945 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003946 self.send_and_expect(self.pg0, p, self.pg1)
Filip Varga18f1e412020-12-03 15:27:40 +01003947
3948 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
3949 self.assertEqual(len(sessions) - session_n, 1)
3950
Klement Sekera56c492a2022-01-10 21:57:27 +00003951 # retransmit FIN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003952 p = (
3953 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
3954 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
3955 / TCP(sport=ext_port, dport=out_port, flags="FA", seq=300, ack=101)
3956 )
Filip Varga18f1e412020-12-03 15:27:40 +01003957
Klement Sekera56c492a2022-01-10 21:57:27 +00003958 self.send_and_expect(self.pg1, p, self.pg0)
Filip Varga18f1e412020-12-03 15:27:40 +01003959
Klement Sekera56c492a2022-01-10 21:57:27 +00003960 # retransmit ACK packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003961 p = (
3962 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3963 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3964 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
3965 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003966 self.send_and_expect(self.pg0, p, self.pg1)
Filip Varga18f1e412020-12-03 15:27:40 +01003967
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02003968 self.virtual_sleep(3)
Klement Sekera56c492a2022-01-10 21:57:27 +00003969 # retransmit ACK packet in -> out - this will cause session to be wiped
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003970 p = (
3971 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3972 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3973 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
3974 )
Klement Sekera56c492a2022-01-10 21:57:27 +00003975 self.send_and_assert_no_replies(self.pg0, p)
Filip Varga18f1e412020-12-03 15:27:40 +01003976 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
3977 self.assertEqual(len(sessions) - session_n, 0)
3978
3979 def test_tcp_session_close_out(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003980 """NAT44ED Close TCP session from outside network"""
Filip Varga18f1e412020-12-03 15:27:40 +01003981
3982 in_port = self.tcp_port_in
3983 out_port = 10505
3984 ext_port = self.tcp_external_port
3985
3986 self.nat_add_address(self.nat_addr)
3987 self.nat_add_inside_interface(self.pg0)
3988 self.nat_add_outside_interface(self.pg1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003989 self.nat_add_static_mapping(
3990 self.pg0.remote_ip4,
3991 self.nat_addr,
3992 in_port,
3993 out_port,
3994 proto=IP_PROTOS.tcp,
3995 flags=self.config_flags.NAT_IS_TWICE_NAT,
3996 )
Filip Varga18f1e412020-12-03 15:27:40 +01003997
3998 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
3999 session_n = len(sessions)
4000
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004001 self.vapi.nat_set_timeouts(
4002 udp=300, tcp_established=7440, tcp_transitory=2, icmp=5
4003 )
Filip Varga18f1e412020-12-03 15:27:40 +01004004
4005 _ = self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port)
4006
4007 # FIN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004008 p = (
4009 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4010 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4011 / TCP(sport=ext_port, dport=out_port, flags="FA", seq=100, ack=300)
4012 )
Filip Varga18f1e412020-12-03 15:27:40 +01004013 self.pg1.add_stream(p)
4014 self.pg_enable_capture(self.pg_interfaces)
4015 self.pg_start()
4016 self.pg0.get_capture(1)
4017
4018 # FIN+ACK packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004019 p = (
4020 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4021 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4022 / TCP(sport=in_port, dport=ext_port, flags="FA", seq=300, ack=101)
4023 )
Filip Varga18f1e412020-12-03 15:27:40 +01004024
4025 self.pg0.add_stream(p)
4026 self.pg_enable_capture(self.pg_interfaces)
4027 self.pg_start()
4028 self.pg1.get_capture(1)
4029
4030 # ACK packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004031 p = (
4032 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4033 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4034 / TCP(sport=ext_port, dport=out_port, flags="A", seq=101, ack=301)
4035 )
Filip Varga18f1e412020-12-03 15:27:40 +01004036 self.pg1.add_stream(p)
4037 self.pg_enable_capture(self.pg_interfaces)
4038 self.pg_start()
4039 self.pg0.get_capture(1)
4040
4041 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4042 self.assertEqual(len(sessions) - session_n, 1)
4043
Klement Sekera56c492a2022-01-10 21:57:27 +00004044 # retransmit FIN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004045 p = (
4046 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4047 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4048 / TCP(sport=ext_port, dport=out_port, flags="FA", seq=300, ack=101)
4049 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004050 self.send_and_expect(self.pg1, p, self.pg0)
Filip Varga18f1e412020-12-03 15:27:40 +01004051
Klement Sekera56c492a2022-01-10 21:57:27 +00004052 # retransmit ACK packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004053 p = (
4054 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4055 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4056 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
4057 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004058 self.send_and_expect(self.pg0, p, self.pg1)
Filip Varga18f1e412020-12-03 15:27:40 +01004059
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02004060 self.virtual_sleep(3)
Klement Sekera56c492a2022-01-10 21:57:27 +00004061 # retransmit ACK packet in -> out - this will cause session to be wiped
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004062 p = (
4063 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4064 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4065 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
4066 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004067 self.send_and_assert_no_replies(self.pg0, p)
Filip Varga18f1e412020-12-03 15:27:40 +01004068 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4069 self.assertEqual(len(sessions) - session_n, 0)
4070
4071 def test_tcp_session_close_simultaneous(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004072 """Simultaneous TCP close from both sides"""
Filip Varga18f1e412020-12-03 15:27:40 +01004073
4074 in_port = self.tcp_port_in
4075 ext_port = 10505
4076
4077 self.nat_add_address(self.nat_addr)
4078 self.nat_add_inside_interface(self.pg0)
4079 self.nat_add_outside_interface(self.pg1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004080 self.nat_add_static_mapping(
4081 self.pg0.remote_ip4,
4082 self.nat_addr,
4083 in_port,
4084 ext_port,
4085 proto=IP_PROTOS.tcp,
4086 flags=self.config_flags.NAT_IS_TWICE_NAT,
4087 )
Filip Varga18f1e412020-12-03 15:27:40 +01004088
4089 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4090 session_n = len(sessions)
4091
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004092 self.vapi.nat_set_timeouts(
4093 udp=300, tcp_established=7440, tcp_transitory=2, icmp=5
4094 )
Filip Varga18f1e412020-12-03 15:27:40 +01004095
4096 out_port = self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port)
4097
4098 # FIN packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004099 p = (
4100 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4101 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4102 / TCP(sport=in_port, dport=ext_port, flags="FA", seq=100, ack=300)
4103 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004104 self.send_and_expect(self.pg0, p, self.pg1)
Filip Varga18f1e412020-12-03 15:27:40 +01004105
4106 # FIN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004107 p = (
4108 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4109 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4110 / TCP(sport=ext_port, dport=out_port, flags="FA", seq=300, ack=100)
4111 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004112 self.send_and_expect(self.pg1, p, self.pg0)
Filip Varga18f1e412020-12-03 15:27:40 +01004113
4114 # ACK packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004115 p = (
4116 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4117 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4118 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
4119 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004120 self.send_and_expect(self.pg0, p, self.pg1)
Filip Varga18f1e412020-12-03 15:27:40 +01004121
4122 # ACK packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004123 p = (
4124 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4125 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4126 / TCP(sport=ext_port, dport=out_port, flags="A", seq=301, ack=101)
4127 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004128 self.send_and_expect(self.pg1, p, self.pg0)
Filip Varga18f1e412020-12-03 15:27:40 +01004129
4130 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4131 self.assertEqual(len(sessions) - session_n, 1)
4132
Klement Sekera56c492a2022-01-10 21:57:27 +00004133 # retransmit FIN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004134 p = (
4135 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4136 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4137 / TCP(sport=ext_port, dport=out_port, flags="FA", seq=300, ack=101)
4138 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004139 self.send_and_expect(self.pg1, p, self.pg0)
Filip Varga18f1e412020-12-03 15:27:40 +01004140
Klement Sekera56c492a2022-01-10 21:57:27 +00004141 # retransmit ACK packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004142 p = (
4143 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4144 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4145 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
4146 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004147 self.send_and_expect(self.pg0, p, self.pg1)
Filip Varga18f1e412020-12-03 15:27:40 +01004148
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02004149 self.virtual_sleep(3)
Klement Sekera56c492a2022-01-10 21:57:27 +00004150 # retransmit ACK packet in -> out - this will cause session to be wiped
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004151 p = (
4152 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4153 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4154 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
4155 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004156 self.pg_send(self.pg0, p)
4157 self.send_and_assert_no_replies(self.pg0, p)
Filip Varga18f1e412020-12-03 15:27:40 +01004158 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4159 self.assertEqual(len(sessions) - session_n, 0)
4160
Klement Sekera56c492a2022-01-10 21:57:27 +00004161 def test_tcp_session_half_reopen_inside(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004162 """TCP session in FIN/FIN state not reopened by in2out SYN only"""
Klement Sekera56c492a2022-01-10 21:57:27 +00004163 in_port = self.tcp_port_in
4164 ext_port = 10505
4165
4166 self.nat_add_address(self.nat_addr)
4167 self.nat_add_inside_interface(self.pg0)
4168 self.nat_add_outside_interface(self.pg1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004169 self.nat_add_static_mapping(
4170 self.pg0.remote_ip4,
4171 self.nat_addr,
4172 in_port,
4173 ext_port,
4174 proto=IP_PROTOS.tcp,
4175 flags=self.config_flags.NAT_IS_TWICE_NAT,
4176 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004177
4178 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4179 session_n = len(sessions)
4180
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004181 self.vapi.nat_set_timeouts(
4182 udp=300, tcp_established=7440, tcp_transitory=2, icmp=5
4183 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004184
4185 out_port = self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port)
4186
4187 # FIN packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004188 p = (
4189 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4190 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4191 / TCP(sport=in_port, dport=ext_port, flags="FA", seq=100, ack=300)
4192 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004193 self.send_and_expect(self.pg0, p, self.pg1)
4194
4195 # FIN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004196 p = (
4197 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4198 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4199 / TCP(sport=ext_port, dport=out_port, flags="FA", seq=300, ack=100)
4200 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004201 self.send_and_expect(self.pg1, p, self.pg0)
4202
4203 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4204 self.assertEqual(len(sessions) - session_n, 1)
4205
4206 # send SYN packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004207 p = (
4208 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4209 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4210 / TCP(sport=in_port, dport=ext_port, flags="S", seq=101, ack=301)
4211 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004212 self.send_and_expect(self.pg0, p, self.pg1)
4213
4214 self.virtual_sleep(3)
4215 # send ACK packet in -> out - session should be wiped
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004216 p = (
4217 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4218 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4219 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
4220 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004221 self.send_and_assert_no_replies(self.pg0, p, self.pg1)
4222 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4223 self.assertEqual(len(sessions) - session_n, 0)
4224
4225 def test_tcp_session_half_reopen_outside(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004226 """TCP session in FIN/FIN state not reopened by out2in SYN only"""
Klement Sekera56c492a2022-01-10 21:57:27 +00004227 in_port = self.tcp_port_in
4228 ext_port = 10505
4229
4230 self.nat_add_address(self.nat_addr)
4231 self.nat_add_inside_interface(self.pg0)
4232 self.nat_add_outside_interface(self.pg1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004233 self.nat_add_static_mapping(
4234 self.pg0.remote_ip4,
4235 self.nat_addr,
4236 in_port,
4237 ext_port,
4238 proto=IP_PROTOS.tcp,
4239 flags=self.config_flags.NAT_IS_TWICE_NAT,
4240 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004241
4242 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4243 session_n = len(sessions)
4244
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004245 self.vapi.nat_set_timeouts(
4246 udp=300, tcp_established=7440, tcp_transitory=2, icmp=5
4247 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004248
4249 out_port = self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port)
4250
4251 # FIN packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004252 p = (
4253 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4254 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4255 / TCP(sport=in_port, dport=ext_port, flags="FA", seq=100, ack=300)
4256 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004257 self.send_and_expect(self.pg0, p, self.pg1)
4258
4259 # FIN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004260 p = (
4261 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4262 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4263 / TCP(sport=ext_port, dport=out_port, flags="FA", seq=300, ack=100)
4264 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004265 self.send_and_expect(self.pg1, p, self.pg0)
4266
4267 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4268 self.assertEqual(len(sessions) - session_n, 1)
4269
4270 # send SYN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004271 p = (
4272 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4273 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4274 / TCP(sport=ext_port, dport=out_port, flags="S", seq=300, ack=101)
4275 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004276 self.send_and_expect(self.pg1, p, self.pg0)
4277
4278 self.virtual_sleep(3)
4279 # send ACK packet in -> out - session should be wiped
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004280 p = (
4281 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4282 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4283 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
4284 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004285 self.send_and_assert_no_replies(self.pg0, p, self.pg1)
4286 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4287 self.assertEqual(len(sessions) - session_n, 0)
4288
4289 def test_tcp_session_reopen(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004290 """TCP session in FIN/FIN state reopened by SYN from both sides"""
Klement Sekera56c492a2022-01-10 21:57:27 +00004291 in_port = self.tcp_port_in
4292 ext_port = 10505
4293
4294 self.nat_add_address(self.nat_addr)
4295 self.nat_add_inside_interface(self.pg0)
4296 self.nat_add_outside_interface(self.pg1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004297 self.nat_add_static_mapping(
4298 self.pg0.remote_ip4,
4299 self.nat_addr,
4300 in_port,
4301 ext_port,
4302 proto=IP_PROTOS.tcp,
4303 flags=self.config_flags.NAT_IS_TWICE_NAT,
4304 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004305
4306 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4307 session_n = len(sessions)
4308
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004309 self.vapi.nat_set_timeouts(
4310 udp=300, tcp_established=7440, tcp_transitory=2, icmp=5
4311 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004312
4313 out_port = self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port)
4314
4315 # FIN packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004316 p = (
4317 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4318 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4319 / TCP(sport=in_port, dport=ext_port, flags="FA", seq=100, ack=300)
4320 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004321 self.send_and_expect(self.pg0, p, self.pg1)
4322
4323 # FIN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004324 p = (
4325 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4326 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4327 / TCP(sport=ext_port, dport=out_port, flags="FA", seq=300, ack=100)
4328 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004329 self.send_and_expect(self.pg1, p, self.pg0)
4330
4331 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4332 self.assertEqual(len(sessions) - session_n, 1)
4333
4334 # send SYN packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004335 p = (
4336 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4337 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4338 / TCP(sport=ext_port, dport=out_port, flags="S", seq=300, ack=101)
4339 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004340 self.send_and_expect(self.pg1, p, self.pg0)
4341
4342 # send SYN packet in -> out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004343 p = (
4344 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4345 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4346 / TCP(sport=in_port, dport=ext_port, flags="SA", seq=101, ack=301)
4347 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004348 self.send_and_expect(self.pg0, p, self.pg1)
4349
Ole Troan52974472022-03-17 11:58:38 +01004350 # send ACK packet out -> in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004351 p = (
4352 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4353 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4354 / TCP(sport=ext_port, dport=out_port, flags="A", seq=300, ack=101)
4355 )
Ole Troan52974472022-03-17 11:58:38 +01004356 self.send_and_expect(self.pg1, p, self.pg0)
4357
Klement Sekera56c492a2022-01-10 21:57:27 +00004358 self.virtual_sleep(3)
4359 # send ACK packet in -> out - should be forwarded and session alive
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004360 p = (
4361 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4362 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4363 / TCP(sport=in_port, dport=ext_port, flags="A", seq=101, ack=301)
4364 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004365 self.send_and_expect(self.pg0, p, self.pg1)
4366 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0)
4367 self.assertEqual(len(sessions) - session_n, 1)
4368
Filip Vargabdd61492021-04-13 17:47:13 +02004369 def test_dynamic_vrf(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004370 """NAT44ED dynamic translation test: different VRF"""
Filip Vargabdd61492021-04-13 17:47:13 +02004371
4372 vrf_id_in = 33
4373 vrf_id_out = 34
4374
4375 self.nat_add_address(self.nat_addr, vrf_id=vrf_id_in)
4376
4377 try:
4378 self.configure_ip4_interface(self.pg7, table_id=vrf_id_in)
4379 self.configure_ip4_interface(self.pg8, table_id=vrf_id_out)
4380
4381 self.nat_add_inside_interface(self.pg7)
4382 self.nat_add_outside_interface(self.pg8)
4383
4384 # just basic stuff nothing special
4385 pkts = self.create_stream_in(self.pg7, self.pg8)
4386 self.pg7.add_stream(pkts)
4387 self.pg_enable_capture(self.pg_interfaces)
4388 self.pg_start()
4389 capture = self.pg8.get_capture(len(pkts))
4390 self.verify_capture_out(capture, ignore_port=True)
4391
4392 pkts = self.create_stream_out(self.pg8)
4393 self.pg8.add_stream(pkts)
4394 self.pg_enable_capture(self.pg_interfaces)
4395 self.pg_start()
4396 capture = self.pg7.get_capture(len(pkts))
4397 self.verify_capture_in(capture, self.pg7)
4398
4399 finally:
4400 self.pg7.unconfig()
4401 self.pg8.unconfig()
4402
BenoƮt Ganneff570d32024-04-16 09:36:05 +02004403 self.vapi.ip_table_add_del_v2(is_add=0, table={"table_id": vrf_id_in})
4404 self.vapi.ip_table_add_del_v2(is_add=0, table={"table_id": vrf_id_out})
Filip Vargabdd61492021-04-13 17:47:13 +02004405
Filip Varga18f1e412020-12-03 15:27:40 +01004406 def test_dynamic_output_feature_vrf(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004407 """NAT44ED dynamic translation test: output-feature, VRF"""
Filip Varga18f1e412020-12-03 15:27:40 +01004408
4409 # other then default (0)
4410 new_vrf_id = 22
4411
4412 self.nat_add_address(self.nat_addr)
Filip Vargab6810822022-02-15 11:56:07 -08004413 self.vapi.nat44_ed_add_del_output_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004414 sw_if_index=self.pg8.sw_if_index, is_add=1
4415 )
Filip Varga18f1e412020-12-03 15:27:40 +01004416 try:
4417 self.configure_ip4_interface(self.pg7, table_id=new_vrf_id)
4418 self.configure_ip4_interface(self.pg8, table_id=new_vrf_id)
4419
4420 # in2out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004421 tcpn = self.statistics["/nat44-ed/in2out/slowpath/tcp"]
4422 udpn = self.statistics["/nat44-ed/in2out/slowpath/udp"]
4423 icmpn = self.statistics["/nat44-ed/in2out/slowpath/icmp"]
4424 drops = self.statistics["/nat44-ed/in2out/slowpath/drops"]
Filip Varga18f1e412020-12-03 15:27:40 +01004425
4426 pkts = self.create_stream_in(self.pg7, self.pg8)
4427 self.pg7.add_stream(pkts)
4428 self.pg_enable_capture(self.pg_interfaces)
4429 self.pg_start()
4430 capture = self.pg8.get_capture(len(pkts))
4431 self.verify_capture_out(capture, ignore_port=True)
4432
Alexander Chernavin4de12b92021-07-06 06:08:26 -04004433 if_idx = self.pg8.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004434 cnt = self.statistics["/nat44-ed/in2out/slowpath/tcp"]
Klement Sekera3887be72021-03-30 20:29:05 +02004435 self.assertEqual(cnt[:, if_idx].sum() - tcpn[:, if_idx].sum(), 2)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004436 cnt = self.statistics["/nat44-ed/in2out/slowpath/udp"]
Klement Sekera3887be72021-03-30 20:29:05 +02004437 self.assertEqual(cnt[:, if_idx].sum() - udpn[:, if_idx].sum(), 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004438 cnt = self.statistics["/nat44-ed/in2out/slowpath/icmp"]
Klement Sekera3887be72021-03-30 20:29:05 +02004439 self.assertEqual(cnt[:, if_idx].sum() - icmpn[:, if_idx].sum(), 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004440 cnt = self.statistics["/nat44-ed/in2out/slowpath/drops"]
Klement Sekera3887be72021-03-30 20:29:05 +02004441 self.assertEqual(cnt[:, if_idx].sum() - drops[:, if_idx].sum(), 0)
Filip Varga18f1e412020-12-03 15:27:40 +01004442
4443 # out2in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004444 tcpn = self.statistics["/nat44-ed/out2in/fastpath/tcp"]
4445 udpn = self.statistics["/nat44-ed/out2in/fastpath/udp"]
4446 icmpn = self.statistics["/nat44-ed/out2in/fastpath/icmp"]
4447 drops = self.statistics["/nat44-ed/out2in/fastpath/drops"]
Filip Varga18f1e412020-12-03 15:27:40 +01004448
4449 pkts = self.create_stream_out(self.pg8)
4450 self.pg8.add_stream(pkts)
4451 self.pg_enable_capture(self.pg_interfaces)
4452 self.pg_start()
4453 capture = self.pg7.get_capture(len(pkts))
4454 self.verify_capture_in(capture, self.pg7)
4455
4456 if_idx = self.pg8.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004457 cnt = self.statistics["/nat44-ed/out2in/fastpath/tcp"]
Klement Sekera3887be72021-03-30 20:29:05 +02004458 self.assertEqual(cnt[:, if_idx].sum() - tcpn[:, if_idx].sum(), 2)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004459 cnt = self.statistics["/nat44-ed/out2in/fastpath/udp"]
Klement Sekera3887be72021-03-30 20:29:05 +02004460 self.assertEqual(cnt[:, if_idx].sum() - udpn[:, if_idx].sum(), 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004461 cnt = self.statistics["/nat44-ed/out2in/fastpath/icmp"]
Klement Sekera3887be72021-03-30 20:29:05 +02004462 self.assertEqual(cnt[:, if_idx].sum() - icmpn[:, if_idx].sum(), 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004463 cnt = self.statistics["/nat44-ed/out2in/fastpath/drops"]
Klement Sekera3887be72021-03-30 20:29:05 +02004464 self.assertEqual(cnt[:, if_idx].sum() - drops[:, if_idx].sum(), 0)
Filip Varga18f1e412020-12-03 15:27:40 +01004465
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004466 sessions = self.statistics["/nat44-ed/total-sessions"]
Klement Sekera3887be72021-03-30 20:29:05 +02004467 self.assertEqual(sessions[:, 0].sum(), 3)
Filip Varga18f1e412020-12-03 15:27:40 +01004468
4469 finally:
Filip Vargabdd61492021-04-13 17:47:13 +02004470 self.pg7.unconfig()
4471 self.pg8.unconfig()
Filip Varga18f1e412020-12-03 15:27:40 +01004472
BenoƮt Ganneff570d32024-04-16 09:36:05 +02004473 self.vapi.ip_table_add_del_v2(is_add=0, table={"table_id": new_vrf_id})
Filip Varga18f1e412020-12-03 15:27:40 +01004474
4475 def test_next_src_nat(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004476 """NAT44ED On way back forward packet to nat44-in2out node."""
Filip Varga18f1e412020-12-03 15:27:40 +01004477
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004478 twice_nat_addr = "10.0.1.3"
Filip Varga18f1e412020-12-03 15:27:40 +01004479 external_port = 80
4480 local_port = 8080
4481 post_twice_nat_port = 0
4482
4483 self.vapi.nat44_forwarding_enable_disable(enable=1)
4484 self.nat_add_address(twice_nat_addr, twice_nat=1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004485 flags = (
4486 self.config_flags.NAT_IS_OUT2IN_ONLY
4487 | self.config_flags.NAT_IS_SELF_TWICE_NAT
4488 )
4489 self.nat_add_static_mapping(
4490 self.pg6.remote_ip4,
4491 self.pg1.remote_ip4,
4492 local_port,
4493 external_port,
4494 proto=IP_PROTOS.tcp,
4495 vrf_id=1,
4496 flags=flags,
4497 )
Filip Varga18f1e412020-12-03 15:27:40 +01004498 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004499 sw_if_index=self.pg6.sw_if_index, is_add=1
4500 )
Filip Varga18f1e412020-12-03 15:27:40 +01004501
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004502 p = (
4503 Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac)
4504 / IP(src=self.pg6.remote_ip4, dst=self.pg1.remote_ip4)
4505 / TCP(sport=12345, dport=external_port)
4506 )
Filip Varga18f1e412020-12-03 15:27:40 +01004507 self.pg6.add_stream(p)
4508 self.pg_enable_capture(self.pg_interfaces)
4509 self.pg_start()
4510 capture = self.pg6.get_capture(1)
4511 p = capture[0]
4512 try:
4513 ip = p[IP]
4514 tcp = p[TCP]
4515 self.assertEqual(ip.src, twice_nat_addr)
4516 self.assertNotEqual(tcp.sport, 12345)
4517 post_twice_nat_port = tcp.sport
4518 self.assertEqual(ip.dst, self.pg6.remote_ip4)
4519 self.assertEqual(tcp.dport, local_port)
4520 self.assert_packet_checksums_valid(p)
4521 except:
4522 self.logger.error(ppp("Unexpected or invalid packet:", p))
4523 raise
4524
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004525 p = (
4526 Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac)
4527 / IP(src=self.pg6.remote_ip4, dst=twice_nat_addr)
4528 / TCP(sport=local_port, dport=post_twice_nat_port)
4529 )
Filip Varga18f1e412020-12-03 15:27:40 +01004530 self.pg6.add_stream(p)
4531 self.pg_enable_capture(self.pg_interfaces)
4532 self.pg_start()
4533 capture = self.pg6.get_capture(1)
4534 p = capture[0]
4535 try:
4536 ip = p[IP]
4537 tcp = p[TCP]
4538 self.assertEqual(ip.src, self.pg1.remote_ip4)
4539 self.assertEqual(tcp.sport, external_port)
4540 self.assertEqual(ip.dst, self.pg6.remote_ip4)
4541 self.assertEqual(tcp.dport, 12345)
4542 self.assert_packet_checksums_valid(p)
4543 except:
4544 self.logger.error(ppp("Unexpected or invalid packet:", p))
4545 raise
4546
4547 def test_one_armed_nat44_static(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004548 """NAT44ED One armed NAT and 1:1 NAPT asymmetrical rule"""
Filip Varga18f1e412020-12-03 15:27:40 +01004549
4550 remote_host = self.pg4.remote_hosts[0]
4551 local_host = self.pg4.remote_hosts[1]
4552 external_port = 80
4553 local_port = 8080
4554 eh_port_in = 0
4555
4556 self.vapi.nat44_forwarding_enable_disable(enable=1)
4557 self.nat_add_address(self.nat_addr, twice_nat=1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004558 flags = (
4559 self.config_flags.NAT_IS_OUT2IN_ONLY | self.config_flags.NAT_IS_TWICE_NAT
4560 )
4561 self.nat_add_static_mapping(
4562 local_host.ip4,
4563 self.nat_addr,
4564 local_port,
4565 external_port,
4566 proto=IP_PROTOS.tcp,
4567 flags=flags,
4568 )
Filip Varga18f1e412020-12-03 15:27:40 +01004569 flags = self.config_flags.NAT_IS_INSIDE
4570 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004571 sw_if_index=self.pg4.sw_if_index, is_add=1
4572 )
Filip Varga18f1e412020-12-03 15:27:40 +01004573 self.vapi.nat44_interface_add_del_feature(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004574 sw_if_index=self.pg4.sw_if_index, flags=flags, is_add=1
4575 )
Filip Varga18f1e412020-12-03 15:27:40 +01004576
4577 # from client to service
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004578 p = (
4579 Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac)
4580 / IP(src=remote_host.ip4, dst=self.nat_addr)
4581 / TCP(sport=12345, dport=external_port)
4582 )
Filip Varga18f1e412020-12-03 15:27:40 +01004583 self.pg4.add_stream(p)
4584 self.pg_enable_capture(self.pg_interfaces)
4585 self.pg_start()
4586 capture = self.pg4.get_capture(1)
4587 p = capture[0]
4588 try:
4589 ip = p[IP]
4590 tcp = p[TCP]
4591 self.assertEqual(ip.dst, local_host.ip4)
4592 self.assertEqual(ip.src, self.nat_addr)
4593 self.assertEqual(tcp.dport, local_port)
4594 self.assertNotEqual(tcp.sport, 12345)
4595 eh_port_in = tcp.sport
4596 self.assert_packet_checksums_valid(p)
4597 except:
4598 self.logger.error(ppp("Unexpected or invalid packet:", p))
4599 raise
4600
4601 # from service back to client
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004602 p = (
4603 Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac)
4604 / IP(src=local_host.ip4, dst=self.nat_addr)
4605 / TCP(sport=local_port, dport=eh_port_in)
4606 )
Filip Varga18f1e412020-12-03 15:27:40 +01004607 self.pg4.add_stream(p)
4608 self.pg_enable_capture(self.pg_interfaces)
4609 self.pg_start()
4610 capture = self.pg4.get_capture(1)
4611 p = capture[0]
4612 try:
4613 ip = p[IP]
4614 tcp = p[TCP]
4615 self.assertEqual(ip.src, self.nat_addr)
4616 self.assertEqual(ip.dst, remote_host.ip4)
4617 self.assertEqual(tcp.sport, external_port)
4618 self.assertEqual(tcp.dport, 12345)
4619 self.assert_packet_checksums_valid(p)
4620 except:
4621 self.logger.error(ppp("Unexpected or invalid packet:", p))
4622 raise
4623
Matthew Smithad510752021-08-10 12:22:14 -05004624 def test_icmp_error_fwd_outbound(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004625 """NAT44ED ICMP error outbound with forwarding enabled"""
Matthew Smithad510752021-08-10 12:22:14 -05004626
4627 # Ensure that an outbound ICMP error message is properly associated
4628 # with the inbound forward bypass session it is related to.
4629 payload = "H" * 10
4630
4631 self.nat_add_address(self.nat_addr)
4632 self.nat_add_inside_interface(self.pg0)
4633 self.nat_add_outside_interface(self.pg1)
4634
4635 # enable forwarding and initiate connection out2in
4636 self.vapi.nat44_forwarding_enable_disable(enable=1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004637 p1 = (
4638 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4639 / IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4)
4640 / UDP(sport=21, dport=20)
4641 / payload
4642 )
Matthew Smithad510752021-08-10 12:22:14 -05004643
4644 self.pg1.add_stream(p1)
4645 self.pg_enable_capture(self.pg_interfaces)
4646 self.pg_start()
4647 capture = self.pg0.get_capture(1)[0]
4648
4649 self.logger.info(self.vapi.cli("show nat44 sessions"))
4650
4651 # reply with ICMP error message in2out
4652 # We cannot reliably retrieve forward bypass sessions via the API.
4653 # session dumps for a user will only look on the worker that the
4654 # user is supposed to be mapped to in2out. The forward bypass session
4655 # is not necessarily created on that worker.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004656 p2 = (
4657 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4658 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4659 / ICMP(type="dest-unreach", code="port-unreachable")
4660 / capture[IP:]
4661 )
Matthew Smithad510752021-08-10 12:22:14 -05004662
4663 self.pg0.add_stream(p2)
4664 self.pg_enable_capture(self.pg_interfaces)
4665 self.pg_start()
4666 capture = self.pg1.get_capture(1)[0]
4667
4668 self.logger.info(self.vapi.cli("show nat44 sessions"))
4669
4670 self.logger.info(ppp("p1 packet:", p1))
4671 self.logger.info(ppp("p2 packet:", p2))
4672 self.logger.info(ppp("capture packet:", capture))
4673
Klement Sekera56c492a2022-01-10 21:57:27 +00004674 def test_tcp_session_open_retransmit1(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004675 """NAT44ED Open TCP session with SYN,ACK retransmit 1
Klement Sekera56c492a2022-01-10 21:57:27 +00004676
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004677 The client does not receive the [SYN,ACK] or the
4678 ACK from the client is lost. Therefore, the [SYN, ACK]
4679 is retransmitted by the server.
Klement Sekera56c492a2022-01-10 21:57:27 +00004680 """
4681
4682 in_port = self.tcp_port_in
4683 ext_port = self.tcp_external_port
4684 payload = "H" * 10
4685
4686 self.nat_add_address(self.nat_addr)
4687 self.nat_add_inside_interface(self.pg0)
4688 self.nat_add_outside_interface(self.pg1)
4689
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004690 self.vapi.nat_set_timeouts(
4691 udp=300, tcp_established=7440, tcp_transitory=5, icmp=60
4692 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004693 # SYN packet in->out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004694 p = (
4695 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4696 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4697 / TCP(sport=in_port, dport=ext_port, flags="S")
4698 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004699 p = self.send_and_expect(self.pg0, p, self.pg1)[0]
4700 out_port = p[TCP].sport
4701
4702 # SYN + ACK packet out->in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004703 p = (
4704 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4705 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4706 / TCP(sport=ext_port, dport=out_port, flags="SA")
4707 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004708 self.send_and_expect(self.pg1, p, self.pg0)
4709
4710 # ACK in->out does not arrive
4711
4712 # resent SYN + ACK packet out->in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004713 p = (
4714 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4715 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4716 / TCP(sport=ext_port, dport=out_port, flags="SA")
4717 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004718 self.send_and_expect(self.pg1, p, self.pg0)
4719
4720 # ACK packet in->out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004721 p = (
4722 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4723 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4724 / TCP(sport=in_port, dport=ext_port, flags="A")
4725 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004726 self.send_and_expect(self.pg0, p, self.pg1)
4727
4728 # Verify that the data can be transmitted after the transitory time
4729 self.virtual_sleep(6)
4730
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004731 p = (
4732 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4733 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4734 / TCP(sport=in_port, dport=ext_port, flags="PA")
4735 / Raw(payload)
4736 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004737 self.send_and_expect(self.pg0, p, self.pg1)
4738
4739 def test_tcp_session_open_retransmit2(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004740 """NAT44ED Open TCP session with SYN,ACK retransmit 2
Klement Sekera56c492a2022-01-10 21:57:27 +00004741
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004742 The ACK is lost to the server after the TCP session is opened.
4743 Data is sent by the client, then the [SYN,ACK] is
4744 retransmitted by the server.
Klement Sekera56c492a2022-01-10 21:57:27 +00004745 """
4746
4747 in_port = self.tcp_port_in
4748 ext_port = self.tcp_external_port
4749 payload = "H" * 10
4750
4751 self.nat_add_address(self.nat_addr)
4752 self.nat_add_inside_interface(self.pg0)
4753 self.nat_add_outside_interface(self.pg1)
4754
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004755 self.vapi.nat_set_timeouts(
4756 udp=300, tcp_established=7440, tcp_transitory=5, icmp=60
4757 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004758 # SYN packet in->out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004759 p = (
4760 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4761 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4762 / TCP(sport=in_port, dport=ext_port, flags="S")
4763 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004764 p = self.send_and_expect(self.pg0, p, self.pg1)[0]
4765 out_port = p[TCP].sport
4766
4767 # SYN + ACK packet out->in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004768 p = (
4769 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4770 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4771 / TCP(sport=ext_port, dport=out_port, flags="SA")
4772 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004773 self.send_and_expect(self.pg1, p, self.pg0)
4774
4775 # ACK packet in->out -- not received by the server
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004776 p = (
4777 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4778 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4779 / TCP(sport=in_port, dport=ext_port, flags="A")
4780 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004781 self.send_and_expect(self.pg0, p, self.pg1)
4782
4783 # PUSH + ACK packet in->out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004784 p = (
4785 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4786 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4787 / TCP(sport=in_port, dport=ext_port, flags="PA")
4788 / Raw(payload)
4789 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004790 self.send_and_expect(self.pg0, p, self.pg1)
4791
4792 # resent SYN + ACK packet out->in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004793 p = (
4794 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4795 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4796 / TCP(sport=ext_port, dport=out_port, flags="SA")
4797 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004798 self.send_and_expect(self.pg1, p, self.pg0)
4799
4800 # resent ACK packet in->out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004801 p = (
4802 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4803 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4804 / TCP(sport=in_port, dport=ext_port, flags="A")
4805 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004806 self.send_and_expect(self.pg0, p, self.pg1)
4807
4808 # resent PUSH + ACK packet in->out
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004809 p = (
4810 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4811 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4812 / TCP(sport=in_port, dport=ext_port, flags="PA")
4813 / Raw(payload)
4814 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004815 self.send_and_expect(self.pg0, p, self.pg1)
4816
4817 # ACK packet out->in
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004818 p = (
4819 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
4820 / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
4821 / TCP(sport=ext_port, dport=out_port, flags="A")
4822 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004823 self.send_and_expect(self.pg1, p, self.pg0)
4824
4825 # Verify that the data can be transmitted after the transitory time
4826 self.virtual_sleep(6)
4827
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004828 p = (
4829 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
4830 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
4831 / TCP(sport=in_port, dport=ext_port, flags="PA")
4832 / Raw(payload)
4833 )
Klement Sekera56c492a2022-01-10 21:57:27 +00004834 self.send_and_expect(self.pg0, p, self.pg1)
4835
Dmitry Valter6b97c432022-12-09 19:34:22 +00004836 def test_dynamic_ports_exhausted(self):
4837 """NAT44ED dynamic translation test: address ports exhaused"""
4838
4839 sessions_per_batch = 128
4840 n_available_ports = 65536 - 1024
4841 n_sessions = n_available_ports + 2 * sessions_per_batch
4842
4843 # set high enough session limit for ports to be exhausted
4844 self.plugin_disable()
4845 self.plugin_enable(max_sessions=n_sessions)
4846
4847 self.nat_add_inside_interface(self.pg0)
4848 self.nat_add_outside_interface(self.pg1)
4849
4850 # set timeouts to high for sessions to reallistically expire
4851 config = self.vapi.nat44_show_running_config()
4852 old_timeouts = config.timeouts
4853 self.vapi.nat_set_timeouts(
4854 udp=21600,
4855 tcp_established=old_timeouts.tcp_established,
4856 tcp_transitory=old_timeouts.tcp_transitory,
4857 icmp=old_timeouts.icmp,
4858 )
4859
4860 # in2out after NAT addresses added
4861 self.nat_add_address(self.nat_addr)
4862
4863 for i in range(n_sessions // sessions_per_batch):
4864 pkts = self.create_udp_stream(
4865 self.pg0,
4866 self.pg1,
4867 sessions_per_batch,
4868 base_port=i * sessions_per_batch + 100,
4869 )
4870
4871 self.pg0.add_stream(pkts)
4872 self.pg_start()
4873
4874 err = self.statistics.get_err_counter(
4875 "/err/nat44-ed-in2out-slowpath/out of ports"
4876 )
4877 if err > sessions_per_batch:
4878 break
4879
4880 # Check for ports to be used no more than once
4881 ports = set()
4882 sessions = self.vapi.cli("show nat44 sessions")
4883 rx = re.compile(
4884 f" *o2i flow: match: saddr {self.pg1.remote_ip4} sport [0-9]+ daddr {self.nat_addr} dport ([0-9]+) proto UDP.*"
4885 )
4886 for line in sessions.splitlines():
4887 m = rx.match(line)
4888 if m:
4889 port = int(m.groups()[0])
4890 self.assertNotIn(port, ports)
4891 ports.add(port)
4892
4893 self.assertGreaterEqual(err, sessions_per_batch)
4894
Filip Varga18f1e412020-12-03 15:27:40 +01004895
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02004896if __name__ == "__main__":
Filip Varga18f1e412020-12-03 15:27:40 +01004897 unittest.main(testRunner=VppTestRunner)