Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import unittest |
| 4 | from io import BytesIO |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 5 | from random import randint, choice |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 6 | |
Dmitry Valter | 6b97c43 | 2022-12-09 19:34:22 +0000 | [diff] [blame] | 7 | import re |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 8 | import scapy.compat |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 9 | from framework import VppTestCase, VppLoInterface |
| 10 | from asfframework import VppTestRunner, tag_fixme_ubuntu2204, is_distro_ubuntu2204 |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 11 | from scapy.data import IP_PROTOS |
| 12 | from scapy.layers.inet import IP, TCP, UDP, ICMP, GRE |
| 13 | from scapy.layers.inet import IPerror, TCPerror |
| 14 | from scapy.layers.l2 import Ether |
| 15 | from scapy.packet import Raw |
Vladislav Grishenko | 579a6fb | 2023-03-16 19:31:00 +0500 | [diff] [blame] | 16 | from statistics import variance |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 17 | from syslog_rfc5424_parser import SyslogMessage, ParseError |
| 18 | from syslog_rfc5424_parser.constants import SyslogSeverity |
Klement Sekera | c2feb65 | 2022-03-08 20:13:57 +0100 | [diff] [blame] | 19 | from util import ppp, pr, ip4_range |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 20 | from vpp_acl import AclRule, VppAcl, VppAclInterface |
| 21 | from vpp_ip_route import VppIpRoute, VppRoutePath |
| 22 | from vpp_papi import VppEnum |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 23 | from util import StatsDiff |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 24 | from config import config |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 25 | |
| 26 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 27 | @unittest.skipIf("nat" in config.excluded_plugins, "Exclude NAT plugin tests") |
Klement Sekera | ff334db | 2021-05-26 13:02:35 +0200 | [diff] [blame] | 28 | class TestNAT44ED(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 29 | """NAT44ED Test Case""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 30 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 31 | nat_addr = "10.0.10.3" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 32 | |
| 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 Sekera | ff334db | 2021-05-26 13:02:35 +0200 | [diff] [blame] | 47 | super().setUp() |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 48 | self.plugin_enable() |
| 49 | |
| 50 | def tearDown(self): |
Klement Sekera | ff334db | 2021-05-26 13:02:35 +0200 | [diff] [blame] | 51 | super().tearDown() |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 52 | if not self.vpp_dead: |
| 53 | self.plugin_disable() |
| 54 | |
Dmitry Valter | 6b97c43 | 2022-12-09 19:34:22 +0000 | [diff] [blame] | 55 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 58 | |
| 59 | def plugin_disable(self): |
Filip Varga | e7a80a9 | 2021-02-26 09:31:21 +0100 | [diff] [blame] | 60 | self.vapi.nat44_ed_plugin_enable_disable(enable=0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 61 | |
| 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 Grishenko | 5f694d1 | 2022-08-19 20:42:22 +0500 | [diff] [blame] | 80 | return randint(1024, 65535) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 81 | |
| 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 Ganne | ff570d3 | 2024-04-16 09:36:05 +0200 | [diff] [blame] | 95 | cls.vapi.ip_table_add_del_v2(is_add=1, table={"table_id": table_id}) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 96 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 104 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 113 | cls.vapi.nat44_add_del_interface_addr(sw_if_index=i.sw_if_index, is_add=1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 114 | |
| 115 | def nat_add_inside_interface(self, i): |
| 116 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 117 | flags=self.config_flags.NAT_IS_INSIDE, sw_if_index=i.sw_if_index, is_add=1 |
| 118 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 119 | |
| 120 | def nat_add_outside_interface(self, i): |
| 121 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 122 | flags=self.config_flags.NAT_IS_OUTSIDE, sw_if_index=i.sw_if_index, is_add=1 |
| 123 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 124 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 125 | def nat_add_address(self, address, twice_nat=0, vrf_id=0xFFFFFFFF, is_add=1): |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 126 | flags = self.config_flags.NAT_IS_TWICE_NAT if twice_nat else 0 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 127 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 134 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 135 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 148 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 158 | vrf_id=vrf_id, |
| 159 | protocol=proto, |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 160 | flags=flags, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 161 | tag=tag, |
| 162 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 163 | |
| 164 | @classmethod |
| 165 | def setUpClass(cls): |
Klement Sekera | ff334db | 2021-05-26 13:02:35 +0200 | [diff] [blame] | 166 | super().setUpClass() |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 167 | |
| 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 Ganne | ff570d3 | 2024-04-16 09:36:05 +0200 | [diff] [blame] | 177 | cls.vapi.ip_table_add_del_v2(is_add=1, table={"table_id": 1}) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 178 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 183 | sw_if_index=cls.pg4.sw_if_index, prefix="10.0.0.1/24" |
| 184 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 185 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 208 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 257 | |
| 258 | for r in rl: |
| 259 | r.add_vpp_config() |
| 260 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 261 | 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 Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 282 | } |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 283 | ) |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 284 | |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 285 | def get_err_counter(self, path): |
| 286 | return self.statistics.get_err_counter(path) |
| 287 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 288 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 297 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 305 | pkts = self.create_stream_frag( |
| 306 | self.pg0, self.nat_addr, host_in_port, server_out_port, data, proto |
| 307 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 308 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 312 | p = self.reass_frags_and_verify(frags, self.nat_addr, server_addr) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 313 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 322 | def frag_out_of_order( |
| 323 | self, proto=IP_PROTOS.tcp, dont_translate=False, ignore_port=False |
| 324 | ): |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 325 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 335 | pkts = self.create_stream_frag( |
| 336 | self.pg0, self.pg1.remote_ip4, self.port_in, 20, data, proto |
| 337 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 338 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 344 | p = self.reass_frags_and_verify( |
| 345 | frags, self.nat_addr, self.pg1.remote_ip4 |
| 346 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 347 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 348 | p = self.reass_frags_and_verify( |
| 349 | frags, self.pg0.remote_ip4, self.pg1.remote_ip4 |
| 350 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 351 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 377 | pkts = self.create_stream_frag( |
| 378 | self.pg1, dst_addr, sport, dport, data, proto, echo_reply=True |
| 379 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 380 | pkts.reverse() |
| 381 | self.pg1.add_stream(pkts) |
| 382 | self.pg_enable_capture(self.pg_interfaces) |
Klement Sekera | e79bbe9 | 2021-03-04 18:41:02 +0100 | [diff] [blame] | 383 | self.logger.info(self.vapi.cli("show trace")) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 384 | self.pg_start() |
| 385 | frags = self.pg0.get_capture(len(pkts)) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 386 | p = self.reass_frags_and_verify( |
| 387 | frags, self.pg1.remote_ip4, self.pg0.remote_ip4 |
| 388 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 389 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 404 | ip = IP(src=frags[0][IP].src, dst=frags[0][IP].dst, proto=frags[0][IP].proto) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 405 | if ip.proto == IP_PROTOS.tcp: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 406 | p = ip / TCP(buffer.getvalue()) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 407 | self.logger.debug(ppp("Reassembled:", p)) |
| 408 | self.assert_tcp_checksum_valid(p) |
| 409 | elif ip.proto == IP_PROTOS.udp: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 410 | p = ip / UDP(buffer.getvalue()[:8]) / Raw(buffer.getvalue()[8:]) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 411 | elif ip.proto == IP_PROTOS.icmp: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 412 | p = ip / ICMP(buffer.getvalue()) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 413 | return p |
| 414 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 415 | def frag_in_order( |
| 416 | self, proto=IP_PROTOS.tcp, dont_translate=False, ignore_port=False |
| 417 | ): |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 418 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 427 | pkts = self.create_stream_frag( |
| 428 | self.pg0, self.pg1.remote_ip4, self.port_in, 20, data, proto |
| 429 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 430 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 435 | p = self.reass_frags_and_verify(frags, self.nat_addr, self.pg1.remote_ip4) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 436 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 437 | p = self.reass_frags_and_verify( |
| 438 | frags, self.pg0.remote_ip4, self.pg1.remote_ip4 |
| 439 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 440 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 466 | pkts = self.create_stream_frag( |
| 467 | self.pg1, dst_addr, sport, dport, data, proto, echo_reply=True |
| 468 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 469 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 473 | p = self.reass_frags_and_verify(frags, self.pg1.remote_ip4, self.pg0.remote_ip4) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 474 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 481 | def verify_capture_out( |
| 482 | self, capture, nat_ip=None, same_port=False, dst_ip=None, ignore_port=False |
| 483 | ): |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 484 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 495 | self.assertEqual(packet[TCP].sport, self.tcp_port_in) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 496 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 497 | self.assertNotEqual(packet[TCP].sport, self.tcp_port_in) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 498 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 503 | self.assertEqual(packet[UDP].sport, self.udp_port_in) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 504 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 505 | self.assertNotEqual(packet[UDP].sport, self.udp_port_in) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 506 | self.udp_port_out = packet[UDP].sport |
| 507 | else: |
| 508 | if not ignore_port: |
| 509 | if same_port: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 510 | self.assertEqual(packet[ICMP].id, self.icmp_id_in) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 511 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 512 | self.assertNotEqual(packet[ICMP].id, self.icmp_id_in) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 513 | self.icmp_id_out = packet[ICMP].id |
| 514 | self.assert_packet_checksums_valid(packet) |
| 515 | except: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 516 | self.logger.error( |
| 517 | ppp("Unexpected or invalid packet (outside network):", packet) |
| 518 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 519 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 533 | self.logger.error( |
| 534 | ppp("Unexpected or invalid packet (inside network):", packet) |
| 535 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 536 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 544 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 549 | pkts.extend([p, p]) |
| 550 | |
| 551 | # UDP |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 552 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 557 | pkts.append(p) |
| 558 | |
| 559 | # ICMP |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 560 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 565 | pkts.append(p) |
| 566 | |
| 567 | return pkts |
| 568 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 569 | def create_stream_out(self, out_if, dst_ip=None, ttl=64, use_inside_ports=False): |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 570 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 582 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 587 | pkts.extend([p, p]) |
| 588 | |
| 589 | # UDP |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 590 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 595 | pkts.append(p) |
| 596 | |
| 597 | # ICMP |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 598 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 603 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 612 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 617 | pkts.append(p) |
| 618 | |
| 619 | return pkts |
| 620 | |
Dmitry Valter | 6b97c43 | 2022-12-09 19:34:22 +0000 | [diff] [blame] | 621 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 631 | def create_stream_frag( |
| 632 | self, src_if, dst, sport, dport, data, proto=IP_PROTOS.tcp, echo_reply=False |
| 633 | ): |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 634 | if proto == IP_PROTOS.tcp: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 635 | p = ( |
| 636 | IP(src=src_if.remote_ip4, dst=dst) |
| 637 | / TCP(sport=sport, dport=dport) |
| 638 | / Raw(data) |
| 639 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 640 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 647 | proto_header = ICMP(id=sport, type="echo-request") |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 648 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 649 | proto_header = ICMP(id=sport, type="echo-reply") |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 650 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 658 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 664 | pkts.append(p) |
| 665 | if proto == IP_PROTOS.tcp: |
| 666 | raw = Raw(data[4:20]) |
| 667 | else: |
| 668 | raw = Raw(data[16:32]) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 669 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 674 | pkts.append(p) |
| 675 | if proto == IP_PROTOS.tcp: |
| 676 | raw = Raw(data[20:]) |
| 677 | else: |
| 678 | raw = Raw(data[32:]) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 679 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 684 | pkts.append(p) |
| 685 | return pkts |
| 686 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 687 | def frag_in_order_in_plus_out( |
| 688 | self, in_addr, out_addr, in_port, out_port, proto=IP_PROTOS.tcp |
| 689 | ): |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 690 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 700 | pkts = self.create_stream_frag( |
| 701 | self.pg0, out_addr, port_in, out_port, data, proto |
| 702 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 703 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 707 | p = self.reass_frags_and_verify(frags, self.pg0.remote_ip4, in_addr) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 708 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 717 | pkts = self.create_stream_frag( |
| 718 | self.pg1, self.pg0.remote_ip4, in_port, p[layer].sport, data, proto |
| 719 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 720 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 721 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 730 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 734 | p = self.reass_frags_and_verify(frags, out_addr, self.pg0.remote_ip4) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 735 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 742 | def frag_out_of_order_in_plus_out( |
| 743 | self, in_addr, out_addr, in_port, out_port, proto=IP_PROTOS.tcp |
| 744 | ): |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 745 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 755 | pkts = self.create_stream_frag( |
| 756 | self.pg0, out_addr, port_in, out_port, data, proto |
| 757 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 758 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 763 | p = self.reass_frags_and_verify(frags, self.pg0.remote_ip4, in_addr) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 764 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 774 | pkts = self.create_stream_frag( |
| 775 | self.pg1, self.pg0.remote_ip4, in_port, p[layer].sport, data, proto |
| 776 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 777 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 778 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 787 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 792 | p = self.reass_frags_and_verify(frags, out_addr, self.pg0.remote_ip4) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 793 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 802 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 807 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 815 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 820 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 826 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 831 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 838 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 842 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 868 | eh_translate = (not self_twice_nat) or (not lb and same_pg) or client_id == 1 |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 869 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 880 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 888 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 889 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 893 | out_addr = self.nat_addr |
| 894 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 895 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 904 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 918 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 923 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 957 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 962 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 982 | 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 Varga | 0eaf4e6 | 2021-02-17 14:34:54 +0100 | [diff] [blame] | 984 | self.logger.info(self.vapi.cli("show nat44 sessions")) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 985 | self.vapi.nat44_del_session( |
| 986 | address=sessions[0].inside_ip_address, |
| 987 | port=sessions[0].inside_port, |
| 988 | protocol=sessions[0].protocol, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 989 | flags=( |
| 990 | self.config_flags.NAT_IS_INSIDE |
| 991 | | self.config_flags.NAT_IS_EXT_HOST_VALID |
| 992 | ), |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 993 | ext_host_address=sessions[0].ext_host_nat_address, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 994 | ext_host_port=sessions[0].ext_host_nat_port, |
| 995 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 996 | sessions = self.vapi.nat44_user_session_dump(server.ip4, 0) |
| 997 | self.assertEqual(len(sessions), 0) |
| 998 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 999 | def verify_syslog_sess(self, data, msgid, is_ip6=False): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1000 | message = data.decode("utf-8") |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1001 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1008 | self.assertEqual(message.appname, "NAT") |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 1009 | self.assertEqual(message.msgid, msgid) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1010 | sd_params = message.sd.get("nsess") |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1011 | self.assertTrue(sd_params is not None) |
| 1012 | if is_ip6: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1013 | self.assertEqual(sd_params.get("IATYP"), "IPv6") |
| 1014 | self.assertEqual(sd_params.get("ISADDR"), self.pg0.remote_ip6) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1015 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1016 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1027 | |
Filip Varga | a0648b6 | 2021-06-21 12:59:41 +0200 | [diff] [blame] | 1028 | def test_icmp_error(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1029 | """NAT44ED test ICMP error message with inner header""" |
Filip Varga | a0648b6 | 2021-06-21 12:59:41 +0200 | [diff] [blame] | 1030 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1038 | 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 Varga | a0648b6 | 2021-06-21 12:59:41 +0200 | [diff] [blame] | 1052 | |
Klement Sekera | c2feb65 | 2022-03-08 20:13:57 +0100 | [diff] [blame] | 1053 | capture = self.send_and_expect(self.pg0, p1, self.pg1) |
Filip Varga | a0648b6 | 2021-06-21 12:59:41 +0200 | [diff] [blame] | 1054 | |
| 1055 | # out2in (send error message) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1056 | 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 Varga | a0648b6 | 2021-06-21 12:59:41 +0200 | [diff] [blame] | 1063 | |
Klement Sekera | c2feb65 | 2022-03-08 20:13:57 +0100 | [diff] [blame] | 1064 | capture = self.send_and_expect(self.pg1, p2, self.pg0) |
Filip Varga | a0648b6 | 2021-06-21 12:59:41 +0200 | [diff] [blame] | 1065 | |
Klement Sekera | c2feb65 | 2022-03-08 20:13:57 +0100 | [diff] [blame] | 1066 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1071 | raise AssertionError(f"Packet {pr(c)} not translated properly") from a |
Filip Varga | a0648b6 | 2021-06-21 12:59:41 +0200 | [diff] [blame] | 1072 | |
Klement Sekera | 254c803 | 2021-07-27 13:33:51 +0200 | [diff] [blame] | 1073 | def test_icmp_echo_reply_trailer(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1074 | """ICMP echo reply with ethernet trailer""" |
Klement Sekera | 254c803 | 2021-07-27 13:33:51 +0200 | [diff] [blame] | 1075 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1081 | 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 Sekera | 254c803 | 2021-07-27 13:33:51 +0200 | [diff] [blame] | 1086 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1095 | 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 Sekera | 254c803 | 2021-07-27 13:33:51 +0200 | [diff] [blame] | 1100 | |
| 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1121 | def test_users_dump(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1122 | """NAT44ED API test - nat44_user_dump""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1123 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1164 | pkts = self.create_stream_out( |
| 1165 | self.pg1, dst_ip=self.pg0.remote_ip4, use_inside_ports=True |
| 1166 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1167 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1178 | self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4, same_port=True) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1179 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1209 | """NAT44ED don't translate fragments arriving out of order""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1210 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1216 | """NAT44ED forwarding test""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1217 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1225 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1232 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1255 | pkts = self.create_stream_out( |
| 1256 | self.pg1, dst_ip=self.pg0.remote_ip4, use_inside_ports=True |
| 1257 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1258 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1269 | self.verify_capture_out( |
| 1270 | capture, nat_ip=self.pg0.remote_ip4, same_port=True |
| 1271 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1272 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1278 | self.assertTrue(sessions[0].flags & self.config_flags.NAT_IS_EXT_HOST_VALID) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1279 | self.vapi.nat44_del_session( |
| 1280 | address=sessions[0].inside_ip_address, |
| 1281 | port=sessions[0].inside_port, |
| 1282 | protocol=sessions[0].protocol, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1283 | flags=( |
| 1284 | self.config_flags.NAT_IS_INSIDE |
| 1285 | | self.config_flags.NAT_IS_EXT_HOST_VALID |
| 1286 | ), |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1287 | ext_host_address=sessions[0].ext_host_address, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1288 | ext_host_port=sessions[0].ext_host_port, |
| 1289 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1290 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1301 | flags=flags, |
| 1302 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1303 | |
| 1304 | def test_output_feature_and_service2(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1305 | """NAT44ED interface output feature and service host direct access""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1306 | self.vapi.nat44_forwarding_enable_disable(enable=1) |
| 1307 | self.nat_add_address(self.nat_addr) |
| 1308 | |
Filip Varga | b681082 | 2022-02-15 11:56:07 -0800 | [diff] [blame] | 1309 | self.vapi.nat44_ed_add_del_output_interface( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1310 | sw_if_index=self.pg1.sw_if_index, is_add=1 |
| 1311 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1312 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1338 | pkts = self.create_stream_out( |
| 1339 | self.pg1, self.pg0.remote_ip4, use_inside_ports=True |
| 1340 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1341 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1352 | self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4, same_port=True) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1353 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1359 | """NAT44ED Twice NAT""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1360 | self.twice_nat_common() |
| 1361 | |
| 1362 | def test_self_twice_nat_positive(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1363 | """NAT44ED Self Twice NAT (positive test)""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1364 | self.twice_nat_common(self_twice_nat=True, same_pg=True) |
| 1365 | |
| 1366 | def test_self_twice_nat_lb_positive(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1367 | """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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1369 | |
| 1370 | def test_twice_nat_lb(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1371 | """NAT44ED Twice NAT local service load balancing""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1372 | self.twice_nat_common(lb=True) |
| 1373 | |
| 1374 | def test_output_feature(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1375 | """NAT44ED interface output feature (in2out postrouting)""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1376 | 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 Varga | b681082 | 2022-02-15 11:56:07 -0800 | [diff] [blame] | 1380 | self.vapi.nat44_ed_add_del_output_interface( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1381 | sw_if_index=self.pg1.sw_if_index, is_add=1 |
| 1382 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1383 | |
| 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 Sekera | 05b5a5b | 2021-06-28 13:40:40 +0200 | [diff] [blame] | 1391 | self.logger.debug(self.vapi.cli("show trace")) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1392 | |
| 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 Sekera | 05b5a5b | 2021-06-28 13:40:40 +0200 | [diff] [blame] | 1400 | self.logger.debug(self.vapi.cli("show trace")) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1401 | |
Klement Sekera | 79699b0 | 2021-06-21 16:04:40 +0200 | [diff] [blame] | 1402 | # 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 Sekera | 05b5a5b | 2021-06-28 13:40:40 +0200 | [diff] [blame] | 1409 | self.logger.debug(self.vapi.cli("show trace")) |
Klement Sekera | 79699b0 | 2021-06-21 16:04:40 +0200 | [diff] [blame] | 1410 | |
| 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 Sekera | 05b5a5b | 2021-06-28 13:40:40 +0200 | [diff] [blame] | 1418 | self.logger.debug(self.vapi.cli("show trace")) |
Klement Sekera | 79699b0 | 2021-06-21 16:04:40 +0200 | [diff] [blame] | 1419 | |
| 1420 | # in2out |
| 1421 | pkts = self.create_stream_in(self.pg0, self.pg1, ttl=1) |
Neale Ranns | 5c6dd17 | 2022-02-17 09:08:47 +0000 | [diff] [blame] | 1422 | capture = self.send_and_expect_some(self.pg0, pkts, self.pg0) |
Klement Sekera | 79699b0 | 2021-06-21 16:04:40 +0200 | [diff] [blame] | 1423 | for p in capture: |
| 1424 | self.assertIn(ICMP, p) |
| 1425 | self.assertEqual(p[ICMP].type, 11) # 11 == time-exceeded |
| 1426 | |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1427 | def test_static_with_port_out2(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1428 | """NAT44ED 1:1 NAPT asymmetrical rule""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1429 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1435 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1443 | |
| 1444 | self.nat_add_inside_interface(self.pg0) |
| 1445 | self.nat_add_outside_interface(self.pg1) |
| 1446 | |
| 1447 | # from client to service |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1448 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1453 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1469 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1475 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1490 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1495 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1511 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1517 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1532 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1537 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1553 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1558 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1574 | """NAT44ED local service load balancing""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1575 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1581 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1585 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1593 | locals=locals, |
| 1594 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1595 | flags = self.config_flags.NAT_IS_INSIDE |
| 1596 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1597 | sw_if_index=self.pg0.sw_if_index, flags=flags, is_add=1 |
| 1598 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1599 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1600 | sw_if_index=self.pg1.sw_if_index, is_add=1 |
| 1601 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1602 | |
| 1603 | # from client to service |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1604 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1609 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1630 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1635 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1652 | self.assertTrue(sessions[0].flags & self.config_flags.NAT_IS_EXT_HOST_VALID) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1653 | self.vapi.nat44_del_session( |
| 1654 | address=sessions[0].inside_ip_address, |
| 1655 | port=sessions[0].inside_port, |
| 1656 | protocol=sessions[0].protocol, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1657 | flags=( |
| 1658 | self.config_flags.NAT_IS_INSIDE |
| 1659 | | self.config_flags.NAT_IS_EXT_HOST_VALID |
| 1660 | ), |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1661 | ext_host_address=sessions[0].ext_host_address, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1662 | ext_host_port=sessions[0].ext_host_port, |
| 1663 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1664 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1668 | """NAT44ED local service load balancing (asymmetrical rule)""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1669 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1675 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1679 | |
| 1680 | self.vapi.nat44_forwarding_enable_disable(enable=1) |
| 1681 | flags = self.config_flags.NAT_IS_OUT2IN_ONLY |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1682 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1691 | flags = self.config_flags.NAT_IS_INSIDE |
| 1692 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1693 | sw_if_index=self.pg0.sw_if_index, flags=flags, is_add=1 |
| 1694 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1695 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1696 | sw_if_index=self.pg1.sw_if_index, is_add=1 |
| 1697 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1698 | |
| 1699 | # from client to service |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1700 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1705 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1726 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1731 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1747 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1752 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1769 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1774 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1790 | """NAT44ED local service load balancing affinity""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1791 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1797 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1801 | |
| 1802 | self.nat_add_address(self.nat_addr) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1803 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1812 | flags = self.config_flags.NAT_IS_INSIDE |
| 1813 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1814 | sw_if_index=self.pg0.sw_if_index, flags=flags, is_add=1 |
| 1815 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1816 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1817 | sw_if_index=self.pg1.sw_if_index, is_add=1 |
| 1818 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1819 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1820 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1825 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1833 | self.assertTrue(sessions[0].flags & self.config_flags.NAT_IS_EXT_HOST_VALID) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1834 | self.vapi.nat44_del_session( |
| 1835 | address=sessions[0].inside_ip_address, |
| 1836 | port=sessions[0].inside_port, |
| 1837 | protocol=sessions[0].protocol, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1838 | flags=( |
| 1839 | self.config_flags.NAT_IS_INSIDE |
| 1840 | | self.config_flags.NAT_IS_EXT_HOST_VALID |
| 1841 | ), |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1842 | ext_host_address=sessions[0].ext_host_address, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1843 | ext_host_port=sessions[0].ext_host_port, |
| 1844 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1845 | |
| 1846 | pkts = [] |
| 1847 | for port in range(1030, 1100): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1848 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1853 | 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 Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 1861 | def test_multiple_vrf_1(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1862 | """Multiple VRF - both client & service in VRF1""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1863 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1864 | external_addr = "1.2.3.4" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1865 | external_port = 80 |
| 1866 | local_port = 8080 |
| 1867 | port = 0 |
| 1868 | |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1869 | flags = self.config_flags.NAT_IS_INSIDE |
| 1870 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1871 | sw_if_index=self.pg5.sw_if_index, is_add=1 |
| 1872 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1873 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1874 | sw_if_index=self.pg5.sw_if_index, is_add=1, flags=flags |
| 1875 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1876 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1877 | sw_if_index=self.pg6.sw_if_index, is_add=1 |
| 1878 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1879 | flags = self.config_flags.NAT_IS_OUT2IN_ONLY |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1880 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1889 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1890 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1895 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1910 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1915 | 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 Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 1930 | def test_multiple_vrf_2(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1931 | """Multiple VRF - dynamic NAT from VRF1 to VRF0 (output-feature)""" |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 1932 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1933 | external_addr = "1.2.3.4" |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 1934 | 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 Varga | b681082 | 2022-02-15 11:56:07 -0800 | [diff] [blame] | 1940 | self.vapi.nat44_ed_add_del_output_interface( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1941 | sw_if_index=self.pg1.sw_if_index, is_add=1 |
| 1942 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 1943 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1944 | sw_if_index=self.pg5.sw_if_index, is_add=1 |
| 1945 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 1946 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1947 | sw_if_index=self.pg5.sw_if_index, is_add=1, flags=flags |
| 1948 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 1949 | flags = self.config_flags.NAT_IS_OUT2IN_ONLY |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1950 | 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 Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 1959 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1960 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1965 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1980 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 1985 | 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 Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2000 | def test_multiple_vrf_3(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2001 | """Multiple VRF - client in VRF1, service in VRF0""" |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2002 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2003 | external_addr = "1.2.3.4" |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2004 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2010 | sw_if_index=self.pg0.sw_if_index, is_add=1 |
| 2011 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2012 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2013 | sw_if_index=self.pg0.sw_if_index, is_add=1, flags=flags |
| 2014 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2015 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2016 | sw_if_index=self.pg6.sw_if_index, is_add=1 |
| 2017 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2018 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2026 | flags=flags, |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2027 | ) |
| 2028 | |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2029 | # from client VRF1 to service VRF0 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2030 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2035 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2051 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2056 | 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 Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2071 | def test_multiple_vrf_4(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2072 | """Multiple VRF - client in VRF0, service in VRF1""" |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2073 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2074 | external_addr = "1.2.3.4" |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2075 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2081 | sw_if_index=self.pg0.sw_if_index, is_add=1 |
| 2082 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2083 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2084 | sw_if_index=self.pg0.sw_if_index, is_add=1, flags=flags |
| 2085 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2086 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2087 | sw_if_index=self.pg5.sw_if_index, is_add=1 |
| 2088 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2089 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2090 | sw_if_index=self.pg5.sw_if_index, is_add=1, flags=flags |
| 2091 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2092 | flags = self.config_flags.NAT_IS_OUT2IN_ONLY |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2093 | 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 Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2102 | |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2103 | # from client VRF0 to service VRF1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2104 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2109 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2125 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2130 | 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 Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2145 | def test_multiple_vrf_5(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2146 | """Multiple VRF - forwarding - no translation""" |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2147 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2148 | external_addr = "1.2.3.4" |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2149 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2156 | sw_if_index=self.pg0.sw_if_index, is_add=1 |
| 2157 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2158 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2159 | sw_if_index=self.pg0.sw_if_index, is_add=1, flags=flags |
| 2160 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2161 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2162 | sw_if_index=self.pg5.sw_if_index, is_add=1 |
| 2163 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2164 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2165 | sw_if_index=self.pg5.sw_if_index, is_add=1, flags=flags |
| 2166 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2167 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2168 | sw_if_index=self.pg6.sw_if_index, is_add=1 |
| 2169 | ) |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2170 | flags = self.config_flags.NAT_IS_OUT2IN_ONLY |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2171 | 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 Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2180 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2187 | flags=flags, |
Klement Sekera | a920af7 | 2021-05-17 13:17:56 +0200 | [diff] [blame] | 2188 | ) |
| 2189 | |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2190 | # from client to server (both VRF1, no translation) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2191 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2196 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2212 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2217 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2233 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2238 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2254 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2259 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2275 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2280 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2296 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2301 | 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 Sekera | dc243ee | 2021-02-25 16:47:23 +0100 | [diff] [blame] | 2316 | def test_outside_address_distribution(self): |
Vladislav Grishenko | 579a6fb | 2023-03-16 19:31:00 +0500 | [diff] [blame] | 2317 | """NAT44ED outside address distribution based on source address""" |
Klement Sekera | dc243ee | 2021-02-25 16:47:23 +0100 | [diff] [blame] | 2318 | |
Vladislav Grishenko | 579a6fb | 2023-03-16 19:31:00 +0500 | [diff] [blame] | 2319 | addresses = 65 |
Klement Sekera | dc243ee | 2021-02-25 16:47:23 +0100 | [diff] [blame] | 2320 | x = 100 |
Klement Sekera | dc243ee | 2021-02-25 16:47:23 +0100 | [diff] [blame] | 2321 | |
Vladislav Grishenko | 579a6fb | 2023-03-16 19:31:00 +0500 | [diff] [blame] | 2322 | nat_addresses = [] |
| 2323 | nat_distribution = {} |
| 2324 | for i in range(1, addresses): |
Klement Sekera | dc243ee | 2021-02-25 16:47:23 +0100 | [diff] [blame] | 2325 | a = "10.0.0.%d" % i |
| 2326 | nat_addresses.append(a) |
Vladislav Grishenko | 579a6fb | 2023-03-16 19:31:00 +0500 | [diff] [blame] | 2327 | nat_distribution[a] = set() |
Klement Sekera | dc243ee | 2021-02-25 16:47:23 +0100 | [diff] [blame] | 2328 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2335 | vrf_id=0xFFFFFFFF, |
| 2336 | is_add=1, |
| 2337 | flags=0, |
| 2338 | ) |
Klement Sekera | dc243ee | 2021-02-25 16:47:23 +0100 | [diff] [blame] | 2339 | |
| 2340 | self.pg0.generate_remote_hosts(x) |
| 2341 | |
| 2342 | pkts = [] |
| 2343 | for i in range(x): |
Klement Sekera | d2b6997 | 2021-03-09 17:53:47 +0100 | [diff] [blame] | 2344 | info = self.create_packet_info(self.pg0, self.pg1) |
| 2345 | payload = self.info_to_payload(info) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2346 | 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 Sekera | d2b6997 | 2021-03-09 17:53:47 +0100 | [diff] [blame] | 2352 | info.data = p |
Klement Sekera | dc243ee | 2021-02-25 16:47:23 +0100 | [diff] [blame] | 2353 | 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 Sekera | d2b6997 | 2021-03-09 17:53:47 +0100 | [diff] [blame] | 2359 | 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 Grishenko | 579a6fb | 2023-03-16 19:31:00 +0500 | [diff] [blame] | 2366 | 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 Sekera | dc243ee | 2021-02-25 16:47:23 +0100 | [diff] [blame] | 2371 | |
Vladislav Grishenko | 5f694d1 | 2022-08-19 20:42:22 +0500 | [diff] [blame] | 2372 | 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 Grishenko | 3abb32c | 2022-12-04 15:02:03 +0500 | [diff] [blame] | 2563 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2608 | |
Dave Wallace | 670724c | 2022-09-20 21:52:18 -0400 | [diff] [blame] | 2609 | @tag_fixme_ubuntu2204 |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 2610 | @unittest.skipIf("nat" in config.excluded_plugins, "Exclude NAT plugin tests") |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2611 | class TestNAT44EDMW(TestNAT44ED): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2612 | """NAT44ED MW Test Case""" |
| 2613 | |
Klement Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2614 | vpp_worker_count = 4 |
| 2615 | max_sessions = 5000 |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2616 | |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2617 | def test_dynamic(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2618 | """NAT44ED dynamic translation test""" |
Klement Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2619 | pkt_count = 1500 |
| 2620 | tcp_port_offset = 20 |
| 2621 | udp_port_offset = 20 |
| 2622 | icmp_id_offset = 20 |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2623 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2629 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2633 | |
Klement Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2634 | i2o_pkts = [[] for x in range(0, self.vpp_worker_count)] |
| 2635 | |
| 2636 | for i in range(pkt_count): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2637 | 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 Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2642 | i2o_pkts[p[TCP].sport % self.vpp_worker_count].append(p) |
| 2643 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2644 | 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 Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2649 | i2o_pkts[p[UDP].sport % self.vpp_worker_count].append(p) |
| 2650 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2651 | 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 Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2656 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2662 | self.pg_enable_capture(self.pg_interfaces) |
| 2663 | self.pg_start() |
Klement Sekera | ff334db | 2021-05-26 13:02:35 +0200 | [diff] [blame] | 2664 | capture = self.pg1.get_capture(pkt_count * 3, timeout=5) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2665 | |
| 2666 | if_idx = self.pg0.sw_if_index |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2667 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2671 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2672 | 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 Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 2675 | self.assertEqual(dc2[:, if_idx].sum() - dc1[:, if_idx].sum(), 0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2676 | |
Klement Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2677 | self.logger.info(self.vapi.cli("show trace")) |
| 2678 | |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2679 | # out2in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2680 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2684 | |
Klement Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2685 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2703 | 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 Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2708 | o2i_pkts[p[TCP].dport % self.vpp_worker_count].append(p) |
| 2709 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2710 | 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 Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2715 | o2i_pkts[p[UDP].dport % self.vpp_worker_count].append(p) |
| 2716 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2717 | 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 Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2722 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2728 | self.pg_enable_capture(self.pg_interfaces) |
| 2729 | self.pg_start() |
Klement Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2730 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2737 | packet[TCP].dport, |
| 2738 | tcp_port_offset, |
| 2739 | tcp_port_offset + pkt_count, |
| 2740 | "dst TCP port", |
| 2741 | ) |
Klement Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2742 | elif packet.haslayer(UDP): |
| 2743 | self.assert_in_range( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2744 | packet[UDP].dport, |
| 2745 | udp_port_offset, |
| 2746 | udp_port_offset + pkt_count, |
| 2747 | "dst UDP port", |
| 2748 | ) |
Klement Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2749 | else: |
| 2750 | self.assert_in_range( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2751 | packet[ICMP].id, |
| 2752 | icmp_id_offset, |
| 2753 | icmp_id_offset + pkt_count, |
| 2754 | "ICMP id", |
| 2755 | ) |
Klement Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2756 | except: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2757 | self.logger.error( |
| 2758 | ppp("Unexpected or invalid packet (inside network):", packet) |
| 2759 | ) |
Klement Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 2760 | raise |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2761 | |
| 2762 | if_idx = self.pg1.sw_if_index |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2763 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2767 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2768 | 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 Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 2771 | self.assertEqual(dc2[:, if_idx].sum() - dc1[:, if_idx].sum(), 0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2772 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2773 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2778 | |
| 2779 | def test_frag_in_order(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2780 | """NAT44ED translate fragments arriving in order""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2781 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2791 | """NAT44ED don't translate fragments arriving in order""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2792 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2801 | """NAT44ED translate fragments arriving out of order""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2802 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2812 | """NAT44ED in+out interface fragments in order""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2813 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2824 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2833 | |
| 2834 | # run tests for each protocol |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2835 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2844 | |
| 2845 | def test_frag_out_of_order_in_plus_out(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2846 | """NAT44ED in+out interface fragments out of order""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2847 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2858 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2867 | |
| 2868 | # run tests for each protocol |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2869 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2878 | |
| 2879 | def test_reass_hairpinning(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2880 | """NAT44ED fragments hairpinning""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2881 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2893 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2907 | self.nat_add_static_mapping(server_addr, self.nat_addr) |
| 2908 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2909 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2933 | |
| 2934 | def test_session_limit_per_vrf(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2935 | """NAT44ED per vrf session limit""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2936 | |
| 2937 | inside = self.pg0 |
| 2938 | inside_vrf10 = self.pg2 |
| 2939 | outside = self.pg1 |
| 2940 | |
| 2941 | limit = 5 |
| 2942 | |
Vladislav Grishenko | 5b3e04c | 2022-09-05 10:32:46 +0500 | [diff] [blame] | 2943 | # 2 interfaces pg0, pg1 (vrf10, limit 5 tcp sessions) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2944 | self.vapi.nat44_set_session_limit(session_limit=limit, vrf_id=10) |
| 2945 | |
Vladislav Grishenko | 5b3e04c | 2022-09-05 10:32:46 +0500 | [diff] [blame] | 2946 | # 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2950 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2976 | def test_show_max_translations(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2977 | """NAT44ED API test - max translations per thread""" |
Filip Varga | b681082 | 2022-02-15 11:56:07 -0800 | [diff] [blame] | 2978 | config = self.vapi.nat44_show_running_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2979 | self.assertEqual(self.max_sessions, config.sessions) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2980 | |
| 2981 | def test_lru_cleanup(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2982 | """NAT44ED LRU cleanup algorithm""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2983 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2989 | udp=1, tcp_established=7440, tcp_transitory=30, icmp=1 |
| 2990 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 2991 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2995 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3000 | 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 Ganne | 56eccdb | 2021-08-20 09:18:31 +0200 | [diff] [blame] | 3006 | self.virtual_sleep(1.5, "wait for timeouts") |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3007 | |
| 3008 | pkts = [] |
| 3009 | for i in range(0, self.max_sessions - 1): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3010 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3015 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3023 | """NAT44ED session RST timeouts""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3024 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3029 | self.vapi.nat_set_timeouts( |
| 3030 | udp=300, tcp_established=7440, tcp_transitory=5, icmp=60 |
| 3031 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3032 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3033 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3041 | self.send_and_expect(self.pg0, p, self.pg1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3042 | |
BenoƮt Ganne | 56eccdb | 2021-08-20 09:18:31 +0200 | [diff] [blame] | 3043 | self.virtual_sleep(6) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3044 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3045 | # The session is already closed |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3046 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3051 | self.send_and_assert_no_replies(self.pg0, p, self.pg1) |
| 3052 | |
| 3053 | # The session can be re-opened |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3054 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3059 | self.send_and_expect(self.pg0, p, self.pg1) |
| 3060 | |
| 3061 | def test_session_rst_established_timeout(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3062 | """NAT44ED session RST timeouts""" |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3063 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3068 | self.vapi.nat_set_timeouts( |
| 3069 | udp=300, tcp_established=7440, tcp_transitory=5, icmp=60 |
| 3070 | ) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3071 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3072 | self.init_tcp_session( |
| 3073 | self.pg0, self.pg1, self.tcp_port_in, self.tcp_external_port |
| 3074 | ) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3075 | |
| 3076 | # Wait at least the transitory time, the session is in established |
Ole Troan | 5297447 | 2022-03-17 11:58:38 +0100 | [diff] [blame] | 3077 | # state anyway. RST followed by a data packet should move it to |
| 3078 | # transitory state. |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3079 | self.virtual_sleep(6) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3080 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3085 | self.send_and_expect(self.pg0, p, self.pg1) |
| 3086 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3087 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3092 | self.send_and_expect(self.pg0, p, self.pg1) |
| 3093 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3094 | # State is transitory, session should be closed after 6 seconds |
| 3095 | self.virtual_sleep(6) |
| 3096 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3097 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3102 | self.send_and_assert_no_replies(self.pg0, p, self.pg1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3103 | |
| 3104 | def test_dynamic_out_of_ports(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3105 | """NAT44ED dynamic translation test: out of ports""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3106 | |
| 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3111 | pkts = self.create_stream_in(self.pg0, self.pg1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3112 | |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 3113 | self.send_and_assert_no_replies( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3114 | self.pg0, |
| 3115 | pkts, |
| 3116 | msg="i2o pkts", |
| 3117 | stats_diff=self.no_diff |
| 3118 | | { |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 3119 | "err": { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3120 | "/err/nat44-ed-in2out-slowpath/out of ports": len(pkts), |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 3121 | }, |
| 3122 | self.pg0.sw_if_index: { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3123 | "/nat44-ed/in2out/slowpath/drops": len(pkts), |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 3124 | }, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3125 | }, |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 3126 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3127 | |
| 3128 | # in2out after NAT addresses added |
| 3129 | self.nat_add_address(self.nat_addr) |
| 3130 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3131 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3134 | |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 3135 | self.send_and_expect( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3136 | self.pg0, |
| 3137 | pkts, |
| 3138 | self.pg1, |
| 3139 | msg="i2o pkts", |
| 3140 | stats_diff=self.no_diff |
| 3141 | | { |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 3142 | "err": { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3143 | "/err/nat44-ed-in2out-slowpath/out of ports": 0, |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 3144 | }, |
| 3145 | self.pg0.sw_if_index: { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3146 | "/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 Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 3150 | }, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3151 | }, |
Klement Sekera | ad3187f | 2022-02-18 10:34:35 +0000 | [diff] [blame] | 3152 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3153 | |
| 3154 | def test_unknown_proto(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3155 | """NAT44ED translate packet with unknown protocol""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3156 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3162 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3167 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3172 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3179 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3194 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3201 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3216 | """NAT44ED translate packet with unknown protocol - hairpinning""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3217 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3231 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3236 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3241 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3248 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3263 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3270 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3285 | """NAT44ED interface output feature and services""" |
| 3286 | external_addr = "1.2.3.4" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3287 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3294 | ip_address=self.pg1.remote_ip4, |
| 3295 | sw_if_index=0xFFFFFFFF, |
| 3296 | flags=flags, |
| 3297 | is_add=1, |
| 3298 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3299 | flags = self.config_flags.NAT_IS_OUT2IN_ONLY |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3300 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3308 | |
| 3309 | self.nat_add_inside_interface(self.pg0) |
| 3310 | self.nat_add_outside_interface(self.pg0) |
Filip Varga | 9c25eb1 | 2021-10-21 13:00:27 +0200 | [diff] [blame] | 3311 | self.vapi.nat44_ed_add_del_output_interface( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3312 | sw_if_index=self.pg1.sw_if_index, is_add=1 |
| 3313 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3314 | |
| 3315 | # from client to service |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3316 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3321 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3337 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3342 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3380 | """NAT44ED interface output feature and DST NAT""" |
| 3381 | external_addr = "1.2.3.4" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3382 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3388 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3396 | |
| 3397 | self.nat_add_inside_interface(self.pg0) |
| 3398 | self.nat_add_outside_interface(self.pg0) |
Filip Varga | 9c25eb1 | 2021-10-21 13:00:27 +0200 | [diff] [blame] | 3399 | self.vapi.nat44_ed_add_del_output_interface( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3400 | sw_if_index=self.pg1.sw_if_index, is_add=1 |
| 3401 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3402 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3403 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3408 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3425 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3430 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3448 | """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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3450 | |
| 3451 | def test_self_twice_nat_negative(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3452 | """NAT44ED Self Twice NAT (negative test)""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3453 | self.twice_nat_common(self_twice_nat=True) |
| 3454 | |
| 3455 | def test_static_lb_multi_clients(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3456 | """NAT44ED local service load balancing - multiple clients""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3457 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3465 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3469 | |
| 3470 | flags = self.config_flags.NAT_IS_INSIDE |
| 3471 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3472 | sw_if_index=self.pg0.sw_if_index, flags=flags, is_add=1 |
| 3473 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3474 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3475 | sw_if_index=self.pg1.sw_if_index, is_add=1 |
| 3476 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3477 | |
| 3478 | self.nat_add_address(self.nat_addr) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3479 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3487 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3493 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3498 | 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 Sekera | 1fbf034 | 2021-03-31 13:38:09 +0200 | [diff] [blame] | 3508 | self.assertGreaterEqual(server1_n, server2_n) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3509 | |
| 3510 | local = { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3511 | "addr": server3.ip4, |
| 3512 | "port": local_port, |
| 3513 | "probability": 20, |
| 3514 | "vrf_id": 0, |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3515 | } |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3523 | protocol=IP_PROTOS.tcp, |
| 3524 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3525 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3531 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3536 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3554 | "addr": server2.ip4, |
| 3555 | "port": local_port, |
| 3556 | "probability": 10, |
| 3557 | "vrf_id": 0, |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3558 | } |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3566 | protocol=IP_PROTOS.tcp, |
| 3567 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3568 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3586 | # 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3590 | """NAT44ED Test syslog session creation and deletion""" |
| 3591 | self.vapi.syslog_set_filter(self.syslog_severity.SYSLOG_API_SEVERITY_INFO) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3592 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3598 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3603 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3609 | self.verify_syslog_sess(capture[0][Raw].load, "SADD") |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3610 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3615 | self.verify_syslog_sess(capture[0][Raw].load, "SDEL") |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3616 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3621 | """Syslog events for session reopen""" |
| 3622 | self.vapi.syslog_set_filter(self.syslog_severity.SYSLOG_API_SEVERITY_INFO) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3623 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3630 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3635 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3638 | self.verify_syslog_sess(capture[0][Raw].load, "SADD") |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3639 | |
| 3640 | # SYN out2in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3641 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3646 | self.send_and_expect(self.pg1, p, self.pg0) |
| 3647 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3648 | 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 Troan | 5297447 | 2022-03-17 11:58:38 +0100 | [diff] [blame] | 3653 | self.send_and_expect(self.pg0, p, self.pg1) |
| 3654 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3655 | # FIN in2out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3656 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3661 | self.send_and_expect(self.pg0, p, self.pg1) |
| 3662 | |
| 3663 | # FIN out2in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3664 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3669 | self.send_and_expect(self.pg1, p, self.pg0) |
| 3670 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3671 | self.init_tcp_session( |
| 3672 | self.pg0, self.pg1, self.tcp_port_in, self.tcp_external_port |
| 3673 | ) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3674 | |
| 3675 | # 2 records should be produced - first one del & add |
| 3676 | capture = self.pg3.get_capture(2) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3677 | self.verify_syslog_sess(capture[0][Raw].load, "SDEL") |
| 3678 | self.verify_syslog_sess(capture[1][Raw].load, "SADD") |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3679 | |
| 3680 | def test_twice_nat_interface_addr(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3681 | """NAT44ED Acquire twice NAT addresses from interface""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3682 | flags = self.config_flags.NAT_IS_TWICE_NAT |
| 3683 | self.vapi.nat44_add_del_interface_addr( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3684 | sw_if_index=self.pg11.sw_if_index, flags=flags, is_add=1 |
| 3685 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3686 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3695 | self.assertEqual(str(adresses[0].ip_address), self.pg11.local_ip4) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3696 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3704 | """NAT44ED output feature works with stateful ACL""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3705 | |
| 3706 | self.nat_add_address(self.nat_addr) |
Filip Varga | 9c25eb1 | 2021-10-21 13:00:27 +0200 | [diff] [blame] | 3707 | self.vapi.nat44_ed_add_del_output_interface( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3708 | sw_if_index=self.pg1.sw_if_index, is_add=1 |
| 3709 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3710 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3719 | capture = self.send_and_expect( |
| 3720 | self.pg0, pkts_in2out, self.pg1, len(pkts_in2out) |
| 3721 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3722 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3726 | capture = self.send_and_expect( |
| 3727 | self.pg1, pkts_out2in, self.pg0, len(pkts_out2in) |
| 3728 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3729 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3742 | acl_if = VppAclInterface( |
| 3743 | self, sw_if_index=self.pg1.sw_if_index, n_input=1, acls=[out2in_acl] |
| 3744 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3745 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3752 | capture = self.send_and_expect( |
| 3753 | self.pg0, pkts_in2out, self.pg1, len(pkts_in2out) |
| 3754 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3755 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3762 | capture = self.send_and_expect( |
| 3763 | self.pg1, pkts_out2in, self.pg0, len(pkts_out2in) |
| 3764 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3765 | self.verify_capture_in(capture, self.pg0) |
| 3766 | self.logger.info(self.vapi.cli("show trace")) |
| 3767 | |
| 3768 | def test_tcp_close(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3769 | """NAT44ED Close TCP session from inside network - output feature""" |
Filip Varga | b681082 | 2022-02-15 11:56:07 -0800 | [diff] [blame] | 3770 | config = self.vapi.nat44_show_running_config() |
| 3771 | old_timeouts = config.timeouts |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3772 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3777 | tcp_transitory=new_transitory, |
| 3778 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3779 | |
| 3780 | self.vapi.nat44_forwarding_enable_disable(enable=1) |
| 3781 | self.nat_add_address(self.pg1.local_ip4) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3782 | twice_nat_addr = "10.0.1.3" |
| 3783 | service_ip = "192.168.16.150" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3784 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3788 | sw_if_index=self.pg0.sw_if_index, is_add=1 |
| 3789 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3790 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3791 | sw_if_index=self.pg0.sw_if_index, flags=flags, is_add=1 |
| 3792 | ) |
Filip Varga | 9c25eb1 | 2021-10-21 13:00:27 +0200 | [diff] [blame] | 3793 | self.vapi.nat44_ed_add_del_output_interface( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3794 | is_add=1, sw_if_index=self.pg1.sw_if_index |
| 3795 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3796 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3797 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3803 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 3804 | start_sessnum = len(sessions) |
| 3805 | |
| 3806 | # SYN packet out->in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3807 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3812 | capture = self.send_and_expect(self.pg1, p, self.pg0, n_rx=1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3813 | p = capture[0] |
| 3814 | tcp_port = p[TCP].sport |
| 3815 | |
| 3816 | # SYN + ACK packet in->out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3817 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3822 | self.send_and_expect(self.pg0, p, self.pg1, n_rx=1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3823 | |
| 3824 | # ACK packet out->in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3825 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3830 | self.send_and_expect(self.pg1, p, self.pg0, n_rx=1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3831 | |
| 3832 | # FIN packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3833 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3838 | self.send_and_expect(self.pg0, p, self.pg1, n_rx=1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3839 | |
| 3840 | # FIN+ACK packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3841 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3846 | self.send_and_expect(self.pg1, p, self.pg0, n_rx=1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3847 | |
| 3848 | # ACK packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3849 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3854 | self.send_and_expect(self.pg0, p, self.pg1, n_rx=1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3855 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3856 | # session now in transitory timeout, but traffic still flows |
| 3857 | # try FIN packet out->in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3858 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3863 | self.pg1.add_stream(p) |
| 3864 | self.pg_enable_capture(self.pg_interfaces) |
| 3865 | self.pg_start() |
| 3866 | |
BenoƮt Ganne | 56eccdb | 2021-08-20 09:18:31 +0200 | [diff] [blame] | 3867 | self.virtual_sleep(new_transitory, "wait for transitory timeout") |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3868 | self.pg0.get_capture(1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3869 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3876 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3881 | self.send_and_assert_no_replies(self.pg1, p) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3882 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 3883 | self.assertEqual(len(sessions) - start_sessnum, 0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3884 | |
| 3885 | def test_tcp_session_close_in(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3886 | """NAT44ED Close TCP session from inside network""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3887 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3895 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3903 | |
| 3904 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 3905 | session_n = len(sessions) |
| 3906 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3907 | self.vapi.nat_set_timeouts( |
| 3908 | udp=300, tcp_established=7440, tcp_transitory=2, icmp=5 |
| 3909 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3910 | |
| 3911 | self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port) |
| 3912 | |
| 3913 | # FIN packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3914 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3919 | self.send_and_expect(self.pg0, p, self.pg1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3920 | pkts = [] |
| 3921 | |
| 3922 | # ACK packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3923 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3928 | pkts.append(p) |
| 3929 | |
| 3930 | # FIN packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3931 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3936 | pkts.append(p) |
| 3937 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3938 | self.send_and_expect(self.pg1, pkts, self.pg0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3939 | |
| 3940 | # ACK packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3941 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3946 | self.send_and_expect(self.pg0, p, self.pg1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3947 | |
| 3948 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 3949 | self.assertEqual(len(sessions) - session_n, 1) |
| 3950 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3951 | # retransmit FIN packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3952 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3957 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3958 | self.send_and_expect(self.pg1, p, self.pg0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3959 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3960 | # retransmit ACK packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3961 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3966 | self.send_and_expect(self.pg0, p, self.pg1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3967 | |
BenoƮt Ganne | 56eccdb | 2021-08-20 09:18:31 +0200 | [diff] [blame] | 3968 | self.virtual_sleep(3) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3969 | # retransmit ACK packet in -> out - this will cause session to be wiped |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3970 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 3975 | self.send_and_assert_no_replies(self.pg0, p) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3976 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3980 | """NAT44ED Close TCP session from outside network""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3981 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3989 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 3997 | |
| 3998 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 3999 | session_n = len(sessions) |
| 4000 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4001 | self.vapi.nat_set_timeouts( |
| 4002 | udp=300, tcp_established=7440, tcp_transitory=2, icmp=5 |
| 4003 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4004 | |
| 4005 | _ = self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port) |
| 4006 | |
| 4007 | # FIN packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4008 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4013 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4019 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4024 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4031 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4036 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4044 | # retransmit FIN packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4045 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4050 | self.send_and_expect(self.pg1, p, self.pg0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4051 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4052 | # retransmit ACK packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4053 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4058 | self.send_and_expect(self.pg0, p, self.pg1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4059 | |
BenoƮt Ganne | 56eccdb | 2021-08-20 09:18:31 +0200 | [diff] [blame] | 4060 | self.virtual_sleep(3) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4061 | # retransmit ACK packet in -> out - this will cause session to be wiped |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4062 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4067 | self.send_and_assert_no_replies(self.pg0, p) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4068 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4072 | """Simultaneous TCP close from both sides""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4073 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4080 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4088 | |
| 4089 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 4090 | session_n = len(sessions) |
| 4091 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4092 | self.vapi.nat_set_timeouts( |
| 4093 | udp=300, tcp_established=7440, tcp_transitory=2, icmp=5 |
| 4094 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4095 | |
| 4096 | out_port = self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port) |
| 4097 | |
| 4098 | # FIN packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4099 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4104 | self.send_and_expect(self.pg0, p, self.pg1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4105 | |
| 4106 | # FIN packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4107 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4112 | self.send_and_expect(self.pg1, p, self.pg0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4113 | |
| 4114 | # ACK packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4115 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4120 | self.send_and_expect(self.pg0, p, self.pg1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4121 | |
| 4122 | # ACK packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4123 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4128 | self.send_and_expect(self.pg1, p, self.pg0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4129 | |
| 4130 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 4131 | self.assertEqual(len(sessions) - session_n, 1) |
| 4132 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4133 | # retransmit FIN packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4134 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4139 | self.send_and_expect(self.pg1, p, self.pg0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4140 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4141 | # retransmit ACK packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4142 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4147 | self.send_and_expect(self.pg0, p, self.pg1) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4148 | |
BenoƮt Ganne | 56eccdb | 2021-08-20 09:18:31 +0200 | [diff] [blame] | 4149 | self.virtual_sleep(3) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4150 | # retransmit ACK packet in -> out - this will cause session to be wiped |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4151 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4156 | self.pg_send(self.pg0, p) |
| 4157 | self.send_and_assert_no_replies(self.pg0, p) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4158 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 4159 | self.assertEqual(len(sessions) - session_n, 0) |
| 4160 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4161 | def test_tcp_session_half_reopen_inside(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4162 | """TCP session in FIN/FIN state not reopened by in2out SYN only""" |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4163 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4169 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4177 | |
| 4178 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 4179 | session_n = len(sessions) |
| 4180 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4181 | self.vapi.nat_set_timeouts( |
| 4182 | udp=300, tcp_established=7440, tcp_transitory=2, icmp=5 |
| 4183 | ) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4184 | |
| 4185 | out_port = self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port) |
| 4186 | |
| 4187 | # FIN packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4188 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4193 | self.send_and_expect(self.pg0, p, self.pg1) |
| 4194 | |
| 4195 | # FIN packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4196 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4201 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4207 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4212 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4216 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4221 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4226 | """TCP session in FIN/FIN state not reopened by out2in SYN only""" |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4227 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4233 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4241 | |
| 4242 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 4243 | session_n = len(sessions) |
| 4244 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4245 | self.vapi.nat_set_timeouts( |
| 4246 | udp=300, tcp_established=7440, tcp_transitory=2, icmp=5 |
| 4247 | ) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4248 | |
| 4249 | out_port = self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port) |
| 4250 | |
| 4251 | # FIN packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4252 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4257 | self.send_and_expect(self.pg0, p, self.pg1) |
| 4258 | |
| 4259 | # FIN packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4260 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4265 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4271 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4276 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4280 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4285 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4290 | """TCP session in FIN/FIN state reopened by SYN from both sides""" |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4291 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4297 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4305 | |
| 4306 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4, 0) |
| 4307 | session_n = len(sessions) |
| 4308 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4309 | self.vapi.nat_set_timeouts( |
| 4310 | udp=300, tcp_established=7440, tcp_transitory=2, icmp=5 |
| 4311 | ) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4312 | |
| 4313 | out_port = self.init_tcp_session(self.pg0, self.pg1, in_port, ext_port) |
| 4314 | |
| 4315 | # FIN packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4316 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4321 | self.send_and_expect(self.pg0, p, self.pg1) |
| 4322 | |
| 4323 | # FIN packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4324 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4329 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4335 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4340 | self.send_and_expect(self.pg1, p, self.pg0) |
| 4341 | |
| 4342 | # send SYN packet in -> out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4343 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4348 | self.send_and_expect(self.pg0, p, self.pg1) |
| 4349 | |
Ole Troan | 5297447 | 2022-03-17 11:58:38 +0100 | [diff] [blame] | 4350 | # send ACK packet out -> in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4351 | 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 Troan | 5297447 | 2022-03-17 11:58:38 +0100 | [diff] [blame] | 4356 | self.send_and_expect(self.pg1, p, self.pg0) |
| 4357 | |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4358 | self.virtual_sleep(3) |
| 4359 | # send ACK packet in -> out - should be forwarded and session alive |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4360 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4365 | 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 Varga | bdd6149 | 2021-04-13 17:47:13 +0200 | [diff] [blame] | 4369 | def test_dynamic_vrf(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4370 | """NAT44ED dynamic translation test: different VRF""" |
Filip Varga | bdd6149 | 2021-04-13 17:47:13 +0200 | [diff] [blame] | 4371 | |
| 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 Ganne | ff570d3 | 2024-04-16 09:36:05 +0200 | [diff] [blame] | 4403 | 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 Varga | bdd6149 | 2021-04-13 17:47:13 +0200 | [diff] [blame] | 4405 | |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4406 | def test_dynamic_output_feature_vrf(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4407 | """NAT44ED dynamic translation test: output-feature, VRF""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4408 | |
| 4409 | # other then default (0) |
| 4410 | new_vrf_id = 22 |
| 4411 | |
| 4412 | self.nat_add_address(self.nat_addr) |
Filip Varga | b681082 | 2022-02-15 11:56:07 -0800 | [diff] [blame] | 4413 | self.vapi.nat44_ed_add_del_output_interface( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4414 | sw_if_index=self.pg8.sw_if_index, is_add=1 |
| 4415 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4416 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4421 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4425 | |
| 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 Chernavin | 4de12b9 | 2021-07-06 06:08:26 -0400 | [diff] [blame] | 4433 | if_idx = self.pg8.sw_if_index |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4434 | cnt = self.statistics["/nat44-ed/in2out/slowpath/tcp"] |
Klement Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 4435 | self.assertEqual(cnt[:, if_idx].sum() - tcpn[:, if_idx].sum(), 2) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4436 | cnt = self.statistics["/nat44-ed/in2out/slowpath/udp"] |
Klement Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 4437 | self.assertEqual(cnt[:, if_idx].sum() - udpn[:, if_idx].sum(), 1) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4438 | cnt = self.statistics["/nat44-ed/in2out/slowpath/icmp"] |
Klement Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 4439 | self.assertEqual(cnt[:, if_idx].sum() - icmpn[:, if_idx].sum(), 1) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4440 | cnt = self.statistics["/nat44-ed/in2out/slowpath/drops"] |
Klement Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 4441 | self.assertEqual(cnt[:, if_idx].sum() - drops[:, if_idx].sum(), 0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4442 | |
| 4443 | # out2in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4444 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4448 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4457 | cnt = self.statistics["/nat44-ed/out2in/fastpath/tcp"] |
Klement Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 4458 | self.assertEqual(cnt[:, if_idx].sum() - tcpn[:, if_idx].sum(), 2) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4459 | cnt = self.statistics["/nat44-ed/out2in/fastpath/udp"] |
Klement Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 4460 | self.assertEqual(cnt[:, if_idx].sum() - udpn[:, if_idx].sum(), 1) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4461 | cnt = self.statistics["/nat44-ed/out2in/fastpath/icmp"] |
Klement Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 4462 | self.assertEqual(cnt[:, if_idx].sum() - icmpn[:, if_idx].sum(), 1) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4463 | cnt = self.statistics["/nat44-ed/out2in/fastpath/drops"] |
Klement Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 4464 | self.assertEqual(cnt[:, if_idx].sum() - drops[:, if_idx].sum(), 0) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4465 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4466 | sessions = self.statistics["/nat44-ed/total-sessions"] |
Klement Sekera | 3887be7 | 2021-03-30 20:29:05 +0200 | [diff] [blame] | 4467 | self.assertEqual(sessions[:, 0].sum(), 3) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4468 | |
| 4469 | finally: |
Filip Varga | bdd6149 | 2021-04-13 17:47:13 +0200 | [diff] [blame] | 4470 | self.pg7.unconfig() |
| 4471 | self.pg8.unconfig() |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4472 | |
BenoƮt Ganne | ff570d3 | 2024-04-16 09:36:05 +0200 | [diff] [blame] | 4473 | self.vapi.ip_table_add_del_v2(is_add=0, table={"table_id": new_vrf_id}) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4474 | |
| 4475 | def test_next_src_nat(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4476 | """NAT44ED On way back forward packet to nat44-in2out node.""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4477 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4478 | twice_nat_addr = "10.0.1.3" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4479 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4485 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4498 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4499 | sw_if_index=self.pg6.sw_if_index, is_add=1 |
| 4500 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4501 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4502 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4507 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4525 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4530 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4548 | """NAT44ED One armed NAT and 1:1 NAPT asymmetrical rule""" |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4549 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4558 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4569 | flags = self.config_flags.NAT_IS_INSIDE |
| 4570 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4571 | sw_if_index=self.pg4.sw_if_index, is_add=1 |
| 4572 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4573 | self.vapi.nat44_interface_add_del_feature( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4574 | sw_if_index=self.pg4.sw_if_index, flags=flags, is_add=1 |
| 4575 | ) |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4576 | |
| 4577 | # from client to service |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4578 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4583 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4602 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4607 | 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 Smith | ad51075 | 2021-08-10 12:22:14 -0500 | [diff] [blame] | 4624 | def test_icmp_error_fwd_outbound(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4625 | """NAT44ED ICMP error outbound with forwarding enabled""" |
Matthew Smith | ad51075 | 2021-08-10 12:22:14 -0500 | [diff] [blame] | 4626 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4637 | 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 Smith | ad51075 | 2021-08-10 12:22:14 -0500 | [diff] [blame] | 4643 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4656 | 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 Smith | ad51075 | 2021-08-10 12:22:14 -0500 | [diff] [blame] | 4662 | |
| 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4674 | def test_tcp_session_open_retransmit1(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4675 | """NAT44ED Open TCP session with SYN,ACK retransmit 1 |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4676 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4677 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4680 | """ |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4690 | self.vapi.nat_set_timeouts( |
| 4691 | udp=300, tcp_established=7440, tcp_transitory=5, icmp=60 |
| 4692 | ) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4693 | # SYN packet in->out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4694 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4699 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4703 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4708 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4713 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4718 | self.send_and_expect(self.pg1, p, self.pg0) |
| 4719 | |
| 4720 | # ACK packet in->out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4721 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4726 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4731 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4737 | self.send_and_expect(self.pg0, p, self.pg1) |
| 4738 | |
| 4739 | def test_tcp_session_open_retransmit2(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4740 | """NAT44ED Open TCP session with SYN,ACK retransmit 2 |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4741 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4742 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4745 | """ |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4755 | self.vapi.nat_set_timeouts( |
| 4756 | udp=300, tcp_established=7440, tcp_transitory=5, icmp=60 |
| 4757 | ) |
Klement Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4758 | # SYN packet in->out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4759 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4764 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4768 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4773 | self.send_and_expect(self.pg1, p, self.pg0) |
| 4774 | |
| 4775 | # ACK packet in->out -- not received by the server |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4776 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4781 | self.send_and_expect(self.pg0, p, self.pg1) |
| 4782 | |
| 4783 | # PUSH + ACK packet in->out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4784 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4790 | self.send_and_expect(self.pg0, p, self.pg1) |
| 4791 | |
| 4792 | # resent SYN + ACK packet out->in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4793 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4798 | self.send_and_expect(self.pg1, p, self.pg0) |
| 4799 | |
| 4800 | # resent ACK packet in->out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4801 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4806 | self.send_and_expect(self.pg0, p, self.pg1) |
| 4807 | |
| 4808 | # resent PUSH + ACK packet in->out |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4809 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4815 | self.send_and_expect(self.pg0, p, self.pg1) |
| 4816 | |
| 4817 | # ACK packet out->in |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4818 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4823 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4828 | 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 Sekera | 56c492a | 2022-01-10 21:57:27 +0000 | [diff] [blame] | 4834 | self.send_and_expect(self.pg0, p, self.pg1) |
| 4835 | |
Dmitry Valter | 6b97c43 | 2022-12-09 19:34:22 +0000 | [diff] [blame] | 4836 | 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 Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4895 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 4896 | if __name__ == "__main__": |
Filip Varga | 18f1e41 | 2020-12-03 15:27:40 +0100 | [diff] [blame] | 4897 | unittest.main(testRunner=VppTestRunner) |