Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import socket |
| 4 | import unittest |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 5 | import struct |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 6 | import StringIO |
| 7 | import random |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 8 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 9 | from framework import VppTestCase, VppTestRunner, running_extended_tests |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 10 | from scapy.layers.inet import IP, TCP, UDP, ICMP |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 11 | from scapy.layers.inet import IPerror, TCPerror, UDPerror, ICMPerror |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 12 | from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest, ICMPv6EchoReply, \ |
| 13 | ICMPv6ND_NS, ICMPv6ND_NA, ICMPv6NDOptDstLLAddr |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 14 | from scapy.layers.inet6 import ICMPv6DestUnreach, IPerror6, IPv6ExtHdrFragment |
Matus Fabian | 328dbc8 | 2017-06-19 04:28:04 -0700 | [diff] [blame] | 15 | from scapy.layers.l2 import Ether, ARP, GRE |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 16 | from scapy.data import IP_PROTOS |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 17 | from scapy.packet import bind_layers, Raw |
| 18 | from scapy.all import fragment6 |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 19 | from util import ppp |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 20 | from ipfix import IPFIX, Set, Template, Data, IPFIXDecoder |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 21 | from time import sleep |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 22 | from util import ip4_range |
Ole Troan | 895b6e8 | 2017-10-20 13:28:20 +0200 | [diff] [blame] | 23 | from util import mactobinary |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 24 | |
| 25 | |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 26 | class MethodHolder(VppTestCase): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 27 | """ NAT create capture and verify method holder """ |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 28 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 29 | def clear_nat44(self): |
| 30 | """ |
| 31 | Clear NAT44 configuration. |
| 32 | """ |
| 33 | if hasattr(self, 'pg7') and hasattr(self, 'pg8'): |
| 34 | # I found no elegant way to do this |
| 35 | self.vapi.ip_add_del_route( |
| 36 | dst_address=self.pg7.remote_ip4n, |
| 37 | dst_address_length=32, |
| 38 | next_hop_address=self.pg7.remote_ip4n, |
| 39 | next_hop_sw_if_index=self.pg7.sw_if_index, |
| 40 | is_add=0) |
| 41 | self.vapi.ip_add_del_route( |
| 42 | dst_address=self.pg8.remote_ip4n, |
| 43 | dst_address_length=32, |
| 44 | next_hop_address=self.pg8.remote_ip4n, |
| 45 | next_hop_sw_if_index=self.pg8.sw_if_index, |
| 46 | is_add=0) |
| 47 | |
| 48 | for intf in [self.pg7, self.pg8]: |
| 49 | neighbors = self.vapi.ip_neighbor_dump(intf.sw_if_index) |
| 50 | for n in neighbors: |
| 51 | self.vapi.ip_neighbor_add_del(intf.sw_if_index, |
| 52 | n.mac_address, |
| 53 | n.ip_address, |
| 54 | is_add=0) |
| 55 | |
| 56 | if self.pg7.has_ip4_config: |
| 57 | self.pg7.unconfig_ip4() |
| 58 | |
| 59 | self.vapi.nat44_forwarding_enable_disable(0) |
| 60 | |
| 61 | interfaces = self.vapi.nat44_interface_addr_dump() |
| 62 | for intf in interfaces: |
| 63 | self.vapi.nat44_add_interface_addr(intf.sw_if_index, |
| 64 | twice_nat=intf.twice_nat, |
| 65 | is_add=0) |
| 66 | |
| 67 | self.vapi.nat_ipfix(enable=0, src_port=self.ipfix_src_port, |
| 68 | domain_id=self.ipfix_domain_id) |
| 69 | self.ipfix_src_port = 4739 |
| 70 | self.ipfix_domain_id = 1 |
| 71 | |
| 72 | interfaces = self.vapi.nat44_interface_dump() |
| 73 | for intf in interfaces: |
| 74 | if intf.is_inside > 1: |
| 75 | self.vapi.nat44_interface_add_del_feature(intf.sw_if_index, |
| 76 | 0, |
| 77 | is_add=0) |
| 78 | self.vapi.nat44_interface_add_del_feature(intf.sw_if_index, |
| 79 | intf.is_inside, |
| 80 | is_add=0) |
| 81 | |
| 82 | interfaces = self.vapi.nat44_interface_output_feature_dump() |
| 83 | for intf in interfaces: |
| 84 | self.vapi.nat44_interface_add_del_output_feature(intf.sw_if_index, |
| 85 | intf.is_inside, |
| 86 | is_add=0) |
| 87 | |
| 88 | static_mappings = self.vapi.nat44_static_mapping_dump() |
| 89 | for sm in static_mappings: |
| 90 | self.vapi.nat44_add_del_static_mapping( |
| 91 | sm.local_ip_address, |
| 92 | sm.external_ip_address, |
| 93 | local_port=sm.local_port, |
| 94 | external_port=sm.external_port, |
| 95 | addr_only=sm.addr_only, |
| 96 | vrf_id=sm.vrf_id, |
| 97 | protocol=sm.protocol, |
| 98 | twice_nat=sm.twice_nat, |
| 99 | self_twice_nat=sm.self_twice_nat, |
| 100 | out2in_only=sm.out2in_only, |
| 101 | tag=sm.tag, |
| 102 | external_sw_if_index=sm.external_sw_if_index, |
| 103 | is_add=0) |
| 104 | |
| 105 | lb_static_mappings = self.vapi.nat44_lb_static_mapping_dump() |
| 106 | for lb_sm in lb_static_mappings: |
| 107 | self.vapi.nat44_add_del_lb_static_mapping( |
| 108 | lb_sm.external_addr, |
| 109 | lb_sm.external_port, |
| 110 | lb_sm.protocol, |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 111 | twice_nat=lb_sm.twice_nat, |
| 112 | self_twice_nat=lb_sm.self_twice_nat, |
| 113 | out2in_only=lb_sm.out2in_only, |
| 114 | tag=lb_sm.tag, |
| 115 | is_add=0, |
| 116 | local_num=0, |
| 117 | locals=[]) |
| 118 | |
| 119 | identity_mappings = self.vapi.nat44_identity_mapping_dump() |
| 120 | for id_m in identity_mappings: |
| 121 | self.vapi.nat44_add_del_identity_mapping( |
| 122 | addr_only=id_m.addr_only, |
| 123 | ip=id_m.ip_address, |
| 124 | port=id_m.port, |
| 125 | sw_if_index=id_m.sw_if_index, |
| 126 | vrf_id=id_m.vrf_id, |
| 127 | protocol=id_m.protocol, |
| 128 | is_add=0) |
| 129 | |
| 130 | adresses = self.vapi.nat44_address_dump() |
| 131 | for addr in adresses: |
| 132 | self.vapi.nat44_add_del_address_range(addr.ip_address, |
| 133 | addr.ip_address, |
| 134 | twice_nat=addr.twice_nat, |
| 135 | is_add=0) |
| 136 | |
| 137 | self.vapi.nat_set_reass() |
| 138 | self.vapi.nat_set_reass(is_ip6=1) |
Matus Fabian | 68ba880 | 2018-08-08 05:52:47 -0700 | [diff] [blame] | 139 | self.verify_no_nat44_user() |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 140 | self.vapi.nat_set_timeouts() |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 141 | self.vapi.nat_set_addr_and_port_alloc_alg() |
Matus Fabian | bb4e022 | 2018-09-13 02:36:25 -0700 | [diff] [blame] | 142 | self.vapi.nat_set_mss_clamping() |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 143 | |
| 144 | def nat44_add_static_mapping(self, local_ip, external_ip='0.0.0.0', |
| 145 | local_port=0, external_port=0, vrf_id=0, |
| 146 | is_add=1, external_sw_if_index=0xFFFFFFFF, |
| 147 | proto=0, twice_nat=0, self_twice_nat=0, |
| 148 | out2in_only=0, tag=""): |
| 149 | """ |
| 150 | Add/delete NAT44 static mapping |
| 151 | |
| 152 | :param local_ip: Local IP address |
| 153 | :param external_ip: External IP address |
| 154 | :param local_port: Local port number (Optional) |
| 155 | :param external_port: External port number (Optional) |
| 156 | :param vrf_id: VRF ID (Default 0) |
| 157 | :param is_add: 1 if add, 0 if delete (Default add) |
| 158 | :param external_sw_if_index: External interface instead of IP address |
| 159 | :param proto: IP protocol (Mandatory if port specified) |
| 160 | :param twice_nat: 1 if translate external host address and port |
| 161 | :param self_twice_nat: 1 if translate external host address and port |
| 162 | whenever external host address equals |
| 163 | local address of internal host |
| 164 | :param out2in_only: if 1 rule is matching only out2in direction |
| 165 | :param tag: Opaque string tag |
| 166 | """ |
| 167 | addr_only = 1 |
| 168 | if local_port and external_port: |
| 169 | addr_only = 0 |
| 170 | l_ip = socket.inet_pton(socket.AF_INET, local_ip) |
| 171 | e_ip = socket.inet_pton(socket.AF_INET, external_ip) |
| 172 | self.vapi.nat44_add_del_static_mapping( |
| 173 | l_ip, |
| 174 | e_ip, |
| 175 | external_sw_if_index, |
| 176 | local_port, |
| 177 | external_port, |
| 178 | addr_only, |
| 179 | vrf_id, |
| 180 | proto, |
| 181 | twice_nat, |
| 182 | self_twice_nat, |
| 183 | out2in_only, |
| 184 | tag, |
| 185 | is_add) |
| 186 | |
| 187 | def nat44_add_address(self, ip, is_add=1, vrf_id=0xFFFFFFFF, twice_nat=0): |
| 188 | """ |
| 189 | Add/delete NAT44 address |
| 190 | |
| 191 | :param ip: IP address |
| 192 | :param is_add: 1 if add, 0 if delete (Default add) |
| 193 | :param twice_nat: twice NAT address for extenal hosts |
| 194 | """ |
| 195 | nat_addr = socket.inet_pton(socket.AF_INET, ip) |
| 196 | self.vapi.nat44_add_del_address_range(nat_addr, nat_addr, is_add, |
| 197 | vrf_id=vrf_id, |
| 198 | twice_nat=twice_nat) |
| 199 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 200 | def create_stream_in(self, in_if, out_if, dst_ip=None, ttl=64): |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 201 | """ |
| 202 | Create packet stream for inside network |
| 203 | |
| 204 | :param in_if: Inside interface |
| 205 | :param out_if: Outside interface |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 206 | :param dst_ip: Destination address |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 207 | :param ttl: TTL of generated packets |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 208 | """ |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 209 | if dst_ip is None: |
| 210 | dst_ip = out_if.remote_ip4 |
| 211 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 212 | pkts = [] |
| 213 | # TCP |
| 214 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 215 | IP(src=in_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 216 | TCP(sport=self.tcp_port_in, dport=20)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 217 | pkts.append(p) |
| 218 | |
| 219 | # UDP |
| 220 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 221 | IP(src=in_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 222 | UDP(sport=self.udp_port_in, dport=20)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 223 | pkts.append(p) |
| 224 | |
| 225 | # ICMP |
| 226 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 227 | IP(src=in_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 228 | ICMP(id=self.icmp_id_in, type='echo-request')) |
| 229 | pkts.append(p) |
| 230 | |
| 231 | return pkts |
| 232 | |
Matus Fabian | 428dc91 | 2017-06-21 06:15:18 -0700 | [diff] [blame] | 233 | def compose_ip6(self, ip4, pref, plen): |
| 234 | """ |
| 235 | Compose IPv4-embedded IPv6 addresses |
| 236 | |
| 237 | :param ip4: IPv4 address |
| 238 | :param pref: IPv6 prefix |
| 239 | :param plen: IPv6 prefix length |
| 240 | :returns: IPv4-embedded IPv6 addresses |
| 241 | """ |
| 242 | pref_n = list(socket.inet_pton(socket.AF_INET6, pref)) |
| 243 | ip4_n = list(socket.inet_pton(socket.AF_INET, ip4)) |
| 244 | if plen == 32: |
| 245 | pref_n[4] = ip4_n[0] |
| 246 | pref_n[5] = ip4_n[1] |
| 247 | pref_n[6] = ip4_n[2] |
| 248 | pref_n[7] = ip4_n[3] |
| 249 | elif plen == 40: |
| 250 | pref_n[5] = ip4_n[0] |
| 251 | pref_n[6] = ip4_n[1] |
| 252 | pref_n[7] = ip4_n[2] |
| 253 | pref_n[9] = ip4_n[3] |
| 254 | elif plen == 48: |
| 255 | pref_n[6] = ip4_n[0] |
| 256 | pref_n[7] = ip4_n[1] |
| 257 | pref_n[9] = ip4_n[2] |
| 258 | pref_n[10] = ip4_n[3] |
| 259 | elif plen == 56: |
| 260 | pref_n[7] = ip4_n[0] |
| 261 | pref_n[9] = ip4_n[1] |
| 262 | pref_n[10] = ip4_n[2] |
| 263 | pref_n[11] = ip4_n[3] |
| 264 | elif plen == 64: |
| 265 | pref_n[9] = ip4_n[0] |
| 266 | pref_n[10] = ip4_n[1] |
| 267 | pref_n[11] = ip4_n[2] |
| 268 | pref_n[12] = ip4_n[3] |
| 269 | elif plen == 96: |
| 270 | pref_n[12] = ip4_n[0] |
| 271 | pref_n[13] = ip4_n[1] |
| 272 | pref_n[14] = ip4_n[2] |
| 273 | pref_n[15] = ip4_n[3] |
| 274 | return socket.inet_ntop(socket.AF_INET6, ''.join(pref_n)) |
| 275 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 276 | def extract_ip4(self, ip6, plen): |
| 277 | """ |
| 278 | Extract IPv4 address embedded in IPv6 addresses |
| 279 | |
| 280 | :param ip6: IPv6 address |
| 281 | :param plen: IPv6 prefix length |
| 282 | :returns: extracted IPv4 address |
| 283 | """ |
| 284 | ip6_n = list(socket.inet_pton(socket.AF_INET6, ip6)) |
| 285 | ip4_n = [None] * 4 |
| 286 | if plen == 32: |
| 287 | ip4_n[0] = ip6_n[4] |
| 288 | ip4_n[1] = ip6_n[5] |
| 289 | ip4_n[2] = ip6_n[6] |
| 290 | ip4_n[3] = ip6_n[7] |
| 291 | elif plen == 40: |
| 292 | ip4_n[0] = ip6_n[5] |
| 293 | ip4_n[1] = ip6_n[6] |
| 294 | ip4_n[2] = ip6_n[7] |
| 295 | ip4_n[3] = ip6_n[9] |
| 296 | elif plen == 48: |
| 297 | ip4_n[0] = ip6_n[6] |
| 298 | ip4_n[1] = ip6_n[7] |
| 299 | ip4_n[2] = ip6_n[9] |
| 300 | ip4_n[3] = ip6_n[10] |
| 301 | elif plen == 56: |
| 302 | ip4_n[0] = ip6_n[7] |
| 303 | ip4_n[1] = ip6_n[9] |
| 304 | ip4_n[2] = ip6_n[10] |
| 305 | ip4_n[3] = ip6_n[11] |
| 306 | elif plen == 64: |
| 307 | ip4_n[0] = ip6_n[9] |
| 308 | ip4_n[1] = ip6_n[10] |
| 309 | ip4_n[2] = ip6_n[11] |
| 310 | ip4_n[3] = ip6_n[12] |
| 311 | elif plen == 96: |
| 312 | ip4_n[0] = ip6_n[12] |
| 313 | ip4_n[1] = ip6_n[13] |
| 314 | ip4_n[2] = ip6_n[14] |
| 315 | ip4_n[3] = ip6_n[15] |
| 316 | return socket.inet_ntop(socket.AF_INET, ''.join(ip4_n)) |
| 317 | |
Matus Fabian | 428dc91 | 2017-06-21 06:15:18 -0700 | [diff] [blame] | 318 | def create_stream_in_ip6(self, in_if, out_if, hlim=64, pref=None, plen=0): |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 319 | """ |
| 320 | Create IPv6 packet stream for inside network |
| 321 | |
| 322 | :param in_if: Inside interface |
| 323 | :param out_if: Outside interface |
| 324 | :param ttl: Hop Limit of generated packets |
Matus Fabian | 428dc91 | 2017-06-21 06:15:18 -0700 | [diff] [blame] | 325 | :param pref: NAT64 prefix |
| 326 | :param plen: NAT64 prefix length |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 327 | """ |
| 328 | pkts = [] |
Matus Fabian | 428dc91 | 2017-06-21 06:15:18 -0700 | [diff] [blame] | 329 | if pref is None: |
| 330 | dst = ''.join(['64:ff9b::', out_if.remote_ip4]) |
| 331 | else: |
| 332 | dst = self.compose_ip6(out_if.remote_ip4, pref, plen) |
| 333 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 334 | # TCP |
| 335 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
| 336 | IPv6(src=in_if.remote_ip6, dst=dst, hlim=hlim) / |
| 337 | TCP(sport=self.tcp_port_in, dport=20)) |
| 338 | pkts.append(p) |
| 339 | |
| 340 | # UDP |
| 341 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
| 342 | IPv6(src=in_if.remote_ip6, dst=dst, hlim=hlim) / |
| 343 | UDP(sport=self.udp_port_in, dport=20)) |
| 344 | pkts.append(p) |
| 345 | |
| 346 | # ICMP |
| 347 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
| 348 | IPv6(src=in_if.remote_ip6, dst=dst, hlim=hlim) / |
| 349 | ICMPv6EchoRequest(id=self.icmp_id_in)) |
| 350 | pkts.append(p) |
| 351 | |
| 352 | return pkts |
| 353 | |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 354 | def create_stream_out(self, out_if, dst_ip=None, ttl=64, |
| 355 | use_inside_ports=False): |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 356 | """ |
| 357 | Create packet stream for outside network |
| 358 | |
| 359 | :param out_if: Outside interface |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 360 | :param dst_ip: Destination IP address (Default use global NAT address) |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 361 | :param ttl: TTL of generated packets |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 362 | :param use_inside_ports: Use inside NAT ports as destination ports |
| 363 | instead of outside ports |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 364 | """ |
| 365 | if dst_ip is None: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 366 | dst_ip = self.nat_addr |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 367 | if not use_inside_ports: |
| 368 | tcp_port = self.tcp_port_out |
| 369 | udp_port = self.udp_port_out |
| 370 | icmp_id = self.icmp_id_out |
| 371 | else: |
| 372 | tcp_port = self.tcp_port_in |
| 373 | udp_port = self.udp_port_in |
| 374 | icmp_id = self.icmp_id_in |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 375 | pkts = [] |
| 376 | # TCP |
| 377 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 378 | IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 379 | TCP(dport=tcp_port, sport=20)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 380 | pkts.append(p) |
| 381 | |
| 382 | # UDP |
| 383 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 384 | IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 385 | UDP(dport=udp_port, sport=20)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 386 | pkts.append(p) |
| 387 | |
| 388 | # ICMP |
| 389 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 390 | IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 391 | ICMP(id=icmp_id, type='echo-reply')) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 392 | pkts.append(p) |
| 393 | |
| 394 | return pkts |
| 395 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 396 | def create_stream_out_ip6(self, out_if, src_ip, dst_ip, hl=64): |
| 397 | """ |
| 398 | Create packet stream for outside network |
| 399 | |
| 400 | :param out_if: Outside interface |
| 401 | :param dst_ip: Destination IP address (Default use global NAT address) |
| 402 | :param hl: HL of generated packets |
| 403 | """ |
| 404 | pkts = [] |
| 405 | # TCP |
| 406 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
| 407 | IPv6(src=src_ip, dst=dst_ip, hlim=hl) / |
| 408 | TCP(dport=self.tcp_port_out, sport=20)) |
| 409 | pkts.append(p) |
| 410 | |
| 411 | # UDP |
| 412 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
| 413 | IPv6(src=src_ip, dst=dst_ip, hlim=hl) / |
| 414 | UDP(dport=self.udp_port_out, sport=20)) |
| 415 | pkts.append(p) |
| 416 | |
| 417 | # ICMP |
| 418 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
| 419 | IPv6(src=src_ip, dst=dst_ip, hlim=hl) / |
| 420 | ICMPv6EchoReply(id=self.icmp_id_out)) |
| 421 | pkts.append(p) |
| 422 | |
| 423 | return pkts |
| 424 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 425 | def verify_capture_out(self, capture, nat_ip=None, same_port=False, |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 426 | dst_ip=None, is_ip6=False): |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 427 | """ |
| 428 | Verify captured packets on outside network |
| 429 | |
| 430 | :param capture: Captured packets |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 431 | :param nat_ip: Translated IP address (Default use global NAT address) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 432 | :param same_port: Sorce port number is not translated (Default False) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 433 | :param dst_ip: Destination IP address (Default do not verify) |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 434 | :param is_ip6: If L3 protocol is IPv6 (Default False) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 435 | """ |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 436 | if is_ip6: |
| 437 | IP46 = IPv6 |
| 438 | ICMP46 = ICMPv6EchoRequest |
| 439 | else: |
| 440 | IP46 = IP |
| 441 | ICMP46 = ICMP |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 442 | if nat_ip is None: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 443 | nat_ip = self.nat_addr |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 444 | for packet in capture: |
| 445 | try: |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 446 | if not is_ip6: |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 447 | self.assert_packet_checksums_valid(packet) |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 448 | self.assertEqual(packet[IP46].src, nat_ip) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 449 | if dst_ip is not None: |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 450 | self.assertEqual(packet[IP46].dst, dst_ip) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 451 | if packet.haslayer(TCP): |
| 452 | if same_port: |
| 453 | self.assertEqual(packet[TCP].sport, self.tcp_port_in) |
| 454 | else: |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 455 | self.assertNotEqual( |
| 456 | packet[TCP].sport, self.tcp_port_in) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 457 | self.tcp_port_out = packet[TCP].sport |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 458 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 459 | elif packet.haslayer(UDP): |
| 460 | if same_port: |
| 461 | self.assertEqual(packet[UDP].sport, self.udp_port_in) |
| 462 | else: |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 463 | self.assertNotEqual( |
| 464 | packet[UDP].sport, self.udp_port_in) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 465 | self.udp_port_out = packet[UDP].sport |
| 466 | else: |
| 467 | if same_port: |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 468 | self.assertEqual(packet[ICMP46].id, self.icmp_id_in) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 469 | else: |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 470 | self.assertNotEqual(packet[ICMP46].id, self.icmp_id_in) |
| 471 | self.icmp_id_out = packet[ICMP46].id |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 472 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 473 | except: |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 474 | self.logger.error(ppp("Unexpected or invalid packet " |
| 475 | "(outside network):", packet)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 476 | raise |
| 477 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 478 | def verify_capture_out_ip6(self, capture, nat_ip, same_port=False, |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 479 | dst_ip=None): |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 480 | """ |
| 481 | Verify captured packets on outside network |
| 482 | |
| 483 | :param capture: Captured packets |
| 484 | :param nat_ip: Translated IP address |
| 485 | :param same_port: Sorce port number is not translated (Default False) |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 486 | :param dst_ip: Destination IP address (Default do not verify) |
| 487 | """ |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 488 | return self.verify_capture_out(capture, nat_ip, same_port, dst_ip, |
| 489 | True) |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 490 | |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 491 | def verify_capture_in(self, capture, in_if): |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 492 | """ |
| 493 | Verify captured packets on inside network |
| 494 | |
| 495 | :param capture: Captured packets |
| 496 | :param in_if: Inside interface |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 497 | """ |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 498 | for packet in capture: |
| 499 | try: |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 500 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 501 | self.assertEqual(packet[IP].dst, in_if.remote_ip4) |
| 502 | if packet.haslayer(TCP): |
| 503 | self.assertEqual(packet[TCP].dport, self.tcp_port_in) |
| 504 | elif packet.haslayer(UDP): |
| 505 | self.assertEqual(packet[UDP].dport, self.udp_port_in) |
| 506 | else: |
| 507 | self.assertEqual(packet[ICMP].id, self.icmp_id_in) |
| 508 | except: |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 509 | self.logger.error(ppp("Unexpected or invalid packet " |
| 510 | "(inside network):", packet)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 511 | raise |
| 512 | |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 513 | def verify_capture_in_ip6(self, capture, src_ip, dst_ip): |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 514 | """ |
| 515 | Verify captured IPv6 packets on inside network |
| 516 | |
| 517 | :param capture: Captured packets |
| 518 | :param src_ip: Source IP |
| 519 | :param dst_ip: Destination IP address |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 520 | """ |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 521 | for packet in capture: |
| 522 | try: |
| 523 | self.assertEqual(packet[IPv6].src, src_ip) |
| 524 | self.assertEqual(packet[IPv6].dst, dst_ip) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 525 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 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[ICMPv6EchoReply].id, |
| 532 | self.icmp_id_in) |
| 533 | except: |
| 534 | self.logger.error(ppp("Unexpected or invalid packet " |
| 535 | "(inside network):", packet)) |
| 536 | raise |
| 537 | |
Matus Fabian | 675a69c | 2017-01-18 01:46:01 -0800 | [diff] [blame] | 538 | def verify_capture_no_translation(self, capture, ingress_if, egress_if): |
| 539 | """ |
| 540 | Verify captured packet that don't have to be translated |
| 541 | |
| 542 | :param capture: Captured packets |
| 543 | :param ingress_if: Ingress interface |
| 544 | :param egress_if: Egress interface |
| 545 | """ |
| 546 | for packet in capture: |
| 547 | try: |
| 548 | self.assertEqual(packet[IP].src, ingress_if.remote_ip4) |
| 549 | self.assertEqual(packet[IP].dst, egress_if.remote_ip4) |
| 550 | if packet.haslayer(TCP): |
| 551 | self.assertEqual(packet[TCP].sport, self.tcp_port_in) |
| 552 | elif packet.haslayer(UDP): |
| 553 | self.assertEqual(packet[UDP].sport, self.udp_port_in) |
| 554 | else: |
| 555 | self.assertEqual(packet[ICMP].id, self.icmp_id_in) |
| 556 | except: |
| 557 | self.logger.error(ppp("Unexpected or invalid packet " |
| 558 | "(inside network):", packet)) |
| 559 | raise |
| 560 | |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 561 | def verify_capture_out_with_icmp_errors(self, capture, src_ip=None, |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 562 | icmp_type=11): |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 563 | """ |
| 564 | Verify captured packets with ICMP errors on outside network |
| 565 | |
| 566 | :param capture: Captured packets |
| 567 | :param src_ip: Translated IP address or IP address of VPP |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 568 | (Default use global NAT address) |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 569 | :param icmp_type: Type of error ICMP packet |
| 570 | we are expecting (Default 11) |
| 571 | """ |
| 572 | if src_ip is None: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 573 | src_ip = self.nat_addr |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 574 | for packet in capture: |
| 575 | try: |
| 576 | self.assertEqual(packet[IP].src, src_ip) |
| 577 | self.assertTrue(packet.haslayer(ICMP)) |
| 578 | icmp = packet[ICMP] |
| 579 | self.assertEqual(icmp.type, icmp_type) |
| 580 | self.assertTrue(icmp.haslayer(IPerror)) |
| 581 | inner_ip = icmp[IPerror] |
| 582 | if inner_ip.haslayer(TCPerror): |
| 583 | self.assertEqual(inner_ip[TCPerror].dport, |
| 584 | self.tcp_port_out) |
| 585 | elif inner_ip.haslayer(UDPerror): |
| 586 | self.assertEqual(inner_ip[UDPerror].dport, |
| 587 | self.udp_port_out) |
| 588 | else: |
| 589 | self.assertEqual(inner_ip[ICMPerror].id, self.icmp_id_out) |
| 590 | except: |
| 591 | self.logger.error(ppp("Unexpected or invalid packet " |
| 592 | "(outside network):", packet)) |
| 593 | raise |
| 594 | |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 595 | def verify_capture_in_with_icmp_errors(self, capture, in_if, icmp_type=11): |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 596 | """ |
| 597 | Verify captured packets with ICMP errors on inside network |
| 598 | |
| 599 | :param capture: Captured packets |
| 600 | :param in_if: Inside interface |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 601 | :param icmp_type: Type of error ICMP packet |
| 602 | we are expecting (Default 11) |
| 603 | """ |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 604 | for packet in capture: |
| 605 | try: |
| 606 | self.assertEqual(packet[IP].dst, in_if.remote_ip4) |
| 607 | self.assertTrue(packet.haslayer(ICMP)) |
| 608 | icmp = packet[ICMP] |
| 609 | self.assertEqual(icmp.type, icmp_type) |
| 610 | self.assertTrue(icmp.haslayer(IPerror)) |
| 611 | inner_ip = icmp[IPerror] |
| 612 | if inner_ip.haslayer(TCPerror): |
| 613 | self.assertEqual(inner_ip[TCPerror].sport, |
| 614 | self.tcp_port_in) |
| 615 | elif inner_ip.haslayer(UDPerror): |
| 616 | self.assertEqual(inner_ip[UDPerror].sport, |
| 617 | self.udp_port_in) |
| 618 | else: |
| 619 | self.assertEqual(inner_ip[ICMPerror].id, self.icmp_id_in) |
| 620 | except: |
| 621 | self.logger.error(ppp("Unexpected or invalid packet " |
| 622 | "(inside network):", packet)) |
| 623 | raise |
| 624 | |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 625 | def create_stream_frag(self, src_if, dst, sport, dport, data, |
| 626 | proto=IP_PROTOS.tcp, echo_reply=False): |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 627 | """ |
| 628 | Create fragmented packet stream |
| 629 | |
| 630 | :param src_if: Source interface |
| 631 | :param dst: Destination IPv4 address |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 632 | :param sport: Source port |
| 633 | :param dport: Destination port |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 634 | :param data: Payload data |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 635 | :param proto: protocol (TCP, UDP, ICMP) |
| 636 | :param echo_reply: use echo_reply if protocol is ICMP |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 637 | :returns: Fragmets |
| 638 | """ |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 639 | if proto == IP_PROTOS.tcp: |
| 640 | p = (IP(src=src_if.remote_ip4, dst=dst) / |
| 641 | TCP(sport=sport, dport=dport) / |
| 642 | Raw(data)) |
| 643 | p = p.__class__(str(p)) |
| 644 | chksum = p['TCP'].chksum |
| 645 | proto_header = TCP(sport=sport, dport=dport, chksum=chksum) |
| 646 | elif proto == IP_PROTOS.udp: |
| 647 | proto_header = UDP(sport=sport, dport=dport) |
| 648 | elif proto == IP_PROTOS.icmp: |
| 649 | if not echo_reply: |
| 650 | proto_header = ICMP(id=sport, type='echo-request') |
| 651 | else: |
| 652 | proto_header = ICMP(id=sport, type='echo-reply') |
| 653 | else: |
| 654 | raise Exception("Unsupported protocol") |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 655 | id = random.randint(0, 65535) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 656 | pkts = [] |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 657 | if proto == IP_PROTOS.tcp: |
| 658 | raw = Raw(data[0:4]) |
| 659 | else: |
| 660 | raw = Raw(data[0:16]) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 661 | p = (Ether(src=src_if.remote_mac, dst=src_if.local_mac) / |
| 662 | IP(src=src_if.remote_ip4, dst=dst, flags="MF", frag=0, id=id) / |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 663 | proto_header / |
| 664 | raw) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 665 | pkts.append(p) |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 666 | if proto == IP_PROTOS.tcp: |
| 667 | raw = Raw(data[4:20]) |
| 668 | else: |
| 669 | raw = Raw(data[16:32]) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 670 | p = (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, |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 672 | proto=proto) / |
| 673 | raw) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 674 | pkts.append(p) |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 675 | if proto == IP_PROTOS.tcp: |
| 676 | raw = Raw(data[20:]) |
| 677 | else: |
| 678 | raw = Raw(data[32:]) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 679 | p = (Ether(src=src_if.remote_mac, dst=src_if.local_mac) / |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 680 | IP(src=src_if.remote_ip4, dst=dst, frag=5, proto=proto, |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 681 | id=id) / |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 682 | raw) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 683 | pkts.append(p) |
| 684 | return pkts |
| 685 | |
| 686 | def create_stream_frag_ip6(self, src_if, dst, sport, dport, data, |
| 687 | pref=None, plen=0, frag_size=128): |
| 688 | """ |
| 689 | Create fragmented packet stream |
| 690 | |
| 691 | :param src_if: Source interface |
| 692 | :param dst: Destination IPv4 address |
| 693 | :param sport: Source TCP port |
| 694 | :param dport: Destination TCP port |
| 695 | :param data: Payload data |
| 696 | :param pref: NAT64 prefix |
| 697 | :param plen: NAT64 prefix length |
| 698 | :param fragsize: size of fragments |
| 699 | :returns: Fragmets |
| 700 | """ |
| 701 | if pref is None: |
| 702 | dst_ip6 = ''.join(['64:ff9b::', dst]) |
| 703 | else: |
| 704 | dst_ip6 = self.compose_ip6(dst, pref, plen) |
| 705 | |
| 706 | p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / |
| 707 | IPv6(src=src_if.remote_ip6, dst=dst_ip6) / |
| 708 | IPv6ExtHdrFragment(id=random.randint(0, 65535)) / |
| 709 | TCP(sport=sport, dport=dport) / |
| 710 | Raw(data)) |
| 711 | |
| 712 | return fragment6(p, frag_size) |
| 713 | |
| 714 | def reass_frags_and_verify(self, frags, src, dst): |
| 715 | """ |
| 716 | Reassemble and verify fragmented packet |
| 717 | |
| 718 | :param frags: Captured fragments |
| 719 | :param src: Source IPv4 address to verify |
| 720 | :param dst: Destination IPv4 address to verify |
| 721 | |
| 722 | :returns: Reassembled IPv4 packet |
| 723 | """ |
| 724 | buffer = StringIO.StringIO() |
| 725 | for p in frags: |
| 726 | self.assertEqual(p[IP].src, src) |
| 727 | self.assertEqual(p[IP].dst, dst) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 728 | self.assert_ip_checksum_valid(p) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 729 | buffer.seek(p[IP].frag * 8) |
| 730 | buffer.write(p[IP].payload) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 731 | ip = IP(src=frags[0][IP].src, dst=frags[0][IP].dst, |
| 732 | proto=frags[0][IP].proto) |
| 733 | if ip.proto == IP_PROTOS.tcp: |
| 734 | p = (ip / TCP(buffer.getvalue())) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 735 | self.assert_tcp_checksum_valid(p) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 736 | elif ip.proto == IP_PROTOS.udp: |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 737 | p = (ip / UDP(buffer.getvalue()[:8]) / |
| 738 | Raw(buffer.getvalue()[8:])) |
| 739 | elif ip.proto == IP_PROTOS.icmp: |
| 740 | p = (ip / ICMP(buffer.getvalue())) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 741 | return p |
| 742 | |
| 743 | def reass_frags_and_verify_ip6(self, frags, src, dst): |
| 744 | """ |
| 745 | Reassemble and verify fragmented packet |
| 746 | |
| 747 | :param frags: Captured fragments |
| 748 | :param src: Source IPv6 address to verify |
| 749 | :param dst: Destination IPv6 address to verify |
| 750 | |
| 751 | :returns: Reassembled IPv6 packet |
| 752 | """ |
| 753 | buffer = StringIO.StringIO() |
| 754 | for p in frags: |
| 755 | self.assertEqual(p[IPv6].src, src) |
| 756 | self.assertEqual(p[IPv6].dst, dst) |
| 757 | buffer.seek(p[IPv6ExtHdrFragment].offset * 8) |
| 758 | buffer.write(p[IPv6ExtHdrFragment].payload) |
| 759 | ip = IPv6(src=frags[0][IPv6].src, dst=frags[0][IPv6].dst, |
| 760 | nh=frags[0][IPv6ExtHdrFragment].nh) |
| 761 | if ip.nh == IP_PROTOS.tcp: |
| 762 | p = (ip / TCP(buffer.getvalue())) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 763 | elif ip.nh == IP_PROTOS.udp: |
| 764 | p = (ip / UDP(buffer.getvalue())) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 765 | self.assert_packet_checksums_valid(p) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 766 | return p |
| 767 | |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 768 | def initiate_tcp_session(self, in_if, out_if): |
| 769 | """ |
| 770 | Initiates TCP session |
| 771 | |
| 772 | :param in_if: Inside interface |
| 773 | :param out_if: Outside interface |
| 774 | """ |
| 775 | try: |
| 776 | # SYN packet in->out |
| 777 | p = (Ether(src=in_if.remote_mac, dst=in_if.local_mac) / |
| 778 | IP(src=in_if.remote_ip4, dst=out_if.remote_ip4) / |
| 779 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 780 | flags="S")) |
| 781 | in_if.add_stream(p) |
| 782 | self.pg_enable_capture(self.pg_interfaces) |
| 783 | self.pg_start() |
| 784 | capture = out_if.get_capture(1) |
| 785 | p = capture[0] |
| 786 | self.tcp_port_out = p[TCP].sport |
| 787 | |
| 788 | # SYN + ACK packet out->in |
| 789 | p = (Ether(src=out_if.remote_mac, dst=out_if.local_mac) / |
| 790 | IP(src=out_if.remote_ip4, dst=self.nat_addr) / |
| 791 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 792 | flags="SA")) |
| 793 | out_if.add_stream(p) |
| 794 | self.pg_enable_capture(self.pg_interfaces) |
| 795 | self.pg_start() |
| 796 | in_if.get_capture(1) |
| 797 | |
| 798 | # ACK packet in->out |
| 799 | p = (Ether(src=in_if.remote_mac, dst=in_if.local_mac) / |
| 800 | IP(src=in_if.remote_ip4, dst=out_if.remote_ip4) / |
| 801 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 802 | flags="A")) |
| 803 | in_if.add_stream(p) |
| 804 | self.pg_enable_capture(self.pg_interfaces) |
| 805 | self.pg_start() |
| 806 | out_if.get_capture(1) |
| 807 | |
| 808 | except: |
| 809 | self.logger.error("TCP 3 way handshake failed") |
| 810 | raise |
| 811 | |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 812 | def verify_ipfix_nat44_ses(self, data): |
| 813 | """ |
| 814 | Verify IPFIX NAT44 session create/delete event |
| 815 | |
| 816 | :param data: Decoded IPFIX data records |
| 817 | """ |
| 818 | nat44_ses_create_num = 0 |
| 819 | nat44_ses_delete_num = 0 |
| 820 | self.assertEqual(6, len(data)) |
| 821 | for record in data: |
| 822 | # natEvent |
| 823 | self.assertIn(ord(record[230]), [4, 5]) |
| 824 | if ord(record[230]) == 4: |
| 825 | nat44_ses_create_num += 1 |
| 826 | else: |
| 827 | nat44_ses_delete_num += 1 |
| 828 | # sourceIPv4Address |
| 829 | self.assertEqual(self.pg0.remote_ip4n, record[8]) |
| 830 | # postNATSourceIPv4Address |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 831 | self.assertEqual(socket.inet_pton(socket.AF_INET, self.nat_addr), |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 832 | record[225]) |
| 833 | # ingressVRFID |
| 834 | self.assertEqual(struct.pack("!I", 0), record[234]) |
| 835 | # protocolIdentifier/sourceTransportPort/postNAPTSourceTransportPort |
| 836 | if IP_PROTOS.icmp == ord(record[4]): |
| 837 | self.assertEqual(struct.pack("!H", self.icmp_id_in), record[7]) |
| 838 | self.assertEqual(struct.pack("!H", self.icmp_id_out), |
| 839 | record[227]) |
| 840 | elif IP_PROTOS.tcp == ord(record[4]): |
| 841 | self.assertEqual(struct.pack("!H", self.tcp_port_in), |
| 842 | record[7]) |
| 843 | self.assertEqual(struct.pack("!H", self.tcp_port_out), |
| 844 | record[227]) |
| 845 | elif IP_PROTOS.udp == ord(record[4]): |
| 846 | self.assertEqual(struct.pack("!H", self.udp_port_in), |
| 847 | record[7]) |
| 848 | self.assertEqual(struct.pack("!H", self.udp_port_out), |
| 849 | record[227]) |
| 850 | else: |
| 851 | self.fail("Invalid protocol") |
| 852 | self.assertEqual(3, nat44_ses_create_num) |
| 853 | self.assertEqual(3, nat44_ses_delete_num) |
| 854 | |
| 855 | def verify_ipfix_addr_exhausted(self, data): |
| 856 | """ |
| 857 | Verify IPFIX NAT addresses event |
| 858 | |
| 859 | :param data: Decoded IPFIX data records |
| 860 | """ |
| 861 | self.assertEqual(1, len(data)) |
| 862 | record = data[0] |
| 863 | # natEvent |
| 864 | self.assertEqual(ord(record[230]), 3) |
| 865 | # natPoolID |
| 866 | self.assertEqual(struct.pack("!I", 0), record[283]) |
| 867 | |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 868 | def verify_ipfix_max_sessions(self, data, limit): |
| 869 | """ |
| 870 | Verify IPFIX maximum session entries exceeded event |
| 871 | |
| 872 | :param data: Decoded IPFIX data records |
| 873 | :param limit: Number of maximum session entries that can be created. |
| 874 | """ |
| 875 | self.assertEqual(1, len(data)) |
| 876 | record = data[0] |
| 877 | # natEvent |
| 878 | self.assertEqual(ord(record[230]), 13) |
| 879 | # natQuotaExceededEvent |
| 880 | self.assertEqual(struct.pack("I", 1), record[466]) |
| 881 | # maxSessionEntries |
| 882 | self.assertEqual(struct.pack("I", limit), record[471]) |
| 883 | |
| 884 | def verify_ipfix_max_bibs(self, data, limit): |
| 885 | """ |
| 886 | Verify IPFIX maximum BIB entries exceeded event |
| 887 | |
| 888 | :param data: Decoded IPFIX data records |
| 889 | :param limit: Number of maximum BIB entries that can be created. |
| 890 | """ |
| 891 | self.assertEqual(1, len(data)) |
| 892 | record = data[0] |
| 893 | # natEvent |
| 894 | self.assertEqual(ord(record[230]), 13) |
| 895 | # natQuotaExceededEvent |
| 896 | self.assertEqual(struct.pack("I", 2), record[466]) |
| 897 | # maxBIBEntries |
| 898 | self.assertEqual(struct.pack("I", limit), record[472]) |
| 899 | |
| 900 | def verify_ipfix_max_fragments_ip6(self, data, limit, src_addr): |
| 901 | """ |
| 902 | Verify IPFIX maximum IPv6 fragments pending reassembly exceeded event |
| 903 | |
| 904 | :param data: Decoded IPFIX data records |
| 905 | :param limit: Number of maximum fragments pending reassembly |
| 906 | :param src_addr: IPv6 source address |
| 907 | """ |
| 908 | self.assertEqual(1, len(data)) |
| 909 | record = data[0] |
| 910 | # natEvent |
| 911 | self.assertEqual(ord(record[230]), 13) |
| 912 | # natQuotaExceededEvent |
| 913 | self.assertEqual(struct.pack("I", 5), record[466]) |
| 914 | # maxFragmentsPendingReassembly |
| 915 | self.assertEqual(struct.pack("I", limit), record[475]) |
| 916 | # sourceIPv6Address |
| 917 | self.assertEqual(src_addr, record[27]) |
| 918 | |
| 919 | def verify_ipfix_max_fragments_ip4(self, data, limit, src_addr): |
| 920 | """ |
| 921 | Verify IPFIX maximum IPv4 fragments pending reassembly exceeded event |
| 922 | |
| 923 | :param data: Decoded IPFIX data records |
| 924 | :param limit: Number of maximum fragments pending reassembly |
| 925 | :param src_addr: IPv4 source address |
| 926 | """ |
| 927 | self.assertEqual(1, len(data)) |
| 928 | record = data[0] |
| 929 | # natEvent |
| 930 | self.assertEqual(ord(record[230]), 13) |
| 931 | # natQuotaExceededEvent |
| 932 | self.assertEqual(struct.pack("I", 5), record[466]) |
| 933 | # maxFragmentsPendingReassembly |
| 934 | self.assertEqual(struct.pack("I", limit), record[475]) |
| 935 | # sourceIPv4Address |
| 936 | self.assertEqual(src_addr, record[8]) |
| 937 | |
| 938 | def verify_ipfix_bib(self, data, is_create, src_addr): |
| 939 | """ |
| 940 | Verify IPFIX NAT64 BIB create and delete events |
| 941 | |
| 942 | :param data: Decoded IPFIX data records |
| 943 | :param is_create: Create event if nonzero value otherwise delete event |
| 944 | :param src_addr: IPv6 source address |
| 945 | """ |
| 946 | self.assertEqual(1, len(data)) |
| 947 | record = data[0] |
| 948 | # natEvent |
| 949 | if is_create: |
| 950 | self.assertEqual(ord(record[230]), 10) |
| 951 | else: |
| 952 | self.assertEqual(ord(record[230]), 11) |
| 953 | # sourceIPv6Address |
| 954 | self.assertEqual(src_addr, record[27]) |
| 955 | # postNATSourceIPv4Address |
| 956 | self.assertEqual(self.nat_addr_n, record[225]) |
| 957 | # protocolIdentifier |
| 958 | self.assertEqual(IP_PROTOS.tcp, ord(record[4])) |
| 959 | # ingressVRFID |
| 960 | self.assertEqual(struct.pack("!I", 0), record[234]) |
| 961 | # sourceTransportPort |
| 962 | self.assertEqual(struct.pack("!H", self.tcp_port_in), record[7]) |
| 963 | # postNAPTSourceTransportPort |
| 964 | self.assertEqual(struct.pack("!H", self.tcp_port_out), record[227]) |
| 965 | |
| 966 | def verify_ipfix_nat64_ses(self, data, is_create, src_addr, dst_addr, |
| 967 | dst_port): |
| 968 | """ |
| 969 | Verify IPFIX NAT64 session create and delete events |
| 970 | |
| 971 | :param data: Decoded IPFIX data records |
| 972 | :param is_create: Create event if nonzero value otherwise delete event |
| 973 | :param src_addr: IPv6 source address |
| 974 | :param dst_addr: IPv4 destination address |
| 975 | :param dst_port: destination TCP port |
| 976 | """ |
| 977 | self.assertEqual(1, len(data)) |
| 978 | record = data[0] |
| 979 | # natEvent |
| 980 | if is_create: |
| 981 | self.assertEqual(ord(record[230]), 6) |
| 982 | else: |
| 983 | self.assertEqual(ord(record[230]), 7) |
| 984 | # sourceIPv6Address |
| 985 | self.assertEqual(src_addr, record[27]) |
| 986 | # destinationIPv6Address |
| 987 | self.assertEqual(socket.inet_pton(socket.AF_INET6, |
| 988 | self.compose_ip6(dst_addr, |
| 989 | '64:ff9b::', |
| 990 | 96)), |
| 991 | record[28]) |
| 992 | # postNATSourceIPv4Address |
| 993 | self.assertEqual(self.nat_addr_n, record[225]) |
| 994 | # postNATDestinationIPv4Address |
| 995 | self.assertEqual(socket.inet_pton(socket.AF_INET, dst_addr), |
| 996 | record[226]) |
| 997 | # protocolIdentifier |
| 998 | self.assertEqual(IP_PROTOS.tcp, ord(record[4])) |
| 999 | # ingressVRFID |
| 1000 | self.assertEqual(struct.pack("!I", 0), record[234]) |
| 1001 | # sourceTransportPort |
| 1002 | self.assertEqual(struct.pack("!H", self.tcp_port_in), record[7]) |
| 1003 | # postNAPTSourceTransportPort |
| 1004 | self.assertEqual(struct.pack("!H", self.tcp_port_out), record[227]) |
| 1005 | # destinationTransportPort |
| 1006 | self.assertEqual(struct.pack("!H", dst_port), record[11]) |
| 1007 | # postNAPTDestinationTransportPort |
| 1008 | self.assertEqual(struct.pack("!H", dst_port), record[228]) |
| 1009 | |
Matus Fabian | 68ba880 | 2018-08-08 05:52:47 -0700 | [diff] [blame] | 1010 | def verify_no_nat44_user(self): |
| 1011 | """ Verify that there is no NAT44 user """ |
| 1012 | users = self.vapi.nat44_user_dump() |
| 1013 | self.assertEqual(len(users), 0) |
| 1014 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 1015 | def verify_ipfix_max_entries_per_user(self, data, limit, src_addr): |
| 1016 | """ |
| 1017 | Verify IPFIX maximum entries per user exceeded event |
| 1018 | |
| 1019 | :param data: Decoded IPFIX data records |
| 1020 | :param limit: Number of maximum entries per user |
| 1021 | :param src_addr: IPv4 source address |
| 1022 | """ |
| 1023 | self.assertEqual(1, len(data)) |
| 1024 | record = data[0] |
| 1025 | # natEvent |
| 1026 | self.assertEqual(ord(record[230]), 13) |
| 1027 | # natQuotaExceededEvent |
| 1028 | self.assertEqual(struct.pack("I", 3), record[466]) |
| 1029 | # maxEntriesPerUser |
| 1030 | self.assertEqual(struct.pack("I", limit), record[473]) |
| 1031 | # sourceIPv4Address |
| 1032 | self.assertEqual(src_addr, record[8]) |
| 1033 | |
Matus Fabian | bb4e022 | 2018-09-13 02:36:25 -0700 | [diff] [blame] | 1034 | def verify_mss_value(self, pkt, mss): |
| 1035 | """ |
| 1036 | Verify TCP MSS value |
| 1037 | |
| 1038 | :param pkt: |
| 1039 | :param mss: |
| 1040 | """ |
| 1041 | if not pkt.haslayer(IP) or not pkt.haslayer(TCP): |
| 1042 | raise TypeError("Not a TCP/IP packet") |
| 1043 | |
| 1044 | for option in pkt[TCP].options: |
| 1045 | if option[0] == 'MSS': |
| 1046 | self.assertEqual(option[1], mss) |
| 1047 | self.assert_tcp_checksum_valid(pkt) |
| 1048 | |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 1049 | @staticmethod |
| 1050 | def proto2layer(proto): |
| 1051 | if proto == IP_PROTOS.tcp: |
| 1052 | return TCP |
| 1053 | elif proto == IP_PROTOS.udp: |
| 1054 | return UDP |
| 1055 | elif proto == IP_PROTOS.icmp: |
| 1056 | return ICMP |
| 1057 | else: |
| 1058 | raise Exception("Unsupported protocol") |
| 1059 | |
| 1060 | def frag_in_order(self, proto=IP_PROTOS.tcp, dont_translate=False): |
| 1061 | layer = self.proto2layer(proto) |
| 1062 | |
| 1063 | if proto == IP_PROTOS.tcp: |
| 1064 | data = "A" * 4 + "B" * 16 + "C" * 3 |
| 1065 | else: |
| 1066 | data = "A" * 16 + "B" * 16 + "C" * 3 |
| 1067 | self.port_in = random.randint(1025, 65535) |
| 1068 | |
| 1069 | reass = self.vapi.nat_reass_dump() |
| 1070 | reass_n_start = len(reass) |
| 1071 | |
| 1072 | # in2out |
| 1073 | pkts = self.create_stream_frag(self.pg0, |
| 1074 | self.pg1.remote_ip4, |
| 1075 | self.port_in, |
| 1076 | 20, |
| 1077 | data, |
| 1078 | proto) |
| 1079 | self.pg0.add_stream(pkts) |
| 1080 | self.pg_enable_capture(self.pg_interfaces) |
| 1081 | self.pg_start() |
| 1082 | frags = self.pg1.get_capture(len(pkts)) |
| 1083 | if not dont_translate: |
| 1084 | p = self.reass_frags_and_verify(frags, |
| 1085 | self.nat_addr, |
| 1086 | self.pg1.remote_ip4) |
| 1087 | else: |
| 1088 | p = self.reass_frags_and_verify(frags, |
| 1089 | self.pg0.remote_ip4, |
| 1090 | self.pg1.remote_ip4) |
| 1091 | if proto != IP_PROTOS.icmp: |
| 1092 | if not dont_translate: |
| 1093 | self.assertEqual(p[layer].dport, 20) |
| 1094 | self.assertNotEqual(p[layer].sport, self.port_in) |
| 1095 | else: |
| 1096 | self.assertEqual(p[layer].sport, self.port_in) |
| 1097 | else: |
| 1098 | if not dont_translate: |
| 1099 | self.assertNotEqual(p[layer].id, self.port_in) |
| 1100 | else: |
| 1101 | self.assertEqual(p[layer].id, self.port_in) |
| 1102 | self.assertEqual(data, p[Raw].load) |
| 1103 | |
| 1104 | # out2in |
| 1105 | if not dont_translate: |
| 1106 | dst_addr = self.nat_addr |
| 1107 | else: |
| 1108 | dst_addr = self.pg0.remote_ip4 |
| 1109 | if proto != IP_PROTOS.icmp: |
| 1110 | sport = 20 |
| 1111 | dport = p[layer].sport |
| 1112 | else: |
| 1113 | sport = p[layer].id |
| 1114 | dport = 0 |
| 1115 | pkts = self.create_stream_frag(self.pg1, |
| 1116 | dst_addr, |
| 1117 | sport, |
| 1118 | dport, |
| 1119 | data, |
| 1120 | proto, |
| 1121 | echo_reply=True) |
| 1122 | self.pg1.add_stream(pkts) |
| 1123 | self.pg_enable_capture(self.pg_interfaces) |
| 1124 | self.pg_start() |
| 1125 | frags = self.pg0.get_capture(len(pkts)) |
| 1126 | p = self.reass_frags_and_verify(frags, |
| 1127 | self.pg1.remote_ip4, |
| 1128 | self.pg0.remote_ip4) |
| 1129 | if proto != IP_PROTOS.icmp: |
| 1130 | self.assertEqual(p[layer].sport, 20) |
| 1131 | self.assertEqual(p[layer].dport, self.port_in) |
| 1132 | else: |
| 1133 | self.assertEqual(p[layer].id, self.port_in) |
| 1134 | self.assertEqual(data, p[Raw].load) |
| 1135 | |
| 1136 | reass = self.vapi.nat_reass_dump() |
| 1137 | reass_n_end = len(reass) |
| 1138 | |
| 1139 | self.assertEqual(reass_n_end - reass_n_start, 2) |
| 1140 | |
| 1141 | def frag_in_order_in_plus_out(self, proto=IP_PROTOS.tcp): |
| 1142 | layer = self.proto2layer(proto) |
| 1143 | |
| 1144 | if proto == IP_PROTOS.tcp: |
| 1145 | data = "A" * 4 + "B" * 16 + "C" * 3 |
| 1146 | else: |
| 1147 | data = "A" * 16 + "B" * 16 + "C" * 3 |
| 1148 | self.port_in = random.randint(1025, 65535) |
| 1149 | |
| 1150 | for i in range(2): |
| 1151 | reass = self.vapi.nat_reass_dump() |
| 1152 | reass_n_start = len(reass) |
| 1153 | |
| 1154 | # out2in |
| 1155 | pkts = self.create_stream_frag(self.pg0, |
| 1156 | self.server_out_addr, |
| 1157 | self.port_in, |
| 1158 | self.server_out_port, |
| 1159 | data, |
| 1160 | proto) |
| 1161 | self.pg0.add_stream(pkts) |
| 1162 | self.pg_enable_capture(self.pg_interfaces) |
| 1163 | self.pg_start() |
| 1164 | frags = self.pg1.get_capture(len(pkts)) |
| 1165 | p = self.reass_frags_and_verify(frags, |
| 1166 | self.pg0.remote_ip4, |
| 1167 | self.server_in_addr) |
| 1168 | if proto != IP_PROTOS.icmp: |
| 1169 | self.assertEqual(p[layer].sport, self.port_in) |
| 1170 | self.assertEqual(p[layer].dport, self.server_in_port) |
| 1171 | else: |
| 1172 | self.assertEqual(p[layer].id, self.port_in) |
| 1173 | self.assertEqual(data, p[Raw].load) |
| 1174 | |
| 1175 | # in2out |
| 1176 | if proto != IP_PROTOS.icmp: |
| 1177 | pkts = self.create_stream_frag(self.pg1, |
| 1178 | self.pg0.remote_ip4, |
| 1179 | self.server_in_port, |
| 1180 | p[layer].sport, |
| 1181 | data, |
| 1182 | proto) |
| 1183 | else: |
| 1184 | pkts = self.create_stream_frag(self.pg1, |
| 1185 | self.pg0.remote_ip4, |
| 1186 | p[layer].id, |
| 1187 | 0, |
| 1188 | data, |
| 1189 | proto, |
| 1190 | echo_reply=True) |
| 1191 | self.pg1.add_stream(pkts) |
| 1192 | self.pg_enable_capture(self.pg_interfaces) |
| 1193 | self.pg_start() |
| 1194 | frags = self.pg0.get_capture(len(pkts)) |
| 1195 | p = self.reass_frags_and_verify(frags, |
| 1196 | self.server_out_addr, |
| 1197 | self.pg0.remote_ip4) |
| 1198 | if proto != IP_PROTOS.icmp: |
| 1199 | self.assertEqual(p[layer].sport, self.server_out_port) |
| 1200 | self.assertEqual(p[layer].dport, self.port_in) |
| 1201 | else: |
| 1202 | self.assertEqual(p[layer].id, self.port_in) |
| 1203 | self.assertEqual(data, p[Raw].load) |
| 1204 | |
| 1205 | reass = self.vapi.nat_reass_dump() |
| 1206 | reass_n_end = len(reass) |
| 1207 | |
| 1208 | self.assertEqual(reass_n_end - reass_n_start, 2) |
| 1209 | |
| 1210 | def reass_hairpinning(self, proto=IP_PROTOS.tcp): |
| 1211 | layer = self.proto2layer(proto) |
| 1212 | |
| 1213 | if proto == IP_PROTOS.tcp: |
| 1214 | data = "A" * 4 + "B" * 16 + "C" * 3 |
| 1215 | else: |
| 1216 | data = "A" * 16 + "B" * 16 + "C" * 3 |
| 1217 | |
| 1218 | # send packet from host to server |
| 1219 | pkts = self.create_stream_frag(self.pg0, |
| 1220 | self.nat_addr, |
| 1221 | self.host_in_port, |
| 1222 | self.server_out_port, |
| 1223 | data, |
| 1224 | proto) |
| 1225 | self.pg0.add_stream(pkts) |
| 1226 | self.pg_enable_capture(self.pg_interfaces) |
| 1227 | self.pg_start() |
| 1228 | frags = self.pg0.get_capture(len(pkts)) |
| 1229 | p = self.reass_frags_and_verify(frags, |
| 1230 | self.nat_addr, |
| 1231 | self.server.ip4) |
| 1232 | if proto != IP_PROTOS.icmp: |
| 1233 | self.assertNotEqual(p[layer].sport, self.host_in_port) |
| 1234 | self.assertEqual(p[layer].dport, self.server_in_port) |
| 1235 | else: |
| 1236 | self.assertNotEqual(p[layer].id, self.host_in_port) |
| 1237 | self.assertEqual(data, p[Raw].load) |
| 1238 | |
| 1239 | def frag_out_of_order(self, proto=IP_PROTOS.tcp, dont_translate=False): |
| 1240 | layer = self.proto2layer(proto) |
| 1241 | |
| 1242 | if proto == IP_PROTOS.tcp: |
| 1243 | data = "A" * 4 + "B" * 16 + "C" * 3 |
| 1244 | else: |
| 1245 | data = "A" * 16 + "B" * 16 + "C" * 3 |
| 1246 | self.port_in = random.randint(1025, 65535) |
| 1247 | |
| 1248 | for i in range(2): |
| 1249 | # in2out |
| 1250 | pkts = self.create_stream_frag(self.pg0, |
| 1251 | self.pg1.remote_ip4, |
| 1252 | self.port_in, |
| 1253 | 20, |
| 1254 | data, |
| 1255 | proto) |
| 1256 | pkts.reverse() |
| 1257 | self.pg0.add_stream(pkts) |
| 1258 | self.pg_enable_capture(self.pg_interfaces) |
| 1259 | self.pg_start() |
| 1260 | frags = self.pg1.get_capture(len(pkts)) |
| 1261 | if not dont_translate: |
| 1262 | p = self.reass_frags_and_verify(frags, |
| 1263 | self.nat_addr, |
| 1264 | self.pg1.remote_ip4) |
| 1265 | else: |
| 1266 | p = self.reass_frags_and_verify(frags, |
| 1267 | self.pg0.remote_ip4, |
| 1268 | self.pg1.remote_ip4) |
| 1269 | if proto != IP_PROTOS.icmp: |
| 1270 | if not dont_translate: |
| 1271 | self.assertEqual(p[layer].dport, 20) |
| 1272 | self.assertNotEqual(p[layer].sport, self.port_in) |
| 1273 | else: |
| 1274 | self.assertEqual(p[layer].sport, self.port_in) |
| 1275 | else: |
| 1276 | if not dont_translate: |
| 1277 | self.assertNotEqual(p[layer].id, self.port_in) |
| 1278 | else: |
| 1279 | self.assertEqual(p[layer].id, self.port_in) |
| 1280 | self.assertEqual(data, p[Raw].load) |
| 1281 | |
| 1282 | # out2in |
| 1283 | if not dont_translate: |
| 1284 | dst_addr = self.nat_addr |
| 1285 | else: |
| 1286 | dst_addr = self.pg0.remote_ip4 |
| 1287 | if proto != IP_PROTOS.icmp: |
| 1288 | sport = 20 |
| 1289 | dport = p[layer].sport |
| 1290 | else: |
| 1291 | sport = p[layer].id |
| 1292 | dport = 0 |
| 1293 | pkts = self.create_stream_frag(self.pg1, |
| 1294 | dst_addr, |
| 1295 | sport, |
| 1296 | dport, |
| 1297 | data, |
| 1298 | proto, |
| 1299 | echo_reply=True) |
| 1300 | pkts.reverse() |
| 1301 | self.pg1.add_stream(pkts) |
| 1302 | self.pg_enable_capture(self.pg_interfaces) |
| 1303 | self.pg_start() |
| 1304 | frags = self.pg0.get_capture(len(pkts)) |
| 1305 | p = self.reass_frags_and_verify(frags, |
| 1306 | self.pg1.remote_ip4, |
| 1307 | self.pg0.remote_ip4) |
| 1308 | if proto != IP_PROTOS.icmp: |
| 1309 | self.assertEqual(p[layer].sport, 20) |
| 1310 | self.assertEqual(p[layer].dport, self.port_in) |
| 1311 | else: |
| 1312 | self.assertEqual(p[layer].id, self.port_in) |
| 1313 | self.assertEqual(data, p[Raw].load) |
| 1314 | |
| 1315 | def frag_out_of_order_in_plus_out(self, proto=IP_PROTOS.tcp): |
| 1316 | layer = self.proto2layer(proto) |
| 1317 | |
| 1318 | if proto == IP_PROTOS.tcp: |
| 1319 | data = "A" * 4 + "B" * 16 + "C" * 3 |
| 1320 | else: |
| 1321 | data = "A" * 16 + "B" * 16 + "C" * 3 |
| 1322 | self.port_in = random.randint(1025, 65535) |
| 1323 | |
| 1324 | for i in range(2): |
| 1325 | # out2in |
| 1326 | pkts = self.create_stream_frag(self.pg0, |
| 1327 | self.server_out_addr, |
| 1328 | self.port_in, |
| 1329 | self.server_out_port, |
| 1330 | data, |
| 1331 | proto) |
| 1332 | pkts.reverse() |
| 1333 | self.pg0.add_stream(pkts) |
| 1334 | self.pg_enable_capture(self.pg_interfaces) |
| 1335 | self.pg_start() |
| 1336 | frags = self.pg1.get_capture(len(pkts)) |
| 1337 | p = self.reass_frags_and_verify(frags, |
| 1338 | self.pg0.remote_ip4, |
| 1339 | self.server_in_addr) |
| 1340 | if proto != IP_PROTOS.icmp: |
| 1341 | self.assertEqual(p[layer].dport, self.server_in_port) |
| 1342 | self.assertEqual(p[layer].sport, self.port_in) |
| 1343 | self.assertEqual(p[layer].dport, self.server_in_port) |
| 1344 | else: |
| 1345 | self.assertEqual(p[layer].id, self.port_in) |
| 1346 | self.assertEqual(data, p[Raw].load) |
| 1347 | |
| 1348 | # in2out |
| 1349 | if proto != IP_PROTOS.icmp: |
| 1350 | pkts = self.create_stream_frag(self.pg1, |
| 1351 | self.pg0.remote_ip4, |
| 1352 | self.server_in_port, |
| 1353 | p[layer].sport, |
| 1354 | data, |
| 1355 | proto) |
| 1356 | else: |
| 1357 | pkts = self.create_stream_frag(self.pg1, |
| 1358 | self.pg0.remote_ip4, |
| 1359 | p[layer].id, |
| 1360 | 0, |
| 1361 | data, |
| 1362 | proto, |
| 1363 | echo_reply=True) |
| 1364 | pkts.reverse() |
| 1365 | self.pg1.add_stream(pkts) |
| 1366 | self.pg_enable_capture(self.pg_interfaces) |
| 1367 | self.pg_start() |
| 1368 | frags = self.pg0.get_capture(len(pkts)) |
| 1369 | p = self.reass_frags_and_verify(frags, |
| 1370 | self.server_out_addr, |
| 1371 | self.pg0.remote_ip4) |
| 1372 | if proto != IP_PROTOS.icmp: |
| 1373 | self.assertEqual(p[layer].sport, self.server_out_port) |
| 1374 | self.assertEqual(p[layer].dport, self.port_in) |
| 1375 | else: |
| 1376 | self.assertEqual(p[layer].id, self.port_in) |
| 1377 | self.assertEqual(data, p[Raw].load) |
| 1378 | |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1379 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1380 | class TestNAT44(MethodHolder): |
| 1381 | """ NAT44 Test Cases """ |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1382 | |
| 1383 | @classmethod |
| 1384 | def setUpClass(cls): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1385 | super(TestNAT44, cls).setUpClass() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 1386 | cls.vapi.cli("set log class nat level debug") |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1387 | |
| 1388 | try: |
| 1389 | cls.tcp_port_in = 6303 |
| 1390 | cls.tcp_port_out = 6303 |
| 1391 | cls.udp_port_in = 6304 |
| 1392 | cls.udp_port_out = 6304 |
| 1393 | cls.icmp_id_in = 6305 |
| 1394 | cls.icmp_id_out = 6305 |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1395 | cls.nat_addr = '10.0.0.3' |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 1396 | cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr) |
Matus Fabian | 6631e9c | 2017-05-24 01:52:20 -0700 | [diff] [blame] | 1397 | cls.ipfix_src_port = 4739 |
| 1398 | cls.ipfix_domain_id = 1 |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 1399 | cls.tcp_external_port = 80 |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1400 | |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1401 | cls.create_pg_interfaces(range(10)) |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1402 | cls.interfaces = list(cls.pg_interfaces[0:4]) |
| 1403 | |
| 1404 | for i in cls.interfaces: |
| 1405 | i.admin_up() |
| 1406 | i.config_ip4() |
| 1407 | i.resolve_arp() |
| 1408 | |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 1409 | cls.pg0.generate_remote_hosts(3) |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1410 | cls.pg0.configure_ipv4_neighbors() |
| 1411 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 1412 | cls.pg1.generate_remote_hosts(1) |
| 1413 | cls.pg1.configure_ipv4_neighbors() |
| 1414 | |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1415 | cls.overlapping_interfaces = list(list(cls.pg_interfaces[4:7])) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 1416 | cls.vapi.ip_table_add_del(10, is_add=1) |
| 1417 | cls.vapi.ip_table_add_del(20, is_add=1) |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1418 | |
| 1419 | cls.pg4._local_ip4 = "172.16.255.1" |
| 1420 | cls.pg4._local_ip4n = socket.inet_pton(socket.AF_INET, i.local_ip4) |
| 1421 | cls.pg4._remote_hosts[0]._ip4 = "172.16.255.2" |
| 1422 | cls.pg4.set_table_ip4(10) |
Matus Fabian | 6912628 | 2017-08-14 23:39:58 -0700 | [diff] [blame] | 1423 | cls.pg5._local_ip4 = "172.17.255.3" |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1424 | cls.pg5._local_ip4n = socket.inet_pton(socket.AF_INET, i.local_ip4) |
Matus Fabian | 6912628 | 2017-08-14 23:39:58 -0700 | [diff] [blame] | 1425 | cls.pg5._remote_hosts[0]._ip4 = "172.17.255.4" |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1426 | cls.pg5.set_table_ip4(10) |
| 1427 | cls.pg6._local_ip4 = "172.16.255.1" |
| 1428 | cls.pg6._local_ip4n = socket.inet_pton(socket.AF_INET, i.local_ip4) |
| 1429 | cls.pg6._remote_hosts[0]._ip4 = "172.16.255.2" |
| 1430 | cls.pg6.set_table_ip4(20) |
| 1431 | for i in cls.overlapping_interfaces: |
| 1432 | i.config_ip4() |
| 1433 | i.admin_up() |
| 1434 | i.resolve_arp() |
| 1435 | |
| 1436 | cls.pg7.admin_up() |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 1437 | cls.pg8.admin_up() |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1438 | |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1439 | cls.pg9.generate_remote_hosts(2) |
| 1440 | cls.pg9.config_ip4() |
| 1441 | ip_addr_n = socket.inet_pton(socket.AF_INET, "10.0.0.1") |
| 1442 | cls.vapi.sw_interface_add_del_address(cls.pg9.sw_if_index, |
| 1443 | ip_addr_n, |
| 1444 | 24) |
| 1445 | cls.pg9.admin_up() |
| 1446 | cls.pg9.resolve_arp() |
| 1447 | cls.pg9._remote_hosts[1]._ip4 = cls.pg9._remote_hosts[0]._ip4 |
| 1448 | cls.pg4._remote_ip4 = cls.pg9._remote_hosts[0]._ip4 = "10.0.0.2" |
| 1449 | cls.pg9.resolve_arp() |
| 1450 | |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1451 | except Exception: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1452 | super(TestNAT44, cls).tearDownClass() |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 1453 | raise |
| 1454 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1455 | def test_dynamic(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1456 | """ NAT44 dynamic translation test """ |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1457 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1458 | self.nat44_add_address(self.nat_addr) |
| 1459 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1460 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1461 | is_inside=0) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1462 | |
| 1463 | # in2out |
| 1464 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1465 | self.pg0.add_stream(pkts) |
| 1466 | self.pg_enable_capture(self.pg_interfaces) |
| 1467 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1468 | capture = self.pg1.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1469 | self.verify_capture_out(capture) |
| 1470 | |
| 1471 | # out2in |
| 1472 | pkts = self.create_stream_out(self.pg1) |
| 1473 | self.pg1.add_stream(pkts) |
| 1474 | self.pg_enable_capture(self.pg_interfaces) |
| 1475 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1476 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1477 | self.verify_capture_in(capture, self.pg0) |
| 1478 | |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 1479 | def test_dynamic_icmp_errors_in2out_ttl_1(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1480 | """ NAT44 handling of client packets with TTL=1 """ |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 1481 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1482 | self.nat44_add_address(self.nat_addr) |
| 1483 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1484 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1485 | is_inside=0) |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 1486 | |
| 1487 | # Client side - generate traffic |
| 1488 | pkts = self.create_stream_in(self.pg0, self.pg1, ttl=1) |
| 1489 | self.pg0.add_stream(pkts) |
| 1490 | self.pg_enable_capture(self.pg_interfaces) |
| 1491 | self.pg_start() |
| 1492 | |
| 1493 | # Client side - verify ICMP type 11 packets |
| 1494 | capture = self.pg0.get_capture(len(pkts)) |
| 1495 | self.verify_capture_in_with_icmp_errors(capture, self.pg0) |
| 1496 | |
| 1497 | def test_dynamic_icmp_errors_out2in_ttl_1(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1498 | """ NAT44 handling of server packets with TTL=1 """ |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 1499 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1500 | self.nat44_add_address(self.nat_addr) |
| 1501 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1502 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1503 | is_inside=0) |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 1504 | |
| 1505 | # Client side - create sessions |
| 1506 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1507 | self.pg0.add_stream(pkts) |
| 1508 | self.pg_enable_capture(self.pg_interfaces) |
| 1509 | self.pg_start() |
| 1510 | |
| 1511 | # Server side - generate traffic |
| 1512 | capture = self.pg1.get_capture(len(pkts)) |
| 1513 | self.verify_capture_out(capture) |
| 1514 | pkts = self.create_stream_out(self.pg1, ttl=1) |
| 1515 | self.pg1.add_stream(pkts) |
| 1516 | self.pg_enable_capture(self.pg_interfaces) |
| 1517 | self.pg_start() |
| 1518 | |
| 1519 | # Server side - verify ICMP type 11 packets |
| 1520 | capture = self.pg1.get_capture(len(pkts)) |
| 1521 | self.verify_capture_out_with_icmp_errors(capture, |
| 1522 | src_ip=self.pg1.local_ip4) |
| 1523 | |
| 1524 | def test_dynamic_icmp_errors_in2out_ttl_2(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1525 | """ NAT44 handling of error responses to client packets with TTL=2 """ |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 1526 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1527 | self.nat44_add_address(self.nat_addr) |
| 1528 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1529 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1530 | is_inside=0) |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 1531 | |
| 1532 | # Client side - generate traffic |
| 1533 | pkts = self.create_stream_in(self.pg0, self.pg1, ttl=2) |
| 1534 | self.pg0.add_stream(pkts) |
| 1535 | self.pg_enable_capture(self.pg_interfaces) |
| 1536 | self.pg_start() |
| 1537 | |
| 1538 | # Server side - simulate ICMP type 11 response |
| 1539 | capture = self.pg1.get_capture(len(pkts)) |
| 1540 | pkts = [Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1541 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 1542 | ICMP(type=11) / packet[IP] for packet in capture] |
| 1543 | self.pg1.add_stream(pkts) |
| 1544 | self.pg_enable_capture(self.pg_interfaces) |
| 1545 | self.pg_start() |
| 1546 | |
| 1547 | # Client side - verify ICMP type 11 packets |
| 1548 | capture = self.pg0.get_capture(len(pkts)) |
| 1549 | self.verify_capture_in_with_icmp_errors(capture, self.pg0) |
| 1550 | |
| 1551 | def test_dynamic_icmp_errors_out2in_ttl_2(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1552 | """ NAT44 handling of error responses to server packets with TTL=2 """ |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 1553 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1554 | self.nat44_add_address(self.nat_addr) |
| 1555 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1556 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1557 | is_inside=0) |
Juraj Sloboda | b33f413 | 2017-02-08 23:54:21 -0800 | [diff] [blame] | 1558 | |
| 1559 | # Client side - create sessions |
| 1560 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1561 | self.pg0.add_stream(pkts) |
| 1562 | self.pg_enable_capture(self.pg_interfaces) |
| 1563 | self.pg_start() |
| 1564 | |
| 1565 | # Server side - generate traffic |
| 1566 | capture = self.pg1.get_capture(len(pkts)) |
| 1567 | self.verify_capture_out(capture) |
| 1568 | pkts = self.create_stream_out(self.pg1, ttl=2) |
| 1569 | self.pg1.add_stream(pkts) |
| 1570 | self.pg_enable_capture(self.pg_interfaces) |
| 1571 | self.pg_start() |
| 1572 | |
| 1573 | # Client side - simulate ICMP type 11 response |
| 1574 | capture = self.pg0.get_capture(len(pkts)) |
| 1575 | pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 1576 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 1577 | ICMP(type=11) / packet[IP] for packet in capture] |
| 1578 | self.pg0.add_stream(pkts) |
| 1579 | self.pg_enable_capture(self.pg_interfaces) |
| 1580 | self.pg_start() |
| 1581 | |
| 1582 | # Server side - verify ICMP type 11 packets |
| 1583 | capture = self.pg1.get_capture(len(pkts)) |
| 1584 | self.verify_capture_out_with_icmp_errors(capture) |
| 1585 | |
Juraj Sloboda | 665e482 | 2017-02-16 17:17:19 -0800 | [diff] [blame] | 1586 | def test_ping_out_interface_from_outside(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1587 | """ Ping NAT44 out interface from outside network """ |
Juraj Sloboda | 665e482 | 2017-02-16 17:17:19 -0800 | [diff] [blame] | 1588 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1589 | self.nat44_add_address(self.nat_addr) |
| 1590 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1591 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1592 | is_inside=0) |
Juraj Sloboda | 665e482 | 2017-02-16 17:17:19 -0800 | [diff] [blame] | 1593 | |
| 1594 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 1595 | IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) / |
| 1596 | ICMP(id=self.icmp_id_out, type='echo-request')) |
| 1597 | pkts = [p] |
| 1598 | self.pg1.add_stream(pkts) |
| 1599 | self.pg_enable_capture(self.pg_interfaces) |
| 1600 | self.pg_start() |
| 1601 | capture = self.pg1.get_capture(len(pkts)) |
Juraj Sloboda | 665e482 | 2017-02-16 17:17:19 -0800 | [diff] [blame] | 1602 | packet = capture[0] |
| 1603 | try: |
| 1604 | self.assertEqual(packet[IP].src, self.pg1.local_ip4) |
| 1605 | self.assertEqual(packet[IP].dst, self.pg1.remote_ip4) |
| 1606 | self.assertEqual(packet[ICMP].id, self.icmp_id_in) |
| 1607 | self.assertEqual(packet[ICMP].type, 0) # echo reply |
| 1608 | except: |
| 1609 | self.logger.error(ppp("Unexpected or invalid packet " |
| 1610 | "(outside network):", packet)) |
| 1611 | raise |
| 1612 | |
Juraj Sloboda | d367768 | 2017-04-14 03:24:45 +0200 | [diff] [blame] | 1613 | def test_ping_internal_host_from_outside(self): |
| 1614 | """ Ping internal host from outside network """ |
| 1615 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1616 | self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr) |
| 1617 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1618 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1619 | is_inside=0) |
Juraj Sloboda | d367768 | 2017-04-14 03:24:45 +0200 | [diff] [blame] | 1620 | |
| 1621 | # out2in |
| 1622 | pkt = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1623 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr, ttl=64) / |
Juraj Sloboda | d367768 | 2017-04-14 03:24:45 +0200 | [diff] [blame] | 1624 | ICMP(id=self.icmp_id_out, type='echo-request')) |
| 1625 | self.pg1.add_stream(pkt) |
| 1626 | self.pg_enable_capture(self.pg_interfaces) |
| 1627 | self.pg_start() |
| 1628 | capture = self.pg0.get_capture(1) |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 1629 | self.verify_capture_in(capture, self.pg0) |
Juraj Sloboda | d367768 | 2017-04-14 03:24:45 +0200 | [diff] [blame] | 1630 | self.assert_equal(capture[0][IP].proto, IP_PROTOS.icmp) |
| 1631 | |
| 1632 | # in2out |
| 1633 | pkt = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 1634 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, ttl=64) / |
| 1635 | ICMP(id=self.icmp_id_in, type='echo-reply')) |
| 1636 | self.pg0.add_stream(pkt) |
| 1637 | self.pg_enable_capture(self.pg_interfaces) |
| 1638 | self.pg_start() |
| 1639 | capture = self.pg1.get_capture(1) |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 1640 | self.verify_capture_out(capture, same_port=True) |
Juraj Sloboda | d367768 | 2017-04-14 03:24:45 +0200 | [diff] [blame] | 1641 | self.assert_equal(capture[0][IP].proto, IP_PROTOS.icmp) |
| 1642 | |
Matus Fabian | c79396e | 2018-07-23 00:05:22 -0700 | [diff] [blame] | 1643 | def test_forwarding(self): |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 1644 | """ NAT44 forwarding test """ |
| 1645 | |
| 1646 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1647 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1648 | is_inside=0) |
| 1649 | self.vapi.nat44_forwarding_enable_disable(1) |
| 1650 | |
| 1651 | real_ip = self.pg0.remote_ip4n |
| 1652 | alias_ip = self.nat_addr_n |
| 1653 | self.vapi.nat44_add_del_static_mapping(local_ip=real_ip, |
| 1654 | external_ip=alias_ip) |
| 1655 | |
| 1656 | try: |
Matus Fabian | c79396e | 2018-07-23 00:05:22 -0700 | [diff] [blame] | 1657 | # static mapping match |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 1658 | |
| 1659 | pkts = self.create_stream_out(self.pg1) |
| 1660 | self.pg1.add_stream(pkts) |
| 1661 | self.pg_enable_capture(self.pg_interfaces) |
| 1662 | self.pg_start() |
| 1663 | capture = self.pg0.get_capture(len(pkts)) |
| 1664 | self.verify_capture_in(capture, self.pg0) |
| 1665 | |
| 1666 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1667 | self.pg0.add_stream(pkts) |
| 1668 | self.pg_enable_capture(self.pg_interfaces) |
| 1669 | self.pg_start() |
| 1670 | capture = self.pg1.get_capture(len(pkts)) |
| 1671 | self.verify_capture_out(capture, same_port=True) |
| 1672 | |
Matus Fabian | c79396e | 2018-07-23 00:05:22 -0700 | [diff] [blame] | 1673 | # no static mapping match |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 1674 | |
| 1675 | host0 = self.pg0.remote_hosts[0] |
| 1676 | self.pg0.remote_hosts[0] = self.pg0.remote_hosts[1] |
| 1677 | try: |
| 1678 | pkts = self.create_stream_out(self.pg1, |
| 1679 | dst_ip=self.pg0.remote_ip4, |
| 1680 | use_inside_ports=True) |
| 1681 | self.pg1.add_stream(pkts) |
| 1682 | self.pg_enable_capture(self.pg_interfaces) |
| 1683 | self.pg_start() |
| 1684 | capture = self.pg0.get_capture(len(pkts)) |
| 1685 | self.verify_capture_in(capture, self.pg0) |
| 1686 | |
| 1687 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1688 | self.pg0.add_stream(pkts) |
| 1689 | self.pg_enable_capture(self.pg_interfaces) |
| 1690 | self.pg_start() |
| 1691 | capture = self.pg1.get_capture(len(pkts)) |
| 1692 | self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4, |
| 1693 | same_port=True) |
| 1694 | finally: |
| 1695 | self.pg0.remote_hosts[0] = host0 |
| 1696 | |
| 1697 | finally: |
| 1698 | self.vapi.nat44_forwarding_enable_disable(0) |
| 1699 | self.vapi.nat44_add_del_static_mapping(local_ip=real_ip, |
| 1700 | external_ip=alias_ip, |
| 1701 | is_add=0) |
| 1702 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1703 | def test_static_in(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1704 | """ 1:1 NAT initialized from inside network """ |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1705 | |
| 1706 | nat_ip = "10.0.0.10" |
| 1707 | self.tcp_port_out = 6303 |
| 1708 | self.udp_port_out = 6304 |
| 1709 | self.icmp_id_out = 6305 |
| 1710 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1711 | self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip) |
| 1712 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1713 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1714 | is_inside=0) |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 1715 | sm = self.vapi.nat44_static_mapping_dump() |
| 1716 | self.assertEqual(len(sm), 1) |
| 1717 | self.assertEqual((sm[0].tag).split('\0', 1)[0], '') |
Matus Fabian | 9dba781 | 2018-01-31 01:13:23 -0800 | [diff] [blame] | 1718 | self.assertEqual(sm[0].protocol, 0) |
| 1719 | self.assertEqual(sm[0].local_port, 0) |
| 1720 | self.assertEqual(sm[0].external_port, 0) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1721 | |
| 1722 | # in2out |
| 1723 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1724 | self.pg0.add_stream(pkts) |
| 1725 | self.pg_enable_capture(self.pg_interfaces) |
| 1726 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1727 | capture = self.pg1.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1728 | self.verify_capture_out(capture, nat_ip, True) |
| 1729 | |
| 1730 | # out2in |
| 1731 | pkts = self.create_stream_out(self.pg1, nat_ip) |
| 1732 | self.pg1.add_stream(pkts) |
| 1733 | self.pg_enable_capture(self.pg_interfaces) |
| 1734 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1735 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1736 | self.verify_capture_in(capture, self.pg0) |
| 1737 | |
| 1738 | def test_static_out(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1739 | """ 1:1 NAT initialized from outside network """ |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1740 | |
| 1741 | nat_ip = "10.0.0.20" |
| 1742 | self.tcp_port_out = 6303 |
| 1743 | self.udp_port_out = 6304 |
| 1744 | self.icmp_id_out = 6305 |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 1745 | tag = "testTAG" |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1746 | |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 1747 | self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip, tag=tag) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1748 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1749 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1750 | is_inside=0) |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 1751 | sm = self.vapi.nat44_static_mapping_dump() |
| 1752 | self.assertEqual(len(sm), 1) |
| 1753 | self.assertEqual((sm[0].tag).split('\0', 1)[0], tag) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1754 | |
| 1755 | # out2in |
| 1756 | pkts = self.create_stream_out(self.pg1, nat_ip) |
| 1757 | self.pg1.add_stream(pkts) |
| 1758 | self.pg_enable_capture(self.pg_interfaces) |
| 1759 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1760 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1761 | self.verify_capture_in(capture, self.pg0) |
| 1762 | |
| 1763 | # in2out |
| 1764 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1765 | self.pg0.add_stream(pkts) |
| 1766 | self.pg_enable_capture(self.pg_interfaces) |
| 1767 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1768 | capture = self.pg1.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1769 | self.verify_capture_out(capture, nat_ip, True) |
| 1770 | |
| 1771 | def test_static_with_port_in(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1772 | """ 1:1 NAPT initialized from inside network """ |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1773 | |
| 1774 | self.tcp_port_out = 3606 |
| 1775 | self.udp_port_out = 3607 |
| 1776 | self.icmp_id_out = 3608 |
| 1777 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1778 | self.nat44_add_address(self.nat_addr) |
| 1779 | self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr, |
| 1780 | self.tcp_port_in, self.tcp_port_out, |
| 1781 | proto=IP_PROTOS.tcp) |
| 1782 | self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr, |
| 1783 | self.udp_port_in, self.udp_port_out, |
| 1784 | proto=IP_PROTOS.udp) |
| 1785 | self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr, |
| 1786 | self.icmp_id_in, self.icmp_id_out, |
| 1787 | proto=IP_PROTOS.icmp) |
| 1788 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1789 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1790 | is_inside=0) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1791 | |
| 1792 | # in2out |
| 1793 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1794 | self.pg0.add_stream(pkts) |
| 1795 | self.pg_enable_capture(self.pg_interfaces) |
| 1796 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1797 | capture = self.pg1.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1798 | self.verify_capture_out(capture) |
| 1799 | |
| 1800 | # out2in |
| 1801 | pkts = self.create_stream_out(self.pg1) |
| 1802 | self.pg1.add_stream(pkts) |
| 1803 | self.pg_enable_capture(self.pg_interfaces) |
| 1804 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1805 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1806 | self.verify_capture_in(capture, self.pg0) |
| 1807 | |
| 1808 | def test_static_with_port_out(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1809 | """ 1:1 NAPT initialized from outside network """ |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1810 | |
| 1811 | self.tcp_port_out = 30606 |
| 1812 | self.udp_port_out = 30607 |
| 1813 | self.icmp_id_out = 30608 |
| 1814 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1815 | self.nat44_add_address(self.nat_addr) |
| 1816 | self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr, |
| 1817 | self.tcp_port_in, self.tcp_port_out, |
| 1818 | proto=IP_PROTOS.tcp) |
| 1819 | self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr, |
| 1820 | self.udp_port_in, self.udp_port_out, |
| 1821 | proto=IP_PROTOS.udp) |
| 1822 | self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr, |
| 1823 | self.icmp_id_in, self.icmp_id_out, |
| 1824 | proto=IP_PROTOS.icmp) |
| 1825 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1826 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1827 | is_inside=0) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1828 | |
| 1829 | # out2in |
| 1830 | pkts = self.create_stream_out(self.pg1) |
| 1831 | self.pg1.add_stream(pkts) |
| 1832 | self.pg_enable_capture(self.pg_interfaces) |
| 1833 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1834 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1835 | self.verify_capture_in(capture, self.pg0) |
| 1836 | |
| 1837 | # in2out |
| 1838 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1839 | self.pg0.add_stream(pkts) |
| 1840 | self.pg_enable_capture(self.pg_interfaces) |
| 1841 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1842 | capture = self.pg1.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1843 | self.verify_capture_out(capture) |
| 1844 | |
| 1845 | def test_static_vrf_aware(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1846 | """ 1:1 NAT VRF awareness """ |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1847 | |
| 1848 | nat_ip1 = "10.0.0.30" |
| 1849 | nat_ip2 = "10.0.0.40" |
| 1850 | self.tcp_port_out = 6303 |
| 1851 | self.udp_port_out = 6304 |
| 1852 | self.icmp_id_out = 6305 |
| 1853 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1854 | self.nat44_add_static_mapping(self.pg4.remote_ip4, nat_ip1, |
| 1855 | vrf_id=10) |
| 1856 | self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip2, |
| 1857 | vrf_id=10) |
| 1858 | self.vapi.nat44_interface_add_del_feature(self.pg3.sw_if_index, |
| 1859 | is_inside=0) |
| 1860 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1861 | self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1862 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1863 | # inside interface VRF match NAT44 static mapping VRF |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1864 | pkts = self.create_stream_in(self.pg4, self.pg3) |
| 1865 | self.pg4.add_stream(pkts) |
| 1866 | self.pg_enable_capture(self.pg_interfaces) |
| 1867 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1868 | capture = self.pg3.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1869 | self.verify_capture_out(capture, nat_ip1, True) |
| 1870 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1871 | # inside interface VRF don't match NAT44 static mapping VRF (packets |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1872 | # are dropped) |
| 1873 | pkts = self.create_stream_in(self.pg0, self.pg3) |
| 1874 | self.pg0.add_stream(pkts) |
| 1875 | self.pg_enable_capture(self.pg_interfaces) |
| 1876 | self.pg_start() |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 1877 | self.pg3.assert_nothing_captured() |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1878 | |
Matus Fabian | b793d09 | 2018-01-31 05:50:21 -0800 | [diff] [blame] | 1879 | def test_dynamic_to_static(self): |
| 1880 | """ Switch from dynamic translation to 1:1NAT """ |
| 1881 | nat_ip = "10.0.0.10" |
| 1882 | self.tcp_port_out = 6303 |
| 1883 | self.udp_port_out = 6304 |
| 1884 | self.icmp_id_out = 6305 |
| 1885 | |
| 1886 | self.nat44_add_address(self.nat_addr) |
| 1887 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1888 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1889 | is_inside=0) |
| 1890 | |
| 1891 | # dynamic |
| 1892 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1893 | self.pg0.add_stream(pkts) |
| 1894 | self.pg_enable_capture(self.pg_interfaces) |
| 1895 | self.pg_start() |
| 1896 | capture = self.pg1.get_capture(len(pkts)) |
| 1897 | self.verify_capture_out(capture) |
| 1898 | |
| 1899 | # 1:1NAT |
| 1900 | self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip) |
| 1901 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 1902 | self.assertEqual(len(sessions), 0) |
| 1903 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1904 | self.pg0.add_stream(pkts) |
| 1905 | self.pg_enable_capture(self.pg_interfaces) |
| 1906 | self.pg_start() |
| 1907 | capture = self.pg1.get_capture(len(pkts)) |
| 1908 | self.verify_capture_out(capture, nat_ip, True) |
| 1909 | |
Matus Fabian | ab7a805 | 2017-11-28 04:29:41 -0800 | [diff] [blame] | 1910 | def test_identity_nat(self): |
| 1911 | """ Identity NAT """ |
| 1912 | |
| 1913 | self.vapi.nat44_add_del_identity_mapping(ip=self.pg0.remote_ip4n) |
| 1914 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1915 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 1916 | is_inside=0) |
| 1917 | |
| 1918 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 1919 | IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) / |
| 1920 | TCP(sport=12345, dport=56789)) |
| 1921 | self.pg1.add_stream(p) |
| 1922 | self.pg_enable_capture(self.pg_interfaces) |
| 1923 | self.pg_start() |
| 1924 | capture = self.pg0.get_capture(1) |
| 1925 | p = capture[0] |
| 1926 | try: |
| 1927 | ip = p[IP] |
| 1928 | tcp = p[TCP] |
| 1929 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 1930 | self.assertEqual(ip.src, self.pg1.remote_ip4) |
| 1931 | self.assertEqual(tcp.dport, 56789) |
| 1932 | self.assertEqual(tcp.sport, 12345) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 1933 | self.assert_packet_checksums_valid(p) |
Matus Fabian | ab7a805 | 2017-11-28 04:29:41 -0800 | [diff] [blame] | 1934 | except: |
| 1935 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 1936 | raise |
| 1937 | |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 1938 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 1939 | self.assertEqual(len(sessions), 0) |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 1940 | self.vapi.nat44_add_del_identity_mapping(ip=self.pg0.remote_ip4n, |
| 1941 | vrf_id=1) |
| 1942 | identity_mappings = self.vapi.nat44_identity_mapping_dump() |
| 1943 | self.assertEqual(len(identity_mappings), 2) |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 1944 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1945 | def test_multiple_inside_interfaces(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1946 | """ NAT44 multiple non-overlapping address space inside interfaces """ |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1947 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1948 | self.nat44_add_address(self.nat_addr) |
| 1949 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 1950 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index) |
| 1951 | self.vapi.nat44_interface_add_del_feature(self.pg3.sw_if_index, |
| 1952 | is_inside=0) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1953 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1954 | # between two NAT44 inside interfaces (no translation) |
Matus Fabian | 675a69c | 2017-01-18 01:46:01 -0800 | [diff] [blame] | 1955 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 1956 | self.pg0.add_stream(pkts) |
| 1957 | self.pg_enable_capture(self.pg_interfaces) |
| 1958 | self.pg_start() |
| 1959 | capture = self.pg1.get_capture(len(pkts)) |
| 1960 | self.verify_capture_no_translation(capture, self.pg0, self.pg1) |
| 1961 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 1962 | # from NAT44 inside to interface without NAT44 feature (no translation) |
Matus Fabian | 675a69c | 2017-01-18 01:46:01 -0800 | [diff] [blame] | 1963 | pkts = self.create_stream_in(self.pg0, self.pg2) |
| 1964 | self.pg0.add_stream(pkts) |
| 1965 | self.pg_enable_capture(self.pg_interfaces) |
| 1966 | self.pg_start() |
| 1967 | capture = self.pg2.get_capture(len(pkts)) |
| 1968 | self.verify_capture_no_translation(capture, self.pg0, self.pg2) |
| 1969 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1970 | # in2out 1st interface |
| 1971 | pkts = self.create_stream_in(self.pg0, self.pg3) |
| 1972 | self.pg0.add_stream(pkts) |
| 1973 | self.pg_enable_capture(self.pg_interfaces) |
| 1974 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1975 | capture = self.pg3.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1976 | self.verify_capture_out(capture) |
| 1977 | |
| 1978 | # out2in 1st interface |
| 1979 | pkts = self.create_stream_out(self.pg3) |
| 1980 | self.pg3.add_stream(pkts) |
| 1981 | self.pg_enable_capture(self.pg_interfaces) |
| 1982 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1983 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1984 | self.verify_capture_in(capture, self.pg0) |
| 1985 | |
| 1986 | # in2out 2nd interface |
| 1987 | pkts = self.create_stream_in(self.pg1, self.pg3) |
| 1988 | self.pg1.add_stream(pkts) |
| 1989 | self.pg_enable_capture(self.pg_interfaces) |
| 1990 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1991 | capture = self.pg3.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 1992 | self.verify_capture_out(capture) |
| 1993 | |
| 1994 | # out2in 2nd interface |
| 1995 | pkts = self.create_stream_out(self.pg3) |
| 1996 | self.pg3.add_stream(pkts) |
| 1997 | self.pg_enable_capture(self.pg_interfaces) |
| 1998 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 1999 | capture = self.pg1.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2000 | self.verify_capture_in(capture, self.pg1) |
| 2001 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2002 | def test_inside_overlapping_interfaces(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2003 | """ NAT44 multiple inside interfaces with overlapping address space """ |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2004 | |
Matus Fabian | 675a69c | 2017-01-18 01:46:01 -0800 | [diff] [blame] | 2005 | static_nat_ip = "10.0.0.10" |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2006 | self.nat44_add_address(self.nat_addr) |
| 2007 | self.vapi.nat44_interface_add_del_feature(self.pg3.sw_if_index, |
| 2008 | is_inside=0) |
| 2009 | self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index) |
| 2010 | self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index) |
| 2011 | self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index) |
| 2012 | self.nat44_add_static_mapping(self.pg6.remote_ip4, static_nat_ip, |
| 2013 | vrf_id=20) |
Matus Fabian | 675a69c | 2017-01-18 01:46:01 -0800 | [diff] [blame] | 2014 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2015 | # between NAT44 inside interfaces with same VRF (no translation) |
Matus Fabian | 675a69c | 2017-01-18 01:46:01 -0800 | [diff] [blame] | 2016 | pkts = self.create_stream_in(self.pg4, self.pg5) |
| 2017 | self.pg4.add_stream(pkts) |
| 2018 | self.pg_enable_capture(self.pg_interfaces) |
| 2019 | self.pg_start() |
| 2020 | capture = self.pg5.get_capture(len(pkts)) |
| 2021 | self.verify_capture_no_translation(capture, self.pg4, self.pg5) |
| 2022 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2023 | # between NAT44 inside interfaces with different VRF (hairpinning) |
Matus Fabian | 675a69c | 2017-01-18 01:46:01 -0800 | [diff] [blame] | 2024 | p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) / |
| 2025 | IP(src=self.pg4.remote_ip4, dst=static_nat_ip) / |
| 2026 | TCP(sport=1234, dport=5678)) |
| 2027 | self.pg4.add_stream(p) |
| 2028 | self.pg_enable_capture(self.pg_interfaces) |
| 2029 | self.pg_start() |
| 2030 | capture = self.pg6.get_capture(1) |
| 2031 | p = capture[0] |
| 2032 | try: |
| 2033 | ip = p[IP] |
| 2034 | tcp = p[TCP] |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2035 | self.assertEqual(ip.src, self.nat_addr) |
Matus Fabian | 675a69c | 2017-01-18 01:46:01 -0800 | [diff] [blame] | 2036 | self.assertEqual(ip.dst, self.pg6.remote_ip4) |
| 2037 | self.assertNotEqual(tcp.sport, 1234) |
| 2038 | self.assertEqual(tcp.dport, 5678) |
| 2039 | except: |
| 2040 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 2041 | raise |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2042 | |
| 2043 | # in2out 1st interface |
| 2044 | pkts = self.create_stream_in(self.pg4, self.pg3) |
| 2045 | self.pg4.add_stream(pkts) |
| 2046 | self.pg_enable_capture(self.pg_interfaces) |
| 2047 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 2048 | capture = self.pg3.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2049 | self.verify_capture_out(capture) |
| 2050 | |
| 2051 | # out2in 1st interface |
| 2052 | pkts = self.create_stream_out(self.pg3) |
| 2053 | self.pg3.add_stream(pkts) |
| 2054 | self.pg_enable_capture(self.pg_interfaces) |
| 2055 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 2056 | capture = self.pg4.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2057 | self.verify_capture_in(capture, self.pg4) |
| 2058 | |
| 2059 | # in2out 2nd interface |
| 2060 | pkts = self.create_stream_in(self.pg5, self.pg3) |
| 2061 | self.pg5.add_stream(pkts) |
| 2062 | self.pg_enable_capture(self.pg_interfaces) |
| 2063 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 2064 | capture = self.pg3.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2065 | self.verify_capture_out(capture) |
| 2066 | |
| 2067 | # out2in 2nd interface |
| 2068 | pkts = self.create_stream_out(self.pg3) |
| 2069 | self.pg3.add_stream(pkts) |
| 2070 | self.pg_enable_capture(self.pg_interfaces) |
| 2071 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 2072 | capture = self.pg5.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2073 | self.verify_capture_in(capture, self.pg5) |
| 2074 | |
magalik | 23caa88 | 2017-02-08 23:25:45 -0800 | [diff] [blame] | 2075 | # pg5 session dump |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2076 | addresses = self.vapi.nat44_address_dump() |
magalik | 23caa88 | 2017-02-08 23:25:45 -0800 | [diff] [blame] | 2077 | self.assertEqual(len(addresses), 1) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2078 | sessions = self.vapi.nat44_user_session_dump(self.pg5.remote_ip4n, 10) |
magalik | 23caa88 | 2017-02-08 23:25:45 -0800 | [diff] [blame] | 2079 | self.assertEqual(len(sessions), 3) |
| 2080 | for session in sessions: |
| 2081 | self.assertFalse(session.is_static) |
| 2082 | self.assertEqual(session.inside_ip_address[0:4], |
| 2083 | self.pg5.remote_ip4n) |
| 2084 | self.assertEqual(session.outside_ip_address, |
| 2085 | addresses[0].ip_address) |
| 2086 | self.assertEqual(sessions[0].protocol, IP_PROTOS.tcp) |
| 2087 | self.assertEqual(sessions[1].protocol, IP_PROTOS.udp) |
| 2088 | self.assertEqual(sessions[2].protocol, IP_PROTOS.icmp) |
| 2089 | self.assertEqual(sessions[0].inside_port, self.tcp_port_in) |
| 2090 | self.assertEqual(sessions[1].inside_port, self.udp_port_in) |
| 2091 | self.assertEqual(sessions[2].inside_port, self.icmp_id_in) |
| 2092 | self.assertEqual(sessions[0].outside_port, self.tcp_port_out) |
| 2093 | self.assertEqual(sessions[1].outside_port, self.udp_port_out) |
| 2094 | self.assertEqual(sessions[2].outside_port, self.icmp_id_out) |
| 2095 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2096 | # in2out 3rd interface |
| 2097 | pkts = self.create_stream_in(self.pg6, self.pg3) |
| 2098 | self.pg6.add_stream(pkts) |
| 2099 | self.pg_enable_capture(self.pg_interfaces) |
| 2100 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 2101 | capture = self.pg3.get_capture(len(pkts)) |
Matus Fabian | 675a69c | 2017-01-18 01:46:01 -0800 | [diff] [blame] | 2102 | self.verify_capture_out(capture, static_nat_ip, True) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2103 | |
| 2104 | # out2in 3rd interface |
Matus Fabian | 675a69c | 2017-01-18 01:46:01 -0800 | [diff] [blame] | 2105 | pkts = self.create_stream_out(self.pg3, static_nat_ip) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2106 | self.pg3.add_stream(pkts) |
| 2107 | self.pg_enable_capture(self.pg_interfaces) |
| 2108 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 2109 | capture = self.pg6.get_capture(len(pkts)) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 2110 | self.verify_capture_in(capture, self.pg6) |
| 2111 | |
magalik | 23caa88 | 2017-02-08 23:25:45 -0800 | [diff] [blame] | 2112 | # general user and session dump verifications |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2113 | users = self.vapi.nat44_user_dump() |
magalik | 23caa88 | 2017-02-08 23:25:45 -0800 | [diff] [blame] | 2114 | self.assertTrue(len(users) >= 3) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2115 | addresses = self.vapi.nat44_address_dump() |
magalik | 23caa88 | 2017-02-08 23:25:45 -0800 | [diff] [blame] | 2116 | self.assertEqual(len(addresses), 1) |
| 2117 | for user in users: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2118 | sessions = self.vapi.nat44_user_session_dump(user.ip_address, |
| 2119 | user.vrf_id) |
magalik | 23caa88 | 2017-02-08 23:25:45 -0800 | [diff] [blame] | 2120 | for session in sessions: |
| 2121 | self.assertEqual(user.ip_address, session.inside_ip_address) |
| 2122 | self.assertTrue(session.total_bytes > session.total_pkts > 0) |
| 2123 | self.assertTrue(session.protocol in |
| 2124 | [IP_PROTOS.tcp, IP_PROTOS.udp, |
| 2125 | IP_PROTOS.icmp]) |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 2126 | self.assertFalse(session.ext_host_valid) |
magalik | 23caa88 | 2017-02-08 23:25:45 -0800 | [diff] [blame] | 2127 | |
| 2128 | # pg4 session dump |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2129 | sessions = self.vapi.nat44_user_session_dump(self.pg4.remote_ip4n, 10) |
magalik | 23caa88 | 2017-02-08 23:25:45 -0800 | [diff] [blame] | 2130 | self.assertTrue(len(sessions) >= 4) |
| 2131 | for session in sessions: |
| 2132 | self.assertFalse(session.is_static) |
| 2133 | self.assertEqual(session.inside_ip_address[0:4], |
| 2134 | self.pg4.remote_ip4n) |
| 2135 | self.assertEqual(session.outside_ip_address, |
| 2136 | addresses[0].ip_address) |
| 2137 | |
| 2138 | # pg6 session dump |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2139 | sessions = self.vapi.nat44_user_session_dump(self.pg6.remote_ip4n, 20) |
magalik | 23caa88 | 2017-02-08 23:25:45 -0800 | [diff] [blame] | 2140 | self.assertTrue(len(sessions) >= 3) |
| 2141 | for session in sessions: |
| 2142 | self.assertTrue(session.is_static) |
| 2143 | self.assertEqual(session.inside_ip_address[0:4], |
| 2144 | self.pg6.remote_ip4n) |
| 2145 | self.assertEqual(map(ord, session.outside_ip_address[0:4]), |
| 2146 | map(int, static_nat_ip.split('.'))) |
| 2147 | self.assertTrue(session.inside_port in |
| 2148 | [self.tcp_port_in, self.udp_port_in, |
| 2149 | self.icmp_id_in]) |
| 2150 | |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2151 | def test_hairpinning(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2152 | """ NAT44 hairpinning - 1:1 NAPT """ |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2153 | |
| 2154 | host = self.pg0.remote_hosts[0] |
| 2155 | server = self.pg0.remote_hosts[1] |
| 2156 | host_in_port = 1234 |
| 2157 | host_out_port = 0 |
| 2158 | server_in_port = 5678 |
| 2159 | server_out_port = 8765 |
| 2160 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2161 | self.nat44_add_address(self.nat_addr) |
| 2162 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 2163 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 2164 | is_inside=0) |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2165 | # add static mapping for server |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2166 | self.nat44_add_static_mapping(server.ip4, self.nat_addr, |
| 2167 | server_in_port, server_out_port, |
| 2168 | proto=IP_PROTOS.tcp) |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2169 | |
| 2170 | # send packet from host to server |
| 2171 | p = (Ether(src=host.mac, dst=self.pg0.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2172 | IP(src=host.ip4, dst=self.nat_addr) / |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2173 | TCP(sport=host_in_port, dport=server_out_port)) |
| 2174 | self.pg0.add_stream(p) |
| 2175 | self.pg_enable_capture(self.pg_interfaces) |
| 2176 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 2177 | capture = self.pg0.get_capture(1) |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2178 | p = capture[0] |
| 2179 | try: |
| 2180 | ip = p[IP] |
| 2181 | tcp = p[TCP] |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2182 | self.assertEqual(ip.src, self.nat_addr) |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2183 | self.assertEqual(ip.dst, server.ip4) |
| 2184 | self.assertNotEqual(tcp.sport, host_in_port) |
| 2185 | self.assertEqual(tcp.dport, server_in_port) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 2186 | self.assert_packet_checksums_valid(p) |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2187 | host_out_port = tcp.sport |
| 2188 | except: |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 2189 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2190 | raise |
| 2191 | |
| 2192 | # send reply from server to host |
| 2193 | p = (Ether(src=server.mac, dst=self.pg0.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2194 | IP(src=server.ip4, dst=self.nat_addr) / |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2195 | TCP(sport=server_in_port, dport=host_out_port)) |
| 2196 | self.pg0.add_stream(p) |
| 2197 | self.pg_enable_capture(self.pg_interfaces) |
| 2198 | self.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 2199 | capture = self.pg0.get_capture(1) |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2200 | p = capture[0] |
| 2201 | try: |
| 2202 | ip = p[IP] |
| 2203 | tcp = p[TCP] |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2204 | self.assertEqual(ip.src, self.nat_addr) |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2205 | self.assertEqual(ip.dst, host.ip4) |
| 2206 | self.assertEqual(tcp.sport, server_out_port) |
| 2207 | self.assertEqual(tcp.dport, host_in_port) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 2208 | self.assert_packet_checksums_valid(p) |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2209 | except: |
Matus Fabian | e82488f | 2018-01-18 03:38:45 -0800 | [diff] [blame] | 2210 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
Matus Fabian | f78a70d | 2016-12-12 04:30:39 -0800 | [diff] [blame] | 2211 | raise |
| 2212 | |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2213 | def test_hairpinning2(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2214 | """ NAT44 hairpinning - 1:1 NAT""" |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2215 | |
| 2216 | server1_nat_ip = "10.0.0.10" |
| 2217 | server2_nat_ip = "10.0.0.11" |
| 2218 | host = self.pg0.remote_hosts[0] |
| 2219 | server1 = self.pg0.remote_hosts[1] |
| 2220 | server2 = self.pg0.remote_hosts[2] |
| 2221 | server_tcp_port = 22 |
| 2222 | server_udp_port = 20 |
| 2223 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2224 | self.nat44_add_address(self.nat_addr) |
| 2225 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 2226 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 2227 | is_inside=0) |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2228 | |
| 2229 | # add static mapping for servers |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2230 | self.nat44_add_static_mapping(server1.ip4, server1_nat_ip) |
| 2231 | self.nat44_add_static_mapping(server2.ip4, server2_nat_ip) |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2232 | |
| 2233 | # host to server1 |
| 2234 | pkts = [] |
| 2235 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2236 | IP(src=host.ip4, dst=server1_nat_ip) / |
| 2237 | TCP(sport=self.tcp_port_in, dport=server_tcp_port)) |
| 2238 | pkts.append(p) |
| 2239 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2240 | IP(src=host.ip4, dst=server1_nat_ip) / |
| 2241 | UDP(sport=self.udp_port_in, dport=server_udp_port)) |
| 2242 | pkts.append(p) |
| 2243 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2244 | IP(src=host.ip4, dst=server1_nat_ip) / |
| 2245 | ICMP(id=self.icmp_id_in, type='echo-request')) |
| 2246 | pkts.append(p) |
| 2247 | self.pg0.add_stream(pkts) |
| 2248 | self.pg_enable_capture(self.pg_interfaces) |
| 2249 | self.pg_start() |
| 2250 | capture = self.pg0.get_capture(len(pkts)) |
| 2251 | for packet in capture: |
| 2252 | try: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2253 | self.assertEqual(packet[IP].src, self.nat_addr) |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2254 | self.assertEqual(packet[IP].dst, server1.ip4) |
| 2255 | if packet.haslayer(TCP): |
| 2256 | self.assertNotEqual(packet[TCP].sport, self.tcp_port_in) |
| 2257 | self.assertEqual(packet[TCP].dport, server_tcp_port) |
| 2258 | self.tcp_port_out = packet[TCP].sport |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 2259 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2260 | elif packet.haslayer(UDP): |
| 2261 | self.assertNotEqual(packet[UDP].sport, self.udp_port_in) |
| 2262 | self.assertEqual(packet[UDP].dport, server_udp_port) |
| 2263 | self.udp_port_out = packet[UDP].sport |
| 2264 | else: |
| 2265 | self.assertNotEqual(packet[ICMP].id, self.icmp_id_in) |
| 2266 | self.icmp_id_out = packet[ICMP].id |
| 2267 | except: |
| 2268 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 2269 | raise |
| 2270 | |
| 2271 | # server1 to host |
| 2272 | pkts = [] |
| 2273 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2274 | IP(src=server1.ip4, dst=self.nat_addr) / |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2275 | TCP(sport=server_tcp_port, dport=self.tcp_port_out)) |
| 2276 | pkts.append(p) |
| 2277 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2278 | IP(src=server1.ip4, dst=self.nat_addr) / |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2279 | UDP(sport=server_udp_port, dport=self.udp_port_out)) |
| 2280 | pkts.append(p) |
| 2281 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2282 | IP(src=server1.ip4, dst=self.nat_addr) / |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2283 | ICMP(id=self.icmp_id_out, type='echo-reply')) |
| 2284 | pkts.append(p) |
| 2285 | self.pg0.add_stream(pkts) |
| 2286 | self.pg_enable_capture(self.pg_interfaces) |
| 2287 | self.pg_start() |
| 2288 | capture = self.pg0.get_capture(len(pkts)) |
| 2289 | for packet in capture: |
| 2290 | try: |
| 2291 | self.assertEqual(packet[IP].src, server1_nat_ip) |
| 2292 | self.assertEqual(packet[IP].dst, host.ip4) |
| 2293 | if packet.haslayer(TCP): |
| 2294 | self.assertEqual(packet[TCP].dport, self.tcp_port_in) |
| 2295 | self.assertEqual(packet[TCP].sport, server_tcp_port) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 2296 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2297 | elif packet.haslayer(UDP): |
| 2298 | self.assertEqual(packet[UDP].dport, self.udp_port_in) |
| 2299 | self.assertEqual(packet[UDP].sport, server_udp_port) |
| 2300 | else: |
| 2301 | self.assertEqual(packet[ICMP].id, self.icmp_id_in) |
| 2302 | except: |
| 2303 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 2304 | raise |
| 2305 | |
| 2306 | # server2 to server1 |
| 2307 | pkts = [] |
| 2308 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2309 | IP(src=server2.ip4, dst=server1_nat_ip) / |
| 2310 | TCP(sport=self.tcp_port_in, dport=server_tcp_port)) |
| 2311 | pkts.append(p) |
| 2312 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2313 | IP(src=server2.ip4, dst=server1_nat_ip) / |
| 2314 | UDP(sport=self.udp_port_in, dport=server_udp_port)) |
| 2315 | pkts.append(p) |
| 2316 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2317 | IP(src=server2.ip4, dst=server1_nat_ip) / |
| 2318 | ICMP(id=self.icmp_id_in, type='echo-request')) |
| 2319 | pkts.append(p) |
| 2320 | self.pg0.add_stream(pkts) |
| 2321 | self.pg_enable_capture(self.pg_interfaces) |
| 2322 | self.pg_start() |
| 2323 | capture = self.pg0.get_capture(len(pkts)) |
| 2324 | for packet in capture: |
| 2325 | try: |
| 2326 | self.assertEqual(packet[IP].src, server2_nat_ip) |
| 2327 | self.assertEqual(packet[IP].dst, server1.ip4) |
| 2328 | if packet.haslayer(TCP): |
| 2329 | self.assertEqual(packet[TCP].sport, self.tcp_port_in) |
| 2330 | self.assertEqual(packet[TCP].dport, server_tcp_port) |
| 2331 | self.tcp_port_out = packet[TCP].sport |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 2332 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2333 | elif packet.haslayer(UDP): |
| 2334 | self.assertEqual(packet[UDP].sport, self.udp_port_in) |
| 2335 | self.assertEqual(packet[UDP].dport, server_udp_port) |
| 2336 | self.udp_port_out = packet[UDP].sport |
| 2337 | else: |
| 2338 | self.assertEqual(packet[ICMP].id, self.icmp_id_in) |
| 2339 | self.icmp_id_out = packet[ICMP].id |
| 2340 | except: |
| 2341 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 2342 | raise |
| 2343 | |
| 2344 | # server1 to server2 |
| 2345 | pkts = [] |
| 2346 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2347 | IP(src=server1.ip4, dst=server2_nat_ip) / |
| 2348 | TCP(sport=server_tcp_port, dport=self.tcp_port_out)) |
| 2349 | pkts.append(p) |
| 2350 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2351 | IP(src=server1.ip4, dst=server2_nat_ip) / |
| 2352 | UDP(sport=server_udp_port, dport=self.udp_port_out)) |
| 2353 | pkts.append(p) |
| 2354 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2355 | IP(src=server1.ip4, dst=server2_nat_ip) / |
| 2356 | ICMP(id=self.icmp_id_out, type='echo-reply')) |
| 2357 | pkts.append(p) |
| 2358 | self.pg0.add_stream(pkts) |
| 2359 | self.pg_enable_capture(self.pg_interfaces) |
| 2360 | self.pg_start() |
| 2361 | capture = self.pg0.get_capture(len(pkts)) |
| 2362 | for packet in capture: |
| 2363 | try: |
| 2364 | self.assertEqual(packet[IP].src, server1_nat_ip) |
| 2365 | self.assertEqual(packet[IP].dst, server2.ip4) |
| 2366 | if packet.haslayer(TCP): |
| 2367 | self.assertEqual(packet[TCP].dport, self.tcp_port_in) |
| 2368 | self.assertEqual(packet[TCP].sport, server_tcp_port) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 2369 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 6fa74c6 | 2017-06-05 05:55:48 -0700 | [diff] [blame] | 2370 | elif packet.haslayer(UDP): |
| 2371 | self.assertEqual(packet[UDP].dport, self.udp_port_in) |
| 2372 | self.assertEqual(packet[UDP].sport, server_udp_port) |
| 2373 | else: |
| 2374 | self.assertEqual(packet[ICMP].id, self.icmp_id_in) |
| 2375 | except: |
| 2376 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 2377 | raise |
| 2378 | |
Matus Fabian | 9902fcd | 2016-12-21 23:58:46 -0800 | [diff] [blame] | 2379 | def test_max_translations_per_user(self): |
| 2380 | """ MAX translations per user - recycle the least recently used """ |
| 2381 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2382 | self.nat44_add_address(self.nat_addr) |
| 2383 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 2384 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 2385 | is_inside=0) |
Matus Fabian | 9902fcd | 2016-12-21 23:58:46 -0800 | [diff] [blame] | 2386 | |
| 2387 | # get maximum number of translations per user |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2388 | nat44_config = self.vapi.nat_show_config() |
Matus Fabian | 9902fcd | 2016-12-21 23:58:46 -0800 | [diff] [blame] | 2389 | |
| 2390 | # send more than maximum number of translations per user packets |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2391 | pkts_num = nat44_config.max_translations_per_user + 5 |
Matus Fabian | 9902fcd | 2016-12-21 23:58:46 -0800 | [diff] [blame] | 2392 | pkts = [] |
| 2393 | for port in range(0, pkts_num): |
| 2394 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2395 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 2396 | TCP(sport=1025 + port)) |
| 2397 | pkts.append(p) |
| 2398 | self.pg0.add_stream(pkts) |
| 2399 | self.pg_enable_capture(self.pg_interfaces) |
| 2400 | self.pg_start() |
| 2401 | |
| 2402 | # verify number of translated packet |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 2403 | self.pg1.get_capture(pkts_num) |
Matus Fabian | 9902fcd | 2016-12-21 23:58:46 -0800 | [diff] [blame] | 2404 | |
Matus Fabian | 132dc49 | 2018-05-09 04:51:03 -0700 | [diff] [blame] | 2405 | users = self.vapi.nat44_user_dump() |
| 2406 | for user in users: |
| 2407 | if user.ip_address == self.pg0.remote_ip4n: |
| 2408 | self.assertEqual(user.nsessions, |
| 2409 | nat44_config.max_translations_per_user) |
| 2410 | self.assertEqual(user.nstaticsessions, 0) |
| 2411 | |
| 2412 | tcp_port = 22 |
| 2413 | self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr, |
| 2414 | tcp_port, tcp_port, |
| 2415 | proto=IP_PROTOS.tcp) |
| 2416 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2417 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 2418 | TCP(sport=tcp_port)) |
| 2419 | self.pg0.add_stream(p) |
| 2420 | self.pg_enable_capture(self.pg_interfaces) |
| 2421 | self.pg_start() |
| 2422 | self.pg1.get_capture(1) |
| 2423 | users = self.vapi.nat44_user_dump() |
| 2424 | for user in users: |
| 2425 | if user.ip_address == self.pg0.remote_ip4n: |
| 2426 | self.assertEqual(user.nsessions, |
| 2427 | nat44_config.max_translations_per_user - 1) |
| 2428 | self.assertEqual(user.nstaticsessions, 1) |
| 2429 | |
Matus Fabian | 8bf68e8 | 2017-01-12 04:24:35 -0800 | [diff] [blame] | 2430 | def test_interface_addr(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2431 | """ Acquire NAT44 addresses from interface """ |
| 2432 | self.vapi.nat44_add_interface_addr(self.pg7.sw_if_index) |
Matus Fabian | 8bf68e8 | 2017-01-12 04:24:35 -0800 | [diff] [blame] | 2433 | |
| 2434 | # no address in NAT pool |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2435 | adresses = self.vapi.nat44_address_dump() |
Matus Fabian | 8bf68e8 | 2017-01-12 04:24:35 -0800 | [diff] [blame] | 2436 | self.assertEqual(0, len(adresses)) |
| 2437 | |
| 2438 | # configure interface address and check NAT address pool |
| 2439 | self.pg7.config_ip4() |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2440 | adresses = self.vapi.nat44_address_dump() |
Matus Fabian | 8bf68e8 | 2017-01-12 04:24:35 -0800 | [diff] [blame] | 2441 | self.assertEqual(1, len(adresses)) |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 2442 | self.assertEqual(adresses[0].ip_address[0:4], self.pg7.local_ip4n) |
Matus Fabian | 8bf68e8 | 2017-01-12 04:24:35 -0800 | [diff] [blame] | 2443 | |
| 2444 | # remove interface address and check NAT address pool |
| 2445 | self.pg7.unconfig_ip4() |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2446 | adresses = self.vapi.nat44_address_dump() |
Matus Fabian | 8bf68e8 | 2017-01-12 04:24:35 -0800 | [diff] [blame] | 2447 | self.assertEqual(0, len(adresses)) |
| 2448 | |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 2449 | def test_interface_addr_static_mapping(self): |
| 2450 | """ Static mapping with addresses from interface """ |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 2451 | tag = "testTAG" |
| 2452 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2453 | self.vapi.nat44_add_interface_addr(self.pg7.sw_if_index) |
| 2454 | self.nat44_add_static_mapping( |
| 2455 | '1.2.3.4', |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 2456 | external_sw_if_index=self.pg7.sw_if_index, |
| 2457 | tag=tag) |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 2458 | |
Matus Fabian | e22e546 | 2017-02-14 23:33:43 -0800 | [diff] [blame] | 2459 | # static mappings with external interface |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2460 | static_mappings = self.vapi.nat44_static_mapping_dump() |
Matus Fabian | e22e546 | 2017-02-14 23:33:43 -0800 | [diff] [blame] | 2461 | self.assertEqual(1, len(static_mappings)) |
| 2462 | self.assertEqual(self.pg7.sw_if_index, |
| 2463 | static_mappings[0].external_sw_if_index) |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 2464 | self.assertEqual((static_mappings[0].tag).split('\0', 1)[0], tag) |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 2465 | |
| 2466 | # configure interface address and check static mappings |
| 2467 | self.pg7.config_ip4() |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2468 | static_mappings = self.vapi.nat44_static_mapping_dump() |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 2469 | self.assertEqual(2, len(static_mappings)) |
| 2470 | resolved = False |
| 2471 | for sm in static_mappings: |
| 2472 | if sm.external_sw_if_index == 0xFFFFFFFF: |
| 2473 | self.assertEqual(sm.external_ip_address[0:4], |
| 2474 | self.pg7.local_ip4n) |
| 2475 | self.assertEqual((sm.tag).split('\0', 1)[0], tag) |
| 2476 | resolved = True |
| 2477 | self.assertTrue(resolved) |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 2478 | |
| 2479 | # remove interface address and check static mappings |
| 2480 | self.pg7.unconfig_ip4() |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2481 | static_mappings = self.vapi.nat44_static_mapping_dump() |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 2482 | self.assertEqual(1, len(static_mappings)) |
| 2483 | self.assertEqual(self.pg7.sw_if_index, |
| 2484 | static_mappings[0].external_sw_if_index) |
| 2485 | self.assertEqual((static_mappings[0].tag).split('\0', 1)[0], tag) |
| 2486 | |
| 2487 | # configure interface address again and check static mappings |
| 2488 | self.pg7.config_ip4() |
| 2489 | static_mappings = self.vapi.nat44_static_mapping_dump() |
| 2490 | self.assertEqual(2, len(static_mappings)) |
| 2491 | resolved = False |
| 2492 | for sm in static_mappings: |
| 2493 | if sm.external_sw_if_index == 0xFFFFFFFF: |
| 2494 | self.assertEqual(sm.external_ip_address[0:4], |
| 2495 | self.pg7.local_ip4n) |
| 2496 | self.assertEqual((sm.tag).split('\0', 1)[0], tag) |
| 2497 | resolved = True |
| 2498 | self.assertTrue(resolved) |
| 2499 | |
| 2500 | # remove static mapping |
| 2501 | self.nat44_add_static_mapping( |
| 2502 | '1.2.3.4', |
| 2503 | external_sw_if_index=self.pg7.sw_if_index, |
| 2504 | tag=tag, |
| 2505 | is_add=0) |
| 2506 | static_mappings = self.vapi.nat44_static_mapping_dump() |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 2507 | self.assertEqual(0, len(static_mappings)) |
| 2508 | |
Matus Fabian | ab7a805 | 2017-11-28 04:29:41 -0800 | [diff] [blame] | 2509 | def test_interface_addr_identity_nat(self): |
| 2510 | """ Identity NAT with addresses from interface """ |
| 2511 | |
| 2512 | port = 53053 |
| 2513 | self.vapi.nat44_add_interface_addr(self.pg7.sw_if_index) |
| 2514 | self.vapi.nat44_add_del_identity_mapping( |
| 2515 | sw_if_index=self.pg7.sw_if_index, |
| 2516 | port=port, |
| 2517 | protocol=IP_PROTOS.tcp, |
| 2518 | addr_only=0) |
| 2519 | |
| 2520 | # identity mappings with external interface |
| 2521 | identity_mappings = self.vapi.nat44_identity_mapping_dump() |
| 2522 | self.assertEqual(1, len(identity_mappings)) |
| 2523 | self.assertEqual(self.pg7.sw_if_index, |
| 2524 | identity_mappings[0].sw_if_index) |
| 2525 | |
| 2526 | # configure interface address and check identity mappings |
| 2527 | self.pg7.config_ip4() |
| 2528 | identity_mappings = self.vapi.nat44_identity_mapping_dump() |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 2529 | resolved = False |
| 2530 | self.assertEqual(2, len(identity_mappings)) |
| 2531 | for sm in identity_mappings: |
| 2532 | if sm.sw_if_index == 0xFFFFFFFF: |
| 2533 | self.assertEqual(identity_mappings[0].ip_address, |
| 2534 | self.pg7.local_ip4n) |
| 2535 | self.assertEqual(port, identity_mappings[0].port) |
| 2536 | self.assertEqual(IP_PROTOS.tcp, identity_mappings[0].protocol) |
| 2537 | resolved = True |
| 2538 | self.assertTrue(resolved) |
Matus Fabian | ab7a805 | 2017-11-28 04:29:41 -0800 | [diff] [blame] | 2539 | |
| 2540 | # remove interface address and check identity mappings |
| 2541 | self.pg7.unconfig_ip4() |
| 2542 | identity_mappings = self.vapi.nat44_identity_mapping_dump() |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 2543 | self.assertEqual(1, len(identity_mappings)) |
| 2544 | self.assertEqual(self.pg7.sw_if_index, |
| 2545 | identity_mappings[0].sw_if_index) |
Matus Fabian | ab7a805 | 2017-11-28 04:29:41 -0800 | [diff] [blame] | 2546 | |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2547 | def test_ipfix_nat44_sess(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2548 | """ IPFIX logging NAT44 session created/delted """ |
Matus Fabian | 6631e9c | 2017-05-24 01:52:20 -0700 | [diff] [blame] | 2549 | self.ipfix_domain_id = 10 |
| 2550 | self.ipfix_src_port = 20202 |
| 2551 | colector_port = 30303 |
| 2552 | bind_layers(UDP, IPFIX, dport=30303) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2553 | self.nat44_add_address(self.nat_addr) |
| 2554 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 2555 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 2556 | is_inside=0) |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2557 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 2558 | src_address=self.pg3.local_ip4n, |
| 2559 | path_mtu=512, |
Matus Fabian | 6631e9c | 2017-05-24 01:52:20 -0700 | [diff] [blame] | 2560 | template_interval=10, |
| 2561 | collector_port=colector_port) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2562 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 2563 | src_port=self.ipfix_src_port) |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2564 | |
| 2565 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 2566 | self.pg0.add_stream(pkts) |
| 2567 | self.pg_enable_capture(self.pg_interfaces) |
| 2568 | self.pg_start() |
| 2569 | capture = self.pg1.get_capture(len(pkts)) |
| 2570 | self.verify_capture_out(capture) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2571 | self.nat44_add_address(self.nat_addr, is_add=0) |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2572 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 2573 | capture = self.pg3.get_capture(9) |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2574 | ipfix = IPFIXDecoder() |
| 2575 | # first load template |
| 2576 | for p in capture: |
| 2577 | self.assertTrue(p.haslayer(IPFIX)) |
Matus Fabian | 6631e9c | 2017-05-24 01:52:20 -0700 | [diff] [blame] | 2578 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 2579 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 2580 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 2581 | self.assertEqual(p[UDP].dport, colector_port) |
| 2582 | self.assertEqual(p[IPFIX].observationDomainID, |
| 2583 | self.ipfix_domain_id) |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2584 | if p.haslayer(Template): |
| 2585 | ipfix.add_template(p.getlayer(Template)) |
| 2586 | # verify events in data set |
| 2587 | for p in capture: |
| 2588 | if p.haslayer(Data): |
| 2589 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 2590 | self.verify_ipfix_nat44_ses(data) |
| 2591 | |
| 2592 | def test_ipfix_addr_exhausted(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2593 | """ IPFIX logging NAT addresses exhausted """ |
| 2594 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 2595 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 2596 | is_inside=0) |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2597 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 2598 | src_address=self.pg3.local_ip4n, |
| 2599 | path_mtu=512, |
| 2600 | template_interval=10) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2601 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 2602 | src_port=self.ipfix_src_port) |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2603 | |
| 2604 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 2605 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 2606 | TCP(sport=3025)) |
| 2607 | self.pg0.add_stream(p) |
| 2608 | self.pg_enable_capture(self.pg_interfaces) |
| 2609 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2610 | self.pg1.assert_nothing_captured() |
| 2611 | sleep(1) |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2612 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 2613 | capture = self.pg3.get_capture(9) |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2614 | ipfix = IPFIXDecoder() |
| 2615 | # first load template |
| 2616 | for p in capture: |
| 2617 | self.assertTrue(p.haslayer(IPFIX)) |
Matus Fabian | 6631e9c | 2017-05-24 01:52:20 -0700 | [diff] [blame] | 2618 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 2619 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 2620 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 2621 | self.assertEqual(p[UDP].dport, 4739) |
| 2622 | self.assertEqual(p[IPFIX].observationDomainID, |
| 2623 | self.ipfix_domain_id) |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2624 | if p.haslayer(Template): |
| 2625 | ipfix.add_template(p.getlayer(Template)) |
| 2626 | # verify events in data set |
| 2627 | for p in capture: |
| 2628 | if p.haslayer(Data): |
| 2629 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 2630 | self.verify_ipfix_addr_exhausted(data) |
| 2631 | |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 2632 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 2633 | def test_ipfix_max_sessions(self): |
| 2634 | """ IPFIX logging maximum session entries exceeded """ |
| 2635 | self.nat44_add_address(self.nat_addr) |
| 2636 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 2637 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 2638 | is_inside=0) |
| 2639 | |
| 2640 | nat44_config = self.vapi.nat_show_config() |
| 2641 | max_sessions = 10 * nat44_config.translation_buckets |
| 2642 | |
| 2643 | pkts = [] |
| 2644 | for i in range(0, max_sessions): |
| 2645 | src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF) |
| 2646 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2647 | IP(src=src, dst=self.pg1.remote_ip4) / |
| 2648 | TCP(sport=1025)) |
| 2649 | pkts.append(p) |
| 2650 | self.pg0.add_stream(pkts) |
| 2651 | self.pg_enable_capture(self.pg_interfaces) |
| 2652 | self.pg_start() |
| 2653 | |
| 2654 | self.pg1.get_capture(max_sessions) |
| 2655 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 2656 | src_address=self.pg3.local_ip4n, |
| 2657 | path_mtu=512, |
| 2658 | template_interval=10) |
| 2659 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 2660 | src_port=self.ipfix_src_port) |
| 2661 | |
| 2662 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2663 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 2664 | TCP(sport=1025)) |
| 2665 | self.pg0.add_stream(p) |
| 2666 | self.pg_enable_capture(self.pg_interfaces) |
| 2667 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2668 | self.pg1.assert_nothing_captured() |
| 2669 | sleep(1) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 2670 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 2671 | capture = self.pg3.get_capture(9) |
| 2672 | ipfix = IPFIXDecoder() |
| 2673 | # first load template |
| 2674 | for p in capture: |
| 2675 | self.assertTrue(p.haslayer(IPFIX)) |
| 2676 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 2677 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 2678 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 2679 | self.assertEqual(p[UDP].dport, 4739) |
| 2680 | self.assertEqual(p[IPFIX].observationDomainID, |
| 2681 | self.ipfix_domain_id) |
| 2682 | if p.haslayer(Template): |
| 2683 | ipfix.add_template(p.getlayer(Template)) |
| 2684 | # verify events in data set |
| 2685 | for p in capture: |
| 2686 | if p.haslayer(Data): |
| 2687 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 2688 | self.verify_ipfix_max_sessions(data, max_sessions) |
| 2689 | |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2690 | def test_pool_addr_fib(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2691 | """ NAT44 add pool addresses to FIB """ |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2692 | static_addr = '10.0.0.10' |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2693 | self.nat44_add_address(self.nat_addr) |
| 2694 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 2695 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 2696 | is_inside=0) |
| 2697 | self.nat44_add_static_mapping(self.pg0.remote_ip4, static_addr) |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2698 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2699 | # NAT44 address |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2700 | p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2701 | ARP(op=ARP.who_has, pdst=self.nat_addr, |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2702 | psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac)) |
| 2703 | self.pg1.add_stream(p) |
| 2704 | self.pg_enable_capture(self.pg_interfaces) |
| 2705 | self.pg_start() |
| 2706 | capture = self.pg1.get_capture(1) |
| 2707 | self.assertTrue(capture[0].haslayer(ARP)) |
| 2708 | self.assertTrue(capture[0][ARP].op, ARP.is_at) |
| 2709 | |
| 2710 | # 1:1 NAT address |
| 2711 | p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') / |
| 2712 | ARP(op=ARP.who_has, pdst=static_addr, |
| 2713 | psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac)) |
| 2714 | self.pg1.add_stream(p) |
| 2715 | self.pg_enable_capture(self.pg_interfaces) |
| 2716 | self.pg_start() |
| 2717 | capture = self.pg1.get_capture(1) |
| 2718 | self.assertTrue(capture[0].haslayer(ARP)) |
| 2719 | self.assertTrue(capture[0][ARP].op, ARP.is_at) |
| 2720 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2721 | # send ARP to non-NAT44 interface |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2722 | p = (Ether(src=self.pg2.remote_mac, dst='ff:ff:ff:ff:ff:ff') / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2723 | ARP(op=ARP.who_has, pdst=self.nat_addr, |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2724 | psrc=self.pg2.remote_ip4, hwsrc=self.pg2.remote_mac)) |
| 2725 | self.pg2.add_stream(p) |
| 2726 | self.pg_enable_capture(self.pg_interfaces) |
| 2727 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2728 | self.pg1.assert_nothing_captured() |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2729 | |
| 2730 | # remove addresses and verify |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2731 | self.nat44_add_address(self.nat_addr, is_add=0) |
| 2732 | self.nat44_add_static_mapping(self.pg0.remote_ip4, static_addr, |
| 2733 | is_add=0) |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2734 | |
| 2735 | p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2736 | ARP(op=ARP.who_has, pdst=self.nat_addr, |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2737 | psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac)) |
| 2738 | self.pg1.add_stream(p) |
| 2739 | self.pg_enable_capture(self.pg_interfaces) |
| 2740 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2741 | self.pg1.assert_nothing_captured() |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2742 | |
| 2743 | p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') / |
| 2744 | ARP(op=ARP.who_has, pdst=static_addr, |
| 2745 | psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac)) |
| 2746 | self.pg1.add_stream(p) |
| 2747 | self.pg_enable_capture(self.pg_interfaces) |
| 2748 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2749 | self.pg1.assert_nothing_captured() |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 2750 | |
Juraj Sloboda | eab38d9 | 2017-03-06 19:55:21 -0800 | [diff] [blame] | 2751 | def test_vrf_mode(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2752 | """ NAT44 tenant VRF aware address pool mode """ |
Juraj Sloboda | eab38d9 | 2017-03-06 19:55:21 -0800 | [diff] [blame] | 2753 | |
| 2754 | vrf_id1 = 1 |
| 2755 | vrf_id2 = 2 |
| 2756 | nat_ip1 = "10.0.0.10" |
| 2757 | nat_ip2 = "10.0.0.11" |
| 2758 | |
| 2759 | self.pg0.unconfig_ip4() |
| 2760 | self.pg1.unconfig_ip4() |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 2761 | self.vapi.ip_table_add_del(vrf_id1, is_add=1) |
| 2762 | self.vapi.ip_table_add_del(vrf_id2, is_add=1) |
Juraj Sloboda | eab38d9 | 2017-03-06 19:55:21 -0800 | [diff] [blame] | 2763 | self.pg0.set_table_ip4(vrf_id1) |
| 2764 | self.pg1.set_table_ip4(vrf_id2) |
| 2765 | self.pg0.config_ip4() |
| 2766 | self.pg1.config_ip4() |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 2767 | self.pg0.resolve_arp() |
| 2768 | self.pg1.resolve_arp() |
Juraj Sloboda | eab38d9 | 2017-03-06 19:55:21 -0800 | [diff] [blame] | 2769 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2770 | self.nat44_add_address(nat_ip1, vrf_id=vrf_id1) |
| 2771 | self.nat44_add_address(nat_ip2, vrf_id=vrf_id2) |
| 2772 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 2773 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index) |
| 2774 | self.vapi.nat44_interface_add_del_feature(self.pg2.sw_if_index, |
| 2775 | is_inside=0) |
Juraj Sloboda | eab38d9 | 2017-03-06 19:55:21 -0800 | [diff] [blame] | 2776 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 2777 | try: |
| 2778 | # first VRF |
| 2779 | pkts = self.create_stream_in(self.pg0, self.pg2) |
| 2780 | self.pg0.add_stream(pkts) |
| 2781 | self.pg_enable_capture(self.pg_interfaces) |
| 2782 | self.pg_start() |
| 2783 | capture = self.pg2.get_capture(len(pkts)) |
| 2784 | self.verify_capture_out(capture, nat_ip1) |
Juraj Sloboda | eab38d9 | 2017-03-06 19:55:21 -0800 | [diff] [blame] | 2785 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 2786 | # second VRF |
| 2787 | pkts = self.create_stream_in(self.pg1, self.pg2) |
| 2788 | self.pg1.add_stream(pkts) |
| 2789 | self.pg_enable_capture(self.pg_interfaces) |
| 2790 | self.pg_start() |
| 2791 | capture = self.pg2.get_capture(len(pkts)) |
| 2792 | self.verify_capture_out(capture, nat_ip2) |
Juraj Sloboda | eab38d9 | 2017-03-06 19:55:21 -0800 | [diff] [blame] | 2793 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 2794 | finally: |
| 2795 | self.pg0.unconfig_ip4() |
| 2796 | self.pg1.unconfig_ip4() |
| 2797 | self.pg0.set_table_ip4(0) |
| 2798 | self.pg1.set_table_ip4(0) |
| 2799 | self.pg0.config_ip4() |
| 2800 | self.pg1.config_ip4() |
| 2801 | self.pg0.resolve_arp() |
| 2802 | self.pg1.resolve_arp() |
| 2803 | self.vapi.ip_table_add_del(vrf_id1, is_add=0) |
| 2804 | self.vapi.ip_table_add_del(vrf_id2, is_add=0) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 2805 | |
Juraj Sloboda | eab38d9 | 2017-03-06 19:55:21 -0800 | [diff] [blame] | 2806 | def test_vrf_feature_independent(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2807 | """ NAT44 tenant VRF independent address pool mode """ |
Juraj Sloboda | eab38d9 | 2017-03-06 19:55:21 -0800 | [diff] [blame] | 2808 | |
| 2809 | nat_ip1 = "10.0.0.10" |
| 2810 | nat_ip2 = "10.0.0.11" |
| 2811 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2812 | self.nat44_add_address(nat_ip1) |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2813 | self.nat44_add_address(nat_ip2, vrf_id=99) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2814 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 2815 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index) |
| 2816 | self.vapi.nat44_interface_add_del_feature(self.pg2.sw_if_index, |
| 2817 | is_inside=0) |
Juraj Sloboda | eab38d9 | 2017-03-06 19:55:21 -0800 | [diff] [blame] | 2818 | |
| 2819 | # first VRF |
| 2820 | pkts = self.create_stream_in(self.pg0, self.pg2) |
| 2821 | self.pg0.add_stream(pkts) |
| 2822 | self.pg_enable_capture(self.pg_interfaces) |
| 2823 | self.pg_start() |
| 2824 | capture = self.pg2.get_capture(len(pkts)) |
| 2825 | self.verify_capture_out(capture, nat_ip1) |
| 2826 | |
| 2827 | # second VRF |
| 2828 | pkts = self.create_stream_in(self.pg1, self.pg2) |
| 2829 | self.pg1.add_stream(pkts) |
| 2830 | self.pg_enable_capture(self.pg_interfaces) |
| 2831 | self.pg_start() |
| 2832 | capture = self.pg2.get_capture(len(pkts)) |
| 2833 | self.verify_capture_out(capture, nat_ip1) |
| 2834 | |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2835 | def test_dynamic_ipless_interfaces(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2836 | """ NAT44 interfaces without configured IP address """ |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2837 | |
| 2838 | self.vapi.ip_neighbor_add_del(self.pg7.sw_if_index, |
Ole Troan | 895b6e8 | 2017-10-20 13:28:20 +0200 | [diff] [blame] | 2839 | mactobinary(self.pg7.remote_mac), |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2840 | self.pg7.remote_ip4n, |
| 2841 | is_static=1) |
| 2842 | self.vapi.ip_neighbor_add_del(self.pg8.sw_if_index, |
Ole Troan | 895b6e8 | 2017-10-20 13:28:20 +0200 | [diff] [blame] | 2843 | mactobinary(self.pg8.remote_mac), |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2844 | self.pg8.remote_ip4n, |
| 2845 | is_static=1) |
| 2846 | |
| 2847 | self.vapi.ip_add_del_route(dst_address=self.pg7.remote_ip4n, |
| 2848 | dst_address_length=32, |
| 2849 | next_hop_address=self.pg7.remote_ip4n, |
| 2850 | next_hop_sw_if_index=self.pg7.sw_if_index) |
| 2851 | self.vapi.ip_add_del_route(dst_address=self.pg8.remote_ip4n, |
| 2852 | dst_address_length=32, |
| 2853 | next_hop_address=self.pg8.remote_ip4n, |
| 2854 | next_hop_sw_if_index=self.pg8.sw_if_index) |
| 2855 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2856 | self.nat44_add_address(self.nat_addr) |
| 2857 | self.vapi.nat44_interface_add_del_feature(self.pg7.sw_if_index) |
| 2858 | self.vapi.nat44_interface_add_del_feature(self.pg8.sw_if_index, |
| 2859 | is_inside=0) |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2860 | |
| 2861 | # in2out |
| 2862 | pkts = self.create_stream_in(self.pg7, self.pg8) |
| 2863 | self.pg7.add_stream(pkts) |
| 2864 | self.pg_enable_capture(self.pg_interfaces) |
| 2865 | self.pg_start() |
| 2866 | capture = self.pg8.get_capture(len(pkts)) |
| 2867 | self.verify_capture_out(capture) |
| 2868 | |
| 2869 | # out2in |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2870 | pkts = self.create_stream_out(self.pg8, self.nat_addr) |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2871 | self.pg8.add_stream(pkts) |
| 2872 | self.pg_enable_capture(self.pg_interfaces) |
| 2873 | self.pg_start() |
| 2874 | capture = self.pg7.get_capture(len(pkts)) |
| 2875 | self.verify_capture_in(capture, self.pg7) |
| 2876 | |
| 2877 | def test_static_ipless_interfaces(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2878 | """ NAT44 interfaces without configured IP address - 1:1 NAT """ |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2879 | |
| 2880 | self.vapi.ip_neighbor_add_del(self.pg7.sw_if_index, |
Ole Troan | 895b6e8 | 2017-10-20 13:28:20 +0200 | [diff] [blame] | 2881 | mactobinary(self.pg7.remote_mac), |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2882 | self.pg7.remote_ip4n, |
| 2883 | is_static=1) |
| 2884 | self.vapi.ip_neighbor_add_del(self.pg8.sw_if_index, |
Ole Troan | 895b6e8 | 2017-10-20 13:28:20 +0200 | [diff] [blame] | 2885 | mactobinary(self.pg8.remote_mac), |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2886 | self.pg8.remote_ip4n, |
| 2887 | is_static=1) |
| 2888 | |
| 2889 | self.vapi.ip_add_del_route(dst_address=self.pg7.remote_ip4n, |
| 2890 | dst_address_length=32, |
| 2891 | next_hop_address=self.pg7.remote_ip4n, |
| 2892 | next_hop_sw_if_index=self.pg7.sw_if_index) |
| 2893 | self.vapi.ip_add_del_route(dst_address=self.pg8.remote_ip4n, |
| 2894 | dst_address_length=32, |
| 2895 | next_hop_address=self.pg8.remote_ip4n, |
| 2896 | next_hop_sw_if_index=self.pg8.sw_if_index) |
| 2897 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2898 | self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr) |
| 2899 | self.vapi.nat44_interface_add_del_feature(self.pg7.sw_if_index) |
| 2900 | self.vapi.nat44_interface_add_del_feature(self.pg8.sw_if_index, |
| 2901 | is_inside=0) |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2902 | |
| 2903 | # out2in |
| 2904 | pkts = self.create_stream_out(self.pg8) |
| 2905 | self.pg8.add_stream(pkts) |
| 2906 | self.pg_enable_capture(self.pg_interfaces) |
| 2907 | self.pg_start() |
| 2908 | capture = self.pg7.get_capture(len(pkts)) |
| 2909 | self.verify_capture_in(capture, self.pg7) |
| 2910 | |
| 2911 | # in2out |
| 2912 | pkts = self.create_stream_in(self.pg7, self.pg8) |
| 2913 | self.pg7.add_stream(pkts) |
| 2914 | self.pg_enable_capture(self.pg_interfaces) |
| 2915 | self.pg_start() |
| 2916 | capture = self.pg8.get_capture(len(pkts)) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2917 | self.verify_capture_out(capture, self.nat_addr, True) |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2918 | |
| 2919 | def test_static_with_port_ipless_interfaces(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2920 | """ NAT44 interfaces without configured IP address - 1:1 NAPT """ |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2921 | |
| 2922 | self.tcp_port_out = 30606 |
| 2923 | self.udp_port_out = 30607 |
| 2924 | self.icmp_id_out = 30608 |
| 2925 | |
| 2926 | self.vapi.ip_neighbor_add_del(self.pg7.sw_if_index, |
Ole Troan | 895b6e8 | 2017-10-20 13:28:20 +0200 | [diff] [blame] | 2927 | mactobinary(self.pg7.remote_mac), |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2928 | self.pg7.remote_ip4n, |
| 2929 | is_static=1) |
| 2930 | self.vapi.ip_neighbor_add_del(self.pg8.sw_if_index, |
Ole Troan | 895b6e8 | 2017-10-20 13:28:20 +0200 | [diff] [blame] | 2931 | mactobinary(self.pg8.remote_mac), |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2932 | self.pg8.remote_ip4n, |
| 2933 | is_static=1) |
| 2934 | |
| 2935 | self.vapi.ip_add_del_route(dst_address=self.pg7.remote_ip4n, |
| 2936 | dst_address_length=32, |
| 2937 | next_hop_address=self.pg7.remote_ip4n, |
| 2938 | next_hop_sw_if_index=self.pg7.sw_if_index) |
| 2939 | self.vapi.ip_add_del_route(dst_address=self.pg8.remote_ip4n, |
| 2940 | dst_address_length=32, |
| 2941 | next_hop_address=self.pg8.remote_ip4n, |
| 2942 | next_hop_sw_if_index=self.pg8.sw_if_index) |
| 2943 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2944 | self.nat44_add_address(self.nat_addr) |
| 2945 | self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr, |
| 2946 | self.tcp_port_in, self.tcp_port_out, |
| 2947 | proto=IP_PROTOS.tcp) |
| 2948 | self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr, |
| 2949 | self.udp_port_in, self.udp_port_out, |
| 2950 | proto=IP_PROTOS.udp) |
| 2951 | self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr, |
| 2952 | self.icmp_id_in, self.icmp_id_out, |
| 2953 | proto=IP_PROTOS.icmp) |
| 2954 | self.vapi.nat44_interface_add_del_feature(self.pg7.sw_if_index) |
| 2955 | self.vapi.nat44_interface_add_del_feature(self.pg8.sw_if_index, |
| 2956 | is_inside=0) |
Martin Gálik | 406eb1d | 2017-05-04 04:35:04 -0700 | [diff] [blame] | 2957 | |
| 2958 | # out2in |
| 2959 | pkts = self.create_stream_out(self.pg8) |
| 2960 | self.pg8.add_stream(pkts) |
| 2961 | self.pg_enable_capture(self.pg_interfaces) |
| 2962 | self.pg_start() |
| 2963 | capture = self.pg7.get_capture(len(pkts)) |
| 2964 | self.verify_capture_in(capture, self.pg7) |
| 2965 | |
| 2966 | # in2out |
| 2967 | pkts = self.create_stream_in(self.pg7, self.pg8) |
| 2968 | self.pg7.add_stream(pkts) |
| 2969 | self.pg_enable_capture(self.pg_interfaces) |
| 2970 | self.pg_start() |
| 2971 | capture = self.pg8.get_capture(len(pkts)) |
| 2972 | self.verify_capture_out(capture) |
| 2973 | |
Matus Fabian | 328dbc8 | 2017-06-19 04:28:04 -0700 | [diff] [blame] | 2974 | def test_static_unknown_proto(self): |
| 2975 | """ 1:1 NAT translate packet with unknown protocol """ |
| 2976 | nat_ip = "10.0.0.10" |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2977 | self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip) |
| 2978 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 2979 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 2980 | is_inside=0) |
Matus Fabian | 328dbc8 | 2017-06-19 04:28:04 -0700 | [diff] [blame] | 2981 | |
| 2982 | # in2out |
| 2983 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 2984 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 2985 | GRE() / |
Matus Fabian | 1e8db5f | 2017-06-20 01:45:49 -0700 | [diff] [blame] | 2986 | IP(src=self.pg2.remote_ip4, dst=self.pg3.remote_ip4) / |
Matus Fabian | 328dbc8 | 2017-06-19 04:28:04 -0700 | [diff] [blame] | 2987 | TCP(sport=1234, dport=1234)) |
| 2988 | self.pg0.add_stream(p) |
| 2989 | self.pg_enable_capture(self.pg_interfaces) |
| 2990 | self.pg_start() |
| 2991 | p = self.pg1.get_capture(1) |
| 2992 | packet = p[0] |
| 2993 | try: |
| 2994 | self.assertEqual(packet[IP].src, nat_ip) |
| 2995 | self.assertEqual(packet[IP].dst, self.pg1.remote_ip4) |
| 2996 | self.assertTrue(packet.haslayer(GRE)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 2997 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 328dbc8 | 2017-06-19 04:28:04 -0700 | [diff] [blame] | 2998 | except: |
| 2999 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 3000 | raise |
| 3001 | |
| 3002 | # out2in |
| 3003 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 3004 | IP(src=self.pg1.remote_ip4, dst=nat_ip) / |
| 3005 | GRE() / |
Matus Fabian | 1e8db5f | 2017-06-20 01:45:49 -0700 | [diff] [blame] | 3006 | IP(src=self.pg3.remote_ip4, dst=self.pg2.remote_ip4) / |
Matus Fabian | 328dbc8 | 2017-06-19 04:28:04 -0700 | [diff] [blame] | 3007 | TCP(sport=1234, dport=1234)) |
| 3008 | self.pg1.add_stream(p) |
| 3009 | self.pg_enable_capture(self.pg_interfaces) |
| 3010 | self.pg_start() |
| 3011 | p = self.pg0.get_capture(1) |
| 3012 | packet = p[0] |
| 3013 | try: |
| 3014 | self.assertEqual(packet[IP].src, self.pg1.remote_ip4) |
| 3015 | self.assertEqual(packet[IP].dst, self.pg0.remote_ip4) |
| 3016 | self.assertTrue(packet.haslayer(GRE)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 3017 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 328dbc8 | 2017-06-19 04:28:04 -0700 | [diff] [blame] | 3018 | except: |
| 3019 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 3020 | raise |
| 3021 | |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 3022 | def test_hairpinning_static_unknown_proto(self): |
Matus Fabian | 1e8db5f | 2017-06-20 01:45:49 -0700 | [diff] [blame] | 3023 | """ 1:1 NAT translate packet with unknown protocol - hairpinning """ |
| 3024 | |
| 3025 | host = self.pg0.remote_hosts[0] |
| 3026 | server = self.pg0.remote_hosts[1] |
| 3027 | |
| 3028 | host_nat_ip = "10.0.0.10" |
| 3029 | server_nat_ip = "10.0.0.11" |
| 3030 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3031 | self.nat44_add_static_mapping(host.ip4, host_nat_ip) |
| 3032 | self.nat44_add_static_mapping(server.ip4, server_nat_ip) |
| 3033 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3034 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3035 | is_inside=0) |
Matus Fabian | 1e8db5f | 2017-06-20 01:45:49 -0700 | [diff] [blame] | 3036 | |
| 3037 | # host to server |
| 3038 | p = (Ether(dst=self.pg0.local_mac, src=host.mac) / |
| 3039 | IP(src=host.ip4, dst=server_nat_ip) / |
| 3040 | GRE() / |
| 3041 | IP(src=self.pg2.remote_ip4, dst=self.pg3.remote_ip4) / |
| 3042 | TCP(sport=1234, dport=1234)) |
| 3043 | self.pg0.add_stream(p) |
| 3044 | self.pg_enable_capture(self.pg_interfaces) |
| 3045 | self.pg_start() |
| 3046 | p = self.pg0.get_capture(1) |
| 3047 | packet = p[0] |
| 3048 | try: |
| 3049 | self.assertEqual(packet[IP].src, host_nat_ip) |
| 3050 | self.assertEqual(packet[IP].dst, server.ip4) |
| 3051 | self.assertTrue(packet.haslayer(GRE)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 3052 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 1e8db5f | 2017-06-20 01:45:49 -0700 | [diff] [blame] | 3053 | except: |
| 3054 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 3055 | raise |
| 3056 | |
| 3057 | # server to host |
| 3058 | p = (Ether(dst=self.pg0.local_mac, src=server.mac) / |
| 3059 | IP(src=server.ip4, dst=host_nat_ip) / |
| 3060 | GRE() / |
| 3061 | IP(src=self.pg3.remote_ip4, dst=self.pg2.remote_ip4) / |
| 3062 | TCP(sport=1234, dport=1234)) |
| 3063 | self.pg0.add_stream(p) |
| 3064 | self.pg_enable_capture(self.pg_interfaces) |
| 3065 | self.pg_start() |
| 3066 | p = self.pg0.get_capture(1) |
| 3067 | packet = p[0] |
| 3068 | try: |
| 3069 | self.assertEqual(packet[IP].src, server_nat_ip) |
| 3070 | self.assertEqual(packet[IP].dst, host.ip4) |
| 3071 | self.assertTrue(packet.haslayer(GRE)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 3072 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 1e8db5f | 2017-06-20 01:45:49 -0700 | [diff] [blame] | 3073 | except: |
| 3074 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 3075 | raise |
| 3076 | |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3077 | def test_output_feature(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3078 | """ NAT44 interface output feature (in2out postrouting) """ |
| 3079 | self.nat44_add_address(self.nat_addr) |
| 3080 | self.vapi.nat44_interface_add_del_output_feature(self.pg0.sw_if_index) |
Matus Fabian | b2d2fc7 | 2017-09-10 22:17:47 -0700 | [diff] [blame] | 3081 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index) |
| 3082 | self.vapi.nat44_interface_add_del_output_feature(self.pg3.sw_if_index, |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3083 | is_inside=0) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3084 | |
| 3085 | # in2out |
Matus Fabian | b2d2fc7 | 2017-09-10 22:17:47 -0700 | [diff] [blame] | 3086 | pkts = self.create_stream_in(self.pg0, self.pg3) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3087 | self.pg0.add_stream(pkts) |
| 3088 | self.pg_enable_capture(self.pg_interfaces) |
| 3089 | self.pg_start() |
Matus Fabian | b2d2fc7 | 2017-09-10 22:17:47 -0700 | [diff] [blame] | 3090 | capture = self.pg3.get_capture(len(pkts)) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3091 | self.verify_capture_out(capture) |
| 3092 | |
| 3093 | # out2in |
Matus Fabian | b2d2fc7 | 2017-09-10 22:17:47 -0700 | [diff] [blame] | 3094 | pkts = self.create_stream_out(self.pg3) |
| 3095 | self.pg3.add_stream(pkts) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3096 | self.pg_enable_capture(self.pg_interfaces) |
| 3097 | self.pg_start() |
| 3098 | capture = self.pg0.get_capture(len(pkts)) |
| 3099 | self.verify_capture_in(capture, self.pg0) |
| 3100 | |
Matus Fabian | b2d2fc7 | 2017-09-10 22:17:47 -0700 | [diff] [blame] | 3101 | # from non-NAT interface to NAT inside interface |
| 3102 | pkts = self.create_stream_in(self.pg2, self.pg0) |
| 3103 | self.pg2.add_stream(pkts) |
| 3104 | self.pg_enable_capture(self.pg_interfaces) |
| 3105 | self.pg_start() |
| 3106 | capture = self.pg0.get_capture(len(pkts)) |
| 3107 | self.verify_capture_no_translation(capture, self.pg2, self.pg0) |
| 3108 | |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3109 | def test_output_feature_vrf_aware(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3110 | """ NAT44 interface output feature VRF aware (in2out postrouting) """ |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3111 | nat_ip_vrf10 = "10.0.0.10" |
| 3112 | nat_ip_vrf20 = "10.0.0.20" |
| 3113 | |
| 3114 | self.vapi.ip_add_del_route(dst_address=self.pg3.remote_ip4n, |
| 3115 | dst_address_length=32, |
| 3116 | next_hop_address=self.pg3.remote_ip4n, |
| 3117 | next_hop_sw_if_index=self.pg3.sw_if_index, |
| 3118 | table_id=10) |
| 3119 | self.vapi.ip_add_del_route(dst_address=self.pg3.remote_ip4n, |
| 3120 | dst_address_length=32, |
| 3121 | next_hop_address=self.pg3.remote_ip4n, |
| 3122 | next_hop_sw_if_index=self.pg3.sw_if_index, |
| 3123 | table_id=20) |
| 3124 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3125 | self.nat44_add_address(nat_ip_vrf10, vrf_id=10) |
| 3126 | self.nat44_add_address(nat_ip_vrf20, vrf_id=20) |
| 3127 | self.vapi.nat44_interface_add_del_output_feature(self.pg4.sw_if_index) |
| 3128 | self.vapi.nat44_interface_add_del_output_feature(self.pg6.sw_if_index) |
| 3129 | self.vapi.nat44_interface_add_del_output_feature(self.pg3.sw_if_index, |
| 3130 | is_inside=0) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3131 | |
| 3132 | # in2out VRF 10 |
| 3133 | pkts = self.create_stream_in(self.pg4, self.pg3) |
| 3134 | self.pg4.add_stream(pkts) |
| 3135 | self.pg_enable_capture(self.pg_interfaces) |
| 3136 | self.pg_start() |
| 3137 | capture = self.pg3.get_capture(len(pkts)) |
| 3138 | self.verify_capture_out(capture, nat_ip=nat_ip_vrf10) |
| 3139 | |
| 3140 | # out2in VRF 10 |
| 3141 | pkts = self.create_stream_out(self.pg3, dst_ip=nat_ip_vrf10) |
| 3142 | self.pg3.add_stream(pkts) |
| 3143 | self.pg_enable_capture(self.pg_interfaces) |
| 3144 | self.pg_start() |
| 3145 | capture = self.pg4.get_capture(len(pkts)) |
| 3146 | self.verify_capture_in(capture, self.pg4) |
| 3147 | |
| 3148 | # in2out VRF 20 |
| 3149 | pkts = self.create_stream_in(self.pg6, self.pg3) |
| 3150 | self.pg6.add_stream(pkts) |
| 3151 | self.pg_enable_capture(self.pg_interfaces) |
| 3152 | self.pg_start() |
| 3153 | capture = self.pg3.get_capture(len(pkts)) |
| 3154 | self.verify_capture_out(capture, nat_ip=nat_ip_vrf20) |
| 3155 | |
| 3156 | # out2in VRF 20 |
| 3157 | pkts = self.create_stream_out(self.pg3, dst_ip=nat_ip_vrf20) |
| 3158 | self.pg3.add_stream(pkts) |
| 3159 | self.pg_enable_capture(self.pg_interfaces) |
| 3160 | self.pg_start() |
| 3161 | capture = self.pg6.get_capture(len(pkts)) |
| 3162 | self.verify_capture_in(capture, self.pg6) |
| 3163 | |
Matus Fabian | 161c59c | 2017-07-21 03:46:03 -0700 | [diff] [blame] | 3164 | def test_output_feature_hairpinning(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3165 | """ NAT44 interface output feature hairpinning (in2out postrouting) """ |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3166 | host = self.pg0.remote_hosts[0] |
| 3167 | server = self.pg0.remote_hosts[1] |
| 3168 | host_in_port = 1234 |
| 3169 | host_out_port = 0 |
| 3170 | server_in_port = 5678 |
| 3171 | server_out_port = 8765 |
| 3172 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3173 | self.nat44_add_address(self.nat_addr) |
| 3174 | self.vapi.nat44_interface_add_del_output_feature(self.pg0.sw_if_index) |
| 3175 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 3176 | is_inside=0) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3177 | |
| 3178 | # add static mapping for server |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3179 | self.nat44_add_static_mapping(server.ip4, self.nat_addr, |
| 3180 | server_in_port, server_out_port, |
| 3181 | proto=IP_PROTOS.tcp) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3182 | |
| 3183 | # send packet from host to server |
| 3184 | p = (Ether(src=host.mac, dst=self.pg0.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3185 | IP(src=host.ip4, dst=self.nat_addr) / |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3186 | TCP(sport=host_in_port, dport=server_out_port)) |
| 3187 | self.pg0.add_stream(p) |
| 3188 | self.pg_enable_capture(self.pg_interfaces) |
| 3189 | self.pg_start() |
| 3190 | capture = self.pg0.get_capture(1) |
| 3191 | p = capture[0] |
| 3192 | try: |
| 3193 | ip = p[IP] |
| 3194 | tcp = p[TCP] |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3195 | self.assertEqual(ip.src, self.nat_addr) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3196 | self.assertEqual(ip.dst, server.ip4) |
| 3197 | self.assertNotEqual(tcp.sport, host_in_port) |
| 3198 | self.assertEqual(tcp.dport, server_in_port) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 3199 | self.assert_packet_checksums_valid(p) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3200 | host_out_port = tcp.sport |
| 3201 | except: |
| 3202 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 3203 | raise |
| 3204 | |
| 3205 | # send reply from server to host |
| 3206 | p = (Ether(src=server.mac, dst=self.pg0.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3207 | IP(src=server.ip4, dst=self.nat_addr) / |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3208 | TCP(sport=server_in_port, dport=host_out_port)) |
| 3209 | self.pg0.add_stream(p) |
| 3210 | self.pg_enable_capture(self.pg_interfaces) |
| 3211 | self.pg_start() |
| 3212 | capture = self.pg0.get_capture(1) |
| 3213 | p = capture[0] |
| 3214 | try: |
| 3215 | ip = p[IP] |
| 3216 | tcp = p[TCP] |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3217 | self.assertEqual(ip.src, self.nat_addr) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3218 | self.assertEqual(ip.dst, host.ip4) |
| 3219 | self.assertEqual(tcp.sport, server_out_port) |
| 3220 | self.assertEqual(tcp.dport, host_in_port) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 3221 | self.assert_packet_checksums_valid(p) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3222 | except: |
Matus Fabian | e82488f | 2018-01-18 03:38:45 -0800 | [diff] [blame] | 3223 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3224 | raise |
| 3225 | |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 3226 | def test_one_armed_nat44(self): |
| 3227 | """ One armed NAT44 """ |
| 3228 | remote_host = self.pg9.remote_hosts[0] |
| 3229 | local_host = self.pg9.remote_hosts[1] |
| 3230 | external_port = 0 |
| 3231 | |
| 3232 | self.nat44_add_address(self.nat_addr) |
| 3233 | self.vapi.nat44_interface_add_del_feature(self.pg9.sw_if_index) |
| 3234 | self.vapi.nat44_interface_add_del_feature(self.pg9.sw_if_index, |
| 3235 | is_inside=0) |
| 3236 | |
| 3237 | # in2out |
| 3238 | p = (Ether(src=self.pg9.remote_mac, dst=self.pg9.local_mac) / |
| 3239 | IP(src=local_host.ip4, dst=remote_host.ip4) / |
| 3240 | TCP(sport=12345, dport=80)) |
| 3241 | self.pg9.add_stream(p) |
| 3242 | self.pg_enable_capture(self.pg_interfaces) |
| 3243 | self.pg_start() |
| 3244 | capture = self.pg9.get_capture(1) |
| 3245 | p = capture[0] |
| 3246 | try: |
| 3247 | ip = p[IP] |
| 3248 | tcp = p[TCP] |
| 3249 | self.assertEqual(ip.src, self.nat_addr) |
| 3250 | self.assertEqual(ip.dst, remote_host.ip4) |
| 3251 | self.assertNotEqual(tcp.sport, 12345) |
| 3252 | external_port = tcp.sport |
| 3253 | self.assertEqual(tcp.dport, 80) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 3254 | self.assert_packet_checksums_valid(p) |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 3255 | except: |
| 3256 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 3257 | raise |
| 3258 | |
| 3259 | # out2in |
| 3260 | p = (Ether(src=self.pg9.remote_mac, dst=self.pg9.local_mac) / |
| 3261 | IP(src=remote_host.ip4, dst=self.nat_addr) / |
| 3262 | TCP(sport=80, dport=external_port)) |
| 3263 | self.pg9.add_stream(p) |
| 3264 | self.pg_enable_capture(self.pg_interfaces) |
| 3265 | self.pg_start() |
| 3266 | capture = self.pg9.get_capture(1) |
| 3267 | p = capture[0] |
| 3268 | try: |
| 3269 | ip = p[IP] |
| 3270 | tcp = p[TCP] |
| 3271 | self.assertEqual(ip.src, remote_host.ip4) |
| 3272 | self.assertEqual(ip.dst, local_host.ip4) |
| 3273 | self.assertEqual(tcp.sport, 80) |
| 3274 | self.assertEqual(tcp.dport, 12345) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 3275 | self.assert_packet_checksums_valid(p) |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 3276 | except: |
| 3277 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 3278 | raise |
| 3279 | |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3280 | def test_del_session(self): |
| 3281 | """ Delete NAT44 session """ |
| 3282 | self.nat44_add_address(self.nat_addr) |
| 3283 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3284 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3285 | is_inside=0) |
| 3286 | |
| 3287 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 3288 | self.pg0.add_stream(pkts) |
| 3289 | self.pg_enable_capture(self.pg_interfaces) |
| 3290 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 3291 | self.pg1.get_capture(len(pkts)) |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3292 | |
| 3293 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 3294 | nsessions = len(sessions) |
| 3295 | |
| 3296 | self.vapi.nat44_del_session(sessions[0].inside_ip_address, |
| 3297 | sessions[0].inside_port, |
| 3298 | sessions[0].protocol) |
| 3299 | self.vapi.nat44_del_session(sessions[1].outside_ip_address, |
| 3300 | sessions[1].outside_port, |
| 3301 | sessions[1].protocol, |
| 3302 | is_in=0) |
| 3303 | |
| 3304 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 3305 | self.assertEqual(nsessions - len(sessions), 2) |
| 3306 | |
Matus Fabian | 68ba880 | 2018-08-08 05:52:47 -0700 | [diff] [blame] | 3307 | self.vapi.nat44_del_session(sessions[0].inside_ip_address, |
| 3308 | sessions[0].inside_port, |
| 3309 | sessions[0].protocol) |
| 3310 | |
| 3311 | self.verify_no_nat44_user() |
| 3312 | |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3313 | def test_set_get_reass(self): |
| 3314 | """ NAT44 set/get virtual fragmentation reassembly """ |
| 3315 | reas_cfg1 = self.vapi.nat_get_reass() |
| 3316 | |
| 3317 | self.vapi.nat_set_reass(timeout=reas_cfg1.ip4_timeout + 5, |
| 3318 | max_reass=reas_cfg1.ip4_max_reass * 2, |
| 3319 | max_frag=reas_cfg1.ip4_max_frag * 2) |
| 3320 | |
| 3321 | reas_cfg2 = self.vapi.nat_get_reass() |
| 3322 | |
| 3323 | self.assertEqual(reas_cfg1.ip4_timeout + 5, reas_cfg2.ip4_timeout) |
| 3324 | self.assertEqual(reas_cfg1.ip4_max_reass * 2, reas_cfg2.ip4_max_reass) |
| 3325 | self.assertEqual(reas_cfg1.ip4_max_frag * 2, reas_cfg2.ip4_max_frag) |
| 3326 | |
| 3327 | self.vapi.nat_set_reass(drop_frag=1) |
| 3328 | self.assertTrue(self.vapi.nat_get_reass().ip4_drop_frag) |
| 3329 | |
| 3330 | def test_frag_in_order(self): |
| 3331 | """ NAT44 translate fragments arriving in order """ |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 3332 | |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3333 | self.nat44_add_address(self.nat_addr) |
| 3334 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3335 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3336 | is_inside=0) |
| 3337 | |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame^] | 3338 | self.frag_in_order(proto=IP_PROTOS.tcp) |
| 3339 | self.frag_in_order(proto=IP_PROTOS.udp) |
| 3340 | self.frag_in_order(proto=IP_PROTOS.icmp) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3341 | |
| 3342 | def test_reass_hairpinning(self): |
| 3343 | """ NAT44 fragments hairpinning """ |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 3344 | |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame^] | 3345 | self.server = self.pg0.remote_hosts[1] |
| 3346 | self.host_in_port = random.randint(1025, 65535) |
| 3347 | self.server_in_port = random.randint(1025, 65535) |
| 3348 | self.server_out_port = random.randint(1025, 65535) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3349 | |
| 3350 | self.nat44_add_address(self.nat_addr) |
| 3351 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3352 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3353 | is_inside=0) |
| 3354 | # add static mapping for server |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame^] | 3355 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr, |
| 3356 | self.server_in_port, |
| 3357 | self.server_out_port, |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3358 | proto=IP_PROTOS.tcp) |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame^] | 3359 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr, |
| 3360 | self.server_in_port, |
| 3361 | self.server_out_port, |
| 3362 | proto=IP_PROTOS.udp) |
| 3363 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3364 | |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame^] | 3365 | self.reass_hairpinning(proto=IP_PROTOS.tcp) |
| 3366 | self.reass_hairpinning(proto=IP_PROTOS.udp) |
| 3367 | self.reass_hairpinning(proto=IP_PROTOS.icmp) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3368 | |
| 3369 | def test_frag_out_of_order(self): |
| 3370 | """ NAT44 translate fragments arriving out of order """ |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 3371 | |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3372 | self.nat44_add_address(self.nat_addr) |
| 3373 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3374 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3375 | is_inside=0) |
| 3376 | |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame^] | 3377 | self.frag_out_of_order(proto=IP_PROTOS.tcp) |
| 3378 | self.frag_out_of_order(proto=IP_PROTOS.udp) |
| 3379 | self.frag_out_of_order(proto=IP_PROTOS.icmp) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3380 | |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 3381 | def test_port_restricted(self): |
| 3382 | """ Port restricted NAT44 (MAP-E CE) """ |
| 3383 | self.nat44_add_address(self.nat_addr) |
| 3384 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3385 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3386 | is_inside=0) |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 3387 | self.vapi.nat_set_addr_and_port_alloc_alg(alg=1, |
| 3388 | psid_offset=6, |
| 3389 | psid_length=6, |
| 3390 | psid=10) |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 3391 | |
| 3392 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 3393 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 3394 | TCP(sport=4567, dport=22)) |
| 3395 | self.pg0.add_stream(p) |
| 3396 | self.pg_enable_capture(self.pg_interfaces) |
| 3397 | self.pg_start() |
| 3398 | capture = self.pg1.get_capture(1) |
| 3399 | p = capture[0] |
| 3400 | try: |
| 3401 | ip = p[IP] |
| 3402 | tcp = p[TCP] |
| 3403 | self.assertEqual(ip.dst, self.pg1.remote_ip4) |
| 3404 | self.assertEqual(ip.src, self.nat_addr) |
| 3405 | self.assertEqual(tcp.dport, 22) |
| 3406 | self.assertNotEqual(tcp.sport, 4567) |
| 3407 | self.assertEqual((tcp.sport >> 6) & 63, 10) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 3408 | self.assert_packet_checksums_valid(p) |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 3409 | except: |
| 3410 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 3411 | raise |
| 3412 | |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 3413 | def test_port_range(self): |
| 3414 | """ External address port range """ |
| 3415 | self.nat44_add_address(self.nat_addr) |
| 3416 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3417 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3418 | is_inside=0) |
| 3419 | self.vapi.nat_set_addr_and_port_alloc_alg(alg=2, |
| 3420 | start_port=1025, |
| 3421 | end_port=1027) |
| 3422 | |
| 3423 | pkts = [] |
| 3424 | for port in range(0, 5): |
| 3425 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 3426 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 3427 | TCP(sport=1125 + port)) |
| 3428 | pkts.append(p) |
| 3429 | self.pg0.add_stream(pkts) |
| 3430 | self.pg_enable_capture(self.pg_interfaces) |
| 3431 | self.pg_start() |
| 3432 | capture = self.pg1.get_capture(3) |
| 3433 | for p in capture: |
| 3434 | tcp = p[TCP] |
| 3435 | self.assertGreaterEqual(tcp.sport, 1025) |
| 3436 | self.assertLessEqual(tcp.sport, 1027) |
| 3437 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3438 | def test_ipfix_max_frags(self): |
| 3439 | """ IPFIX logging maximum fragments pending reassembly exceeded """ |
| 3440 | self.nat44_add_address(self.nat_addr) |
| 3441 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3442 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3443 | is_inside=0) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 3444 | self.vapi.nat_set_reass(max_frag=1) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3445 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 3446 | src_address=self.pg3.local_ip4n, |
| 3447 | path_mtu=512, |
| 3448 | template_interval=10) |
| 3449 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 3450 | src_port=self.ipfix_src_port) |
| 3451 | |
| 3452 | data = "A" * 4 + "B" * 16 + "C" * 3 |
| 3453 | self.tcp_port_in = random.randint(1025, 65535) |
| 3454 | pkts = self.create_stream_frag(self.pg0, |
| 3455 | self.pg1.remote_ip4, |
| 3456 | self.tcp_port_in, |
| 3457 | 20, |
| 3458 | data) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 3459 | pkts.reverse() |
| 3460 | self.pg0.add_stream(pkts) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3461 | self.pg_enable_capture(self.pg_interfaces) |
| 3462 | self.pg_start() |
| 3463 | self.pg1.assert_nothing_captured() |
| 3464 | sleep(1) |
| 3465 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 3466 | capture = self.pg3.get_capture(9) |
| 3467 | ipfix = IPFIXDecoder() |
| 3468 | # first load template |
| 3469 | for p in capture: |
| 3470 | self.assertTrue(p.haslayer(IPFIX)) |
| 3471 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 3472 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 3473 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 3474 | self.assertEqual(p[UDP].dport, 4739) |
| 3475 | self.assertEqual(p[IPFIX].observationDomainID, |
| 3476 | self.ipfix_domain_id) |
| 3477 | if p.haslayer(Template): |
| 3478 | ipfix.add_template(p.getlayer(Template)) |
| 3479 | # verify events in data set |
| 3480 | for p in capture: |
| 3481 | if p.haslayer(Data): |
| 3482 | data = ipfix.decode_data_set(p.getlayer(Set)) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 3483 | self.verify_ipfix_max_fragments_ip4(data, 1, |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3484 | self.pg0.remote_ip4n) |
| 3485 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 3486 | def test_multiple_outside_vrf(self): |
| 3487 | """ Multiple outside VRF """ |
| 3488 | vrf_id1 = 1 |
| 3489 | vrf_id2 = 2 |
| 3490 | |
| 3491 | self.pg1.unconfig_ip4() |
| 3492 | self.pg2.unconfig_ip4() |
| 3493 | self.vapi.ip_table_add_del(vrf_id1, is_add=1) |
| 3494 | self.vapi.ip_table_add_del(vrf_id2, is_add=1) |
| 3495 | self.pg1.set_table_ip4(vrf_id1) |
| 3496 | self.pg2.set_table_ip4(vrf_id2) |
| 3497 | self.pg1.config_ip4() |
| 3498 | self.pg2.config_ip4() |
| 3499 | self.pg1.resolve_arp() |
| 3500 | self.pg2.resolve_arp() |
| 3501 | |
| 3502 | self.nat44_add_address(self.nat_addr) |
| 3503 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3504 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3505 | is_inside=0) |
| 3506 | self.vapi.nat44_interface_add_del_feature(self.pg2.sw_if_index, |
| 3507 | is_inside=0) |
| 3508 | |
| 3509 | try: |
| 3510 | # first VRF |
| 3511 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 3512 | self.pg0.add_stream(pkts) |
| 3513 | self.pg_enable_capture(self.pg_interfaces) |
| 3514 | self.pg_start() |
| 3515 | capture = self.pg1.get_capture(len(pkts)) |
| 3516 | self.verify_capture_out(capture, self.nat_addr) |
| 3517 | |
| 3518 | pkts = self.create_stream_out(self.pg1, self.nat_addr) |
| 3519 | self.pg1.add_stream(pkts) |
| 3520 | self.pg_enable_capture(self.pg_interfaces) |
| 3521 | self.pg_start() |
| 3522 | capture = self.pg0.get_capture(len(pkts)) |
| 3523 | self.verify_capture_in(capture, self.pg0) |
| 3524 | |
| 3525 | self.tcp_port_in = 60303 |
| 3526 | self.udp_port_in = 60304 |
| 3527 | self.icmp_id_in = 60305 |
| 3528 | |
| 3529 | # second VRF |
| 3530 | pkts = self.create_stream_in(self.pg0, self.pg2) |
| 3531 | self.pg0.add_stream(pkts) |
| 3532 | self.pg_enable_capture(self.pg_interfaces) |
| 3533 | self.pg_start() |
| 3534 | capture = self.pg2.get_capture(len(pkts)) |
| 3535 | self.verify_capture_out(capture, self.nat_addr) |
| 3536 | |
| 3537 | pkts = self.create_stream_out(self.pg2, self.nat_addr) |
| 3538 | self.pg2.add_stream(pkts) |
| 3539 | self.pg_enable_capture(self.pg_interfaces) |
| 3540 | self.pg_start() |
| 3541 | capture = self.pg0.get_capture(len(pkts)) |
| 3542 | self.verify_capture_in(capture, self.pg0) |
| 3543 | |
| 3544 | finally: |
| 3545 | self.pg1.unconfig_ip4() |
| 3546 | self.pg2.unconfig_ip4() |
| 3547 | self.pg1.set_table_ip4(0) |
| 3548 | self.pg2.set_table_ip4(0) |
| 3549 | self.pg1.config_ip4() |
| 3550 | self.pg2.config_ip4() |
| 3551 | self.pg1.resolve_arp() |
| 3552 | self.pg2.resolve_arp() |
| 3553 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 3554 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 3555 | def test_session_timeout(self): |
| 3556 | """ NAT44 session timeouts """ |
| 3557 | self.nat44_add_address(self.nat_addr) |
| 3558 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3559 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3560 | is_inside=0) |
| 3561 | self.vapi.nat_set_timeouts(udp=5) |
| 3562 | |
| 3563 | max_sessions = 1000 |
| 3564 | pkts = [] |
| 3565 | for i in range(0, max_sessions): |
| 3566 | src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF) |
| 3567 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 3568 | IP(src=src, dst=self.pg1.remote_ip4) / |
| 3569 | UDP(sport=1025, dport=53)) |
| 3570 | pkts.append(p) |
| 3571 | self.pg0.add_stream(pkts) |
| 3572 | self.pg_enable_capture(self.pg_interfaces) |
| 3573 | self.pg_start() |
| 3574 | self.pg1.get_capture(max_sessions) |
| 3575 | |
| 3576 | sleep(6) |
| 3577 | |
| 3578 | pkts = [] |
| 3579 | for i in range(0, max_sessions): |
| 3580 | src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF) |
| 3581 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 3582 | IP(src=src, dst=self.pg1.remote_ip4) / |
| 3583 | UDP(sport=1026, dport=53)) |
| 3584 | pkts.append(p) |
| 3585 | self.pg0.add_stream(pkts) |
| 3586 | self.pg_enable_capture(self.pg_interfaces) |
| 3587 | self.pg_start() |
| 3588 | self.pg1.get_capture(max_sessions) |
| 3589 | |
| 3590 | nsessions = 0 |
| 3591 | users = self.vapi.nat44_user_dump() |
| 3592 | for user in users: |
| 3593 | nsessions = nsessions + user.nsessions |
| 3594 | self.assertLess(nsessions, 2 * max_sessions) |
| 3595 | |
Matus Fabian | bb4e022 | 2018-09-13 02:36:25 -0700 | [diff] [blame] | 3596 | def test_mss_clamping(self): |
| 3597 | """ TCP MSS clamping """ |
| 3598 | self.nat44_add_address(self.nat_addr) |
| 3599 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3600 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3601 | is_inside=0) |
| 3602 | |
| 3603 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 3604 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 3605 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 3606 | flags="S", options=[('MSS', 1400)])) |
| 3607 | |
| 3608 | self.vapi.nat_set_mss_clamping(enable=1, mss_value=1000) |
| 3609 | self.pg0.add_stream(p) |
| 3610 | self.pg_enable_capture(self.pg_interfaces) |
| 3611 | self.pg_start() |
| 3612 | capture = self.pg1.get_capture(1) |
| 3613 | # Negotiated MSS value greater than configured - changed |
| 3614 | self.verify_mss_value(capture[0], 1000) |
| 3615 | |
| 3616 | self.vapi.nat_set_mss_clamping(enable=0) |
| 3617 | self.pg0.add_stream(p) |
| 3618 | self.pg_enable_capture(self.pg_interfaces) |
| 3619 | self.pg_start() |
| 3620 | capture = self.pg1.get_capture(1) |
| 3621 | # MSS clamping disabled - negotiated MSS unchanged |
| 3622 | self.verify_mss_value(capture[0], 1400) |
| 3623 | |
| 3624 | self.vapi.nat_set_mss_clamping(enable=1, mss_value=1500) |
| 3625 | self.pg0.add_stream(p) |
| 3626 | self.pg_enable_capture(self.pg_interfaces) |
| 3627 | self.pg_start() |
| 3628 | capture = self.pg1.get_capture(1) |
| 3629 | # Negotiated MSS value smaller than configured - unchanged |
| 3630 | self.verify_mss_value(capture[0], 1400) |
| 3631 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3632 | def tearDown(self): |
| 3633 | super(TestNAT44, self).tearDown() |
| 3634 | if not self.vpp_dead: |
| 3635 | self.logger.info(self.vapi.cli("show nat44 addresses")) |
| 3636 | self.logger.info(self.vapi.cli("show nat44 interfaces")) |
| 3637 | self.logger.info(self.vapi.cli("show nat44 static mappings")) |
| 3638 | self.logger.info(self.vapi.cli("show nat44 interface address")) |
| 3639 | self.logger.info(self.vapi.cli("show nat44 sessions detail")) |
| 3640 | self.logger.info(self.vapi.cli("show nat virtual-reassembly")) |
| 3641 | self.logger.info(self.vapi.cli("show nat44 hash tables detail")) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 3642 | self.logger.info(self.vapi.cli("show nat timeouts")) |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 3643 | self.logger.info( |
| 3644 | self.vapi.cli("show nat addr-port-assignment-alg")) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3645 | self.clear_nat44() |
| 3646 | self.vapi.cli("clear logging") |
| 3647 | |
| 3648 | |
| 3649 | class TestNAT44EndpointDependent(MethodHolder): |
| 3650 | """ Endpoint-Dependent mapping and filtering test cases """ |
| 3651 | |
| 3652 | @classmethod |
| 3653 | def setUpConstants(cls): |
| 3654 | super(TestNAT44EndpointDependent, cls).setUpConstants() |
| 3655 | cls.vpp_cmdline.extend(["nat", "{", "endpoint-dependent", "}"]) |
| 3656 | |
| 3657 | @classmethod |
| 3658 | def setUpClass(cls): |
| 3659 | super(TestNAT44EndpointDependent, cls).setUpClass() |
| 3660 | cls.vapi.cli("set log class nat level debug") |
| 3661 | try: |
| 3662 | cls.tcp_port_in = 6303 |
| 3663 | cls.tcp_port_out = 6303 |
| 3664 | cls.udp_port_in = 6304 |
| 3665 | cls.udp_port_out = 6304 |
| 3666 | cls.icmp_id_in = 6305 |
| 3667 | cls.icmp_id_out = 6305 |
| 3668 | cls.nat_addr = '10.0.0.3' |
| 3669 | cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr) |
| 3670 | cls.ipfix_src_port = 4739 |
| 3671 | cls.ipfix_domain_id = 1 |
| 3672 | cls.tcp_external_port = 80 |
| 3673 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 3674 | cls.create_pg_interfaces(range(7)) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3675 | cls.interfaces = list(cls.pg_interfaces[0:3]) |
| 3676 | |
| 3677 | for i in cls.interfaces: |
| 3678 | i.admin_up() |
| 3679 | i.config_ip4() |
| 3680 | i.resolve_arp() |
| 3681 | |
| 3682 | cls.pg0.generate_remote_hosts(3) |
| 3683 | cls.pg0.configure_ipv4_neighbors() |
| 3684 | |
| 3685 | cls.pg3.admin_up() |
| 3686 | |
| 3687 | cls.pg4.generate_remote_hosts(2) |
| 3688 | cls.pg4.config_ip4() |
| 3689 | ip_addr_n = socket.inet_pton(socket.AF_INET, "10.0.0.1") |
| 3690 | cls.vapi.sw_interface_add_del_address(cls.pg4.sw_if_index, |
| 3691 | ip_addr_n, |
| 3692 | 24) |
| 3693 | cls.pg4.admin_up() |
| 3694 | cls.pg4.resolve_arp() |
| 3695 | cls.pg4._remote_hosts[1]._ip4 = cls.pg4._remote_hosts[0]._ip4 |
| 3696 | cls.pg4.resolve_arp() |
| 3697 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 3698 | zero_ip4n = socket.inet_pton(socket.AF_INET, "0.0.0.0") |
| 3699 | cls.vapi.ip_table_add_del(1, is_add=1) |
| 3700 | |
| 3701 | cls.pg5._local_ip4 = "10.1.1.1" |
| 3702 | cls.pg5._local_ip4n = socket.inet_pton(socket.AF_INET, |
| 3703 | cls.pg5.local_ip4) |
| 3704 | cls.pg5._remote_hosts[0]._ip4 = "10.1.1.2" |
| 3705 | cls.pg5._remote_hosts[0]._ip4n = socket.inet_pton( |
| 3706 | socket.AF_INET, cls.pg5.remote_ip4) |
| 3707 | cls.pg5.set_table_ip4(1) |
| 3708 | cls.pg5.config_ip4() |
| 3709 | cls.pg5.admin_up() |
| 3710 | cls.vapi.ip_add_del_route(dst_address=cls.pg5.remote_ip4n, |
| 3711 | dst_address_length=32, |
| 3712 | table_id=1, |
| 3713 | next_hop_sw_if_index=cls.pg5.sw_if_index, |
| 3714 | next_hop_address=zero_ip4n) |
| 3715 | |
| 3716 | cls.pg6._local_ip4 = "10.1.2.1" |
| 3717 | cls.pg6._local_ip4n = socket.inet_pton(socket.AF_INET, |
| 3718 | cls.pg6.local_ip4) |
| 3719 | cls.pg6._remote_hosts[0]._ip4 = "10.1.2.2" |
| 3720 | cls.pg6._remote_hosts[0]._ip4n = socket.inet_pton( |
| 3721 | socket.AF_INET, cls.pg6.remote_ip4) |
| 3722 | cls.pg6.set_table_ip4(1) |
| 3723 | cls.pg6.config_ip4() |
| 3724 | cls.pg6.admin_up() |
| 3725 | cls.vapi.ip_add_del_route(dst_address=cls.pg6.remote_ip4n, |
| 3726 | dst_address_length=32, |
| 3727 | table_id=1, |
| 3728 | next_hop_sw_if_index=cls.pg6.sw_if_index, |
| 3729 | next_hop_address=zero_ip4n) |
| 3730 | |
| 3731 | cls.vapi.ip_add_del_route(dst_address=cls.pg6.remote_ip4n, |
| 3732 | dst_address_length=16, |
| 3733 | next_hop_address=zero_ip4n, |
| 3734 | table_id=0, |
| 3735 | next_hop_table_id=1) |
| 3736 | cls.vapi.ip_add_del_route(dst_address=zero_ip4n, |
| 3737 | dst_address_length=0, |
| 3738 | next_hop_address=zero_ip4n, |
| 3739 | table_id=1, |
| 3740 | next_hop_table_id=0) |
| 3741 | cls.vapi.ip_add_del_route(dst_address=zero_ip4n, |
| 3742 | dst_address_length=0, |
| 3743 | table_id=0, |
| 3744 | next_hop_sw_if_index=cls.pg1.sw_if_index, |
| 3745 | next_hop_address=cls.pg1.local_ip4n) |
| 3746 | |
| 3747 | cls.pg5.resolve_arp() |
| 3748 | cls.pg6.resolve_arp() |
| 3749 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3750 | except Exception: |
| 3751 | super(TestNAT44EndpointDependent, cls).tearDownClass() |
| 3752 | raise |
| 3753 | |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 3754 | def test_frag_in_order(self): |
| 3755 | """ NAT44 translate fragments arriving in order """ |
| 3756 | self.nat44_add_address(self.nat_addr) |
| 3757 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3758 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3759 | is_inside=0) |
| 3760 | self.frag_in_order(proto=IP_PROTOS.tcp) |
| 3761 | self.frag_in_order(proto=IP_PROTOS.udp) |
| 3762 | self.frag_in_order(proto=IP_PROTOS.icmp) |
| 3763 | |
| 3764 | def test_frag_in_order_dont_translate(self): |
| 3765 | """ NAT44 don't translate fragments arriving in order """ |
| 3766 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3767 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3768 | is_inside=0) |
| 3769 | self.vapi.nat44_forwarding_enable_disable(enable=True) |
| 3770 | self.frag_in_order(proto=IP_PROTOS.tcp, dont_translate=True) |
| 3771 | |
| 3772 | def test_frag_out_of_order(self): |
| 3773 | """ NAT44 translate fragments arriving out of order """ |
| 3774 | self.nat44_add_address(self.nat_addr) |
| 3775 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3776 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3777 | is_inside=0) |
| 3778 | self.frag_out_of_order(proto=IP_PROTOS.tcp) |
| 3779 | self.frag_out_of_order(proto=IP_PROTOS.udp) |
| 3780 | self.frag_out_of_order(proto=IP_PROTOS.icmp) |
| 3781 | |
| 3782 | def test_frag_out_of_order_dont_translate(self): |
| 3783 | """ NAT44 don't translate fragments arriving out of order """ |
| 3784 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3785 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3786 | is_inside=0) |
| 3787 | self.vapi.nat44_forwarding_enable_disable(enable=True) |
| 3788 | self.frag_out_of_order(proto=IP_PROTOS.tcp, dont_translate=True) |
| 3789 | |
| 3790 | def test_frag_in_order_in_plus_out(self): |
| 3791 | """ in+out interface fragments in order """ |
| 3792 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3793 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 3794 | is_inside=0) |
| 3795 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index) |
| 3796 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3797 | is_inside=0) |
| 3798 | |
| 3799 | self.server = self.pg1.remote_hosts[0] |
| 3800 | |
| 3801 | self.server_in_addr = self.server.ip4 |
| 3802 | self.server_out_addr = '11.11.11.11' |
| 3803 | self.server_in_port = random.randint(1025, 65535) |
| 3804 | self.server_out_port = random.randint(1025, 65535) |
| 3805 | |
| 3806 | self.nat44_add_address(self.server_out_addr) |
| 3807 | |
| 3808 | # add static mappings for server |
| 3809 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3810 | self.server_out_addr, |
| 3811 | self.server_in_port, |
| 3812 | self.server_out_port, |
| 3813 | proto=IP_PROTOS.tcp) |
| 3814 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3815 | self.server_out_addr, |
| 3816 | self.server_in_port, |
| 3817 | self.server_out_port, |
| 3818 | proto=IP_PROTOS.udp) |
| 3819 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3820 | self.server_out_addr, |
| 3821 | proto=IP_PROTOS.icmp) |
| 3822 | |
| 3823 | self.vapi.nat_set_reass(timeout=10) |
| 3824 | |
| 3825 | self.frag_in_order_in_plus_out(proto=IP_PROTOS.tcp) |
| 3826 | self.frag_in_order_in_plus_out(proto=IP_PROTOS.udp) |
| 3827 | self.frag_in_order_in_plus_out(proto=IP_PROTOS.icmp) |
| 3828 | |
| 3829 | def test_frag_out_of_order_in_plus_out(self): |
| 3830 | """ in+out interface fragments out of order """ |
| 3831 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3832 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 3833 | is_inside=0) |
| 3834 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index) |
| 3835 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3836 | is_inside=0) |
| 3837 | |
| 3838 | self.server = self.pg1.remote_hosts[0] |
| 3839 | |
| 3840 | self.server_in_addr = self.server.ip4 |
| 3841 | self.server_out_addr = '11.11.11.11' |
| 3842 | self.server_in_port = random.randint(1025, 65535) |
| 3843 | self.server_out_port = random.randint(1025, 65535) |
| 3844 | |
| 3845 | self.nat44_add_address(self.server_out_addr) |
| 3846 | |
| 3847 | # add static mappings for server |
| 3848 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3849 | self.server_out_addr, |
| 3850 | self.server_in_port, |
| 3851 | self.server_out_port, |
| 3852 | proto=IP_PROTOS.tcp) |
| 3853 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3854 | self.server_out_addr, |
| 3855 | self.server_in_port, |
| 3856 | self.server_out_port, |
| 3857 | proto=IP_PROTOS.udp) |
| 3858 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3859 | self.server_out_addr, |
| 3860 | proto=IP_PROTOS.icmp) |
| 3861 | |
| 3862 | self.vapi.nat_set_reass(timeout=10) |
| 3863 | |
| 3864 | self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.tcp) |
| 3865 | self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.udp) |
| 3866 | self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.icmp) |
| 3867 | |
| 3868 | def test_reass_hairpinning(self): |
| 3869 | """ NAT44 fragments hairpinning """ |
| 3870 | self.server = self.pg0.remote_hosts[1] |
| 3871 | self.host_in_port = random.randint(1025, 65535) |
| 3872 | self.server_in_port = random.randint(1025, 65535) |
| 3873 | self.server_out_port = random.randint(1025, 65535) |
| 3874 | |
| 3875 | self.nat44_add_address(self.nat_addr) |
| 3876 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3877 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3878 | is_inside=0) |
| 3879 | # add static mapping for server |
| 3880 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr, |
| 3881 | self.server_in_port, |
| 3882 | self.server_out_port, |
| 3883 | proto=IP_PROTOS.tcp) |
| 3884 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr, |
| 3885 | self.server_in_port, |
| 3886 | self.server_out_port, |
| 3887 | proto=IP_PROTOS.udp) |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame^] | 3888 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr) |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 3889 | |
| 3890 | self.reass_hairpinning(proto=IP_PROTOS.tcp) |
| 3891 | self.reass_hairpinning(proto=IP_PROTOS.udp) |
| 3892 | self.reass_hairpinning(proto=IP_PROTOS.icmp) |
| 3893 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3894 | def test_dynamic(self): |
| 3895 | """ NAT44 dynamic translation test """ |
| 3896 | |
| 3897 | self.nat44_add_address(self.nat_addr) |
| 3898 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3899 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3900 | is_inside=0) |
| 3901 | |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 3902 | nat_config = self.vapi.nat_show_config() |
| 3903 | self.assertEqual(1, nat_config.endpoint_dependent) |
| 3904 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3905 | # in2out |
| 3906 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 3907 | self.pg0.add_stream(pkts) |
| 3908 | self.pg_enable_capture(self.pg_interfaces) |
| 3909 | self.pg_start() |
| 3910 | capture = self.pg1.get_capture(len(pkts)) |
| 3911 | self.verify_capture_out(capture) |
| 3912 | |
| 3913 | # out2in |
| 3914 | pkts = self.create_stream_out(self.pg1) |
| 3915 | self.pg1.add_stream(pkts) |
| 3916 | self.pg_enable_capture(self.pg_interfaces) |
| 3917 | self.pg_start() |
| 3918 | capture = self.pg0.get_capture(len(pkts)) |
| 3919 | self.verify_capture_in(capture, self.pg0) |
| 3920 | |
| 3921 | def test_forwarding(self): |
| 3922 | """ NAT44 forwarding test """ |
| 3923 | |
| 3924 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3925 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3926 | is_inside=0) |
| 3927 | self.vapi.nat44_forwarding_enable_disable(1) |
| 3928 | |
| 3929 | real_ip = self.pg0.remote_ip4n |
| 3930 | alias_ip = self.nat_addr_n |
| 3931 | self.vapi.nat44_add_del_static_mapping(local_ip=real_ip, |
| 3932 | external_ip=alias_ip) |
| 3933 | |
| 3934 | try: |
| 3935 | # in2out - static mapping match |
| 3936 | |
| 3937 | pkts = self.create_stream_out(self.pg1) |
| 3938 | self.pg1.add_stream(pkts) |
| 3939 | self.pg_enable_capture(self.pg_interfaces) |
| 3940 | self.pg_start() |
| 3941 | capture = self.pg0.get_capture(len(pkts)) |
| 3942 | self.verify_capture_in(capture, self.pg0) |
| 3943 | |
| 3944 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 3945 | self.pg0.add_stream(pkts) |
| 3946 | self.pg_enable_capture(self.pg_interfaces) |
| 3947 | self.pg_start() |
| 3948 | capture = self.pg1.get_capture(len(pkts)) |
| 3949 | self.verify_capture_out(capture, same_port=True) |
| 3950 | |
| 3951 | # in2out - no static mapping match |
| 3952 | |
| 3953 | host0 = self.pg0.remote_hosts[0] |
| 3954 | self.pg0.remote_hosts[0] = self.pg0.remote_hosts[1] |
| 3955 | try: |
| 3956 | pkts = self.create_stream_out(self.pg1, |
| 3957 | dst_ip=self.pg0.remote_ip4, |
| 3958 | use_inside_ports=True) |
| 3959 | self.pg1.add_stream(pkts) |
| 3960 | self.pg_enable_capture(self.pg_interfaces) |
| 3961 | self.pg_start() |
| 3962 | capture = self.pg0.get_capture(len(pkts)) |
| 3963 | self.verify_capture_in(capture, self.pg0) |
| 3964 | |
| 3965 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 3966 | self.pg0.add_stream(pkts) |
| 3967 | self.pg_enable_capture(self.pg_interfaces) |
| 3968 | self.pg_start() |
| 3969 | capture = self.pg1.get_capture(len(pkts)) |
| 3970 | self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4, |
| 3971 | same_port=True) |
| 3972 | finally: |
| 3973 | self.pg0.remote_hosts[0] = host0 |
| 3974 | |
| 3975 | user = self.pg0.remote_hosts[1] |
| 3976 | sessions = self.vapi.nat44_user_session_dump(user.ip4n, 0) |
| 3977 | self.assertEqual(len(sessions), 3) |
| 3978 | self.assertTrue(sessions[0].ext_host_valid) |
| 3979 | self.vapi.nat44_del_session( |
| 3980 | sessions[0].inside_ip_address, |
| 3981 | sessions[0].inside_port, |
| 3982 | sessions[0].protocol, |
| 3983 | ext_host_address=sessions[0].ext_host_address, |
| 3984 | ext_host_port=sessions[0].ext_host_port) |
| 3985 | sessions = self.vapi.nat44_user_session_dump(user.ip4n, 0) |
| 3986 | self.assertEqual(len(sessions), 2) |
| 3987 | |
| 3988 | finally: |
| 3989 | self.vapi.nat44_forwarding_enable_disable(0) |
| 3990 | self.vapi.nat44_add_del_static_mapping(local_ip=real_ip, |
| 3991 | external_ip=alias_ip, |
| 3992 | is_add=0) |
| 3993 | |
| 3994 | def test_static_lb(self): |
| 3995 | """ NAT44 local service load balancing """ |
| 3996 | external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr) |
| 3997 | external_port = 80 |
| 3998 | local_port = 8080 |
| 3999 | server1 = self.pg0.remote_hosts[0] |
| 4000 | server2 = self.pg0.remote_hosts[1] |
| 4001 | |
| 4002 | locals = [{'addr': server1.ip4n, |
| 4003 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4004 | 'probability': 70, |
| 4005 | 'vrf_id': 0}, |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4006 | {'addr': server2.ip4n, |
| 4007 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4008 | 'probability': 30, |
| 4009 | 'vrf_id': 0}] |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4010 | |
| 4011 | self.nat44_add_address(self.nat_addr) |
| 4012 | self.vapi.nat44_add_del_lb_static_mapping(external_addr_n, |
| 4013 | external_port, |
| 4014 | IP_PROTOS.tcp, |
| 4015 | local_num=len(locals), |
| 4016 | locals=locals) |
| 4017 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4018 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4019 | is_inside=0) |
| 4020 | |
| 4021 | # from client to service |
| 4022 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4023 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4024 | TCP(sport=12345, dport=external_port)) |
| 4025 | self.pg1.add_stream(p) |
| 4026 | self.pg_enable_capture(self.pg_interfaces) |
| 4027 | self.pg_start() |
| 4028 | capture = self.pg0.get_capture(1) |
| 4029 | p = capture[0] |
| 4030 | server = None |
| 4031 | try: |
| 4032 | ip = p[IP] |
| 4033 | tcp = p[TCP] |
| 4034 | self.assertIn(ip.dst, [server1.ip4, server2.ip4]) |
| 4035 | if ip.dst == server1.ip4: |
| 4036 | server = server1 |
| 4037 | else: |
| 4038 | server = server2 |
| 4039 | self.assertEqual(tcp.dport, local_port) |
| 4040 | self.assert_packet_checksums_valid(p) |
| 4041 | except: |
| 4042 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4043 | raise |
| 4044 | |
| 4045 | # from service back to client |
| 4046 | p = (Ether(src=server.mac, dst=self.pg0.local_mac) / |
| 4047 | IP(src=server.ip4, dst=self.pg1.remote_ip4) / |
| 4048 | TCP(sport=local_port, dport=12345)) |
| 4049 | self.pg0.add_stream(p) |
| 4050 | self.pg_enable_capture(self.pg_interfaces) |
| 4051 | self.pg_start() |
| 4052 | capture = self.pg1.get_capture(1) |
| 4053 | p = capture[0] |
| 4054 | try: |
| 4055 | ip = p[IP] |
| 4056 | tcp = p[TCP] |
| 4057 | self.assertEqual(ip.src, self.nat_addr) |
| 4058 | self.assertEqual(tcp.sport, external_port) |
| 4059 | self.assert_packet_checksums_valid(p) |
| 4060 | except: |
| 4061 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4062 | raise |
| 4063 | |
| 4064 | sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0) |
| 4065 | self.assertEqual(len(sessions), 1) |
| 4066 | self.assertTrue(sessions[0].ext_host_valid) |
| 4067 | self.vapi.nat44_del_session( |
| 4068 | sessions[0].inside_ip_address, |
| 4069 | sessions[0].inside_port, |
| 4070 | sessions[0].protocol, |
| 4071 | ext_host_address=sessions[0].ext_host_address, |
| 4072 | ext_host_port=sessions[0].ext_host_port) |
| 4073 | sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0) |
| 4074 | self.assertEqual(len(sessions), 0) |
| 4075 | |
| 4076 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 4077 | def test_static_lb_multi_clients(self): |
| 4078 | """ NAT44 local service load balancing - multiple clients""" |
| 4079 | |
| 4080 | external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr) |
| 4081 | external_port = 80 |
| 4082 | local_port = 8080 |
| 4083 | server1 = self.pg0.remote_hosts[0] |
| 4084 | server2 = self.pg0.remote_hosts[1] |
| 4085 | |
| 4086 | locals = [{'addr': server1.ip4n, |
| 4087 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4088 | 'probability': 90, |
| 4089 | 'vrf_id': 0}, |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4090 | {'addr': server2.ip4n, |
| 4091 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4092 | 'probability': 10, |
| 4093 | 'vrf_id': 0}] |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4094 | |
| 4095 | self.nat44_add_address(self.nat_addr) |
| 4096 | self.vapi.nat44_add_del_lb_static_mapping(external_addr_n, |
| 4097 | external_port, |
| 4098 | IP_PROTOS.tcp, |
| 4099 | local_num=len(locals), |
| 4100 | locals=locals) |
| 4101 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4102 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4103 | is_inside=0) |
| 4104 | |
| 4105 | server1_n = 0 |
| 4106 | server2_n = 0 |
| 4107 | clients = ip4_range(self.pg1.remote_ip4, 10, 50) |
| 4108 | pkts = [] |
| 4109 | for client in clients: |
| 4110 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4111 | IP(src=client, dst=self.nat_addr) / |
| 4112 | TCP(sport=12345, dport=external_port)) |
| 4113 | pkts.append(p) |
| 4114 | self.pg1.add_stream(pkts) |
| 4115 | self.pg_enable_capture(self.pg_interfaces) |
| 4116 | self.pg_start() |
| 4117 | capture = self.pg0.get_capture(len(pkts)) |
| 4118 | for p in capture: |
| 4119 | if p[IP].dst == server1.ip4: |
| 4120 | server1_n += 1 |
| 4121 | else: |
| 4122 | server2_n += 1 |
| 4123 | self.assertTrue(server1_n > server2_n) |
| 4124 | |
| 4125 | def test_static_lb_2(self): |
| 4126 | """ NAT44 local service load balancing (asymmetrical rule) """ |
| 4127 | external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr) |
| 4128 | external_port = 80 |
| 4129 | local_port = 8080 |
| 4130 | server1 = self.pg0.remote_hosts[0] |
| 4131 | server2 = self.pg0.remote_hosts[1] |
| 4132 | |
| 4133 | locals = [{'addr': server1.ip4n, |
| 4134 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4135 | 'probability': 70, |
| 4136 | 'vrf_id': 0}, |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4137 | {'addr': server2.ip4n, |
| 4138 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4139 | 'probability': 30, |
| 4140 | 'vrf_id': 0}] |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4141 | |
| 4142 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4143 | self.vapi.nat44_add_del_lb_static_mapping(external_addr_n, |
| 4144 | external_port, |
| 4145 | IP_PROTOS.tcp, |
| 4146 | out2in_only=1, |
| 4147 | local_num=len(locals), |
| 4148 | locals=locals) |
| 4149 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4150 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4151 | is_inside=0) |
| 4152 | |
| 4153 | # from client to service |
| 4154 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4155 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4156 | TCP(sport=12345, dport=external_port)) |
| 4157 | self.pg1.add_stream(p) |
| 4158 | self.pg_enable_capture(self.pg_interfaces) |
| 4159 | self.pg_start() |
| 4160 | capture = self.pg0.get_capture(1) |
| 4161 | p = capture[0] |
| 4162 | server = None |
| 4163 | try: |
| 4164 | ip = p[IP] |
| 4165 | tcp = p[TCP] |
| 4166 | self.assertIn(ip.dst, [server1.ip4, server2.ip4]) |
| 4167 | if ip.dst == server1.ip4: |
| 4168 | server = server1 |
| 4169 | else: |
| 4170 | server = server2 |
| 4171 | self.assertEqual(tcp.dport, local_port) |
| 4172 | self.assert_packet_checksums_valid(p) |
| 4173 | except: |
| 4174 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4175 | raise |
| 4176 | |
| 4177 | # from service back to client |
| 4178 | p = (Ether(src=server.mac, dst=self.pg0.local_mac) / |
| 4179 | IP(src=server.ip4, dst=self.pg1.remote_ip4) / |
| 4180 | TCP(sport=local_port, dport=12345)) |
| 4181 | self.pg0.add_stream(p) |
| 4182 | self.pg_enable_capture(self.pg_interfaces) |
| 4183 | self.pg_start() |
| 4184 | capture = self.pg1.get_capture(1) |
| 4185 | p = capture[0] |
| 4186 | try: |
| 4187 | ip = p[IP] |
| 4188 | tcp = p[TCP] |
| 4189 | self.assertEqual(ip.src, self.nat_addr) |
| 4190 | self.assertEqual(tcp.sport, external_port) |
| 4191 | self.assert_packet_checksums_valid(p) |
| 4192 | except: |
| 4193 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4194 | raise |
| 4195 | |
| 4196 | # from client to server (no translation) |
| 4197 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4198 | IP(src=self.pg1.remote_ip4, dst=server1.ip4) / |
| 4199 | TCP(sport=12346, dport=local_port)) |
| 4200 | self.pg1.add_stream(p) |
| 4201 | self.pg_enable_capture(self.pg_interfaces) |
| 4202 | self.pg_start() |
| 4203 | capture = self.pg0.get_capture(1) |
| 4204 | p = capture[0] |
| 4205 | server = None |
| 4206 | try: |
| 4207 | ip = p[IP] |
| 4208 | tcp = p[TCP] |
| 4209 | self.assertEqual(ip.dst, server1.ip4) |
| 4210 | self.assertEqual(tcp.dport, local_port) |
| 4211 | self.assert_packet_checksums_valid(p) |
| 4212 | except: |
| 4213 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4214 | raise |
| 4215 | |
| 4216 | # from service back to client (no translation) |
| 4217 | p = (Ether(src=server1.mac, dst=self.pg0.local_mac) / |
| 4218 | IP(src=server1.ip4, dst=self.pg1.remote_ip4) / |
| 4219 | TCP(sport=local_port, dport=12346)) |
| 4220 | self.pg0.add_stream(p) |
| 4221 | self.pg_enable_capture(self.pg_interfaces) |
| 4222 | self.pg_start() |
| 4223 | capture = self.pg1.get_capture(1) |
| 4224 | p = capture[0] |
| 4225 | try: |
| 4226 | ip = p[IP] |
| 4227 | tcp = p[TCP] |
| 4228 | self.assertEqual(ip.src, server1.ip4) |
| 4229 | self.assertEqual(tcp.sport, local_port) |
| 4230 | self.assert_packet_checksums_valid(p) |
| 4231 | except: |
| 4232 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4233 | raise |
| 4234 | |
Matus Fabian | ea5b5be | 2018-09-03 05:02:23 -0700 | [diff] [blame] | 4235 | def test_lb_affinity(self): |
| 4236 | """ NAT44 local service load balancing affinity """ |
| 4237 | external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr) |
| 4238 | external_port = 80 |
| 4239 | local_port = 8080 |
| 4240 | server1 = self.pg0.remote_hosts[0] |
| 4241 | server2 = self.pg0.remote_hosts[1] |
| 4242 | |
| 4243 | locals = [{'addr': server1.ip4n, |
| 4244 | 'port': local_port, |
| 4245 | 'probability': 50, |
| 4246 | 'vrf_id': 0}, |
| 4247 | {'addr': server2.ip4n, |
| 4248 | 'port': local_port, |
| 4249 | 'probability': 50, |
| 4250 | 'vrf_id': 0}] |
| 4251 | |
| 4252 | self.nat44_add_address(self.nat_addr) |
| 4253 | self.vapi.nat44_add_del_lb_static_mapping(external_addr_n, |
| 4254 | external_port, |
| 4255 | IP_PROTOS.tcp, |
| 4256 | affinity=10800, |
| 4257 | local_num=len(locals), |
| 4258 | locals=locals) |
| 4259 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4260 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4261 | is_inside=0) |
| 4262 | |
| 4263 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 4264 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4265 | TCP(sport=1025, dport=external_port)) |
| 4266 | self.pg1.add_stream(p) |
| 4267 | self.pg_enable_capture(self.pg_interfaces) |
| 4268 | self.pg_start() |
| 4269 | capture = self.pg0.get_capture(1) |
| 4270 | backend = capture[0][IP].dst |
| 4271 | |
| 4272 | sessions = self.vapi.nat44_user_session_dump( |
| 4273 | socket.inet_pton(socket.AF_INET, backend), 0) |
| 4274 | self.assertEqual(len(sessions), 1) |
| 4275 | self.assertTrue(sessions[0].ext_host_valid) |
| 4276 | self.vapi.nat44_del_session( |
| 4277 | sessions[0].inside_ip_address, |
| 4278 | sessions[0].inside_port, |
| 4279 | sessions[0].protocol, |
| 4280 | ext_host_address=sessions[0].ext_host_address, |
| 4281 | ext_host_port=sessions[0].ext_host_port) |
| 4282 | |
| 4283 | pkts = [] |
| 4284 | for port in range(1030, 1100): |
| 4285 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 4286 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4287 | TCP(sport=port, dport=external_port)) |
| 4288 | pkts.append(p) |
| 4289 | self.pg1.add_stream(pkts) |
| 4290 | self.pg_enable_capture(self.pg_interfaces) |
| 4291 | self.pg_start() |
| 4292 | capture = self.pg0.get_capture(len(pkts)) |
| 4293 | for p in capture: |
| 4294 | self.assertEqual(p[IP].dst, backend) |
| 4295 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4296 | def test_unknown_proto(self): |
| 4297 | """ NAT44 translate packet with unknown protocol """ |
| 4298 | self.nat44_add_address(self.nat_addr) |
| 4299 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4300 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4301 | is_inside=0) |
| 4302 | |
| 4303 | # in2out |
| 4304 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 4305 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4306 | TCP(sport=self.tcp_port_in, dport=20)) |
| 4307 | self.pg0.add_stream(p) |
| 4308 | self.pg_enable_capture(self.pg_interfaces) |
| 4309 | self.pg_start() |
| 4310 | p = self.pg1.get_capture(1) |
| 4311 | |
| 4312 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 4313 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4314 | GRE() / |
| 4315 | IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) / |
| 4316 | TCP(sport=1234, dport=1234)) |
| 4317 | self.pg0.add_stream(p) |
| 4318 | self.pg_enable_capture(self.pg_interfaces) |
| 4319 | self.pg_start() |
| 4320 | p = self.pg1.get_capture(1) |
| 4321 | packet = p[0] |
| 4322 | try: |
| 4323 | self.assertEqual(packet[IP].src, self.nat_addr) |
| 4324 | self.assertEqual(packet[IP].dst, self.pg1.remote_ip4) |
| 4325 | self.assertTrue(packet.haslayer(GRE)) |
| 4326 | self.assert_packet_checksums_valid(packet) |
| 4327 | except: |
| 4328 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 4329 | raise |
| 4330 | |
| 4331 | # out2in |
| 4332 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 4333 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4334 | GRE() / |
| 4335 | IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) / |
| 4336 | TCP(sport=1234, dport=1234)) |
| 4337 | self.pg1.add_stream(p) |
| 4338 | self.pg_enable_capture(self.pg_interfaces) |
| 4339 | self.pg_start() |
| 4340 | p = self.pg0.get_capture(1) |
| 4341 | packet = p[0] |
| 4342 | try: |
| 4343 | self.assertEqual(packet[IP].src, self.pg1.remote_ip4) |
| 4344 | self.assertEqual(packet[IP].dst, self.pg0.remote_ip4) |
| 4345 | self.assertTrue(packet.haslayer(GRE)) |
| 4346 | self.assert_packet_checksums_valid(packet) |
| 4347 | except: |
| 4348 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 4349 | raise |
| 4350 | |
| 4351 | def test_hairpinning_unknown_proto(self): |
| 4352 | """ NAT44 translate packet with unknown protocol - hairpinning """ |
| 4353 | host = self.pg0.remote_hosts[0] |
| 4354 | server = self.pg0.remote_hosts[1] |
| 4355 | host_in_port = 1234 |
| 4356 | server_out_port = 8765 |
| 4357 | server_nat_ip = "10.0.0.11" |
| 4358 | |
| 4359 | self.nat44_add_address(self.nat_addr) |
| 4360 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4361 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4362 | is_inside=0) |
| 4363 | |
| 4364 | # add static mapping for server |
| 4365 | self.nat44_add_static_mapping(server.ip4, server_nat_ip) |
| 4366 | |
| 4367 | # host to server |
| 4368 | p = (Ether(src=host.mac, dst=self.pg0.local_mac) / |
| 4369 | IP(src=host.ip4, dst=server_nat_ip) / |
| 4370 | TCP(sport=host_in_port, dport=server_out_port)) |
| 4371 | self.pg0.add_stream(p) |
| 4372 | self.pg_enable_capture(self.pg_interfaces) |
| 4373 | self.pg_start() |
| 4374 | self.pg0.get_capture(1) |
| 4375 | |
| 4376 | p = (Ether(dst=self.pg0.local_mac, src=host.mac) / |
| 4377 | IP(src=host.ip4, dst=server_nat_ip) / |
| 4378 | GRE() / |
| 4379 | IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) / |
| 4380 | TCP(sport=1234, dport=1234)) |
| 4381 | self.pg0.add_stream(p) |
| 4382 | self.pg_enable_capture(self.pg_interfaces) |
| 4383 | self.pg_start() |
| 4384 | p = self.pg0.get_capture(1) |
| 4385 | packet = p[0] |
| 4386 | try: |
| 4387 | self.assertEqual(packet[IP].src, self.nat_addr) |
| 4388 | self.assertEqual(packet[IP].dst, server.ip4) |
| 4389 | self.assertTrue(packet.haslayer(GRE)) |
| 4390 | self.assert_packet_checksums_valid(packet) |
| 4391 | except: |
| 4392 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 4393 | raise |
| 4394 | |
| 4395 | # server to host |
| 4396 | p = (Ether(dst=self.pg0.local_mac, src=server.mac) / |
| 4397 | IP(src=server.ip4, dst=self.nat_addr) / |
| 4398 | GRE() / |
| 4399 | IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) / |
| 4400 | TCP(sport=1234, dport=1234)) |
| 4401 | self.pg0.add_stream(p) |
| 4402 | self.pg_enable_capture(self.pg_interfaces) |
| 4403 | self.pg_start() |
| 4404 | p = self.pg0.get_capture(1) |
| 4405 | packet = p[0] |
| 4406 | try: |
| 4407 | self.assertEqual(packet[IP].src, server_nat_ip) |
| 4408 | self.assertEqual(packet[IP].dst, host.ip4) |
| 4409 | self.assertTrue(packet.haslayer(GRE)) |
| 4410 | self.assert_packet_checksums_valid(packet) |
| 4411 | except: |
| 4412 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 4413 | raise |
| 4414 | |
| 4415 | def test_output_feature_and_service(self): |
| 4416 | """ NAT44 interface output feature and services """ |
| 4417 | external_addr = '1.2.3.4' |
| 4418 | external_port = 80 |
| 4419 | local_port = 8080 |
| 4420 | |
| 4421 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4422 | self.nat44_add_address(self.nat_addr) |
| 4423 | self.vapi.nat44_add_del_identity_mapping(ip=self.pg1.remote_ip4n) |
| 4424 | self.nat44_add_static_mapping(self.pg0.remote_ip4, external_addr, |
| 4425 | local_port, external_port, |
| 4426 | proto=IP_PROTOS.tcp, out2in_only=1) |
| 4427 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4428 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 4429 | is_inside=0) |
| 4430 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 4431 | is_inside=0) |
| 4432 | |
| 4433 | # from client to service |
| 4434 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4435 | IP(src=self.pg1.remote_ip4, dst=external_addr) / |
| 4436 | TCP(sport=12345, dport=external_port)) |
| 4437 | self.pg1.add_stream(p) |
| 4438 | self.pg_enable_capture(self.pg_interfaces) |
| 4439 | self.pg_start() |
| 4440 | capture = self.pg0.get_capture(1) |
| 4441 | p = capture[0] |
| 4442 | try: |
| 4443 | ip = p[IP] |
| 4444 | tcp = p[TCP] |
| 4445 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 4446 | self.assertEqual(tcp.dport, local_port) |
| 4447 | self.assert_packet_checksums_valid(p) |
| 4448 | except: |
| 4449 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4450 | raise |
| 4451 | |
| 4452 | # from service back to client |
| 4453 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4454 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4455 | TCP(sport=local_port, dport=12345)) |
| 4456 | self.pg0.add_stream(p) |
| 4457 | self.pg_enable_capture(self.pg_interfaces) |
| 4458 | self.pg_start() |
| 4459 | capture = self.pg1.get_capture(1) |
| 4460 | p = capture[0] |
| 4461 | try: |
| 4462 | ip = p[IP] |
| 4463 | tcp = p[TCP] |
| 4464 | self.assertEqual(ip.src, external_addr) |
| 4465 | self.assertEqual(tcp.sport, external_port) |
| 4466 | self.assert_packet_checksums_valid(p) |
| 4467 | except: |
| 4468 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4469 | raise |
| 4470 | |
| 4471 | # from local network host to external network |
| 4472 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 4473 | self.pg0.add_stream(pkts) |
| 4474 | self.pg_enable_capture(self.pg_interfaces) |
| 4475 | self.pg_start() |
| 4476 | capture = self.pg1.get_capture(len(pkts)) |
| 4477 | self.verify_capture_out(capture) |
| 4478 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 4479 | self.pg0.add_stream(pkts) |
| 4480 | self.pg_enable_capture(self.pg_interfaces) |
| 4481 | self.pg_start() |
| 4482 | capture = self.pg1.get_capture(len(pkts)) |
| 4483 | self.verify_capture_out(capture) |
| 4484 | |
| 4485 | # from external network back to local network host |
| 4486 | pkts = self.create_stream_out(self.pg1) |
| 4487 | self.pg1.add_stream(pkts) |
| 4488 | self.pg_enable_capture(self.pg_interfaces) |
| 4489 | self.pg_start() |
| 4490 | capture = self.pg0.get_capture(len(pkts)) |
| 4491 | self.verify_capture_in(capture, self.pg0) |
| 4492 | |
| 4493 | def test_output_feature_and_service2(self): |
| 4494 | """ NAT44 interface output feature and service host direct access """ |
| 4495 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4496 | self.nat44_add_address(self.nat_addr) |
| 4497 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 4498 | is_inside=0) |
| 4499 | |
| 4500 | # session initiaded from service host - translate |
| 4501 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 4502 | self.pg0.add_stream(pkts) |
| 4503 | self.pg_enable_capture(self.pg_interfaces) |
| 4504 | self.pg_start() |
| 4505 | capture = self.pg1.get_capture(len(pkts)) |
| 4506 | self.verify_capture_out(capture) |
| 4507 | |
| 4508 | pkts = self.create_stream_out(self.pg1) |
| 4509 | self.pg1.add_stream(pkts) |
| 4510 | self.pg_enable_capture(self.pg_interfaces) |
| 4511 | self.pg_start() |
| 4512 | capture = self.pg0.get_capture(len(pkts)) |
| 4513 | self.verify_capture_in(capture, self.pg0) |
| 4514 | |
| 4515 | # session initiaded from remote host - do not translate |
| 4516 | self.tcp_port_in = 60303 |
| 4517 | self.udp_port_in = 60304 |
| 4518 | self.icmp_id_in = 60305 |
| 4519 | pkts = self.create_stream_out(self.pg1, |
| 4520 | self.pg0.remote_ip4, |
| 4521 | use_inside_ports=True) |
| 4522 | self.pg1.add_stream(pkts) |
| 4523 | self.pg_enable_capture(self.pg_interfaces) |
| 4524 | self.pg_start() |
| 4525 | capture = self.pg0.get_capture(len(pkts)) |
| 4526 | self.verify_capture_in(capture, self.pg0) |
| 4527 | |
| 4528 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 4529 | self.pg0.add_stream(pkts) |
| 4530 | self.pg_enable_capture(self.pg_interfaces) |
| 4531 | self.pg_start() |
| 4532 | capture = self.pg1.get_capture(len(pkts)) |
| 4533 | self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4, |
| 4534 | same_port=True) |
| 4535 | |
| 4536 | def test_output_feature_and_service3(self): |
| 4537 | """ NAT44 interface output feature and DST NAT """ |
| 4538 | external_addr = '1.2.3.4' |
| 4539 | external_port = 80 |
| 4540 | local_port = 8080 |
| 4541 | |
| 4542 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4543 | self.nat44_add_address(self.nat_addr) |
| 4544 | self.nat44_add_static_mapping(self.pg1.remote_ip4, external_addr, |
| 4545 | local_port, external_port, |
| 4546 | proto=IP_PROTOS.tcp, out2in_only=1) |
| 4547 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4548 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 4549 | is_inside=0) |
| 4550 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 4551 | is_inside=0) |
| 4552 | |
| 4553 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4554 | IP(src=self.pg0.remote_ip4, dst=external_addr) / |
| 4555 | TCP(sport=12345, dport=external_port)) |
| 4556 | self.pg0.add_stream(p) |
| 4557 | self.pg_enable_capture(self.pg_interfaces) |
| 4558 | self.pg_start() |
| 4559 | capture = self.pg1.get_capture(1) |
| 4560 | p = capture[0] |
| 4561 | try: |
| 4562 | ip = p[IP] |
| 4563 | tcp = p[TCP] |
| 4564 | self.assertEqual(ip.src, self.pg0.remote_ip4) |
| 4565 | self.assertEqual(tcp.sport, 12345) |
| 4566 | self.assertEqual(ip.dst, self.pg1.remote_ip4) |
| 4567 | self.assertEqual(tcp.dport, local_port) |
| 4568 | self.assert_packet_checksums_valid(p) |
| 4569 | except: |
| 4570 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4571 | raise |
| 4572 | |
| 4573 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4574 | IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) / |
| 4575 | TCP(sport=local_port, dport=12345)) |
| 4576 | self.pg1.add_stream(p) |
| 4577 | self.pg_enable_capture(self.pg_interfaces) |
| 4578 | self.pg_start() |
| 4579 | capture = self.pg0.get_capture(1) |
| 4580 | p = capture[0] |
| 4581 | try: |
| 4582 | ip = p[IP] |
| 4583 | tcp = p[TCP] |
| 4584 | self.assertEqual(ip.src, external_addr) |
| 4585 | self.assertEqual(tcp.sport, external_port) |
| 4586 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 4587 | self.assertEqual(tcp.dport, 12345) |
| 4588 | self.assert_packet_checksums_valid(p) |
| 4589 | except: |
| 4590 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4591 | raise |
| 4592 | |
Matus Fabian | 182e37e | 2018-08-14 04:21:26 -0700 | [diff] [blame] | 4593 | def test_next_src_nat(self): |
| 4594 | """ On way back forward packet to nat44-in2out node. """ |
| 4595 | twice_nat_addr = '10.0.1.3' |
| 4596 | external_port = 80 |
| 4597 | local_port = 8080 |
| 4598 | post_twice_nat_port = 0 |
| 4599 | |
| 4600 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4601 | self.nat44_add_address(twice_nat_addr, twice_nat=1) |
| 4602 | self.nat44_add_static_mapping(self.pg6.remote_ip4, self.pg1.remote_ip4, |
| 4603 | local_port, external_port, |
| 4604 | proto=IP_PROTOS.tcp, out2in_only=1, |
| 4605 | self_twice_nat=1, vrf_id=1) |
| 4606 | self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index, |
| 4607 | is_inside=0) |
| 4608 | |
| 4609 | p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) / |
| 4610 | IP(src=self.pg6.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4611 | TCP(sport=12345, dport=external_port)) |
| 4612 | self.pg6.add_stream(p) |
| 4613 | self.pg_enable_capture(self.pg_interfaces) |
| 4614 | self.pg_start() |
| 4615 | capture = self.pg6.get_capture(1) |
| 4616 | p = capture[0] |
| 4617 | try: |
| 4618 | ip = p[IP] |
| 4619 | tcp = p[TCP] |
| 4620 | self.assertEqual(ip.src, twice_nat_addr) |
| 4621 | self.assertNotEqual(tcp.sport, 12345) |
| 4622 | post_twice_nat_port = tcp.sport |
| 4623 | self.assertEqual(ip.dst, self.pg6.remote_ip4) |
| 4624 | self.assertEqual(tcp.dport, local_port) |
| 4625 | self.assert_packet_checksums_valid(p) |
| 4626 | except: |
| 4627 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4628 | raise |
| 4629 | |
| 4630 | p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) / |
| 4631 | IP(src=self.pg6.remote_ip4, dst=twice_nat_addr) / |
| 4632 | TCP(sport=local_port, dport=post_twice_nat_port)) |
| 4633 | self.pg6.add_stream(p) |
| 4634 | self.pg_enable_capture(self.pg_interfaces) |
| 4635 | self.pg_start() |
| 4636 | capture = self.pg6.get_capture(1) |
| 4637 | p = capture[0] |
| 4638 | try: |
| 4639 | ip = p[IP] |
| 4640 | tcp = p[TCP] |
| 4641 | self.assertEqual(ip.src, self.pg1.remote_ip4) |
| 4642 | self.assertEqual(tcp.sport, external_port) |
| 4643 | self.assertEqual(ip.dst, self.pg6.remote_ip4) |
| 4644 | self.assertEqual(tcp.dport, 12345) |
| 4645 | self.assert_packet_checksums_valid(p) |
| 4646 | except: |
| 4647 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4648 | raise |
| 4649 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4650 | def twice_nat_common(self, self_twice_nat=False, same_pg=False, lb=False, |
| 4651 | client_id=None): |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4652 | twice_nat_addr = '10.0.1.3' |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4653 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4654 | port_in = 8080 |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4655 | if lb: |
| 4656 | if not same_pg: |
| 4657 | port_in1 = port_in |
| 4658 | port_in2 = port_in |
| 4659 | else: |
| 4660 | port_in1 = port_in+1 |
| 4661 | port_in2 = port_in+2 |
| 4662 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4663 | port_out = 80 |
| 4664 | eh_port_out = 4567 |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4665 | |
| 4666 | server1 = self.pg0.remote_hosts[0] |
| 4667 | server2 = self.pg0.remote_hosts[1] |
| 4668 | if lb and same_pg: |
| 4669 | server2 = server1 |
| 4670 | if not lb: |
| 4671 | server = server1 |
| 4672 | |
| 4673 | pg0 = self.pg0 |
| 4674 | if same_pg: |
| 4675 | pg1 = self.pg0 |
| 4676 | else: |
| 4677 | pg1 = self.pg1 |
| 4678 | |
| 4679 | eh_translate = ((not self_twice_nat) or (not lb and same_pg) or |
| 4680 | client_id == 1) |
| 4681 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4682 | self.nat44_add_address(self.nat_addr) |
| 4683 | self.nat44_add_address(twice_nat_addr, twice_nat=1) |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4684 | if not lb: |
| 4685 | self.nat44_add_static_mapping(pg0.remote_ip4, self.nat_addr, |
| 4686 | port_in, port_out, |
| 4687 | proto=IP_PROTOS.tcp, |
| 4688 | twice_nat=int(not self_twice_nat), |
| 4689 | self_twice_nat=int(self_twice_nat)) |
| 4690 | else: |
| 4691 | locals = [{'addr': server1.ip4n, |
| 4692 | 'port': port_in1, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4693 | 'probability': 50, |
| 4694 | 'vrf_id': 0}, |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4695 | {'addr': server2.ip4n, |
| 4696 | 'port': port_in2, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4697 | 'probability': 50, |
| 4698 | 'vrf_id': 0}] |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4699 | out_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr) |
| 4700 | self.vapi.nat44_add_del_lb_static_mapping(out_addr_n, |
| 4701 | port_out, |
| 4702 | IP_PROTOS.tcp, |
| 4703 | twice_nat=int( |
| 4704 | not self_twice_nat), |
| 4705 | self_twice_nat=int( |
| 4706 | self_twice_nat), |
| 4707 | local_num=len(locals), |
| 4708 | locals=locals) |
| 4709 | self.vapi.nat44_interface_add_del_feature(pg0.sw_if_index) |
| 4710 | self.vapi.nat44_interface_add_del_feature(pg1.sw_if_index, |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4711 | is_inside=0) |
| 4712 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4713 | if same_pg: |
| 4714 | if not lb: |
| 4715 | client = server |
| 4716 | else: |
| 4717 | assert client_id is not None |
| 4718 | if client_id == 1: |
| 4719 | client = self.pg0.remote_hosts[0] |
| 4720 | elif client_id == 2: |
| 4721 | client = self.pg0.remote_hosts[1] |
| 4722 | else: |
| 4723 | client = pg1.remote_hosts[0] |
| 4724 | p = (Ether(src=pg1.remote_mac, dst=pg1.local_mac) / |
| 4725 | IP(src=client.ip4, dst=self.nat_addr) / |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4726 | TCP(sport=eh_port_out, dport=port_out)) |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4727 | pg1.add_stream(p) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4728 | self.pg_enable_capture(self.pg_interfaces) |
| 4729 | self.pg_start() |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4730 | capture = pg0.get_capture(1) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4731 | p = capture[0] |
| 4732 | try: |
| 4733 | ip = p[IP] |
| 4734 | tcp = p[TCP] |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4735 | if lb: |
| 4736 | if ip.dst == server1.ip4: |
| 4737 | server = server1 |
| 4738 | port_in = port_in1 |
| 4739 | else: |
| 4740 | server = server2 |
| 4741 | port_in = port_in2 |
| 4742 | self.assertEqual(ip.dst, server.ip4) |
| 4743 | if lb and same_pg: |
| 4744 | self.assertIn(tcp.dport, [port_in1, port_in2]) |
| 4745 | else: |
| 4746 | self.assertEqual(tcp.dport, port_in) |
| 4747 | if eh_translate: |
| 4748 | self.assertEqual(ip.src, twice_nat_addr) |
| 4749 | self.assertNotEqual(tcp.sport, eh_port_out) |
| 4750 | else: |
| 4751 | self.assertEqual(ip.src, client.ip4) |
| 4752 | self.assertEqual(tcp.sport, eh_port_out) |
| 4753 | eh_addr_in = ip.src |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4754 | eh_port_in = tcp.sport |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4755 | saved_port_in = tcp.dport |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 4756 | self.assert_packet_checksums_valid(p) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4757 | except: |
| 4758 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4759 | raise |
| 4760 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4761 | p = (Ether(src=server.mac, dst=pg0.local_mac) / |
| 4762 | IP(src=server.ip4, dst=eh_addr_in) / |
| 4763 | TCP(sport=saved_port_in, dport=eh_port_in)) |
| 4764 | pg0.add_stream(p) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4765 | self.pg_enable_capture(self.pg_interfaces) |
| 4766 | self.pg_start() |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4767 | capture = pg1.get_capture(1) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4768 | p = capture[0] |
| 4769 | try: |
| 4770 | ip = p[IP] |
| 4771 | tcp = p[TCP] |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4772 | self.assertEqual(ip.dst, client.ip4) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4773 | self.assertEqual(ip.src, self.nat_addr) |
| 4774 | self.assertEqual(tcp.dport, eh_port_out) |
| 4775 | self.assertEqual(tcp.sport, port_out) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 4776 | self.assert_packet_checksums_valid(p) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4777 | except: |
| 4778 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4779 | raise |
| 4780 | |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 4781 | if eh_translate: |
| 4782 | sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0) |
| 4783 | self.assertEqual(len(sessions), 1) |
| 4784 | self.assertTrue(sessions[0].ext_host_valid) |
| 4785 | self.assertTrue(sessions[0].is_twicenat) |
| 4786 | self.vapi.nat44_del_session( |
| 4787 | sessions[0].inside_ip_address, |
| 4788 | sessions[0].inside_port, |
| 4789 | sessions[0].protocol, |
| 4790 | ext_host_address=sessions[0].ext_host_nat_address, |
| 4791 | ext_host_port=sessions[0].ext_host_nat_port) |
| 4792 | sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0) |
| 4793 | self.assertEqual(len(sessions), 0) |
| 4794 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4795 | def test_twice_nat(self): |
| 4796 | """ Twice NAT44 """ |
| 4797 | self.twice_nat_common() |
| 4798 | |
| 4799 | def test_self_twice_nat_positive(self): |
| 4800 | """ Self Twice NAT44 (positive test) """ |
| 4801 | self.twice_nat_common(self_twice_nat=True, same_pg=True) |
| 4802 | |
| 4803 | def test_self_twice_nat_negative(self): |
| 4804 | """ Self Twice NAT44 (negative test) """ |
| 4805 | self.twice_nat_common(self_twice_nat=True) |
| 4806 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4807 | def test_twice_nat_lb(self): |
| 4808 | """ Twice NAT44 local service load balancing """ |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4809 | self.twice_nat_common(lb=True) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4810 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4811 | def test_self_twice_nat_lb_positive(self): |
| 4812 | """ Self Twice NAT44 local service load balancing (positive test) """ |
| 4813 | self.twice_nat_common(lb=True, self_twice_nat=True, same_pg=True, |
| 4814 | client_id=1) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4815 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4816 | def test_self_twice_nat_lb_negative(self): |
| 4817 | """ Self Twice NAT44 local service load balancing (negative test) """ |
| 4818 | self.twice_nat_common(lb=True, self_twice_nat=True, same_pg=True, |
| 4819 | client_id=2) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4820 | |
| 4821 | def test_twice_nat_interface_addr(self): |
| 4822 | """ Acquire twice NAT44 addresses from interface """ |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4823 | self.vapi.nat44_add_interface_addr(self.pg3.sw_if_index, twice_nat=1) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4824 | |
| 4825 | # no address in NAT pool |
| 4826 | adresses = self.vapi.nat44_address_dump() |
| 4827 | self.assertEqual(0, len(adresses)) |
| 4828 | |
| 4829 | # configure interface address and check NAT address pool |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4830 | self.pg3.config_ip4() |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4831 | adresses = self.vapi.nat44_address_dump() |
| 4832 | self.assertEqual(1, len(adresses)) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4833 | self.assertEqual(adresses[0].ip_address[0:4], self.pg3.local_ip4n) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4834 | self.assertEqual(adresses[0].twice_nat, 1) |
| 4835 | |
| 4836 | # remove interface address and check NAT address pool |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4837 | self.pg3.unconfig_ip4() |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4838 | adresses = self.vapi.nat44_address_dump() |
| 4839 | self.assertEqual(0, len(adresses)) |
| 4840 | |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4841 | def test_tcp_session_close_in(self): |
| 4842 | """ Close TCP session from inside network """ |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4843 | self.tcp_port_out = 10505 |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4844 | self.nat44_add_address(self.nat_addr) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4845 | self.nat44_add_static_mapping(self.pg0.remote_ip4, |
| 4846 | self.nat_addr, |
| 4847 | self.tcp_port_in, |
| 4848 | self.tcp_port_out, |
| 4849 | proto=IP_PROTOS.tcp, |
| 4850 | twice_nat=1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4851 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4852 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4853 | is_inside=0) |
| 4854 | |
| 4855 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 4856 | start_sessnum = len(sessions) |
| 4857 | |
| 4858 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 4859 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4860 | # FIN packet in -> out |
| 4861 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4862 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4863 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 4864 | flags="FA", seq=100, ack=300)) |
| 4865 | self.pg0.add_stream(p) |
| 4866 | self.pg_enable_capture(self.pg_interfaces) |
| 4867 | self.pg_start() |
| 4868 | self.pg1.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4869 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4870 | pkts = [] |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4871 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4872 | # ACK packet out -> in |
| 4873 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4874 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4875 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 4876 | flags="A", seq=300, ack=101)) |
| 4877 | pkts.append(p) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4878 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4879 | # FIN packet out -> in |
| 4880 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4881 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4882 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 4883 | flags="FA", seq=300, ack=101)) |
| 4884 | pkts.append(p) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4885 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4886 | self.pg1.add_stream(pkts) |
| 4887 | self.pg_enable_capture(self.pg_interfaces) |
| 4888 | self.pg_start() |
| 4889 | self.pg0.get_capture(2) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4890 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4891 | # ACK packet in -> out |
| 4892 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4893 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4894 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 4895 | flags="A", seq=101, ack=301)) |
| 4896 | self.pg0.add_stream(p) |
| 4897 | self.pg_enable_capture(self.pg_interfaces) |
| 4898 | self.pg_start() |
| 4899 | self.pg1.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4900 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4901 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, |
| 4902 | 0) |
| 4903 | self.assertEqual(len(sessions) - start_sessnum, 0) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4904 | |
| 4905 | def test_tcp_session_close_out(self): |
| 4906 | """ Close TCP session from outside network """ |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4907 | self.tcp_port_out = 10505 |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4908 | self.nat44_add_address(self.nat_addr) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4909 | self.nat44_add_static_mapping(self.pg0.remote_ip4, |
| 4910 | self.nat_addr, |
| 4911 | self.tcp_port_in, |
| 4912 | self.tcp_port_out, |
| 4913 | proto=IP_PROTOS.tcp, |
| 4914 | twice_nat=1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4915 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4916 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4917 | is_inside=0) |
| 4918 | |
| 4919 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 4920 | start_sessnum = len(sessions) |
| 4921 | |
| 4922 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 4923 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4924 | # FIN packet out -> in |
| 4925 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4926 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4927 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 4928 | flags="FA", seq=100, ack=300)) |
| 4929 | self.pg1.add_stream(p) |
| 4930 | self.pg_enable_capture(self.pg_interfaces) |
| 4931 | self.pg_start() |
| 4932 | self.pg0.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4933 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4934 | # FIN+ACK packet in -> out |
| 4935 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4936 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4937 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 4938 | flags="FA", seq=300, ack=101)) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4939 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4940 | self.pg0.add_stream(p) |
| 4941 | self.pg_enable_capture(self.pg_interfaces) |
| 4942 | self.pg_start() |
| 4943 | self.pg1.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4944 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4945 | # ACK packet out -> in |
| 4946 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4947 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4948 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 4949 | flags="A", seq=101, ack=301)) |
| 4950 | self.pg1.add_stream(p) |
| 4951 | self.pg_enable_capture(self.pg_interfaces) |
| 4952 | self.pg_start() |
| 4953 | self.pg0.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4954 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4955 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, |
| 4956 | 0) |
| 4957 | self.assertEqual(len(sessions) - start_sessnum, 0) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4958 | |
| 4959 | def test_tcp_session_close_simultaneous(self): |
| 4960 | """ Close TCP session from inside network """ |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4961 | self.tcp_port_out = 10505 |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4962 | self.nat44_add_address(self.nat_addr) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4963 | self.nat44_add_static_mapping(self.pg0.remote_ip4, |
| 4964 | self.nat_addr, |
| 4965 | self.tcp_port_in, |
| 4966 | self.tcp_port_out, |
| 4967 | proto=IP_PROTOS.tcp, |
| 4968 | twice_nat=1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4969 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4970 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4971 | is_inside=0) |
| 4972 | |
| 4973 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 4974 | start_sessnum = len(sessions) |
| 4975 | |
| 4976 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 4977 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4978 | # FIN packet in -> out |
| 4979 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4980 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4981 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 4982 | flags="FA", seq=100, ack=300)) |
| 4983 | self.pg0.add_stream(p) |
| 4984 | self.pg_enable_capture(self.pg_interfaces) |
| 4985 | self.pg_start() |
| 4986 | self.pg1.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4987 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4988 | # FIN packet out -> in |
| 4989 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4990 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4991 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 4992 | flags="FA", seq=300, ack=100)) |
| 4993 | self.pg1.add_stream(p) |
| 4994 | self.pg_enable_capture(self.pg_interfaces) |
| 4995 | self.pg_start() |
| 4996 | self.pg0.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4997 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4998 | # ACK packet in -> out |
| 4999 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5000 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5001 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 5002 | flags="A", seq=101, ack=301)) |
| 5003 | self.pg0.add_stream(p) |
| 5004 | self.pg_enable_capture(self.pg_interfaces) |
| 5005 | self.pg_start() |
| 5006 | self.pg1.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5007 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5008 | # ACK packet out -> in |
| 5009 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5010 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5011 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 5012 | flags="A", seq=301, ack=101)) |
| 5013 | self.pg1.add_stream(p) |
| 5014 | self.pg_enable_capture(self.pg_interfaces) |
| 5015 | self.pg_start() |
| 5016 | self.pg0.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5017 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5018 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, |
| 5019 | 0) |
| 5020 | self.assertEqual(len(sessions) - start_sessnum, 0) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5021 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 5022 | def test_one_armed_nat44_static(self): |
| 5023 | """ One armed NAT44 and 1:1 NAPT asymmetrical rule """ |
| 5024 | remote_host = self.pg4.remote_hosts[0] |
| 5025 | local_host = self.pg4.remote_hosts[1] |
| 5026 | external_port = 80 |
| 5027 | local_port = 8080 |
| 5028 | eh_port_in = 0 |
| 5029 | |
| 5030 | self.vapi.nat44_forwarding_enable_disable(1) |
| 5031 | self.nat44_add_address(self.nat_addr, twice_nat=1) |
| 5032 | self.nat44_add_static_mapping(local_host.ip4, self.nat_addr, |
| 5033 | local_port, external_port, |
| 5034 | proto=IP_PROTOS.tcp, out2in_only=1, |
| 5035 | twice_nat=1) |
| 5036 | self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index) |
| 5037 | self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index, |
| 5038 | is_inside=0) |
| 5039 | |
| 5040 | # from client to service |
| 5041 | p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) / |
| 5042 | IP(src=remote_host.ip4, dst=self.nat_addr) / |
| 5043 | TCP(sport=12345, dport=external_port)) |
| 5044 | self.pg4.add_stream(p) |
| 5045 | self.pg_enable_capture(self.pg_interfaces) |
| 5046 | self.pg_start() |
| 5047 | capture = self.pg4.get_capture(1) |
| 5048 | p = capture[0] |
| 5049 | try: |
| 5050 | ip = p[IP] |
| 5051 | tcp = p[TCP] |
| 5052 | self.assertEqual(ip.dst, local_host.ip4) |
| 5053 | self.assertEqual(ip.src, self.nat_addr) |
| 5054 | self.assertEqual(tcp.dport, local_port) |
| 5055 | self.assertNotEqual(tcp.sport, 12345) |
| 5056 | eh_port_in = tcp.sport |
| 5057 | self.assert_packet_checksums_valid(p) |
| 5058 | except: |
| 5059 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5060 | raise |
| 5061 | |
| 5062 | # from service back to client |
| 5063 | p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) / |
| 5064 | IP(src=local_host.ip4, dst=self.nat_addr) / |
| 5065 | TCP(sport=local_port, dport=eh_port_in)) |
| 5066 | self.pg4.add_stream(p) |
| 5067 | self.pg_enable_capture(self.pg_interfaces) |
| 5068 | self.pg_start() |
| 5069 | capture = self.pg4.get_capture(1) |
| 5070 | p = capture[0] |
| 5071 | try: |
| 5072 | ip = p[IP] |
| 5073 | tcp = p[TCP] |
| 5074 | self.assertEqual(ip.src, self.nat_addr) |
| 5075 | self.assertEqual(ip.dst, remote_host.ip4) |
| 5076 | self.assertEqual(tcp.sport, external_port) |
| 5077 | self.assertEqual(tcp.dport, 12345) |
| 5078 | self.assert_packet_checksums_valid(p) |
| 5079 | except: |
| 5080 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5081 | raise |
| 5082 | |
| 5083 | def test_static_with_port_out2(self): |
| 5084 | """ 1:1 NAPT asymmetrical rule """ |
| 5085 | |
| 5086 | external_port = 80 |
| 5087 | local_port = 8080 |
| 5088 | |
| 5089 | self.vapi.nat44_forwarding_enable_disable(1) |
| 5090 | self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr, |
| 5091 | local_port, external_port, |
| 5092 | proto=IP_PROTOS.tcp, out2in_only=1) |
| 5093 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5094 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 5095 | is_inside=0) |
| 5096 | |
| 5097 | # from client to service |
| 5098 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5099 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5100 | TCP(sport=12345, dport=external_port)) |
| 5101 | self.pg1.add_stream(p) |
| 5102 | self.pg_enable_capture(self.pg_interfaces) |
| 5103 | self.pg_start() |
| 5104 | capture = self.pg0.get_capture(1) |
| 5105 | p = capture[0] |
| 5106 | try: |
| 5107 | ip = p[IP] |
| 5108 | tcp = p[TCP] |
| 5109 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 5110 | self.assertEqual(tcp.dport, local_port) |
| 5111 | self.assert_packet_checksums_valid(p) |
| 5112 | except: |
| 5113 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5114 | raise |
| 5115 | |
| 5116 | # ICMP error |
| 5117 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 5118 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5119 | ICMP(type=11) / capture[0][IP]) |
| 5120 | self.pg0.add_stream(p) |
| 5121 | self.pg_enable_capture(self.pg_interfaces) |
| 5122 | self.pg_start() |
| 5123 | capture = self.pg1.get_capture(1) |
| 5124 | p = capture[0] |
| 5125 | try: |
| 5126 | self.assertEqual(p[IP].src, self.nat_addr) |
| 5127 | inner = p[IPerror] |
| 5128 | self.assertEqual(inner.dst, self.nat_addr) |
| 5129 | self.assertEqual(inner[TCPerror].dport, external_port) |
| 5130 | except: |
| 5131 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5132 | raise |
| 5133 | |
| 5134 | # from service back to client |
| 5135 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5136 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5137 | TCP(sport=local_port, dport=12345)) |
| 5138 | self.pg0.add_stream(p) |
| 5139 | self.pg_enable_capture(self.pg_interfaces) |
| 5140 | self.pg_start() |
| 5141 | capture = self.pg1.get_capture(1) |
| 5142 | p = capture[0] |
| 5143 | try: |
| 5144 | ip = p[IP] |
| 5145 | tcp = p[TCP] |
| 5146 | self.assertEqual(ip.src, self.nat_addr) |
| 5147 | self.assertEqual(tcp.sport, external_port) |
| 5148 | self.assert_packet_checksums_valid(p) |
| 5149 | except: |
| 5150 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5151 | raise |
| 5152 | |
| 5153 | # ICMP error |
| 5154 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 5155 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5156 | ICMP(type=11) / capture[0][IP]) |
| 5157 | self.pg1.add_stream(p) |
| 5158 | self.pg_enable_capture(self.pg_interfaces) |
| 5159 | self.pg_start() |
| 5160 | capture = self.pg0.get_capture(1) |
| 5161 | p = capture[0] |
| 5162 | try: |
| 5163 | self.assertEqual(p[IP].dst, self.pg0.remote_ip4) |
| 5164 | inner = p[IPerror] |
| 5165 | self.assertEqual(inner.src, self.pg0.remote_ip4) |
| 5166 | self.assertEqual(inner[TCPerror].sport, local_port) |
| 5167 | except: |
| 5168 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5169 | raise |
| 5170 | |
| 5171 | # from client to server (no translation) |
| 5172 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5173 | IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) / |
| 5174 | TCP(sport=12346, dport=local_port)) |
| 5175 | self.pg1.add_stream(p) |
| 5176 | self.pg_enable_capture(self.pg_interfaces) |
| 5177 | self.pg_start() |
| 5178 | capture = self.pg0.get_capture(1) |
| 5179 | p = capture[0] |
| 5180 | try: |
| 5181 | ip = p[IP] |
| 5182 | tcp = p[TCP] |
| 5183 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 5184 | self.assertEqual(tcp.dport, local_port) |
| 5185 | self.assert_packet_checksums_valid(p) |
| 5186 | except: |
| 5187 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5188 | raise |
| 5189 | |
| 5190 | # from service back to client (no translation) |
| 5191 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5192 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5193 | TCP(sport=local_port, dport=12346)) |
| 5194 | self.pg0.add_stream(p) |
| 5195 | self.pg_enable_capture(self.pg_interfaces) |
| 5196 | self.pg_start() |
| 5197 | capture = self.pg1.get_capture(1) |
| 5198 | p = capture[0] |
| 5199 | try: |
| 5200 | ip = p[IP] |
| 5201 | tcp = p[TCP] |
| 5202 | self.assertEqual(ip.src, self.pg0.remote_ip4) |
| 5203 | self.assertEqual(tcp.sport, local_port) |
| 5204 | self.assert_packet_checksums_valid(p) |
| 5205 | except: |
| 5206 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5207 | raise |
| 5208 | |
Matus Fabian | 235a47e | 2018-06-25 16:42:36 -0700 | [diff] [blame] | 5209 | def test_output_feature(self): |
| 5210 | """ NAT44 interface output feature (in2out postrouting) """ |
| 5211 | self.vapi.nat44_forwarding_enable_disable(1) |
| 5212 | self.nat44_add_address(self.nat_addr) |
| 5213 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 5214 | is_inside=0) |
| 5215 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 5216 | is_inside=0) |
| 5217 | |
| 5218 | # in2out |
| 5219 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 5220 | self.pg0.add_stream(pkts) |
| 5221 | self.pg_enable_capture(self.pg_interfaces) |
| 5222 | self.pg_start() |
| 5223 | capture = self.pg1.get_capture(len(pkts)) |
| 5224 | self.verify_capture_out(capture) |
| 5225 | |
| 5226 | # out2in |
| 5227 | pkts = self.create_stream_out(self.pg1) |
| 5228 | self.pg1.add_stream(pkts) |
| 5229 | self.pg_enable_capture(self.pg_interfaces) |
| 5230 | self.pg_start() |
| 5231 | capture = self.pg0.get_capture(len(pkts)) |
| 5232 | self.verify_capture_in(capture, self.pg0) |
| 5233 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 5234 | def test_multiple_vrf(self): |
| 5235 | """ Multiple VRF setup """ |
| 5236 | external_addr = '1.2.3.4' |
| 5237 | external_port = 80 |
| 5238 | local_port = 8080 |
| 5239 | port = 0 |
| 5240 | |
| 5241 | self.vapi.nat44_forwarding_enable_disable(1) |
| 5242 | self.nat44_add_address(self.nat_addr) |
| 5243 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5244 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 5245 | is_inside=0) |
| 5246 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 5247 | is_inside=0) |
| 5248 | self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index) |
| 5249 | self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index, |
| 5250 | is_inside=0) |
| 5251 | self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index, |
| 5252 | is_inside=0) |
| 5253 | self.nat44_add_static_mapping(self.pg5.remote_ip4, external_addr, |
| 5254 | local_port, external_port, vrf_id=1, |
| 5255 | proto=IP_PROTOS.tcp, out2in_only=1) |
| 5256 | self.nat44_add_static_mapping( |
| 5257 | self.pg0.remote_ip4, external_sw_if_index=self.pg0.sw_if_index, |
| 5258 | local_port=local_port, vrf_id=0, external_port=external_port, |
| 5259 | proto=IP_PROTOS.tcp, out2in_only=1) |
| 5260 | |
| 5261 | # from client to service (both VRF1) |
| 5262 | p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) / |
| 5263 | IP(src=self.pg6.remote_ip4, dst=external_addr) / |
| 5264 | TCP(sport=12345, dport=external_port)) |
| 5265 | self.pg6.add_stream(p) |
| 5266 | self.pg_enable_capture(self.pg_interfaces) |
| 5267 | self.pg_start() |
| 5268 | capture = self.pg5.get_capture(1) |
| 5269 | p = capture[0] |
| 5270 | try: |
| 5271 | ip = p[IP] |
| 5272 | tcp = p[TCP] |
| 5273 | self.assertEqual(ip.dst, self.pg5.remote_ip4) |
| 5274 | self.assertEqual(tcp.dport, local_port) |
| 5275 | self.assert_packet_checksums_valid(p) |
| 5276 | except: |
| 5277 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5278 | raise |
| 5279 | |
| 5280 | # from service back to client (both VRF1) |
| 5281 | p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) / |
| 5282 | IP(src=self.pg5.remote_ip4, dst=self.pg6.remote_ip4) / |
| 5283 | TCP(sport=local_port, dport=12345)) |
| 5284 | self.pg5.add_stream(p) |
| 5285 | self.pg_enable_capture(self.pg_interfaces) |
| 5286 | self.pg_start() |
| 5287 | capture = self.pg6.get_capture(1) |
| 5288 | p = capture[0] |
| 5289 | try: |
| 5290 | ip = p[IP] |
| 5291 | tcp = p[TCP] |
| 5292 | self.assertEqual(ip.src, external_addr) |
| 5293 | self.assertEqual(tcp.sport, external_port) |
| 5294 | self.assert_packet_checksums_valid(p) |
| 5295 | except: |
| 5296 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5297 | raise |
| 5298 | |
| 5299 | # dynamic NAT from VRF1 to VRF0 (output-feature) |
| 5300 | p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) / |
| 5301 | IP(src=self.pg5.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5302 | TCP(sport=2345, dport=22)) |
| 5303 | self.pg5.add_stream(p) |
| 5304 | self.pg_enable_capture(self.pg_interfaces) |
| 5305 | self.pg_start() |
| 5306 | capture = self.pg1.get_capture(1) |
| 5307 | p = capture[0] |
| 5308 | try: |
| 5309 | ip = p[IP] |
| 5310 | tcp = p[TCP] |
| 5311 | self.assertEqual(ip.src, self.nat_addr) |
| 5312 | self.assertNotEqual(tcp.sport, 2345) |
| 5313 | self.assert_packet_checksums_valid(p) |
| 5314 | port = tcp.sport |
| 5315 | except: |
| 5316 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5317 | raise |
| 5318 | |
| 5319 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5320 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5321 | TCP(sport=22, dport=port)) |
| 5322 | self.pg1.add_stream(p) |
| 5323 | self.pg_enable_capture(self.pg_interfaces) |
| 5324 | self.pg_start() |
| 5325 | capture = self.pg5.get_capture(1) |
| 5326 | p = capture[0] |
| 5327 | try: |
| 5328 | ip = p[IP] |
| 5329 | tcp = p[TCP] |
| 5330 | self.assertEqual(ip.dst, self.pg5.remote_ip4) |
| 5331 | self.assertEqual(tcp.dport, 2345) |
| 5332 | self.assert_packet_checksums_valid(p) |
| 5333 | except: |
| 5334 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5335 | raise |
| 5336 | |
| 5337 | # from client VRF1 to service VRF0 |
| 5338 | p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) / |
| 5339 | IP(src=self.pg6.remote_ip4, dst=self.pg0.local_ip4) / |
| 5340 | TCP(sport=12346, dport=external_port)) |
| 5341 | self.pg6.add_stream(p) |
| 5342 | self.pg_enable_capture(self.pg_interfaces) |
| 5343 | self.pg_start() |
| 5344 | capture = self.pg0.get_capture(1) |
| 5345 | p = capture[0] |
| 5346 | try: |
| 5347 | ip = p[IP] |
| 5348 | tcp = p[TCP] |
| 5349 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 5350 | self.assertEqual(tcp.dport, local_port) |
| 5351 | self.assert_packet_checksums_valid(p) |
| 5352 | except: |
| 5353 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5354 | raise |
| 5355 | |
| 5356 | # from service VRF0 back to client VRF1 |
| 5357 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5358 | IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) / |
| 5359 | TCP(sport=local_port, dport=12346)) |
| 5360 | self.pg0.add_stream(p) |
| 5361 | self.pg_enable_capture(self.pg_interfaces) |
| 5362 | self.pg_start() |
| 5363 | capture = self.pg6.get_capture(1) |
| 5364 | p = capture[0] |
| 5365 | try: |
| 5366 | ip = p[IP] |
| 5367 | tcp = p[TCP] |
| 5368 | self.assertEqual(ip.src, self.pg0.local_ip4) |
| 5369 | self.assertEqual(tcp.sport, external_port) |
| 5370 | self.assert_packet_checksums_valid(p) |
| 5371 | except: |
| 5372 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5373 | raise |
| 5374 | |
| 5375 | # from client VRF0 to service VRF1 |
| 5376 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5377 | IP(src=self.pg0.remote_ip4, dst=external_addr) / |
| 5378 | TCP(sport=12347, dport=external_port)) |
| 5379 | self.pg0.add_stream(p) |
| 5380 | self.pg_enable_capture(self.pg_interfaces) |
| 5381 | self.pg_start() |
| 5382 | capture = self.pg5.get_capture(1) |
| 5383 | p = capture[0] |
| 5384 | try: |
| 5385 | ip = p[IP] |
| 5386 | tcp = p[TCP] |
| 5387 | self.assertEqual(ip.dst, self.pg5.remote_ip4) |
| 5388 | self.assertEqual(tcp.dport, local_port) |
| 5389 | self.assert_packet_checksums_valid(p) |
| 5390 | except: |
| 5391 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5392 | raise |
| 5393 | |
| 5394 | # from service VRF1 back to client VRF0 |
| 5395 | p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) / |
| 5396 | IP(src=self.pg5.remote_ip4, dst=self.pg0.remote_ip4) / |
| 5397 | TCP(sport=local_port, dport=12347)) |
| 5398 | self.pg5.add_stream(p) |
| 5399 | self.pg_enable_capture(self.pg_interfaces) |
| 5400 | self.pg_start() |
| 5401 | capture = self.pg0.get_capture(1) |
| 5402 | p = capture[0] |
| 5403 | try: |
| 5404 | ip = p[IP] |
| 5405 | tcp = p[TCP] |
| 5406 | self.assertEqual(ip.src, external_addr) |
| 5407 | self.assertEqual(tcp.sport, external_port) |
| 5408 | self.assert_packet_checksums_valid(p) |
| 5409 | except: |
| 5410 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5411 | raise |
| 5412 | |
| 5413 | # from client to server (both VRF1, no translation) |
| 5414 | p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) / |
| 5415 | IP(src=self.pg6.remote_ip4, dst=self.pg5.remote_ip4) / |
| 5416 | TCP(sport=12348, dport=local_port)) |
| 5417 | self.pg6.add_stream(p) |
| 5418 | self.pg_enable_capture(self.pg_interfaces) |
| 5419 | self.pg_start() |
| 5420 | capture = self.pg5.get_capture(1) |
| 5421 | p = capture[0] |
| 5422 | try: |
| 5423 | ip = p[IP] |
| 5424 | tcp = p[TCP] |
| 5425 | self.assertEqual(ip.dst, self.pg5.remote_ip4) |
| 5426 | self.assertEqual(tcp.dport, local_port) |
| 5427 | self.assert_packet_checksums_valid(p) |
| 5428 | except: |
| 5429 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5430 | raise |
| 5431 | |
| 5432 | # from server back to client (both VRF1, no translation) |
| 5433 | p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) / |
| 5434 | IP(src=self.pg5.remote_ip4, dst=self.pg6.remote_ip4) / |
| 5435 | TCP(sport=local_port, dport=12348)) |
| 5436 | self.pg5.add_stream(p) |
| 5437 | self.pg_enable_capture(self.pg_interfaces) |
| 5438 | self.pg_start() |
| 5439 | capture = self.pg6.get_capture(1) |
| 5440 | p = capture[0] |
| 5441 | try: |
| 5442 | ip = p[IP] |
| 5443 | tcp = p[TCP] |
| 5444 | self.assertEqual(ip.src, self.pg5.remote_ip4) |
| 5445 | self.assertEqual(tcp.sport, local_port) |
| 5446 | self.assert_packet_checksums_valid(p) |
| 5447 | except: |
| 5448 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5449 | raise |
| 5450 | |
| 5451 | # from client VRF1 to server VRF0 (no translation) |
| 5452 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5453 | IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) / |
| 5454 | TCP(sport=local_port, dport=12349)) |
| 5455 | self.pg0.add_stream(p) |
| 5456 | self.pg_enable_capture(self.pg_interfaces) |
| 5457 | self.pg_start() |
| 5458 | capture = self.pg6.get_capture(1) |
| 5459 | p = capture[0] |
| 5460 | try: |
| 5461 | ip = p[IP] |
| 5462 | tcp = p[TCP] |
| 5463 | self.assertEqual(ip.src, self.pg0.remote_ip4) |
| 5464 | self.assertEqual(tcp.sport, local_port) |
| 5465 | self.assert_packet_checksums_valid(p) |
| 5466 | except: |
| 5467 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5468 | raise |
| 5469 | |
| 5470 | # from server VRF0 back to client VRF1 (no translation) |
| 5471 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5472 | IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) / |
| 5473 | TCP(sport=local_port, dport=12349)) |
| 5474 | self.pg0.add_stream(p) |
| 5475 | self.pg_enable_capture(self.pg_interfaces) |
| 5476 | self.pg_start() |
| 5477 | capture = self.pg6.get_capture(1) |
| 5478 | p = capture[0] |
| 5479 | try: |
| 5480 | ip = p[IP] |
| 5481 | tcp = p[TCP] |
| 5482 | self.assertEqual(ip.src, self.pg0.remote_ip4) |
| 5483 | self.assertEqual(tcp.sport, local_port) |
| 5484 | self.assert_packet_checksums_valid(p) |
| 5485 | except: |
| 5486 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5487 | raise |
| 5488 | |
| 5489 | # from client VRF0 to server VRF1 (no translation) |
| 5490 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5491 | IP(src=self.pg0.remote_ip4, dst=self.pg5.remote_ip4) / |
| 5492 | TCP(sport=12344, dport=local_port)) |
| 5493 | self.pg0.add_stream(p) |
| 5494 | self.pg_enable_capture(self.pg_interfaces) |
| 5495 | self.pg_start() |
| 5496 | capture = self.pg5.get_capture(1) |
| 5497 | p = capture[0] |
| 5498 | try: |
| 5499 | ip = p[IP] |
| 5500 | tcp = p[TCP] |
| 5501 | self.assertEqual(ip.dst, self.pg5.remote_ip4) |
| 5502 | self.assertEqual(tcp.dport, local_port) |
| 5503 | self.assert_packet_checksums_valid(p) |
| 5504 | except: |
| 5505 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5506 | raise |
| 5507 | |
| 5508 | # from server VRF1 back to client VRF0 (no translation) |
| 5509 | p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) / |
| 5510 | IP(src=self.pg5.remote_ip4, dst=self.pg0.remote_ip4) / |
| 5511 | TCP(sport=local_port, dport=12344)) |
| 5512 | self.pg5.add_stream(p) |
| 5513 | self.pg_enable_capture(self.pg_interfaces) |
| 5514 | self.pg_start() |
| 5515 | capture = self.pg0.get_capture(1) |
| 5516 | p = capture[0] |
| 5517 | try: |
| 5518 | ip = p[IP] |
| 5519 | tcp = p[TCP] |
| 5520 | self.assertEqual(ip.src, self.pg5.remote_ip4) |
| 5521 | self.assertEqual(tcp.sport, local_port) |
| 5522 | self.assert_packet_checksums_valid(p) |
| 5523 | except: |
| 5524 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5525 | raise |
| 5526 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5527 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 5528 | def test_session_timeout(self): |
| 5529 | """ NAT44 session timeouts """ |
| 5530 | self.nat44_add_address(self.nat_addr) |
| 5531 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5532 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 5533 | is_inside=0) |
| 5534 | self.vapi.nat_set_timeouts(icmp=5) |
| 5535 | |
| 5536 | max_sessions = 1000 |
| 5537 | pkts = [] |
| 5538 | for i in range(0, max_sessions): |
| 5539 | src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF) |
| 5540 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 5541 | IP(src=src, dst=self.pg1.remote_ip4) / |
| 5542 | ICMP(id=1025, type='echo-request')) |
| 5543 | pkts.append(p) |
| 5544 | self.pg0.add_stream(pkts) |
| 5545 | self.pg_enable_capture(self.pg_interfaces) |
| 5546 | self.pg_start() |
| 5547 | self.pg1.get_capture(max_sessions) |
| 5548 | |
| 5549 | sleep(10) |
| 5550 | |
| 5551 | pkts = [] |
| 5552 | for i in range(0, max_sessions): |
| 5553 | src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF) |
| 5554 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 5555 | IP(src=src, dst=self.pg1.remote_ip4) / |
| 5556 | ICMP(id=1026, type='echo-request')) |
| 5557 | pkts.append(p) |
| 5558 | self.pg0.add_stream(pkts) |
| 5559 | self.pg_enable_capture(self.pg_interfaces) |
| 5560 | self.pg_start() |
| 5561 | self.pg1.get_capture(max_sessions) |
| 5562 | |
| 5563 | nsessions = 0 |
| 5564 | users = self.vapi.nat44_user_dump() |
| 5565 | for user in users: |
| 5566 | nsessions = nsessions + user.nsessions |
| 5567 | self.assertLess(nsessions, 2 * max_sessions) |
| 5568 | |
| 5569 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 5570 | def test_session_limit_per_user(self): |
| 5571 | """ Maximum sessions per user limit """ |
| 5572 | self.nat44_add_address(self.nat_addr) |
| 5573 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5574 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 5575 | is_inside=0) |
| 5576 | self.vapi.set_ipfix_exporter(collector_address=self.pg2.remote_ip4n, |
| 5577 | src_address=self.pg2.local_ip4n, |
| 5578 | path_mtu=512, |
| 5579 | template_interval=10) |
Matus Fabian | 8fdc015 | 2018-09-24 04:41:28 -0700 | [diff] [blame] | 5580 | self.vapi.nat_set_timeouts(udp=5) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5581 | |
| 5582 | # get maximum number of translations per user |
| 5583 | nat44_config = self.vapi.nat_show_config() |
| 5584 | |
| 5585 | pkts = [] |
| 5586 | for port in range(0, nat44_config.max_translations_per_user): |
| 5587 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5588 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5589 | UDP(sport=1025 + port, dport=1025 + port)) |
| 5590 | pkts.append(p) |
| 5591 | |
| 5592 | self.pg0.add_stream(pkts) |
| 5593 | self.pg_enable_capture(self.pg_interfaces) |
| 5594 | self.pg_start() |
| 5595 | capture = self.pg1.get_capture(len(pkts)) |
| 5596 | |
| 5597 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 5598 | src_port=self.ipfix_src_port) |
| 5599 | |
| 5600 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5601 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5602 | UDP(sport=3001, dport=3002)) |
| 5603 | self.pg0.add_stream(p) |
| 5604 | self.pg_enable_capture(self.pg_interfaces) |
| 5605 | self.pg_start() |
| 5606 | capture = self.pg1.assert_nothing_captured() |
| 5607 | |
| 5608 | # verify IPFIX logging |
| 5609 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 5610 | sleep(1) |
| 5611 | capture = self.pg2.get_capture(10) |
| 5612 | ipfix = IPFIXDecoder() |
| 5613 | # first load template |
| 5614 | for p in capture: |
| 5615 | self.assertTrue(p.haslayer(IPFIX)) |
| 5616 | if p.haslayer(Template): |
| 5617 | ipfix.add_template(p.getlayer(Template)) |
| 5618 | # verify events in data set |
| 5619 | for p in capture: |
| 5620 | if p.haslayer(Data): |
| 5621 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 5622 | self.verify_ipfix_max_entries_per_user( |
| 5623 | data, |
| 5624 | nat44_config.max_translations_per_user, |
| 5625 | self.pg0.remote_ip4n) |
| 5626 | |
Matus Fabian | 8fdc015 | 2018-09-24 04:41:28 -0700 | [diff] [blame] | 5627 | sleep(6) |
| 5628 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5629 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5630 | UDP(sport=3001, dport=3002)) |
| 5631 | self.pg0.add_stream(p) |
| 5632 | self.pg_enable_capture(self.pg_interfaces) |
| 5633 | self.pg_start() |
| 5634 | self.pg1.get_capture(1) |
| 5635 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 5636 | def tearDown(self): |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 5637 | super(TestNAT44EndpointDependent, self).tearDown() |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 5638 | if not self.vpp_dead: |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 5639 | self.logger.info(self.vapi.cli("show nat44 addresses")) |
| 5640 | self.logger.info(self.vapi.cli("show nat44 interfaces")) |
| 5641 | self.logger.info(self.vapi.cli("show nat44 static mappings")) |
| 5642 | self.logger.info(self.vapi.cli("show nat44 interface address")) |
| 5643 | self.logger.info(self.vapi.cli("show nat44 sessions detail")) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5644 | self.logger.info(self.vapi.cli("show nat44 hash tables detail")) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5645 | self.logger.info(self.vapi.cli("show nat timeouts")) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5646 | self.clear_nat44() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5647 | self.vapi.cli("clear logging") |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 5648 | |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 5649 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 5650 | class TestNAT44Out2InDPO(MethodHolder): |
| 5651 | """ NAT44 Test Cases using out2in DPO """ |
| 5652 | |
| 5653 | @classmethod |
| 5654 | def setUpConstants(cls): |
| 5655 | super(TestNAT44Out2InDPO, cls).setUpConstants() |
| 5656 | cls.vpp_cmdline.extend(["nat", "{", "out2in dpo", "}"]) |
| 5657 | |
| 5658 | @classmethod |
| 5659 | def setUpClass(cls): |
| 5660 | super(TestNAT44Out2InDPO, cls).setUpClass() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5661 | cls.vapi.cli("set log class nat level debug") |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 5662 | |
| 5663 | try: |
| 5664 | cls.tcp_port_in = 6303 |
| 5665 | cls.tcp_port_out = 6303 |
| 5666 | cls.udp_port_in = 6304 |
| 5667 | cls.udp_port_out = 6304 |
| 5668 | cls.icmp_id_in = 6305 |
| 5669 | cls.icmp_id_out = 6305 |
| 5670 | cls.nat_addr = '10.0.0.3' |
| 5671 | cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr) |
| 5672 | cls.dst_ip4 = '192.168.70.1' |
| 5673 | |
| 5674 | cls.create_pg_interfaces(range(2)) |
| 5675 | |
| 5676 | cls.pg0.admin_up() |
| 5677 | cls.pg0.config_ip4() |
| 5678 | cls.pg0.resolve_arp() |
| 5679 | |
| 5680 | cls.pg1.admin_up() |
| 5681 | cls.pg1.config_ip6() |
| 5682 | cls.pg1.resolve_ndp() |
| 5683 | |
| 5684 | cls.vapi.ip_add_del_route(is_ipv6=True, dst_address='\x00'*16, |
| 5685 | dst_address_length=0, |
| 5686 | next_hop_address=cls.pg1.remote_ip6n, |
| 5687 | next_hop_sw_if_index=cls.pg1.sw_if_index) |
| 5688 | |
| 5689 | except Exception: |
| 5690 | super(TestNAT44Out2InDPO, cls).tearDownClass() |
| 5691 | raise |
| 5692 | |
| 5693 | def configure_xlat(self): |
| 5694 | self.dst_ip6_pfx = '1:2:3::' |
| 5695 | self.dst_ip6_pfx_n = socket.inet_pton(socket.AF_INET6, |
| 5696 | self.dst_ip6_pfx) |
| 5697 | self.dst_ip6_pfx_len = 96 |
| 5698 | self.src_ip6_pfx = '4:5:6::' |
| 5699 | self.src_ip6_pfx_n = socket.inet_pton(socket.AF_INET6, |
| 5700 | self.src_ip6_pfx) |
| 5701 | self.src_ip6_pfx_len = 96 |
| 5702 | self.vapi.map_add_domain(self.dst_ip6_pfx_n, self.dst_ip6_pfx_len, |
| 5703 | self.src_ip6_pfx_n, self.src_ip6_pfx_len, |
| 5704 | '\x00\x00\x00\x00', 0, is_translation=1, |
| 5705 | is_rfc6052=1) |
| 5706 | |
| 5707 | def test_464xlat_ce(self): |
| 5708 | """ Test 464XLAT CE with NAT44 """ |
| 5709 | |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 5710 | nat_config = self.vapi.nat_show_config() |
| 5711 | self.assertEqual(1, nat_config.out2in_dpo) |
| 5712 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 5713 | self.configure_xlat() |
| 5714 | |
| 5715 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5716 | self.vapi.nat44_add_del_address_range(self.nat_addr_n, self.nat_addr_n) |
| 5717 | |
| 5718 | out_src_ip6 = self.compose_ip6(self.dst_ip4, self.dst_ip6_pfx, |
| 5719 | self.dst_ip6_pfx_len) |
| 5720 | out_dst_ip6 = self.compose_ip6(self.nat_addr, self.src_ip6_pfx, |
| 5721 | self.src_ip6_pfx_len) |
| 5722 | |
| 5723 | try: |
| 5724 | pkts = self.create_stream_in(self.pg0, self.pg1, self.dst_ip4) |
| 5725 | self.pg0.add_stream(pkts) |
| 5726 | self.pg_enable_capture(self.pg_interfaces) |
| 5727 | self.pg_start() |
| 5728 | capture = self.pg1.get_capture(len(pkts)) |
| 5729 | self.verify_capture_out_ip6(capture, nat_ip=out_dst_ip6, |
| 5730 | dst_ip=out_src_ip6) |
| 5731 | |
| 5732 | pkts = self.create_stream_out_ip6(self.pg1, out_src_ip6, |
| 5733 | out_dst_ip6) |
| 5734 | self.pg1.add_stream(pkts) |
| 5735 | self.pg_enable_capture(self.pg_interfaces) |
| 5736 | self.pg_start() |
| 5737 | capture = self.pg0.get_capture(len(pkts)) |
| 5738 | self.verify_capture_in(capture, self.pg0) |
| 5739 | finally: |
| 5740 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 5741 | is_add=0) |
| 5742 | self.vapi.nat44_add_del_address_range(self.nat_addr_n, |
| 5743 | self.nat_addr_n, is_add=0) |
| 5744 | |
| 5745 | def test_464xlat_ce_no_nat(self): |
| 5746 | """ Test 464XLAT CE without NAT44 """ |
| 5747 | |
| 5748 | self.configure_xlat() |
| 5749 | |
| 5750 | out_src_ip6 = self.compose_ip6(self.dst_ip4, self.dst_ip6_pfx, |
| 5751 | self.dst_ip6_pfx_len) |
| 5752 | out_dst_ip6 = self.compose_ip6(self.pg0.remote_ip4, self.src_ip6_pfx, |
| 5753 | self.src_ip6_pfx_len) |
| 5754 | |
| 5755 | pkts = self.create_stream_in(self.pg0, self.pg1, self.dst_ip4) |
| 5756 | self.pg0.add_stream(pkts) |
| 5757 | self.pg_enable_capture(self.pg_interfaces) |
| 5758 | self.pg_start() |
| 5759 | capture = self.pg1.get_capture(len(pkts)) |
| 5760 | self.verify_capture_out_ip6(capture, dst_ip=out_src_ip6, |
| 5761 | nat_ip=out_dst_ip6, same_port=True) |
| 5762 | |
| 5763 | pkts = self.create_stream_out_ip6(self.pg1, out_src_ip6, out_dst_ip6) |
| 5764 | self.pg1.add_stream(pkts) |
| 5765 | self.pg_enable_capture(self.pg_interfaces) |
| 5766 | self.pg_start() |
| 5767 | capture = self.pg0.get_capture(len(pkts)) |
| 5768 | self.verify_capture_in(capture, self.pg0) |
| 5769 | |
| 5770 | |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 5771 | class TestDeterministicNAT(MethodHolder): |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5772 | """ Deterministic NAT Test Cases """ |
| 5773 | |
| 5774 | @classmethod |
| 5775 | def setUpConstants(cls): |
| 5776 | super(TestDeterministicNAT, cls).setUpConstants() |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5777 | cls.vpp_cmdline.extend(["nat", "{", "deterministic", "}"]) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5778 | |
| 5779 | @classmethod |
| 5780 | def setUpClass(cls): |
| 5781 | super(TestDeterministicNAT, cls).setUpClass() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5782 | cls.vapi.cli("set log class nat level debug") |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5783 | |
| 5784 | try: |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5785 | cls.tcp_port_in = 6303 |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5786 | cls.tcp_external_port = 6303 |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5787 | cls.udp_port_in = 6304 |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5788 | cls.udp_external_port = 6304 |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5789 | cls.icmp_id_in = 6305 |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5790 | cls.nat_addr = '10.0.0.3' |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5791 | |
Matus Fabian | 2f2db1c | 2017-04-18 05:29:59 -0700 | [diff] [blame] | 5792 | cls.create_pg_interfaces(range(3)) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5793 | cls.interfaces = list(cls.pg_interfaces) |
| 5794 | |
| 5795 | for i in cls.interfaces: |
| 5796 | i.admin_up() |
| 5797 | i.config_ip4() |
| 5798 | i.resolve_arp() |
| 5799 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5800 | cls.pg0.generate_remote_hosts(2) |
| 5801 | cls.pg0.configure_ipv4_neighbors() |
| 5802 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5803 | except Exception: |
| 5804 | super(TestDeterministicNAT, cls).tearDownClass() |
| 5805 | raise |
| 5806 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5807 | def create_stream_in(self, in_if, out_if, ttl=64): |
| 5808 | """ |
| 5809 | Create packet stream for inside network |
| 5810 | |
| 5811 | :param in_if: Inside interface |
| 5812 | :param out_if: Outside interface |
| 5813 | :param ttl: TTL of generated packets |
| 5814 | """ |
| 5815 | pkts = [] |
| 5816 | # TCP |
| 5817 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
| 5818 | IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=ttl) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5819 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5820 | pkts.append(p) |
| 5821 | |
| 5822 | # UDP |
| 5823 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
| 5824 | IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=ttl) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5825 | UDP(sport=self.udp_port_in, dport=self.udp_external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5826 | pkts.append(p) |
| 5827 | |
| 5828 | # ICMP |
| 5829 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
| 5830 | IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=ttl) / |
| 5831 | ICMP(id=self.icmp_id_in, type='echo-request')) |
| 5832 | pkts.append(p) |
| 5833 | |
| 5834 | return pkts |
| 5835 | |
| 5836 | def create_stream_out(self, out_if, dst_ip=None, ttl=64): |
| 5837 | """ |
| 5838 | Create packet stream for outside network |
| 5839 | |
| 5840 | :param out_if: Outside interface |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5841 | :param dst_ip: Destination IP address (Default use global NAT address) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5842 | :param ttl: TTL of generated packets |
| 5843 | """ |
| 5844 | if dst_ip is None: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5845 | dst_ip = self.nat_addr |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5846 | pkts = [] |
| 5847 | # TCP |
| 5848 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
| 5849 | IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5850 | TCP(dport=self.tcp_port_out, sport=self.tcp_external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5851 | pkts.append(p) |
| 5852 | |
| 5853 | # UDP |
| 5854 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
| 5855 | IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5856 | UDP(dport=self.udp_port_out, sport=self.udp_external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5857 | pkts.append(p) |
| 5858 | |
| 5859 | # ICMP |
| 5860 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
| 5861 | IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
| 5862 | ICMP(id=self.icmp_external_id, type='echo-reply')) |
| 5863 | pkts.append(p) |
| 5864 | |
| 5865 | return pkts |
| 5866 | |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 5867 | def verify_capture_out(self, capture, nat_ip=None): |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5868 | """ |
| 5869 | Verify captured packets on outside network |
| 5870 | |
| 5871 | :param capture: Captured packets |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5872 | :param nat_ip: Translated IP address (Default use global NAT address) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5873 | :param same_port: Sorce port number is not translated (Default False) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5874 | """ |
| 5875 | if nat_ip is None: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5876 | nat_ip = self.nat_addr |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5877 | for packet in capture: |
| 5878 | try: |
| 5879 | self.assertEqual(packet[IP].src, nat_ip) |
| 5880 | if packet.haslayer(TCP): |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5881 | self.tcp_port_out = packet[TCP].sport |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5882 | elif packet.haslayer(UDP): |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5883 | self.udp_port_out = packet[UDP].sport |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5884 | else: |
| 5885 | self.icmp_external_id = packet[ICMP].id |
| 5886 | except: |
| 5887 | self.logger.error(ppp("Unexpected or invalid packet " |
| 5888 | "(outside network):", packet)) |
| 5889 | raise |
| 5890 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5891 | def test_deterministic_mode(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5892 | """ NAT plugin run deterministic mode """ |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5893 | in_addr = '172.16.255.0' |
| 5894 | out_addr = '172.17.255.50' |
| 5895 | in_addr_t = '172.16.255.20' |
| 5896 | in_addr_n = socket.inet_aton(in_addr) |
| 5897 | out_addr_n = socket.inet_aton(out_addr) |
| 5898 | in_addr_t_n = socket.inet_aton(in_addr_t) |
| 5899 | in_plen = 24 |
| 5900 | out_plen = 32 |
| 5901 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5902 | nat_config = self.vapi.nat_show_config() |
| 5903 | self.assertEqual(1, nat_config.deterministic) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5904 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5905 | self.vapi.nat_det_add_del_map(in_addr_n, in_plen, out_addr_n, out_plen) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5906 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5907 | rep1 = self.vapi.nat_det_forward(in_addr_t_n) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5908 | self.assertEqual(rep1.out_addr[:4], out_addr_n) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5909 | rep2 = self.vapi.nat_det_reverse(out_addr_n, rep1.out_port_hi) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5910 | self.assertEqual(rep2.in_addr[:4], in_addr_t_n) |
| 5911 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5912 | deterministic_mappings = self.vapi.nat_det_map_dump() |
Martin | 17a75cb | 2017-03-08 05:53:20 -0800 | [diff] [blame] | 5913 | self.assertEqual(len(deterministic_mappings), 1) |
| 5914 | dsm = deterministic_mappings[0] |
| 5915 | self.assertEqual(in_addr_n, dsm.in_addr[:4]) |
| 5916 | self.assertEqual(in_plen, dsm.in_plen) |
| 5917 | self.assertEqual(out_addr_n, dsm.out_addr[:4]) |
| 5918 | self.assertEqual(out_plen, dsm.out_plen) |
| 5919 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5920 | self.clear_nat_det() |
| 5921 | deterministic_mappings = self.vapi.nat_det_map_dump() |
Martin | b616e9f | 2017-03-14 02:25:45 -0700 | [diff] [blame] | 5922 | self.assertEqual(len(deterministic_mappings), 0) |
| 5923 | |
Matus Fabian | 6a0946f | 2017-04-12 03:36:13 -0700 | [diff] [blame] | 5924 | def test_set_timeouts(self): |
| 5925 | """ Set deterministic NAT timeouts """ |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5926 | timeouts_before = self.vapi.nat_get_timeouts() |
Matus Fabian | 6a0946f | 2017-04-12 03:36:13 -0700 | [diff] [blame] | 5927 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5928 | self.vapi.nat_set_timeouts(timeouts_before.udp + 10, |
| 5929 | timeouts_before.tcp_established + 10, |
| 5930 | timeouts_before.tcp_transitory + 10, |
| 5931 | timeouts_before.icmp + 10) |
Matus Fabian | 6a0946f | 2017-04-12 03:36:13 -0700 | [diff] [blame] | 5932 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5933 | timeouts_after = self.vapi.nat_get_timeouts() |
Matus Fabian | 6a0946f | 2017-04-12 03:36:13 -0700 | [diff] [blame] | 5934 | |
| 5935 | self.assertNotEqual(timeouts_before.udp, timeouts_after.udp) |
| 5936 | self.assertNotEqual(timeouts_before.icmp, timeouts_after.icmp) |
| 5937 | self.assertNotEqual(timeouts_before.tcp_established, |
| 5938 | timeouts_after.tcp_established) |
| 5939 | self.assertNotEqual(timeouts_before.tcp_transitory, |
| 5940 | timeouts_after.tcp_transitory) |
| 5941 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5942 | def test_det_in(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5943 | """ Deterministic NAT translation test (TCP, UDP, ICMP) """ |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5944 | |
| 5945 | nat_ip = "10.0.0.10" |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5946 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5947 | self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n, |
| 5948 | 32, |
| 5949 | socket.inet_aton(nat_ip), |
| 5950 | 32) |
| 5951 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5952 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 5953 | is_inside=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5954 | |
| 5955 | # in2out |
| 5956 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 5957 | self.pg0.add_stream(pkts) |
| 5958 | self.pg_enable_capture(self.pg_interfaces) |
| 5959 | self.pg_start() |
| 5960 | capture = self.pg1.get_capture(len(pkts)) |
| 5961 | self.verify_capture_out(capture, nat_ip) |
| 5962 | |
| 5963 | # out2in |
| 5964 | pkts = self.create_stream_out(self.pg1, nat_ip) |
| 5965 | self.pg1.add_stream(pkts) |
| 5966 | self.pg_enable_capture(self.pg_interfaces) |
| 5967 | self.pg_start() |
| 5968 | capture = self.pg0.get_capture(len(pkts)) |
| 5969 | self.verify_capture_in(capture, self.pg0) |
| 5970 | |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5971 | # session dump test |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5972 | sessions = self.vapi.nat_det_session_dump(self.pg0.remote_ip4n) |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5973 | self.assertEqual(len(sessions), 3) |
| 5974 | |
| 5975 | # TCP session |
| 5976 | s = sessions[0] |
| 5977 | self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n) |
| 5978 | self.assertEqual(s.in_port, self.tcp_port_in) |
| 5979 | self.assertEqual(s.out_port, self.tcp_port_out) |
| 5980 | self.assertEqual(s.ext_port, self.tcp_external_port) |
| 5981 | |
| 5982 | # UDP session |
| 5983 | s = sessions[1] |
| 5984 | self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n) |
| 5985 | self.assertEqual(s.in_port, self.udp_port_in) |
| 5986 | self.assertEqual(s.out_port, self.udp_port_out) |
| 5987 | self.assertEqual(s.ext_port, self.udp_external_port) |
| 5988 | |
| 5989 | # ICMP session |
| 5990 | s = sessions[2] |
| 5991 | self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n) |
| 5992 | self.assertEqual(s.in_port, self.icmp_id_in) |
| 5993 | self.assertEqual(s.out_port, self.icmp_external_id) |
| 5994 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5995 | def test_multiple_users(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5996 | """ Deterministic NAT multiple users """ |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5997 | |
| 5998 | nat_ip = "10.0.0.10" |
| 5999 | port_in = 80 |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6000 | external_port = 6303 |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6001 | |
| 6002 | host0 = self.pg0.remote_hosts[0] |
| 6003 | host1 = self.pg0.remote_hosts[1] |
| 6004 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6005 | self.vapi.nat_det_add_del_map(host0.ip4n, |
| 6006 | 24, |
| 6007 | socket.inet_aton(nat_ip), |
| 6008 | 32) |
| 6009 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6010 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6011 | is_inside=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6012 | |
| 6013 | # host0 to out |
| 6014 | p = (Ether(src=host0.mac, dst=self.pg0.local_mac) / |
| 6015 | IP(src=host0.ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6016 | TCP(sport=port_in, dport=external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6017 | self.pg0.add_stream(p) |
| 6018 | self.pg_enable_capture(self.pg_interfaces) |
| 6019 | self.pg_start() |
| 6020 | capture = self.pg1.get_capture(1) |
| 6021 | p = capture[0] |
| 6022 | try: |
| 6023 | ip = p[IP] |
| 6024 | tcp = p[TCP] |
| 6025 | self.assertEqual(ip.src, nat_ip) |
| 6026 | self.assertEqual(ip.dst, self.pg1.remote_ip4) |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6027 | self.assertEqual(tcp.dport, external_port) |
| 6028 | port_out0 = tcp.sport |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6029 | except: |
| 6030 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 6031 | raise |
| 6032 | |
| 6033 | # host1 to out |
| 6034 | p = (Ether(src=host1.mac, dst=self.pg0.local_mac) / |
| 6035 | IP(src=host1.ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6036 | TCP(sport=port_in, dport=external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6037 | self.pg0.add_stream(p) |
| 6038 | self.pg_enable_capture(self.pg_interfaces) |
| 6039 | self.pg_start() |
| 6040 | capture = self.pg1.get_capture(1) |
| 6041 | p = capture[0] |
| 6042 | try: |
| 6043 | ip = p[IP] |
| 6044 | tcp = p[TCP] |
| 6045 | self.assertEqual(ip.src, nat_ip) |
| 6046 | self.assertEqual(ip.dst, self.pg1.remote_ip4) |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6047 | self.assertEqual(tcp.dport, external_port) |
| 6048 | port_out1 = tcp.sport |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6049 | except: |
| 6050 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 6051 | raise |
| 6052 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6053 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6054 | self.assertEqual(1, len(dms)) |
| 6055 | self.assertEqual(2, dms[0].ses_num) |
| 6056 | |
| 6057 | # out to host0 |
| 6058 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 6059 | IP(src=self.pg1.remote_ip4, dst=nat_ip) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6060 | TCP(sport=external_port, dport=port_out0)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6061 | self.pg1.add_stream(p) |
| 6062 | self.pg_enable_capture(self.pg_interfaces) |
| 6063 | self.pg_start() |
| 6064 | capture = self.pg0.get_capture(1) |
| 6065 | p = capture[0] |
| 6066 | try: |
| 6067 | ip = p[IP] |
| 6068 | tcp = p[TCP] |
| 6069 | self.assertEqual(ip.src, self.pg1.remote_ip4) |
| 6070 | self.assertEqual(ip.dst, host0.ip4) |
| 6071 | self.assertEqual(tcp.dport, port_in) |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6072 | self.assertEqual(tcp.sport, external_port) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6073 | except: |
| 6074 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 6075 | raise |
| 6076 | |
| 6077 | # out to host1 |
| 6078 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 6079 | IP(src=self.pg1.remote_ip4, dst=nat_ip) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6080 | TCP(sport=external_port, dport=port_out1)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6081 | self.pg1.add_stream(p) |
| 6082 | self.pg_enable_capture(self.pg_interfaces) |
| 6083 | self.pg_start() |
| 6084 | capture = self.pg0.get_capture(1) |
| 6085 | p = capture[0] |
| 6086 | try: |
| 6087 | ip = p[IP] |
| 6088 | tcp = p[TCP] |
| 6089 | self.assertEqual(ip.src, self.pg1.remote_ip4) |
| 6090 | self.assertEqual(ip.dst, host1.ip4) |
| 6091 | self.assertEqual(tcp.dport, port_in) |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6092 | self.assertEqual(tcp.sport, external_port) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6093 | except: |
| 6094 | self.logger.error(ppp("Unexpected or invalid packet", p)) |
| 6095 | raise |
| 6096 | |
Martin Gálik | 6bc8c64 | 2017-04-19 01:12:27 -0700 | [diff] [blame] | 6097 | # session close api test |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6098 | self.vapi.nat_det_close_session_out(socket.inet_aton(nat_ip), |
| 6099 | port_out1, |
Martin Gálik | 6bc8c64 | 2017-04-19 01:12:27 -0700 | [diff] [blame] | 6100 | self.pg1.remote_ip4n, |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6101 | external_port) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6102 | dms = self.vapi.nat_det_map_dump() |
| 6103 | self.assertEqual(dms[0].ses_num, 1) |
| 6104 | |
| 6105 | self.vapi.nat_det_close_session_in(host0.ip4n, |
| 6106 | port_in, |
| 6107 | self.pg1.remote_ip4n, |
| 6108 | external_port) |
| 6109 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 6bc8c64 | 2017-04-19 01:12:27 -0700 | [diff] [blame] | 6110 | self.assertEqual(dms[0].ses_num, 0) |
| 6111 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6112 | def test_tcp_session_close_detection_in(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6113 | """ Deterministic NAT TCP session close from inside network """ |
| 6114 | self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n, |
| 6115 | 32, |
| 6116 | socket.inet_aton(self.nat_addr), |
| 6117 | 32) |
| 6118 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6119 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6120 | is_inside=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6121 | |
| 6122 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 6123 | |
| 6124 | # close the session from inside |
| 6125 | try: |
| 6126 | # FIN packet in -> out |
| 6127 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6128 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6129 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6130 | flags="F")) |
| 6131 | self.pg0.add_stream(p) |
| 6132 | self.pg_enable_capture(self.pg_interfaces) |
| 6133 | self.pg_start() |
| 6134 | self.pg1.get_capture(1) |
| 6135 | |
| 6136 | pkts = [] |
| 6137 | |
| 6138 | # ACK packet out -> in |
| 6139 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6140 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6141 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6142 | flags="A")) |
| 6143 | pkts.append(p) |
| 6144 | |
| 6145 | # FIN packet out -> in |
| 6146 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6147 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6148 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6149 | flags="F")) |
| 6150 | pkts.append(p) |
| 6151 | |
| 6152 | self.pg1.add_stream(pkts) |
| 6153 | self.pg_enable_capture(self.pg_interfaces) |
| 6154 | self.pg_start() |
| 6155 | self.pg0.get_capture(2) |
| 6156 | |
| 6157 | # ACK packet in -> out |
| 6158 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6159 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6160 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6161 | flags="A")) |
| 6162 | self.pg0.add_stream(p) |
| 6163 | self.pg_enable_capture(self.pg_interfaces) |
| 6164 | self.pg_start() |
| 6165 | self.pg1.get_capture(1) |
| 6166 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6167 | # Check if deterministic NAT44 closed the session |
| 6168 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6169 | self.assertEqual(0, dms[0].ses_num) |
| 6170 | except: |
| 6171 | self.logger.error("TCP session termination failed") |
| 6172 | raise |
| 6173 | |
| 6174 | def test_tcp_session_close_detection_out(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6175 | """ Deterministic NAT TCP session close from outside network """ |
| 6176 | self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n, |
| 6177 | 32, |
| 6178 | socket.inet_aton(self.nat_addr), |
| 6179 | 32) |
| 6180 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6181 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6182 | is_inside=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6183 | |
| 6184 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 6185 | |
| 6186 | # close the session from outside |
| 6187 | try: |
| 6188 | # FIN packet out -> in |
| 6189 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6190 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6191 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6192 | flags="F")) |
| 6193 | self.pg1.add_stream(p) |
| 6194 | self.pg_enable_capture(self.pg_interfaces) |
| 6195 | self.pg_start() |
| 6196 | self.pg0.get_capture(1) |
| 6197 | |
| 6198 | pkts = [] |
| 6199 | |
| 6200 | # ACK packet in -> out |
| 6201 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6202 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6203 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6204 | flags="A")) |
| 6205 | pkts.append(p) |
| 6206 | |
| 6207 | # ACK packet in -> out |
| 6208 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6209 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6210 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6211 | flags="F")) |
| 6212 | pkts.append(p) |
| 6213 | |
| 6214 | self.pg0.add_stream(pkts) |
| 6215 | self.pg_enable_capture(self.pg_interfaces) |
| 6216 | self.pg_start() |
| 6217 | self.pg1.get_capture(2) |
| 6218 | |
| 6219 | # ACK packet out -> in |
| 6220 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6221 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6222 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6223 | flags="A")) |
| 6224 | self.pg1.add_stream(p) |
| 6225 | self.pg_enable_capture(self.pg_interfaces) |
| 6226 | self.pg_start() |
| 6227 | self.pg0.get_capture(1) |
| 6228 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6229 | # Check if deterministic NAT44 closed the session |
| 6230 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6231 | self.assertEqual(0, dms[0].ses_num) |
| 6232 | except: |
| 6233 | self.logger.error("TCP session termination failed") |
| 6234 | raise |
| 6235 | |
| 6236 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 6237 | def test_session_timeout(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6238 | """ Deterministic NAT session timeouts """ |
| 6239 | self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n, |
| 6240 | 32, |
| 6241 | socket.inet_aton(self.nat_addr), |
| 6242 | 32) |
| 6243 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6244 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6245 | is_inside=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6246 | |
| 6247 | self.initiate_tcp_session(self.pg0, self.pg1) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6248 | self.vapi.nat_set_timeouts(5, 5, 5, 5) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6249 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 6250 | self.pg0.add_stream(pkts) |
| 6251 | self.pg_enable_capture(self.pg_interfaces) |
| 6252 | self.pg_start() |
| 6253 | capture = self.pg1.get_capture(len(pkts)) |
| 6254 | sleep(15) |
| 6255 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6256 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6257 | self.assertEqual(0, dms[0].ses_num) |
| 6258 | |
Matus Fabian | 7c0aecc | 2017-07-03 01:21:38 -0700 | [diff] [blame] | 6259 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6260 | def test_session_limit_per_user(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6261 | """ Deterministic NAT maximum sessions per user limit """ |
| 6262 | self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n, |
| 6263 | 32, |
| 6264 | socket.inet_aton(self.nat_addr), |
| 6265 | 32) |
| 6266 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6267 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6268 | is_inside=0) |
Matus Fabian | 2f2db1c | 2017-04-18 05:29:59 -0700 | [diff] [blame] | 6269 | self.vapi.set_ipfix_exporter(collector_address=self.pg2.remote_ip4n, |
| 6270 | src_address=self.pg2.local_ip4n, |
| 6271 | path_mtu=512, |
| 6272 | template_interval=10) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6273 | self.vapi.nat_ipfix() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6274 | |
| 6275 | pkts = [] |
| 6276 | for port in range(1025, 2025): |
| 6277 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6278 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 6279 | UDP(sport=port, dport=port)) |
| 6280 | pkts.append(p) |
| 6281 | |
| 6282 | self.pg0.add_stream(pkts) |
| 6283 | self.pg_enable_capture(self.pg_interfaces) |
| 6284 | self.pg_start() |
| 6285 | capture = self.pg1.get_capture(len(pkts)) |
| 6286 | |
| 6287 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6288 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | f7e655d | 2017-04-27 02:13:26 -0700 | [diff] [blame] | 6289 | UDP(sport=3001, dport=3002)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6290 | self.pg0.add_stream(p) |
| 6291 | self.pg_enable_capture(self.pg_interfaces) |
| 6292 | self.pg_start() |
| 6293 | capture = self.pg1.assert_nothing_captured() |
| 6294 | |
Martin Gálik | f7e655d | 2017-04-27 02:13:26 -0700 | [diff] [blame] | 6295 | # verify ICMP error packet |
| 6296 | capture = self.pg0.get_capture(1) |
| 6297 | p = capture[0] |
| 6298 | self.assertTrue(p.haslayer(ICMP)) |
| 6299 | icmp = p[ICMP] |
| 6300 | self.assertEqual(icmp.type, 3) |
| 6301 | self.assertEqual(icmp.code, 1) |
| 6302 | self.assertTrue(icmp.haslayer(IPerror)) |
| 6303 | inner_ip = icmp[IPerror] |
| 6304 | self.assertEqual(inner_ip[UDPerror].sport, 3001) |
| 6305 | self.assertEqual(inner_ip[UDPerror].dport, 3002) |
| 6306 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6307 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6308 | |
| 6309 | self.assertEqual(1000, dms[0].ses_num) |
| 6310 | |
Matus Fabian | 2f2db1c | 2017-04-18 05:29:59 -0700 | [diff] [blame] | 6311 | # verify IPFIX logging |
| 6312 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
Matus Fabian | 7c0aecc | 2017-07-03 01:21:38 -0700 | [diff] [blame] | 6313 | sleep(1) |
Matus Fabian | 2f2db1c | 2017-04-18 05:29:59 -0700 | [diff] [blame] | 6314 | capture = self.pg2.get_capture(2) |
| 6315 | ipfix = IPFIXDecoder() |
| 6316 | # first load template |
| 6317 | for p in capture: |
| 6318 | self.assertTrue(p.haslayer(IPFIX)) |
| 6319 | if p.haslayer(Template): |
| 6320 | ipfix.add_template(p.getlayer(Template)) |
| 6321 | # verify events in data set |
| 6322 | for p in capture: |
| 6323 | if p.haslayer(Data): |
| 6324 | data = ipfix.decode_data_set(p.getlayer(Set)) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6325 | self.verify_ipfix_max_entries_per_user(data, |
| 6326 | 1000, |
| 6327 | self.pg0.remote_ip4n) |
Matus Fabian | 2f2db1c | 2017-04-18 05:29:59 -0700 | [diff] [blame] | 6328 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6329 | def clear_nat_det(self): |
Martin | 17a75cb | 2017-03-08 05:53:20 -0800 | [diff] [blame] | 6330 | """ |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6331 | Clear deterministic NAT configuration. |
Martin | 17a75cb | 2017-03-08 05:53:20 -0800 | [diff] [blame] | 6332 | """ |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6333 | self.vapi.nat_ipfix(enable=0) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6334 | self.vapi.nat_set_timeouts() |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6335 | deterministic_mappings = self.vapi.nat_det_map_dump() |
Martin | 17a75cb | 2017-03-08 05:53:20 -0800 | [diff] [blame] | 6336 | for dsm in deterministic_mappings: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6337 | self.vapi.nat_det_add_del_map(dsm.in_addr, |
| 6338 | dsm.in_plen, |
| 6339 | dsm.out_addr, |
| 6340 | dsm.out_plen, |
| 6341 | is_add=0) |
Martin | 17a75cb | 2017-03-08 05:53:20 -0800 | [diff] [blame] | 6342 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6343 | interfaces = self.vapi.nat44_interface_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6344 | for intf in interfaces: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6345 | self.vapi.nat44_interface_add_del_feature(intf.sw_if_index, |
| 6346 | intf.is_inside, |
| 6347 | is_add=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6348 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 6349 | def tearDown(self): |
| 6350 | super(TestDeterministicNAT, self).tearDown() |
| 6351 | if not self.vpp_dead: |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 6352 | self.logger.info(self.vapi.cli("show nat44 interfaces")) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6353 | self.logger.info(self.vapi.cli("show nat timeouts")) |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 6354 | self.logger.info( |
| 6355 | self.vapi.cli("show nat44 deterministic mappings")) |
| 6356 | self.logger.info( |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 6357 | self.vapi.cli("show nat44 deterministic sessions")) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6358 | self.clear_nat_det() |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 6359 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6360 | |
| 6361 | class TestNAT64(MethodHolder): |
| 6362 | """ NAT64 Test Cases """ |
| 6363 | |
| 6364 | @classmethod |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 6365 | def setUpConstants(cls): |
| 6366 | super(TestNAT64, cls).setUpConstants() |
| 6367 | cls.vpp_cmdline.extend(["nat", "{", "nat64 bib hash buckets 128", |
| 6368 | "nat64 st hash buckets 256", "}"]) |
| 6369 | |
| 6370 | @classmethod |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6371 | def setUpClass(cls): |
| 6372 | super(TestNAT64, cls).setUpClass() |
| 6373 | |
| 6374 | try: |
| 6375 | cls.tcp_port_in = 6303 |
| 6376 | cls.tcp_port_out = 6303 |
| 6377 | cls.udp_port_in = 6304 |
| 6378 | cls.udp_port_out = 6304 |
| 6379 | cls.icmp_id_in = 6305 |
| 6380 | cls.icmp_id_out = 6305 |
| 6381 | cls.nat_addr = '10.0.0.3' |
| 6382 | cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6383 | cls.vrf1_id = 10 |
| 6384 | cls.vrf1_nat_addr = '10.0.10.3' |
| 6385 | cls.vrf1_nat_addr_n = socket.inet_pton(socket.AF_INET, |
| 6386 | cls.vrf1_nat_addr) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 6387 | cls.ipfix_src_port = 4739 |
| 6388 | cls.ipfix_domain_id = 1 |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6389 | |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 6390 | cls.create_pg_interfaces(range(6)) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6391 | cls.ip6_interfaces = list(cls.pg_interfaces[0:1]) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6392 | cls.ip6_interfaces.append(cls.pg_interfaces[2]) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6393 | cls.ip4_interfaces = list(cls.pg_interfaces[1:2]) |
| 6394 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 6395 | cls.vapi.ip_table_add_del(cls.vrf1_id, is_add=1, is_ipv6=1) |
| 6396 | |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6397 | cls.pg_interfaces[2].set_table_ip6(cls.vrf1_id) |
| 6398 | |
| 6399 | cls.pg0.generate_remote_hosts(2) |
| 6400 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6401 | for i in cls.ip6_interfaces: |
| 6402 | i.admin_up() |
| 6403 | i.config_ip6() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6404 | i.configure_ipv6_neighbors() |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6405 | |
| 6406 | for i in cls.ip4_interfaces: |
| 6407 | i.admin_up() |
| 6408 | i.config_ip4() |
| 6409 | i.resolve_arp() |
| 6410 | |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 6411 | cls.pg3.admin_up() |
| 6412 | cls.pg3.config_ip4() |
| 6413 | cls.pg3.resolve_arp() |
| 6414 | cls.pg3.config_ip6() |
| 6415 | cls.pg3.configure_ipv6_neighbors() |
| 6416 | |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 6417 | cls.pg5.admin_up() |
| 6418 | cls.pg5.config_ip6() |
| 6419 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6420 | except Exception: |
| 6421 | super(TestNAT64, cls).tearDownClass() |
| 6422 | raise |
| 6423 | |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 6424 | def test_nat64_inside_interface_handles_neighbor_advertisement(self): |
| 6425 | """ NAT64 inside interface handles Neighbor Advertisement """ |
| 6426 | |
| 6427 | self.vapi.nat64_add_del_interface(self.pg5.sw_if_index) |
| 6428 | |
| 6429 | # Try to send ping |
| 6430 | ping = (Ether(dst=self.pg5.local_mac, src=self.pg5.remote_mac) / |
| 6431 | IPv6(src=self.pg5.remote_ip6, dst=self.pg5.local_ip6) / |
| 6432 | ICMPv6EchoRequest()) |
| 6433 | pkts = [ping] |
| 6434 | self.pg5.add_stream(pkts) |
| 6435 | self.pg_enable_capture(self.pg_interfaces) |
| 6436 | self.pg_start() |
| 6437 | |
| 6438 | # Wait for Neighbor Solicitation |
| 6439 | capture = self.pg5.get_capture(len(pkts)) |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 6440 | packet = capture[0] |
| 6441 | try: |
| 6442 | self.assertEqual(packet[IPv6].src, self.pg5.local_ip6) |
| 6443 | self.assertTrue(packet.haslayer(ICMPv6ND_NS)) |
| 6444 | tgt = packet[ICMPv6ND_NS].tgt |
| 6445 | except: |
| 6446 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6447 | raise |
| 6448 | |
| 6449 | # Send Neighbor Advertisement |
| 6450 | p = (Ether(dst=self.pg5.local_mac, src=self.pg5.remote_mac) / |
| 6451 | IPv6(src=self.pg5.remote_ip6, dst=self.pg5.local_ip6) / |
| 6452 | ICMPv6ND_NA(tgt=tgt) / |
| 6453 | ICMPv6NDOptDstLLAddr(lladdr=self.pg5.remote_mac)) |
| 6454 | pkts = [p] |
| 6455 | self.pg5.add_stream(pkts) |
| 6456 | self.pg_enable_capture(self.pg_interfaces) |
| 6457 | self.pg_start() |
| 6458 | |
| 6459 | # Try to send ping again |
| 6460 | pkts = [ping] |
| 6461 | self.pg5.add_stream(pkts) |
| 6462 | self.pg_enable_capture(self.pg_interfaces) |
| 6463 | self.pg_start() |
| 6464 | |
| 6465 | # Wait for ping reply |
| 6466 | capture = self.pg5.get_capture(len(pkts)) |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 6467 | packet = capture[0] |
| 6468 | try: |
| 6469 | self.assertEqual(packet[IPv6].src, self.pg5.local_ip6) |
| 6470 | self.assertEqual(packet[IPv6].dst, self.pg5.remote_ip6) |
| 6471 | self.assertTrue(packet.haslayer(ICMPv6EchoReply)) |
| 6472 | except: |
| 6473 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6474 | raise |
| 6475 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6476 | def test_pool(self): |
| 6477 | """ Add/delete address to NAT64 pool """ |
| 6478 | nat_addr = socket.inet_pton(socket.AF_INET, '1.2.3.4') |
| 6479 | |
| 6480 | self.vapi.nat64_add_del_pool_addr_range(nat_addr, nat_addr) |
| 6481 | |
| 6482 | addresses = self.vapi.nat64_pool_addr_dump() |
| 6483 | self.assertEqual(len(addresses), 1) |
| 6484 | self.assertEqual(addresses[0].address, nat_addr) |
| 6485 | |
| 6486 | self.vapi.nat64_add_del_pool_addr_range(nat_addr, nat_addr, is_add=0) |
| 6487 | |
| 6488 | addresses = self.vapi.nat64_pool_addr_dump() |
| 6489 | self.assertEqual(len(addresses), 0) |
| 6490 | |
| 6491 | def test_interface(self): |
| 6492 | """ Enable/disable NAT64 feature on the interface """ |
| 6493 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6494 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6495 | |
| 6496 | interfaces = self.vapi.nat64_interface_dump() |
| 6497 | self.assertEqual(len(interfaces), 2) |
| 6498 | pg0_found = False |
| 6499 | pg1_found = False |
| 6500 | for intf in interfaces: |
| 6501 | if intf.sw_if_index == self.pg0.sw_if_index: |
| 6502 | self.assertEqual(intf.is_inside, 1) |
| 6503 | pg0_found = True |
| 6504 | elif intf.sw_if_index == self.pg1.sw_if_index: |
| 6505 | self.assertEqual(intf.is_inside, 0) |
| 6506 | pg1_found = True |
| 6507 | self.assertTrue(pg0_found) |
| 6508 | self.assertTrue(pg1_found) |
| 6509 | |
| 6510 | features = self.vapi.cli("show interface features pg0") |
| 6511 | self.assertNotEqual(features.find('nat64-in2out'), -1) |
| 6512 | features = self.vapi.cli("show interface features pg1") |
| 6513 | self.assertNotEqual(features.find('nat64-out2in'), -1) |
| 6514 | |
| 6515 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index, is_add=0) |
| 6516 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_add=0) |
| 6517 | |
| 6518 | interfaces = self.vapi.nat64_interface_dump() |
| 6519 | self.assertEqual(len(interfaces), 0) |
| 6520 | |
| 6521 | def test_static_bib(self): |
| 6522 | """ Add/delete static BIB entry """ |
| 6523 | in_addr = socket.inet_pton(socket.AF_INET6, |
| 6524 | '2001:db8:85a3::8a2e:370:7334') |
| 6525 | out_addr = socket.inet_pton(socket.AF_INET, '10.1.1.3') |
| 6526 | in_port = 1234 |
| 6527 | out_port = 5678 |
| 6528 | proto = IP_PROTOS.tcp |
| 6529 | |
| 6530 | self.vapi.nat64_add_del_static_bib(in_addr, |
| 6531 | out_addr, |
| 6532 | in_port, |
| 6533 | out_port, |
| 6534 | proto) |
| 6535 | bib = self.vapi.nat64_bib_dump(IP_PROTOS.tcp) |
| 6536 | static_bib_num = 0 |
| 6537 | for bibe in bib: |
| 6538 | if bibe.is_static: |
| 6539 | static_bib_num += 1 |
| 6540 | self.assertEqual(bibe.i_addr, in_addr) |
| 6541 | self.assertEqual(bibe.o_addr, out_addr) |
| 6542 | self.assertEqual(bibe.i_port, in_port) |
| 6543 | self.assertEqual(bibe.o_port, out_port) |
| 6544 | self.assertEqual(static_bib_num, 1) |
| 6545 | |
| 6546 | self.vapi.nat64_add_del_static_bib(in_addr, |
| 6547 | out_addr, |
| 6548 | in_port, |
| 6549 | out_port, |
| 6550 | proto, |
| 6551 | is_add=0) |
| 6552 | bib = self.vapi.nat64_bib_dump(IP_PROTOS.tcp) |
| 6553 | static_bib_num = 0 |
| 6554 | for bibe in bib: |
| 6555 | if bibe.is_static: |
| 6556 | static_bib_num += 1 |
| 6557 | self.assertEqual(static_bib_num, 0) |
| 6558 | |
| 6559 | def test_set_timeouts(self): |
| 6560 | """ Set NAT64 timeouts """ |
| 6561 | # verify default values |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6562 | timeouts = self.vapi.nat_get_timeouts() |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6563 | self.assertEqual(timeouts.udp, 300) |
| 6564 | self.assertEqual(timeouts.icmp, 60) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6565 | self.assertEqual(timeouts.tcp_transitory, 240) |
| 6566 | self.assertEqual(timeouts.tcp_established, 7440) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6567 | |
| 6568 | # set and verify custom values |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6569 | self.vapi.nat_set_timeouts(udp=200, icmp=30, tcp_transitory=250, |
| 6570 | tcp_established=7450) |
| 6571 | timeouts = self.vapi.nat_get_timeouts() |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6572 | self.assertEqual(timeouts.udp, 200) |
| 6573 | self.assertEqual(timeouts.icmp, 30) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6574 | self.assertEqual(timeouts.tcp_transitory, 250) |
| 6575 | self.assertEqual(timeouts.tcp_established, 7450) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6576 | |
| 6577 | def test_dynamic(self): |
| 6578 | """ NAT64 dynamic translation test """ |
| 6579 | self.tcp_port_in = 6303 |
| 6580 | self.udp_port_in = 6304 |
| 6581 | self.icmp_id_in = 6305 |
| 6582 | |
| 6583 | ses_num_start = self.nat64_get_ses_num() |
| 6584 | |
| 6585 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6586 | self.nat_addr_n) |
| 6587 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6588 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6589 | |
| 6590 | # in2out |
| 6591 | pkts = self.create_stream_in_ip6(self.pg0, self.pg1) |
| 6592 | self.pg0.add_stream(pkts) |
| 6593 | self.pg_enable_capture(self.pg_interfaces) |
| 6594 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6595 | capture = self.pg1.get_capture(len(pkts)) |
| 6596 | self.verify_capture_out(capture, nat_ip=self.nat_addr, |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6597 | dst_ip=self.pg1.remote_ip4) |
| 6598 | |
| 6599 | # out2in |
| 6600 | pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr) |
| 6601 | self.pg1.add_stream(pkts) |
| 6602 | self.pg_enable_capture(self.pg_interfaces) |
| 6603 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6604 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6605 | ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4])) |
| 6606 | self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6) |
| 6607 | |
| 6608 | # in2out |
| 6609 | pkts = self.create_stream_in_ip6(self.pg0, self.pg1) |
| 6610 | self.pg0.add_stream(pkts) |
| 6611 | self.pg_enable_capture(self.pg_interfaces) |
| 6612 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6613 | capture = self.pg1.get_capture(len(pkts)) |
| 6614 | self.verify_capture_out(capture, nat_ip=self.nat_addr, |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6615 | dst_ip=self.pg1.remote_ip4) |
| 6616 | |
| 6617 | # out2in |
| 6618 | pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr) |
| 6619 | self.pg1.add_stream(pkts) |
| 6620 | self.pg_enable_capture(self.pg_interfaces) |
| 6621 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6622 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6623 | self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6) |
| 6624 | |
| 6625 | ses_num_end = self.nat64_get_ses_num() |
| 6626 | |
| 6627 | self.assertEqual(ses_num_end - ses_num_start, 3) |
| 6628 | |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6629 | # tenant with specific VRF |
| 6630 | self.vapi.nat64_add_del_pool_addr_range(self.vrf1_nat_addr_n, |
| 6631 | self.vrf1_nat_addr_n, |
| 6632 | vrf_id=self.vrf1_id) |
| 6633 | self.vapi.nat64_add_del_interface(self.pg2.sw_if_index) |
| 6634 | |
| 6635 | pkts = self.create_stream_in_ip6(self.pg2, self.pg1) |
| 6636 | self.pg2.add_stream(pkts) |
| 6637 | self.pg_enable_capture(self.pg_interfaces) |
| 6638 | self.pg_start() |
| 6639 | capture = self.pg1.get_capture(len(pkts)) |
| 6640 | self.verify_capture_out(capture, nat_ip=self.vrf1_nat_addr, |
| 6641 | dst_ip=self.pg1.remote_ip4) |
| 6642 | |
| 6643 | pkts = self.create_stream_out(self.pg1, dst_ip=self.vrf1_nat_addr) |
| 6644 | self.pg1.add_stream(pkts) |
| 6645 | self.pg_enable_capture(self.pg_interfaces) |
| 6646 | self.pg_start() |
| 6647 | capture = self.pg2.get_capture(len(pkts)) |
| 6648 | self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg2.remote_ip6) |
| 6649 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6650 | def test_static(self): |
| 6651 | """ NAT64 static translation test """ |
| 6652 | self.tcp_port_in = 60303 |
| 6653 | self.udp_port_in = 60304 |
| 6654 | self.icmp_id_in = 60305 |
| 6655 | self.tcp_port_out = 60303 |
| 6656 | self.udp_port_out = 60304 |
| 6657 | self.icmp_id_out = 60305 |
| 6658 | |
| 6659 | ses_num_start = self.nat64_get_ses_num() |
| 6660 | |
| 6661 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6662 | self.nat_addr_n) |
| 6663 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6664 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6665 | |
| 6666 | self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n, |
| 6667 | self.nat_addr_n, |
| 6668 | self.tcp_port_in, |
| 6669 | self.tcp_port_out, |
| 6670 | IP_PROTOS.tcp) |
| 6671 | self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n, |
| 6672 | self.nat_addr_n, |
| 6673 | self.udp_port_in, |
| 6674 | self.udp_port_out, |
| 6675 | IP_PROTOS.udp) |
| 6676 | self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n, |
| 6677 | self.nat_addr_n, |
| 6678 | self.icmp_id_in, |
| 6679 | self.icmp_id_out, |
| 6680 | IP_PROTOS.icmp) |
| 6681 | |
| 6682 | # in2out |
| 6683 | pkts = self.create_stream_in_ip6(self.pg0, self.pg1) |
| 6684 | self.pg0.add_stream(pkts) |
| 6685 | self.pg_enable_capture(self.pg_interfaces) |
| 6686 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6687 | capture = self.pg1.get_capture(len(pkts)) |
| 6688 | self.verify_capture_out(capture, nat_ip=self.nat_addr, |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6689 | dst_ip=self.pg1.remote_ip4, same_port=True) |
| 6690 | |
| 6691 | # out2in |
| 6692 | pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr) |
| 6693 | self.pg1.add_stream(pkts) |
| 6694 | self.pg_enable_capture(self.pg_interfaces) |
| 6695 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6696 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6697 | ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4])) |
| 6698 | self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6) |
| 6699 | |
| 6700 | ses_num_end = self.nat64_get_ses_num() |
| 6701 | |
| 6702 | self.assertEqual(ses_num_end - ses_num_start, 3) |
| 6703 | |
| 6704 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 6705 | def test_session_timeout(self): |
| 6706 | """ NAT64 session timeout """ |
| 6707 | self.icmp_id_in = 1234 |
| 6708 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6709 | self.nat_addr_n) |
| 6710 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6711 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6712 | self.vapi.nat_set_timeouts(icmp=5, tcp_transitory=5, tcp_established=5) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6713 | |
| 6714 | pkts = self.create_stream_in_ip6(self.pg0, self.pg1) |
| 6715 | self.pg0.add_stream(pkts) |
| 6716 | self.pg_enable_capture(self.pg_interfaces) |
| 6717 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6718 | capture = self.pg1.get_capture(len(pkts)) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6719 | |
| 6720 | ses_num_before_timeout = self.nat64_get_ses_num() |
| 6721 | |
| 6722 | sleep(15) |
| 6723 | |
Matus Fabian | 8fed424 | 2018-08-14 05:14:55 -0700 | [diff] [blame] | 6724 | # ICMP and TCP session after timeout |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6725 | ses_num_after_timeout = self.nat64_get_ses_num() |
Matus Fabian | 8fed424 | 2018-08-14 05:14:55 -0700 | [diff] [blame] | 6726 | self.assertEqual(ses_num_before_timeout - ses_num_after_timeout, 2) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6727 | |
Matus Fabian | 732036d | 2017-06-08 05:24:28 -0700 | [diff] [blame] | 6728 | def test_icmp_error(self): |
| 6729 | """ NAT64 ICMP Error message translation """ |
| 6730 | self.tcp_port_in = 6303 |
| 6731 | self.udp_port_in = 6304 |
| 6732 | self.icmp_id_in = 6305 |
| 6733 | |
Matus Fabian | 732036d | 2017-06-08 05:24:28 -0700 | [diff] [blame] | 6734 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6735 | self.nat_addr_n) |
| 6736 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6737 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6738 | |
| 6739 | # send some packets to create sessions |
| 6740 | pkts = self.create_stream_in_ip6(self.pg0, self.pg1) |
| 6741 | self.pg0.add_stream(pkts) |
| 6742 | self.pg_enable_capture(self.pg_interfaces) |
| 6743 | self.pg_start() |
| 6744 | capture_ip4 = self.pg1.get_capture(len(pkts)) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6745 | self.verify_capture_out(capture_ip4, |
Matus Fabian | 732036d | 2017-06-08 05:24:28 -0700 | [diff] [blame] | 6746 | nat_ip=self.nat_addr, |
| 6747 | dst_ip=self.pg1.remote_ip4) |
| 6748 | |
| 6749 | pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr) |
| 6750 | self.pg1.add_stream(pkts) |
| 6751 | self.pg_enable_capture(self.pg_interfaces) |
| 6752 | self.pg_start() |
| 6753 | capture_ip6 = self.pg0.get_capture(len(pkts)) |
| 6754 | ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4])) |
| 6755 | self.verify_capture_in_ip6(capture_ip6, ip[IPv6].src, |
| 6756 | self.pg0.remote_ip6) |
| 6757 | |
| 6758 | # in2out |
| 6759 | pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 6760 | IPv6(src=self.pg0.remote_ip6, dst=ip[IPv6].src) / |
| 6761 | ICMPv6DestUnreach(code=1) / |
| 6762 | packet[IPv6] for packet in capture_ip6] |
| 6763 | self.pg0.add_stream(pkts) |
| 6764 | self.pg_enable_capture(self.pg_interfaces) |
| 6765 | self.pg_start() |
| 6766 | capture = self.pg1.get_capture(len(pkts)) |
| 6767 | for packet in capture: |
| 6768 | try: |
| 6769 | self.assertEqual(packet[IP].src, self.nat_addr) |
| 6770 | self.assertEqual(packet[IP].dst, self.pg1.remote_ip4) |
| 6771 | self.assertEqual(packet[ICMP].type, 3) |
| 6772 | self.assertEqual(packet[ICMP].code, 13) |
| 6773 | inner = packet[IPerror] |
| 6774 | self.assertEqual(inner.src, self.pg1.remote_ip4) |
| 6775 | self.assertEqual(inner.dst, self.nat_addr) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 6776 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 732036d | 2017-06-08 05:24:28 -0700 | [diff] [blame] | 6777 | if inner.haslayer(TCPerror): |
| 6778 | self.assertEqual(inner[TCPerror].dport, self.tcp_port_out) |
| 6779 | elif inner.haslayer(UDPerror): |
| 6780 | self.assertEqual(inner[UDPerror].dport, self.udp_port_out) |
| 6781 | else: |
| 6782 | self.assertEqual(inner[ICMPerror].id, self.icmp_id_out) |
| 6783 | except: |
| 6784 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6785 | raise |
| 6786 | |
| 6787 | # out2in |
| 6788 | pkts = [Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 6789 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 6790 | ICMP(type=3, code=13) / |
| 6791 | packet[IP] for packet in capture_ip4] |
| 6792 | self.pg1.add_stream(pkts) |
| 6793 | self.pg_enable_capture(self.pg_interfaces) |
| 6794 | self.pg_start() |
| 6795 | capture = self.pg0.get_capture(len(pkts)) |
| 6796 | for packet in capture: |
| 6797 | try: |
| 6798 | self.assertEqual(packet[IPv6].src, ip.src) |
| 6799 | self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6) |
| 6800 | icmp = packet[ICMPv6DestUnreach] |
| 6801 | self.assertEqual(icmp.code, 1) |
| 6802 | inner = icmp[IPerror6] |
| 6803 | self.assertEqual(inner.src, self.pg0.remote_ip6) |
| 6804 | self.assertEqual(inner.dst, ip.src) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 6805 | self.assert_icmpv6_checksum_valid(packet) |
Matus Fabian | 732036d | 2017-06-08 05:24:28 -0700 | [diff] [blame] | 6806 | if inner.haslayer(TCPerror): |
| 6807 | self.assertEqual(inner[TCPerror].sport, self.tcp_port_in) |
| 6808 | elif inner.haslayer(UDPerror): |
| 6809 | self.assertEqual(inner[UDPerror].sport, self.udp_port_in) |
| 6810 | else: |
| 6811 | self.assertEqual(inner[ICMPv6EchoRequest].id, |
| 6812 | self.icmp_id_in) |
| 6813 | except: |
| 6814 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6815 | raise |
| 6816 | |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6817 | def test_hairpinning(self): |
| 6818 | """ NAT64 hairpinning """ |
| 6819 | |
| 6820 | client = self.pg0.remote_hosts[0] |
| 6821 | server = self.pg0.remote_hosts[1] |
| 6822 | server_tcp_in_port = 22 |
| 6823 | server_tcp_out_port = 4022 |
| 6824 | server_udp_in_port = 23 |
| 6825 | server_udp_out_port = 4023 |
| 6826 | client_tcp_in_port = 1234 |
| 6827 | client_udp_in_port = 1235 |
| 6828 | client_tcp_out_port = 0 |
| 6829 | client_udp_out_port = 0 |
| 6830 | ip = IPv6(src=''.join(['64:ff9b::', self.nat_addr])) |
| 6831 | nat_addr_ip6 = ip.src |
| 6832 | |
| 6833 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6834 | self.nat_addr_n) |
| 6835 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6836 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6837 | |
| 6838 | self.vapi.nat64_add_del_static_bib(server.ip6n, |
| 6839 | self.nat_addr_n, |
| 6840 | server_tcp_in_port, |
| 6841 | server_tcp_out_port, |
| 6842 | IP_PROTOS.tcp) |
| 6843 | self.vapi.nat64_add_del_static_bib(server.ip6n, |
| 6844 | self.nat_addr_n, |
| 6845 | server_udp_in_port, |
| 6846 | server_udp_out_port, |
| 6847 | IP_PROTOS.udp) |
| 6848 | |
| 6849 | # client to server |
| 6850 | pkts = [] |
| 6851 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 6852 | IPv6(src=client.ip6, dst=nat_addr_ip6) / |
| 6853 | TCP(sport=client_tcp_in_port, dport=server_tcp_out_port)) |
| 6854 | pkts.append(p) |
| 6855 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 6856 | IPv6(src=client.ip6, dst=nat_addr_ip6) / |
| 6857 | UDP(sport=client_udp_in_port, dport=server_udp_out_port)) |
| 6858 | pkts.append(p) |
| 6859 | self.pg0.add_stream(pkts) |
| 6860 | self.pg_enable_capture(self.pg_interfaces) |
| 6861 | self.pg_start() |
| 6862 | capture = self.pg0.get_capture(len(pkts)) |
| 6863 | for packet in capture: |
| 6864 | try: |
| 6865 | self.assertEqual(packet[IPv6].src, nat_addr_ip6) |
| 6866 | self.assertEqual(packet[IPv6].dst, server.ip6) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 6867 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6868 | if packet.haslayer(TCP): |
| 6869 | self.assertNotEqual(packet[TCP].sport, client_tcp_in_port) |
| 6870 | self.assertEqual(packet[TCP].dport, server_tcp_in_port) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6871 | client_tcp_out_port = packet[TCP].sport |
| 6872 | else: |
| 6873 | self.assertNotEqual(packet[UDP].sport, client_udp_in_port) |
| 6874 | self.assertEqual(packet[UDP].dport, server_udp_in_port) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6875 | client_udp_out_port = packet[UDP].sport |
| 6876 | except: |
| 6877 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6878 | raise |
| 6879 | |
| 6880 | # server to client |
| 6881 | pkts = [] |
| 6882 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 6883 | IPv6(src=server.ip6, dst=nat_addr_ip6) / |
| 6884 | TCP(sport=server_tcp_in_port, dport=client_tcp_out_port)) |
| 6885 | pkts.append(p) |
| 6886 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 6887 | IPv6(src=server.ip6, dst=nat_addr_ip6) / |
| 6888 | UDP(sport=server_udp_in_port, dport=client_udp_out_port)) |
| 6889 | pkts.append(p) |
| 6890 | self.pg0.add_stream(pkts) |
| 6891 | self.pg_enable_capture(self.pg_interfaces) |
| 6892 | self.pg_start() |
| 6893 | capture = self.pg0.get_capture(len(pkts)) |
| 6894 | for packet in capture: |
| 6895 | try: |
| 6896 | self.assertEqual(packet[IPv6].src, nat_addr_ip6) |
| 6897 | self.assertEqual(packet[IPv6].dst, client.ip6) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 6898 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6899 | if packet.haslayer(TCP): |
| 6900 | self.assertEqual(packet[TCP].sport, server_tcp_out_port) |
| 6901 | self.assertEqual(packet[TCP].dport, client_tcp_in_port) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6902 | else: |
| 6903 | self.assertEqual(packet[UDP].sport, server_udp_out_port) |
| 6904 | self.assertEqual(packet[UDP].dport, client_udp_in_port) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6905 | except: |
| 6906 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6907 | raise |
| 6908 | |
| 6909 | # ICMP error |
| 6910 | pkts = [] |
| 6911 | pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 6912 | IPv6(src=client.ip6, dst=nat_addr_ip6) / |
| 6913 | ICMPv6DestUnreach(code=1) / |
| 6914 | packet[IPv6] for packet in capture] |
| 6915 | self.pg0.add_stream(pkts) |
| 6916 | self.pg_enable_capture(self.pg_interfaces) |
| 6917 | self.pg_start() |
| 6918 | capture = self.pg0.get_capture(len(pkts)) |
| 6919 | for packet in capture: |
| 6920 | try: |
| 6921 | self.assertEqual(packet[IPv6].src, nat_addr_ip6) |
| 6922 | self.assertEqual(packet[IPv6].dst, server.ip6) |
| 6923 | icmp = packet[ICMPv6DestUnreach] |
| 6924 | self.assertEqual(icmp.code, 1) |
| 6925 | inner = icmp[IPerror6] |
| 6926 | self.assertEqual(inner.src, server.ip6) |
| 6927 | self.assertEqual(inner.dst, nat_addr_ip6) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 6928 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6929 | if inner.haslayer(TCPerror): |
| 6930 | self.assertEqual(inner[TCPerror].sport, server_tcp_in_port) |
| 6931 | self.assertEqual(inner[TCPerror].dport, |
| 6932 | client_tcp_out_port) |
| 6933 | else: |
| 6934 | self.assertEqual(inner[UDPerror].sport, server_udp_in_port) |
| 6935 | self.assertEqual(inner[UDPerror].dport, |
| 6936 | client_udp_out_port) |
| 6937 | except: |
| 6938 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6939 | raise |
| 6940 | |
Matus Fabian | 428dc91 | 2017-06-21 06:15:18 -0700 | [diff] [blame] | 6941 | def test_prefix(self): |
| 6942 | """ NAT64 Network-Specific Prefix """ |
| 6943 | |
| 6944 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6945 | self.nat_addr_n) |
| 6946 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6947 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6948 | self.vapi.nat64_add_del_pool_addr_range(self.vrf1_nat_addr_n, |
| 6949 | self.vrf1_nat_addr_n, |
| 6950 | vrf_id=self.vrf1_id) |
| 6951 | self.vapi.nat64_add_del_interface(self.pg2.sw_if_index) |
| 6952 | |
| 6953 | # Add global prefix |
| 6954 | global_pref64 = "2001:db8::" |
| 6955 | global_pref64_n = socket.inet_pton(socket.AF_INET6, global_pref64) |
| 6956 | global_pref64_len = 32 |
| 6957 | self.vapi.nat64_add_del_prefix(global_pref64_n, global_pref64_len) |
| 6958 | |
| 6959 | prefix = self.vapi.nat64_prefix_dump() |
| 6960 | self.assertEqual(len(prefix), 1) |
| 6961 | self.assertEqual(prefix[0].prefix, global_pref64_n) |
| 6962 | self.assertEqual(prefix[0].prefix_len, global_pref64_len) |
| 6963 | self.assertEqual(prefix[0].vrf_id, 0) |
| 6964 | |
| 6965 | # Add tenant specific prefix |
| 6966 | vrf1_pref64 = "2001:db8:122:300::" |
| 6967 | vrf1_pref64_n = socket.inet_pton(socket.AF_INET6, vrf1_pref64) |
| 6968 | vrf1_pref64_len = 56 |
| 6969 | self.vapi.nat64_add_del_prefix(vrf1_pref64_n, |
| 6970 | vrf1_pref64_len, |
| 6971 | vrf_id=self.vrf1_id) |
| 6972 | prefix = self.vapi.nat64_prefix_dump() |
| 6973 | self.assertEqual(len(prefix), 2) |
| 6974 | |
| 6975 | # Global prefix |
| 6976 | pkts = self.create_stream_in_ip6(self.pg0, |
| 6977 | self.pg1, |
| 6978 | pref=global_pref64, |
| 6979 | plen=global_pref64_len) |
| 6980 | self.pg0.add_stream(pkts) |
| 6981 | self.pg_enable_capture(self.pg_interfaces) |
| 6982 | self.pg_start() |
| 6983 | capture = self.pg1.get_capture(len(pkts)) |
| 6984 | self.verify_capture_out(capture, nat_ip=self.nat_addr, |
| 6985 | dst_ip=self.pg1.remote_ip4) |
| 6986 | |
| 6987 | pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr) |
| 6988 | self.pg1.add_stream(pkts) |
| 6989 | self.pg_enable_capture(self.pg_interfaces) |
| 6990 | self.pg_start() |
| 6991 | capture = self.pg0.get_capture(len(pkts)) |
| 6992 | dst_ip = self.compose_ip6(self.pg1.remote_ip4, |
| 6993 | global_pref64, |
| 6994 | global_pref64_len) |
| 6995 | self.verify_capture_in_ip6(capture, dst_ip, self.pg0.remote_ip6) |
| 6996 | |
| 6997 | # Tenant specific prefix |
| 6998 | pkts = self.create_stream_in_ip6(self.pg2, |
| 6999 | self.pg1, |
| 7000 | pref=vrf1_pref64, |
| 7001 | plen=vrf1_pref64_len) |
| 7002 | self.pg2.add_stream(pkts) |
| 7003 | self.pg_enable_capture(self.pg_interfaces) |
| 7004 | self.pg_start() |
| 7005 | capture = self.pg1.get_capture(len(pkts)) |
| 7006 | self.verify_capture_out(capture, nat_ip=self.vrf1_nat_addr, |
| 7007 | dst_ip=self.pg1.remote_ip4) |
| 7008 | |
| 7009 | pkts = self.create_stream_out(self.pg1, dst_ip=self.vrf1_nat_addr) |
| 7010 | self.pg1.add_stream(pkts) |
| 7011 | self.pg_enable_capture(self.pg_interfaces) |
| 7012 | self.pg_start() |
| 7013 | capture = self.pg2.get_capture(len(pkts)) |
| 7014 | dst_ip = self.compose_ip6(self.pg1.remote_ip4, |
| 7015 | vrf1_pref64, |
| 7016 | vrf1_pref64_len) |
| 7017 | self.verify_capture_in_ip6(capture, dst_ip, self.pg2.remote_ip6) |
| 7018 | |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7019 | def test_unknown_proto(self): |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7020 | """ NAT64 translate packet with unknown protocol """ |
| 7021 | |
| 7022 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7023 | self.nat_addr_n) |
| 7024 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7025 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7026 | remote_ip6 = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96) |
| 7027 | |
| 7028 | # in2out |
| 7029 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7030 | IPv6(src=self.pg0.remote_ip6, dst=remote_ip6) / |
| 7031 | TCP(sport=self.tcp_port_in, dport=20)) |
| 7032 | self.pg0.add_stream(p) |
| 7033 | self.pg_enable_capture(self.pg_interfaces) |
| 7034 | self.pg_start() |
| 7035 | p = self.pg1.get_capture(1) |
| 7036 | |
| 7037 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7038 | IPv6(src=self.pg0.remote_ip6, dst=remote_ip6, nh=47) / |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7039 | GRE() / |
| 7040 | IP(src=self.pg2.local_ip4, dst=self.pg2.remote_ip4) / |
| 7041 | TCP(sport=1234, dport=1234)) |
| 7042 | self.pg0.add_stream(p) |
| 7043 | self.pg_enable_capture(self.pg_interfaces) |
| 7044 | self.pg_start() |
| 7045 | p = self.pg1.get_capture(1) |
| 7046 | packet = p[0] |
| 7047 | try: |
| 7048 | self.assertEqual(packet[IP].src, self.nat_addr) |
| 7049 | self.assertEqual(packet[IP].dst, self.pg1.remote_ip4) |
| 7050 | self.assertTrue(packet.haslayer(GRE)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7051 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7052 | except: |
| 7053 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7054 | raise |
| 7055 | |
| 7056 | # out2in |
| 7057 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7058 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 7059 | GRE() / |
| 7060 | IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) / |
| 7061 | TCP(sport=1234, dport=1234)) |
| 7062 | self.pg1.add_stream(p) |
| 7063 | self.pg_enable_capture(self.pg_interfaces) |
| 7064 | self.pg_start() |
| 7065 | p = self.pg0.get_capture(1) |
| 7066 | packet = p[0] |
| 7067 | try: |
| 7068 | self.assertEqual(packet[IPv6].src, remote_ip6) |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7069 | self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6) |
| 7070 | self.assertEqual(packet[IPv6].nh, 47) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7071 | except: |
| 7072 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7073 | raise |
| 7074 | |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7075 | def test_hairpinning_unknown_proto(self): |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7076 | """ NAT64 translate packet with unknown protocol - hairpinning """ |
| 7077 | |
| 7078 | client = self.pg0.remote_hosts[0] |
| 7079 | server = self.pg0.remote_hosts[1] |
| 7080 | server_tcp_in_port = 22 |
| 7081 | server_tcp_out_port = 4022 |
| 7082 | client_tcp_in_port = 1234 |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7083 | client_tcp_out_port = 1235 |
| 7084 | server_nat_ip = "10.0.0.100" |
| 7085 | client_nat_ip = "10.0.0.110" |
| 7086 | server_nat_ip_n = socket.inet_pton(socket.AF_INET, server_nat_ip) |
| 7087 | client_nat_ip_n = socket.inet_pton(socket.AF_INET, client_nat_ip) |
| 7088 | server_nat_ip6 = self.compose_ip6(server_nat_ip, '64:ff9b::', 96) |
| 7089 | client_nat_ip6 = self.compose_ip6(client_nat_ip, '64:ff9b::', 96) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7090 | |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7091 | self.vapi.nat64_add_del_pool_addr_range(server_nat_ip_n, |
| 7092 | client_nat_ip_n) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7093 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7094 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7095 | |
| 7096 | self.vapi.nat64_add_del_static_bib(server.ip6n, |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7097 | server_nat_ip_n, |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7098 | server_tcp_in_port, |
| 7099 | server_tcp_out_port, |
| 7100 | IP_PROTOS.tcp) |
| 7101 | |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7102 | self.vapi.nat64_add_del_static_bib(server.ip6n, |
| 7103 | server_nat_ip_n, |
| 7104 | 0, |
| 7105 | 0, |
| 7106 | IP_PROTOS.gre) |
| 7107 | |
| 7108 | self.vapi.nat64_add_del_static_bib(client.ip6n, |
| 7109 | client_nat_ip_n, |
| 7110 | client_tcp_in_port, |
| 7111 | client_tcp_out_port, |
| 7112 | IP_PROTOS.tcp) |
| 7113 | |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7114 | # client to server |
| 7115 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7116 | IPv6(src=client.ip6, dst=server_nat_ip6) / |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7117 | TCP(sport=client_tcp_in_port, dport=server_tcp_out_port)) |
| 7118 | self.pg0.add_stream(p) |
| 7119 | self.pg_enable_capture(self.pg_interfaces) |
| 7120 | self.pg_start() |
| 7121 | p = self.pg0.get_capture(1) |
| 7122 | |
| 7123 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7124 | IPv6(src=client.ip6, dst=server_nat_ip6, nh=IP_PROTOS.gre) / |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7125 | GRE() / |
| 7126 | IP(src=self.pg2.local_ip4, dst=self.pg2.remote_ip4) / |
| 7127 | TCP(sport=1234, dport=1234)) |
| 7128 | self.pg0.add_stream(p) |
| 7129 | self.pg_enable_capture(self.pg_interfaces) |
| 7130 | self.pg_start() |
| 7131 | p = self.pg0.get_capture(1) |
| 7132 | packet = p[0] |
| 7133 | try: |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7134 | self.assertEqual(packet[IPv6].src, client_nat_ip6) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7135 | self.assertEqual(packet[IPv6].dst, server.ip6) |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7136 | self.assertEqual(packet[IPv6].nh, IP_PROTOS.gre) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7137 | except: |
| 7138 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7139 | raise |
| 7140 | |
| 7141 | # server to client |
| 7142 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7143 | IPv6(src=server.ip6, dst=client_nat_ip6, nh=IP_PROTOS.gre) / |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7144 | GRE() / |
| 7145 | IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) / |
| 7146 | TCP(sport=1234, dport=1234)) |
| 7147 | self.pg0.add_stream(p) |
| 7148 | self.pg_enable_capture(self.pg_interfaces) |
| 7149 | self.pg_start() |
| 7150 | p = self.pg0.get_capture(1) |
| 7151 | packet = p[0] |
| 7152 | try: |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7153 | self.assertEqual(packet[IPv6].src, server_nat_ip6) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7154 | self.assertEqual(packet[IPv6].dst, client.ip6) |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7155 | self.assertEqual(packet[IPv6].nh, IP_PROTOS.gre) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7156 | except: |
| 7157 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7158 | raise |
| 7159 | |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 7160 | def test_one_armed_nat64(self): |
| 7161 | """ One armed NAT64 """ |
| 7162 | external_port = 0 |
| 7163 | remote_host_ip6 = self.compose_ip6(self.pg3.remote_ip4, |
| 7164 | '64:ff9b::', |
| 7165 | 96) |
| 7166 | |
| 7167 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7168 | self.nat_addr_n) |
| 7169 | self.vapi.nat64_add_del_interface(self.pg3.sw_if_index) |
| 7170 | self.vapi.nat64_add_del_interface(self.pg3.sw_if_index, is_inside=0) |
| 7171 | |
| 7172 | # in2out |
| 7173 | p = (Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) / |
| 7174 | IPv6(src=self.pg3.remote_ip6, dst=remote_host_ip6) / |
| 7175 | TCP(sport=12345, dport=80)) |
| 7176 | self.pg3.add_stream(p) |
| 7177 | self.pg_enable_capture(self.pg_interfaces) |
| 7178 | self.pg_start() |
| 7179 | capture = self.pg3.get_capture(1) |
| 7180 | p = capture[0] |
| 7181 | try: |
| 7182 | ip = p[IP] |
| 7183 | tcp = p[TCP] |
| 7184 | self.assertEqual(ip.src, self.nat_addr) |
| 7185 | self.assertEqual(ip.dst, self.pg3.remote_ip4) |
| 7186 | self.assertNotEqual(tcp.sport, 12345) |
| 7187 | external_port = tcp.sport |
| 7188 | self.assertEqual(tcp.dport, 80) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7189 | self.assert_packet_checksums_valid(p) |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 7190 | except: |
| 7191 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 7192 | raise |
| 7193 | |
| 7194 | # out2in |
| 7195 | p = (Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) / |
| 7196 | IP(src=self.pg3.remote_ip4, dst=self.nat_addr) / |
| 7197 | TCP(sport=80, dport=external_port)) |
| 7198 | self.pg3.add_stream(p) |
| 7199 | self.pg_enable_capture(self.pg_interfaces) |
| 7200 | self.pg_start() |
| 7201 | capture = self.pg3.get_capture(1) |
| 7202 | p = capture[0] |
| 7203 | try: |
| 7204 | ip = p[IPv6] |
| 7205 | tcp = p[TCP] |
| 7206 | self.assertEqual(ip.src, remote_host_ip6) |
| 7207 | self.assertEqual(ip.dst, self.pg3.remote_ip6) |
| 7208 | self.assertEqual(tcp.sport, 80) |
| 7209 | self.assertEqual(tcp.dport, 12345) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7210 | self.assert_packet_checksums_valid(p) |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 7211 | except: |
| 7212 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 7213 | raise |
| 7214 | |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 7215 | def test_frag_in_order(self): |
| 7216 | """ NAT64 translate fragments arriving in order """ |
| 7217 | self.tcp_port_in = random.randint(1025, 65535) |
| 7218 | |
| 7219 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7220 | self.nat_addr_n) |
| 7221 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7222 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7223 | |
| 7224 | reass = self.vapi.nat_reass_dump() |
| 7225 | reass_n_start = len(reass) |
| 7226 | |
| 7227 | # in2out |
| 7228 | data = 'a' * 200 |
| 7229 | pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4, |
| 7230 | self.tcp_port_in, 20, data) |
| 7231 | self.pg0.add_stream(pkts) |
| 7232 | self.pg_enable_capture(self.pg_interfaces) |
| 7233 | self.pg_start() |
| 7234 | frags = self.pg1.get_capture(len(pkts)) |
| 7235 | p = self.reass_frags_and_verify(frags, |
| 7236 | self.nat_addr, |
| 7237 | self.pg1.remote_ip4) |
| 7238 | self.assertEqual(p[TCP].dport, 20) |
| 7239 | self.assertNotEqual(p[TCP].sport, self.tcp_port_in) |
| 7240 | self.tcp_port_out = p[TCP].sport |
| 7241 | self.assertEqual(data, p[Raw].load) |
| 7242 | |
| 7243 | # out2in |
| 7244 | data = "A" * 4 + "b" * 16 + "C" * 3 |
| 7245 | pkts = self.create_stream_frag(self.pg1, |
| 7246 | self.nat_addr, |
| 7247 | 20, |
| 7248 | self.tcp_port_out, |
| 7249 | data) |
| 7250 | self.pg1.add_stream(pkts) |
| 7251 | self.pg_enable_capture(self.pg_interfaces) |
| 7252 | self.pg_start() |
| 7253 | frags = self.pg0.get_capture(len(pkts)) |
| 7254 | src = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96) |
| 7255 | p = self.reass_frags_and_verify_ip6(frags, src, self.pg0.remote_ip6) |
| 7256 | self.assertEqual(p[TCP].sport, 20) |
| 7257 | self.assertEqual(p[TCP].dport, self.tcp_port_in) |
| 7258 | self.assertEqual(data, p[Raw].load) |
| 7259 | |
| 7260 | reass = self.vapi.nat_reass_dump() |
| 7261 | reass_n_end = len(reass) |
| 7262 | |
| 7263 | self.assertEqual(reass_n_end - reass_n_start, 2) |
| 7264 | |
| 7265 | def test_reass_hairpinning(self): |
| 7266 | """ NAT64 fragments hairpinning """ |
| 7267 | data = 'a' * 200 |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 7268 | server = self.pg0.remote_hosts[1] |
| 7269 | server_in_port = random.randint(1025, 65535) |
| 7270 | server_out_port = random.randint(1025, 65535) |
| 7271 | client_in_port = random.randint(1025, 65535) |
| 7272 | ip = IPv6(src=''.join(['64:ff9b::', self.nat_addr])) |
| 7273 | nat_addr_ip6 = ip.src |
| 7274 | |
| 7275 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7276 | self.nat_addr_n) |
| 7277 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7278 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7279 | |
| 7280 | # add static BIB entry for server |
| 7281 | self.vapi.nat64_add_del_static_bib(server.ip6n, |
| 7282 | self.nat_addr_n, |
| 7283 | server_in_port, |
| 7284 | server_out_port, |
| 7285 | IP_PROTOS.tcp) |
| 7286 | |
| 7287 | # send packet from host to server |
| 7288 | pkts = self.create_stream_frag_ip6(self.pg0, |
| 7289 | self.nat_addr, |
| 7290 | client_in_port, |
| 7291 | server_out_port, |
| 7292 | data) |
| 7293 | self.pg0.add_stream(pkts) |
| 7294 | self.pg_enable_capture(self.pg_interfaces) |
| 7295 | self.pg_start() |
| 7296 | frags = self.pg0.get_capture(len(pkts)) |
| 7297 | p = self.reass_frags_and_verify_ip6(frags, nat_addr_ip6, server.ip6) |
| 7298 | self.assertNotEqual(p[TCP].sport, client_in_port) |
| 7299 | self.assertEqual(p[TCP].dport, server_in_port) |
| 7300 | self.assertEqual(data, p[Raw].load) |
| 7301 | |
| 7302 | def test_frag_out_of_order(self): |
| 7303 | """ NAT64 translate fragments arriving out of order """ |
| 7304 | self.tcp_port_in = random.randint(1025, 65535) |
| 7305 | |
| 7306 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7307 | self.nat_addr_n) |
| 7308 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7309 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7310 | |
| 7311 | # in2out |
| 7312 | data = 'a' * 200 |
| 7313 | pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4, |
| 7314 | self.tcp_port_in, 20, data) |
| 7315 | pkts.reverse() |
| 7316 | self.pg0.add_stream(pkts) |
| 7317 | self.pg_enable_capture(self.pg_interfaces) |
| 7318 | self.pg_start() |
| 7319 | frags = self.pg1.get_capture(len(pkts)) |
| 7320 | p = self.reass_frags_and_verify(frags, |
| 7321 | self.nat_addr, |
| 7322 | self.pg1.remote_ip4) |
| 7323 | self.assertEqual(p[TCP].dport, 20) |
| 7324 | self.assertNotEqual(p[TCP].sport, self.tcp_port_in) |
| 7325 | self.tcp_port_out = p[TCP].sport |
| 7326 | self.assertEqual(data, p[Raw].load) |
| 7327 | |
| 7328 | # out2in |
| 7329 | data = "A" * 4 + "B" * 16 + "C" * 3 |
| 7330 | pkts = self.create_stream_frag(self.pg1, |
| 7331 | self.nat_addr, |
| 7332 | 20, |
| 7333 | self.tcp_port_out, |
| 7334 | data) |
| 7335 | pkts.reverse() |
| 7336 | self.pg1.add_stream(pkts) |
| 7337 | self.pg_enable_capture(self.pg_interfaces) |
| 7338 | self.pg_start() |
| 7339 | frags = self.pg0.get_capture(len(pkts)) |
| 7340 | src = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96) |
| 7341 | p = self.reass_frags_and_verify_ip6(frags, src, self.pg0.remote_ip6) |
| 7342 | self.assertEqual(p[TCP].sport, 20) |
| 7343 | self.assertEqual(p[TCP].dport, self.tcp_port_in) |
| 7344 | self.assertEqual(data, p[Raw].load) |
| 7345 | |
Matus Fabian | 0938dcf | 2017-11-08 01:59:38 -0800 | [diff] [blame] | 7346 | def test_interface_addr(self): |
| 7347 | """ Acquire NAT64 pool addresses from interface """ |
| 7348 | self.vapi.nat64_add_interface_addr(self.pg4.sw_if_index) |
| 7349 | |
| 7350 | # no address in NAT64 pool |
| 7351 | adresses = self.vapi.nat44_address_dump() |
| 7352 | self.assertEqual(0, len(adresses)) |
| 7353 | |
| 7354 | # configure interface address and check NAT64 address pool |
| 7355 | self.pg4.config_ip4() |
| 7356 | addresses = self.vapi.nat64_pool_addr_dump() |
| 7357 | self.assertEqual(len(addresses), 1) |
| 7358 | self.assertEqual(addresses[0].address, self.pg4.local_ip4n) |
| 7359 | |
| 7360 | # remove interface address and check NAT64 address pool |
| 7361 | self.pg4.unconfig_ip4() |
| 7362 | addresses = self.vapi.nat64_pool_addr_dump() |
| 7363 | self.assertEqual(0, len(adresses)) |
| 7364 | |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7365 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 7366 | def test_ipfix_max_bibs_sessions(self): |
| 7367 | """ IPFIX logging maximum session and BIB entries exceeded """ |
| 7368 | max_bibs = 1280 |
| 7369 | max_sessions = 2560 |
| 7370 | remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4, |
| 7371 | '64:ff9b::', |
| 7372 | 96) |
| 7373 | |
| 7374 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7375 | self.nat_addr_n) |
| 7376 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7377 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7378 | |
| 7379 | pkts = [] |
| 7380 | src = "" |
| 7381 | for i in range(0, max_bibs): |
| 7382 | src = "fd01:aa::%x" % (i) |
| 7383 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 7384 | IPv6(src=src, dst=remote_host_ip6) / |
| 7385 | TCP(sport=12345, dport=80)) |
| 7386 | pkts.append(p) |
| 7387 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 7388 | IPv6(src=src, dst=remote_host_ip6) / |
| 7389 | TCP(sport=12345, dport=22)) |
| 7390 | pkts.append(p) |
| 7391 | self.pg0.add_stream(pkts) |
| 7392 | self.pg_enable_capture(self.pg_interfaces) |
| 7393 | self.pg_start() |
| 7394 | self.pg1.get_capture(max_sessions) |
| 7395 | |
| 7396 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 7397 | src_address=self.pg3.local_ip4n, |
| 7398 | path_mtu=512, |
| 7399 | template_interval=10) |
| 7400 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 7401 | src_port=self.ipfix_src_port) |
| 7402 | |
| 7403 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 7404 | IPv6(src=src, dst=remote_host_ip6) / |
| 7405 | TCP(sport=12345, dport=25)) |
| 7406 | self.pg0.add_stream(p) |
| 7407 | self.pg_enable_capture(self.pg_interfaces) |
| 7408 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 7409 | self.pg1.assert_nothing_captured() |
| 7410 | sleep(1) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7411 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 7412 | capture = self.pg3.get_capture(9) |
| 7413 | ipfix = IPFIXDecoder() |
| 7414 | # first load template |
| 7415 | for p in capture: |
| 7416 | self.assertTrue(p.haslayer(IPFIX)) |
| 7417 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 7418 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 7419 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 7420 | self.assertEqual(p[UDP].dport, 4739) |
| 7421 | self.assertEqual(p[IPFIX].observationDomainID, |
| 7422 | self.ipfix_domain_id) |
| 7423 | if p.haslayer(Template): |
| 7424 | ipfix.add_template(p.getlayer(Template)) |
| 7425 | # verify events in data set |
| 7426 | for p in capture: |
| 7427 | if p.haslayer(Data): |
| 7428 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 7429 | self.verify_ipfix_max_sessions(data, max_sessions) |
| 7430 | |
| 7431 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 7432 | IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6) / |
| 7433 | TCP(sport=12345, dport=80)) |
| 7434 | self.pg0.add_stream(p) |
| 7435 | self.pg_enable_capture(self.pg_interfaces) |
| 7436 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 7437 | self.pg1.assert_nothing_captured() |
| 7438 | sleep(1) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7439 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 7440 | capture = self.pg3.get_capture(1) |
| 7441 | # verify events in data set |
| 7442 | for p in capture: |
| 7443 | self.assertTrue(p.haslayer(IPFIX)) |
| 7444 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 7445 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 7446 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 7447 | self.assertEqual(p[UDP].dport, 4739) |
| 7448 | self.assertEqual(p[IPFIX].observationDomainID, |
| 7449 | self.ipfix_domain_id) |
| 7450 | if p.haslayer(Data): |
| 7451 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 7452 | self.verify_ipfix_max_bibs(data, max_bibs) |
| 7453 | |
| 7454 | def test_ipfix_max_frags(self): |
| 7455 | """ IPFIX logging maximum fragments pending reassembly exceeded """ |
| 7456 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7457 | self.nat_addr_n) |
| 7458 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7459 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 7460 | self.vapi.nat_set_reass(max_frag=1, is_ip6=1) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7461 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 7462 | src_address=self.pg3.local_ip4n, |
| 7463 | path_mtu=512, |
| 7464 | template_interval=10) |
| 7465 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 7466 | src_port=self.ipfix_src_port) |
| 7467 | |
| 7468 | data = 'a' * 200 |
| 7469 | pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4, |
| 7470 | self.tcp_port_in, 20, data) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 7471 | pkts.reverse() |
| 7472 | self.pg0.add_stream(pkts) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7473 | self.pg_enable_capture(self.pg_interfaces) |
| 7474 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 7475 | self.pg1.assert_nothing_captured() |
| 7476 | sleep(1) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7477 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 7478 | capture = self.pg3.get_capture(9) |
| 7479 | ipfix = IPFIXDecoder() |
| 7480 | # first load template |
| 7481 | for p in capture: |
| 7482 | self.assertTrue(p.haslayer(IPFIX)) |
| 7483 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 7484 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 7485 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 7486 | self.assertEqual(p[UDP].dport, 4739) |
| 7487 | self.assertEqual(p[IPFIX].observationDomainID, |
| 7488 | self.ipfix_domain_id) |
| 7489 | if p.haslayer(Template): |
| 7490 | ipfix.add_template(p.getlayer(Template)) |
| 7491 | # verify events in data set |
| 7492 | for p in capture: |
| 7493 | if p.haslayer(Data): |
| 7494 | data = ipfix.decode_data_set(p.getlayer(Set)) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 7495 | self.verify_ipfix_max_fragments_ip6(data, 1, |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7496 | self.pg0.remote_ip6n) |
| 7497 | |
| 7498 | def test_ipfix_bib_ses(self): |
| 7499 | """ IPFIX logging NAT64 BIB/session create and delete events """ |
| 7500 | self.tcp_port_in = random.randint(1025, 65535) |
| 7501 | remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4, |
| 7502 | '64:ff9b::', |
| 7503 | 96) |
| 7504 | |
| 7505 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7506 | self.nat_addr_n) |
| 7507 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7508 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7509 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 7510 | src_address=self.pg3.local_ip4n, |
| 7511 | path_mtu=512, |
| 7512 | template_interval=10) |
| 7513 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 7514 | src_port=self.ipfix_src_port) |
| 7515 | |
| 7516 | # Create |
| 7517 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 7518 | IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6) / |
| 7519 | TCP(sport=self.tcp_port_in, dport=25)) |
| 7520 | self.pg0.add_stream(p) |
| 7521 | self.pg_enable_capture(self.pg_interfaces) |
| 7522 | self.pg_start() |
| 7523 | p = self.pg1.get_capture(1) |
| 7524 | self.tcp_port_out = p[0][TCP].sport |
| 7525 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 7526 | capture = self.pg3.get_capture(10) |
| 7527 | ipfix = IPFIXDecoder() |
| 7528 | # first load template |
| 7529 | for p in capture: |
| 7530 | self.assertTrue(p.haslayer(IPFIX)) |
| 7531 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 7532 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 7533 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 7534 | self.assertEqual(p[UDP].dport, 4739) |
| 7535 | self.assertEqual(p[IPFIX].observationDomainID, |
| 7536 | self.ipfix_domain_id) |
| 7537 | if p.haslayer(Template): |
| 7538 | ipfix.add_template(p.getlayer(Template)) |
| 7539 | # verify events in data set |
| 7540 | for p in capture: |
| 7541 | if p.haslayer(Data): |
| 7542 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 7543 | if ord(data[0][230]) == 10: |
| 7544 | self.verify_ipfix_bib(data, 1, self.pg0.remote_ip6n) |
| 7545 | elif ord(data[0][230]) == 6: |
| 7546 | self.verify_ipfix_nat64_ses(data, |
| 7547 | 1, |
| 7548 | self.pg0.remote_ip6n, |
| 7549 | self.pg1.remote_ip4, |
| 7550 | 25) |
| 7551 | else: |
| 7552 | self.logger.error(ppp("Unexpected or invalid packet: ", p)) |
| 7553 | |
| 7554 | # Delete |
| 7555 | self.pg_enable_capture(self.pg_interfaces) |
| 7556 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7557 | self.nat_addr_n, |
| 7558 | is_add=0) |
| 7559 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 7560 | capture = self.pg3.get_capture(2) |
| 7561 | # verify events in data set |
| 7562 | for p in capture: |
| 7563 | self.assertTrue(p.haslayer(IPFIX)) |
| 7564 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 7565 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 7566 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 7567 | self.assertEqual(p[UDP].dport, 4739) |
| 7568 | self.assertEqual(p[IPFIX].observationDomainID, |
| 7569 | self.ipfix_domain_id) |
| 7570 | if p.haslayer(Data): |
| 7571 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 7572 | if ord(data[0][230]) == 11: |
| 7573 | self.verify_ipfix_bib(data, 0, self.pg0.remote_ip6n) |
| 7574 | elif ord(data[0][230]) == 7: |
| 7575 | self.verify_ipfix_nat64_ses(data, |
| 7576 | 0, |
| 7577 | self.pg0.remote_ip6n, |
| 7578 | self.pg1.remote_ip4, |
| 7579 | 25) |
| 7580 | else: |
| 7581 | self.logger.error(ppp("Unexpected or invalid packet: ", p)) |
| 7582 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7583 | def nat64_get_ses_num(self): |
| 7584 | """ |
| 7585 | Return number of active NAT64 sessions. |
| 7586 | """ |
Matus Fabian | ab9a59c | 2017-08-16 05:37:36 -0700 | [diff] [blame] | 7587 | st = self.vapi.nat64_st_dump() |
| 7588 | return len(st) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7589 | |
| 7590 | def clear_nat64(self): |
| 7591 | """ |
| 7592 | Clear NAT64 configuration. |
| 7593 | """ |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7594 | self.vapi.nat_ipfix(enable=0, src_port=self.ipfix_src_port, |
| 7595 | domain_id=self.ipfix_domain_id) |
| 7596 | self.ipfix_src_port = 4739 |
| 7597 | self.ipfix_domain_id = 1 |
| 7598 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 7599 | self.vapi.nat_set_timeouts() |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7600 | |
| 7601 | interfaces = self.vapi.nat64_interface_dump() |
| 7602 | for intf in interfaces: |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 7603 | if intf.is_inside > 1: |
| 7604 | self.vapi.nat64_add_del_interface(intf.sw_if_index, |
| 7605 | 0, |
| 7606 | is_add=0) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7607 | self.vapi.nat64_add_del_interface(intf.sw_if_index, |
| 7608 | intf.is_inside, |
| 7609 | is_add=0) |
| 7610 | |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7611 | bib = self.vapi.nat64_bib_dump(255) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7612 | for bibe in bib: |
| 7613 | if bibe.is_static: |
| 7614 | self.vapi.nat64_add_del_static_bib(bibe.i_addr, |
| 7615 | bibe.o_addr, |
| 7616 | bibe.i_port, |
| 7617 | bibe.o_port, |
| 7618 | bibe.proto, |
| 7619 | bibe.vrf_id, |
| 7620 | is_add=0) |
| 7621 | |
| 7622 | adresses = self.vapi.nat64_pool_addr_dump() |
| 7623 | for addr in adresses: |
| 7624 | self.vapi.nat64_add_del_pool_addr_range(addr.address, |
| 7625 | addr.address, |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 7626 | vrf_id=addr.vrf_id, |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7627 | is_add=0) |
| 7628 | |
Matus Fabian | 428dc91 | 2017-06-21 06:15:18 -0700 | [diff] [blame] | 7629 | prefixes = self.vapi.nat64_prefix_dump() |
| 7630 | for prefix in prefixes: |
| 7631 | self.vapi.nat64_add_del_prefix(prefix.prefix, |
| 7632 | prefix.prefix_len, |
| 7633 | vrf_id=prefix.vrf_id, |
| 7634 | is_add=0) |
| 7635 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7636 | def tearDown(self): |
| 7637 | super(TestNAT64, self).tearDown() |
| 7638 | if not self.vpp_dead: |
| 7639 | self.logger.info(self.vapi.cli("show nat64 pool")) |
| 7640 | self.logger.info(self.vapi.cli("show nat64 interfaces")) |
Matus Fabian | 428dc91 | 2017-06-21 06:15:18 -0700 | [diff] [blame] | 7641 | self.logger.info(self.vapi.cli("show nat64 prefix")) |
Matus Fabian | ab9a59c | 2017-08-16 05:37:36 -0700 | [diff] [blame] | 7642 | self.logger.info(self.vapi.cli("show nat64 bib all")) |
| 7643 | self.logger.info(self.vapi.cli("show nat64 session table all")) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 7644 | self.logger.info(self.vapi.cli("show nat virtual-reassembly")) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7645 | self.clear_nat64() |
| 7646 | |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7647 | |
| 7648 | class TestDSlite(MethodHolder): |
| 7649 | """ DS-Lite Test Cases """ |
| 7650 | |
| 7651 | @classmethod |
| 7652 | def setUpClass(cls): |
| 7653 | super(TestDSlite, cls).setUpClass() |
| 7654 | |
| 7655 | try: |
| 7656 | cls.nat_addr = '10.0.0.3' |
| 7657 | cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr) |
| 7658 | |
| 7659 | cls.create_pg_interfaces(range(2)) |
| 7660 | cls.pg0.admin_up() |
| 7661 | cls.pg0.config_ip4() |
| 7662 | cls.pg0.resolve_arp() |
| 7663 | cls.pg1.admin_up() |
| 7664 | cls.pg1.config_ip6() |
| 7665 | cls.pg1.generate_remote_hosts(2) |
| 7666 | cls.pg1.configure_ipv6_neighbors() |
| 7667 | |
| 7668 | except Exception: |
| 7669 | super(TestDSlite, cls).tearDownClass() |
| 7670 | raise |
| 7671 | |
| 7672 | def test_dslite(self): |
| 7673 | """ Test DS-Lite """ |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 7674 | nat_config = self.vapi.nat_show_config() |
| 7675 | self.assertEqual(0, nat_config.dslite_ce) |
| 7676 | |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7677 | self.vapi.dslite_add_del_pool_addr_range(self.nat_addr_n, |
| 7678 | self.nat_addr_n) |
| 7679 | aftr_ip4 = '192.0.0.1' |
| 7680 | aftr_ip4_n = socket.inet_pton(socket.AF_INET, aftr_ip4) |
| 7681 | aftr_ip6 = '2001:db8:85a3::8a2e:370:1' |
| 7682 | aftr_ip6_n = socket.inet_pton(socket.AF_INET6, aftr_ip6) |
| 7683 | self.vapi.dslite_set_aftr_addr(aftr_ip6_n, aftr_ip4_n) |
| 7684 | |
| 7685 | # UDP |
| 7686 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7687 | IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[0].ip6) / |
| 7688 | IP(dst=self.pg0.remote_ip4, src='192.168.1.1') / |
| 7689 | UDP(sport=20000, dport=10000)) |
| 7690 | self.pg1.add_stream(p) |
| 7691 | self.pg_enable_capture(self.pg_interfaces) |
| 7692 | self.pg_start() |
| 7693 | capture = self.pg0.get_capture(1) |
| 7694 | capture = capture[0] |
| 7695 | self.assertFalse(capture.haslayer(IPv6)) |
| 7696 | self.assertEqual(capture[IP].src, self.nat_addr) |
| 7697 | self.assertEqual(capture[IP].dst, self.pg0.remote_ip4) |
| 7698 | self.assertNotEqual(capture[UDP].sport, 20000) |
| 7699 | self.assertEqual(capture[UDP].dport, 10000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7700 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7701 | out_port = capture[UDP].sport |
| 7702 | |
| 7703 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7704 | IP(dst=self.nat_addr, src=self.pg0.remote_ip4) / |
| 7705 | UDP(sport=10000, dport=out_port)) |
| 7706 | self.pg0.add_stream(p) |
| 7707 | self.pg_enable_capture(self.pg_interfaces) |
| 7708 | self.pg_start() |
| 7709 | capture = self.pg1.get_capture(1) |
| 7710 | capture = capture[0] |
| 7711 | self.assertEqual(capture[IPv6].src, aftr_ip6) |
| 7712 | self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[0].ip6) |
| 7713 | self.assertEqual(capture[IP].src, self.pg0.remote_ip4) |
| 7714 | self.assertEqual(capture[IP].dst, '192.168.1.1') |
| 7715 | self.assertEqual(capture[UDP].sport, 10000) |
| 7716 | self.assertEqual(capture[UDP].dport, 20000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7717 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7718 | |
| 7719 | # TCP |
| 7720 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7721 | IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[1].ip6) / |
| 7722 | IP(dst=self.pg0.remote_ip4, src='192.168.1.1') / |
| 7723 | TCP(sport=20001, dport=10001)) |
| 7724 | self.pg1.add_stream(p) |
| 7725 | self.pg_enable_capture(self.pg_interfaces) |
| 7726 | self.pg_start() |
| 7727 | capture = self.pg0.get_capture(1) |
| 7728 | capture = capture[0] |
| 7729 | self.assertFalse(capture.haslayer(IPv6)) |
| 7730 | self.assertEqual(capture[IP].src, self.nat_addr) |
| 7731 | self.assertEqual(capture[IP].dst, self.pg0.remote_ip4) |
| 7732 | self.assertNotEqual(capture[TCP].sport, 20001) |
| 7733 | self.assertEqual(capture[TCP].dport, 10001) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7734 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7735 | out_port = capture[TCP].sport |
| 7736 | |
| 7737 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7738 | IP(dst=self.nat_addr, src=self.pg0.remote_ip4) / |
| 7739 | TCP(sport=10001, dport=out_port)) |
| 7740 | self.pg0.add_stream(p) |
| 7741 | self.pg_enable_capture(self.pg_interfaces) |
| 7742 | self.pg_start() |
| 7743 | capture = self.pg1.get_capture(1) |
| 7744 | capture = capture[0] |
| 7745 | self.assertEqual(capture[IPv6].src, aftr_ip6) |
| 7746 | self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6) |
| 7747 | self.assertEqual(capture[IP].src, self.pg0.remote_ip4) |
| 7748 | self.assertEqual(capture[IP].dst, '192.168.1.1') |
| 7749 | self.assertEqual(capture[TCP].sport, 10001) |
| 7750 | self.assertEqual(capture[TCP].dport, 20001) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7751 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7752 | |
| 7753 | # ICMP |
| 7754 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7755 | IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[1].ip6) / |
| 7756 | IP(dst=self.pg0.remote_ip4, src='192.168.1.1') / |
| 7757 | ICMP(id=4000, type='echo-request')) |
| 7758 | self.pg1.add_stream(p) |
| 7759 | self.pg_enable_capture(self.pg_interfaces) |
| 7760 | self.pg_start() |
| 7761 | capture = self.pg0.get_capture(1) |
| 7762 | capture = capture[0] |
| 7763 | self.assertFalse(capture.haslayer(IPv6)) |
| 7764 | self.assertEqual(capture[IP].src, self.nat_addr) |
| 7765 | self.assertEqual(capture[IP].dst, self.pg0.remote_ip4) |
| 7766 | self.assertNotEqual(capture[ICMP].id, 4000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7767 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7768 | out_id = capture[ICMP].id |
| 7769 | |
| 7770 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7771 | IP(dst=self.nat_addr, src=self.pg0.remote_ip4) / |
| 7772 | ICMP(id=out_id, type='echo-reply')) |
| 7773 | self.pg0.add_stream(p) |
| 7774 | self.pg_enable_capture(self.pg_interfaces) |
| 7775 | self.pg_start() |
| 7776 | capture = self.pg1.get_capture(1) |
| 7777 | capture = capture[0] |
| 7778 | self.assertEqual(capture[IPv6].src, aftr_ip6) |
| 7779 | self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6) |
| 7780 | self.assertEqual(capture[IP].src, self.pg0.remote_ip4) |
| 7781 | self.assertEqual(capture[IP].dst, '192.168.1.1') |
| 7782 | self.assertEqual(capture[ICMP].id, 4000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7783 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7784 | |
Matus Fabian | 331acc6 | 2017-12-08 03:38:51 -0800 | [diff] [blame] | 7785 | # ping DS-Lite AFTR tunnel endpoint address |
| 7786 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7787 | IPv6(src=self.pg1.remote_hosts[1].ip6, dst=aftr_ip6) / |
| 7788 | ICMPv6EchoRequest()) |
| 7789 | self.pg1.add_stream(p) |
| 7790 | self.pg_enable_capture(self.pg_interfaces) |
| 7791 | self.pg_start() |
| 7792 | capture = self.pg1.get_capture(1) |
Matus Fabian | 331acc6 | 2017-12-08 03:38:51 -0800 | [diff] [blame] | 7793 | capture = capture[0] |
| 7794 | self.assertEqual(capture[IPv6].src, aftr_ip6) |
| 7795 | self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6) |
| 7796 | self.assertTrue(capture.haslayer(ICMPv6EchoReply)) |
| 7797 | |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7798 | def tearDown(self): |
| 7799 | super(TestDSlite, self).tearDown() |
| 7800 | if not self.vpp_dead: |
| 7801 | self.logger.info(self.vapi.cli("show dslite pool")) |
| 7802 | self.logger.info( |
| 7803 | self.vapi.cli("show dslite aftr-tunnel-endpoint-address")) |
| 7804 | self.logger.info(self.vapi.cli("show dslite sessions")) |
| 7805 | |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 7806 | |
| 7807 | class TestDSliteCE(MethodHolder): |
| 7808 | """ DS-Lite CE Test Cases """ |
| 7809 | |
| 7810 | @classmethod |
| 7811 | def setUpConstants(cls): |
| 7812 | super(TestDSliteCE, cls).setUpConstants() |
| 7813 | cls.vpp_cmdline.extend(["nat", "{", "dslite ce", "}"]) |
| 7814 | |
| 7815 | @classmethod |
| 7816 | def setUpClass(cls): |
| 7817 | super(TestDSliteCE, cls).setUpClass() |
| 7818 | |
| 7819 | try: |
| 7820 | cls.create_pg_interfaces(range(2)) |
| 7821 | cls.pg0.admin_up() |
| 7822 | cls.pg0.config_ip4() |
| 7823 | cls.pg0.resolve_arp() |
| 7824 | cls.pg1.admin_up() |
| 7825 | cls.pg1.config_ip6() |
| 7826 | cls.pg1.generate_remote_hosts(1) |
| 7827 | cls.pg1.configure_ipv6_neighbors() |
| 7828 | |
| 7829 | except Exception: |
| 7830 | super(TestDSliteCE, cls).tearDownClass() |
| 7831 | raise |
| 7832 | |
| 7833 | def test_dslite_ce(self): |
| 7834 | """ Test DS-Lite CE """ |
| 7835 | |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 7836 | nat_config = self.vapi.nat_show_config() |
| 7837 | self.assertEqual(1, nat_config.dslite_ce) |
| 7838 | |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 7839 | b4_ip4 = '192.0.0.2' |
| 7840 | b4_ip4_n = socket.inet_pton(socket.AF_INET, b4_ip4) |
| 7841 | b4_ip6 = '2001:db8:62aa::375e:f4c1:1' |
| 7842 | b4_ip6_n = socket.inet_pton(socket.AF_INET6, b4_ip6) |
| 7843 | self.vapi.dslite_set_b4_addr(b4_ip6_n, b4_ip4_n) |
| 7844 | |
| 7845 | aftr_ip4 = '192.0.0.1' |
| 7846 | aftr_ip4_n = socket.inet_pton(socket.AF_INET, aftr_ip4) |
| 7847 | aftr_ip6 = '2001:db8:85a3::8a2e:370:1' |
| 7848 | aftr_ip6_n = socket.inet_pton(socket.AF_INET6, aftr_ip6) |
| 7849 | self.vapi.dslite_set_aftr_addr(aftr_ip6_n, aftr_ip4_n) |
| 7850 | |
| 7851 | self.vapi.ip_add_del_route(dst_address=aftr_ip6_n, |
| 7852 | dst_address_length=128, |
| 7853 | next_hop_address=self.pg1.remote_ip6n, |
| 7854 | next_hop_sw_if_index=self.pg1.sw_if_index, |
| 7855 | is_ipv6=1) |
| 7856 | |
| 7857 | # UDP encapsulation |
| 7858 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7859 | IP(dst=self.pg1.remote_ip4, src=self.pg0.remote_ip4) / |
| 7860 | UDP(sport=10000, dport=20000)) |
| 7861 | self.pg0.add_stream(p) |
| 7862 | self.pg_enable_capture(self.pg_interfaces) |
| 7863 | self.pg_start() |
| 7864 | capture = self.pg1.get_capture(1) |
| 7865 | capture = capture[0] |
| 7866 | self.assertEqual(capture[IPv6].src, b4_ip6) |
| 7867 | self.assertEqual(capture[IPv6].dst, aftr_ip6) |
| 7868 | self.assertEqual(capture[IP].src, self.pg0.remote_ip4) |
| 7869 | self.assertEqual(capture[IP].dst, self.pg1.remote_ip4) |
| 7870 | self.assertEqual(capture[UDP].sport, 10000) |
| 7871 | self.assertEqual(capture[UDP].dport, 20000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7872 | self.assert_packet_checksums_valid(capture) |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 7873 | |
| 7874 | # UDP decapsulation |
| 7875 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7876 | IPv6(dst=b4_ip6, src=aftr_ip6) / |
| 7877 | IP(dst=self.pg0.remote_ip4, src=self.pg1.remote_ip4) / |
| 7878 | UDP(sport=20000, dport=10000)) |
| 7879 | self.pg1.add_stream(p) |
| 7880 | self.pg_enable_capture(self.pg_interfaces) |
| 7881 | self.pg_start() |
| 7882 | capture = self.pg0.get_capture(1) |
| 7883 | capture = capture[0] |
| 7884 | self.assertFalse(capture.haslayer(IPv6)) |
| 7885 | self.assertEqual(capture[IP].src, self.pg1.remote_ip4) |
| 7886 | self.assertEqual(capture[IP].dst, self.pg0.remote_ip4) |
| 7887 | self.assertEqual(capture[UDP].sport, 20000) |
| 7888 | self.assertEqual(capture[UDP].dport, 10000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7889 | self.assert_packet_checksums_valid(capture) |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 7890 | |
| 7891 | # ping DS-Lite B4 tunnel endpoint address |
| 7892 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7893 | IPv6(src=self.pg1.remote_hosts[0].ip6, dst=b4_ip6) / |
| 7894 | ICMPv6EchoRequest()) |
| 7895 | self.pg1.add_stream(p) |
| 7896 | self.pg_enable_capture(self.pg_interfaces) |
| 7897 | self.pg_start() |
| 7898 | capture = self.pg1.get_capture(1) |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 7899 | capture = capture[0] |
| 7900 | self.assertEqual(capture[IPv6].src, b4_ip6) |
| 7901 | self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[0].ip6) |
| 7902 | self.assertTrue(capture.haslayer(ICMPv6EchoReply)) |
| 7903 | |
| 7904 | def tearDown(self): |
| 7905 | super(TestDSliteCE, self).tearDown() |
| 7906 | if not self.vpp_dead: |
| 7907 | self.logger.info( |
| 7908 | self.vapi.cli("show dslite aftr-tunnel-endpoint-address")) |
| 7909 | self.logger.info( |
| 7910 | self.vapi.cli("show dslite b4-tunnel-endpoint-address")) |
| 7911 | |
Matus Fabian | f2a23cc | 2018-01-22 03:41:53 -0800 | [diff] [blame] | 7912 | |
| 7913 | class TestNAT66(MethodHolder): |
| 7914 | """ NAT66 Test Cases """ |
| 7915 | |
| 7916 | @classmethod |
| 7917 | def setUpClass(cls): |
| 7918 | super(TestNAT66, cls).setUpClass() |
| 7919 | |
| 7920 | try: |
| 7921 | cls.nat_addr = 'fd01:ff::2' |
| 7922 | cls.nat_addr_n = socket.inet_pton(socket.AF_INET6, cls.nat_addr) |
| 7923 | |
| 7924 | cls.create_pg_interfaces(range(2)) |
| 7925 | cls.interfaces = list(cls.pg_interfaces) |
| 7926 | |
| 7927 | for i in cls.interfaces: |
| 7928 | i.admin_up() |
| 7929 | i.config_ip6() |
| 7930 | i.configure_ipv6_neighbors() |
| 7931 | |
| 7932 | except Exception: |
| 7933 | super(TestNAT66, cls).tearDownClass() |
| 7934 | raise |
| 7935 | |
| 7936 | def test_static(self): |
| 7937 | """ 1:1 NAT66 test """ |
| 7938 | self.vapi.nat66_add_del_interface(self.pg0.sw_if_index) |
| 7939 | self.vapi.nat66_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7940 | self.vapi.nat66_add_del_static_mapping(self.pg0.remote_ip6n, |
| 7941 | self.nat_addr_n) |
| 7942 | |
| 7943 | # in2out |
| 7944 | pkts = [] |
| 7945 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7946 | IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) / |
| 7947 | TCP()) |
| 7948 | pkts.append(p) |
| 7949 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7950 | IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) / |
| 7951 | UDP()) |
| 7952 | pkts.append(p) |
| 7953 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7954 | IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) / |
| 7955 | ICMPv6EchoRequest()) |
| 7956 | pkts.append(p) |
| 7957 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7958 | IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) / |
| 7959 | GRE() / IP() / TCP()) |
| 7960 | pkts.append(p) |
| 7961 | self.pg0.add_stream(pkts) |
| 7962 | self.pg_enable_capture(self.pg_interfaces) |
| 7963 | self.pg_start() |
| 7964 | capture = self.pg1.get_capture(len(pkts)) |
| 7965 | for packet in capture: |
| 7966 | try: |
| 7967 | self.assertEqual(packet[IPv6].src, self.nat_addr) |
| 7968 | self.assertEqual(packet[IPv6].dst, self.pg1.remote_ip6) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7969 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | f2a23cc | 2018-01-22 03:41:53 -0800 | [diff] [blame] | 7970 | except: |
| 7971 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7972 | raise |
| 7973 | |
| 7974 | # out2in |
| 7975 | pkts = [] |
| 7976 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7977 | IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) / |
| 7978 | TCP()) |
| 7979 | pkts.append(p) |
| 7980 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7981 | IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) / |
| 7982 | UDP()) |
| 7983 | pkts.append(p) |
| 7984 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7985 | IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) / |
| 7986 | ICMPv6EchoReply()) |
| 7987 | pkts.append(p) |
| 7988 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7989 | IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) / |
| 7990 | GRE() / IP() / TCP()) |
| 7991 | pkts.append(p) |
| 7992 | self.pg1.add_stream(pkts) |
| 7993 | self.pg_enable_capture(self.pg_interfaces) |
| 7994 | self.pg_start() |
| 7995 | capture = self.pg0.get_capture(len(pkts)) |
| 7996 | for packet in capture: |
| 7997 | try: |
| 7998 | self.assertEqual(packet[IPv6].src, self.pg1.remote_ip6) |
| 7999 | self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 8000 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | f2a23cc | 2018-01-22 03:41:53 -0800 | [diff] [blame] | 8001 | except: |
| 8002 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 8003 | raise |
| 8004 | |
| 8005 | sm = self.vapi.nat66_static_mapping_dump() |
| 8006 | self.assertEqual(len(sm), 1) |
| 8007 | self.assertEqual(sm[0].total_pkts, 8) |
| 8008 | |
Juraj Sloboda | 9341e34 | 2018-04-13 12:00:46 +0200 | [diff] [blame] | 8009 | def test_check_no_translate(self): |
| 8010 | """ NAT66 translate only when egress interface is outside interface """ |
| 8011 | self.vapi.nat66_add_del_interface(self.pg0.sw_if_index) |
| 8012 | self.vapi.nat66_add_del_interface(self.pg1.sw_if_index) |
| 8013 | self.vapi.nat66_add_del_static_mapping(self.pg0.remote_ip6n, |
| 8014 | self.nat_addr_n) |
| 8015 | |
| 8016 | # in2out |
| 8017 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 8018 | IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) / |
| 8019 | UDP()) |
| 8020 | self.pg0.add_stream([p]) |
| 8021 | self.pg_enable_capture(self.pg_interfaces) |
| 8022 | self.pg_start() |
| 8023 | capture = self.pg1.get_capture(1) |
| 8024 | packet = capture[0] |
| 8025 | try: |
| 8026 | self.assertEqual(packet[IPv6].src, self.pg0.remote_ip6) |
| 8027 | self.assertEqual(packet[IPv6].dst, self.pg1.remote_ip6) |
| 8028 | except: |
| 8029 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 8030 | raise |
| 8031 | |
Matus Fabian | f2a23cc | 2018-01-22 03:41:53 -0800 | [diff] [blame] | 8032 | def clear_nat66(self): |
| 8033 | """ |
| 8034 | Clear NAT66 configuration. |
| 8035 | """ |
| 8036 | interfaces = self.vapi.nat66_interface_dump() |
| 8037 | for intf in interfaces: |
| 8038 | self.vapi.nat66_add_del_interface(intf.sw_if_index, |
| 8039 | intf.is_inside, |
| 8040 | is_add=0) |
| 8041 | |
| 8042 | static_mappings = self.vapi.nat66_static_mapping_dump() |
| 8043 | for sm in static_mappings: |
| 8044 | self.vapi.nat66_add_del_static_mapping(sm.local_ip_address, |
| 8045 | sm.external_ip_address, |
| 8046 | sm.vrf_id, |
| 8047 | is_add=0) |
| 8048 | |
| 8049 | def tearDown(self): |
| 8050 | super(TestNAT66, self).tearDown() |
| 8051 | if not self.vpp_dead: |
| 8052 | self.logger.info(self.vapi.cli("show nat66 interfaces")) |
| 8053 | self.logger.info(self.vapi.cli("show nat66 static mappings")) |
| 8054 | self.clear_nat66() |
| 8055 | |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 8056 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 8057 | if __name__ == '__main__': |
| 8058 | unittest.main(testRunner=VppTestRunner) |