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 | |
Matus Fabian | 111add7 | 2018-11-20 02:19:05 -0800 | [diff] [blame] | 3342 | def test_frag_forwarding(self): |
| 3343 | """ NAT44 forwarding fragment test """ |
| 3344 | self.vapi.nat44_add_interface_addr(self.pg1.sw_if_index) |
| 3345 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3346 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3347 | is_inside=0) |
| 3348 | self.vapi.nat44_forwarding_enable_disable(1) |
| 3349 | |
| 3350 | data = "A" * 16 + "B" * 16 + "C" * 3 |
| 3351 | pkts = self.create_stream_frag(self.pg1, |
| 3352 | self.pg0.remote_ip4, |
| 3353 | 4789, |
| 3354 | 4789, |
| 3355 | data, |
| 3356 | proto=IP_PROTOS.udp) |
| 3357 | self.pg1.add_stream(pkts) |
| 3358 | self.pg_enable_capture(self.pg_interfaces) |
| 3359 | self.pg_start() |
| 3360 | frags = self.pg0.get_capture(len(pkts)) |
| 3361 | p = self.reass_frags_and_verify(frags, |
| 3362 | self.pg1.remote_ip4, |
| 3363 | self.pg0.remote_ip4) |
| 3364 | self.assertEqual(p[UDP].sport, 4789) |
| 3365 | self.assertEqual(p[UDP].dport, 4789) |
| 3366 | self.assertEqual(data, p[Raw].load) |
| 3367 | |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3368 | def test_reass_hairpinning(self): |
| 3369 | """ NAT44 fragments hairpinning """ |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 3370 | |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame] | 3371 | self.server = self.pg0.remote_hosts[1] |
| 3372 | self.host_in_port = random.randint(1025, 65535) |
| 3373 | self.server_in_port = random.randint(1025, 65535) |
| 3374 | self.server_out_port = random.randint(1025, 65535) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3375 | |
| 3376 | self.nat44_add_address(self.nat_addr) |
| 3377 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3378 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3379 | is_inside=0) |
| 3380 | # add static mapping for server |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame] | 3381 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr, |
| 3382 | self.server_in_port, |
| 3383 | self.server_out_port, |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3384 | proto=IP_PROTOS.tcp) |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame] | 3385 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr, |
| 3386 | self.server_in_port, |
| 3387 | self.server_out_port, |
| 3388 | proto=IP_PROTOS.udp) |
| 3389 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3390 | |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame] | 3391 | self.reass_hairpinning(proto=IP_PROTOS.tcp) |
| 3392 | self.reass_hairpinning(proto=IP_PROTOS.udp) |
| 3393 | self.reass_hairpinning(proto=IP_PROTOS.icmp) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3394 | |
| 3395 | def test_frag_out_of_order(self): |
| 3396 | """ NAT44 translate fragments arriving out of order """ |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 3397 | |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3398 | self.nat44_add_address(self.nat_addr) |
| 3399 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3400 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3401 | is_inside=0) |
| 3402 | |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame] | 3403 | self.frag_out_of_order(proto=IP_PROTOS.tcp) |
| 3404 | self.frag_out_of_order(proto=IP_PROTOS.udp) |
| 3405 | self.frag_out_of_order(proto=IP_PROTOS.icmp) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 3406 | |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 3407 | def test_port_restricted(self): |
| 3408 | """ Port restricted NAT44 (MAP-E CE) """ |
| 3409 | self.nat44_add_address(self.nat_addr) |
| 3410 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3411 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3412 | is_inside=0) |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 3413 | self.vapi.nat_set_addr_and_port_alloc_alg(alg=1, |
| 3414 | psid_offset=6, |
| 3415 | psid_length=6, |
| 3416 | psid=10) |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 3417 | |
| 3418 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 3419 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 3420 | TCP(sport=4567, dport=22)) |
| 3421 | self.pg0.add_stream(p) |
| 3422 | self.pg_enable_capture(self.pg_interfaces) |
| 3423 | self.pg_start() |
| 3424 | capture = self.pg1.get_capture(1) |
| 3425 | p = capture[0] |
| 3426 | try: |
| 3427 | ip = p[IP] |
| 3428 | tcp = p[TCP] |
| 3429 | self.assertEqual(ip.dst, self.pg1.remote_ip4) |
| 3430 | self.assertEqual(ip.src, self.nat_addr) |
| 3431 | self.assertEqual(tcp.dport, 22) |
| 3432 | self.assertNotEqual(tcp.sport, 4567) |
| 3433 | self.assertEqual((tcp.sport >> 6) & 63, 10) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 3434 | self.assert_packet_checksums_valid(p) |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 3435 | except: |
| 3436 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 3437 | raise |
| 3438 | |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 3439 | def test_port_range(self): |
| 3440 | """ External address port range """ |
| 3441 | self.nat44_add_address(self.nat_addr) |
| 3442 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3443 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3444 | is_inside=0) |
| 3445 | self.vapi.nat_set_addr_and_port_alloc_alg(alg=2, |
| 3446 | start_port=1025, |
| 3447 | end_port=1027) |
| 3448 | |
| 3449 | pkts = [] |
| 3450 | for port in range(0, 5): |
| 3451 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 3452 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 3453 | TCP(sport=1125 + port)) |
| 3454 | pkts.append(p) |
| 3455 | self.pg0.add_stream(pkts) |
| 3456 | self.pg_enable_capture(self.pg_interfaces) |
| 3457 | self.pg_start() |
| 3458 | capture = self.pg1.get_capture(3) |
| 3459 | for p in capture: |
| 3460 | tcp = p[TCP] |
| 3461 | self.assertGreaterEqual(tcp.sport, 1025) |
| 3462 | self.assertLessEqual(tcp.sport, 1027) |
| 3463 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3464 | def test_ipfix_max_frags(self): |
| 3465 | """ IPFIX logging maximum fragments pending reassembly exceeded """ |
| 3466 | self.nat44_add_address(self.nat_addr) |
| 3467 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3468 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3469 | is_inside=0) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 3470 | self.vapi.nat_set_reass(max_frag=1) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3471 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 3472 | src_address=self.pg3.local_ip4n, |
| 3473 | path_mtu=512, |
| 3474 | template_interval=10) |
| 3475 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 3476 | src_port=self.ipfix_src_port) |
| 3477 | |
| 3478 | data = "A" * 4 + "B" * 16 + "C" * 3 |
| 3479 | self.tcp_port_in = random.randint(1025, 65535) |
| 3480 | pkts = self.create_stream_frag(self.pg0, |
| 3481 | self.pg1.remote_ip4, |
| 3482 | self.tcp_port_in, |
| 3483 | 20, |
| 3484 | data) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 3485 | pkts.reverse() |
| 3486 | self.pg0.add_stream(pkts) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3487 | self.pg_enable_capture(self.pg_interfaces) |
| 3488 | self.pg_start() |
| 3489 | self.pg1.assert_nothing_captured() |
| 3490 | sleep(1) |
| 3491 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 3492 | capture = self.pg3.get_capture(9) |
| 3493 | ipfix = IPFIXDecoder() |
| 3494 | # first load template |
| 3495 | for p in capture: |
| 3496 | self.assertTrue(p.haslayer(IPFIX)) |
| 3497 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 3498 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 3499 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 3500 | self.assertEqual(p[UDP].dport, 4739) |
| 3501 | self.assertEqual(p[IPFIX].observationDomainID, |
| 3502 | self.ipfix_domain_id) |
| 3503 | if p.haslayer(Template): |
| 3504 | ipfix.add_template(p.getlayer(Template)) |
| 3505 | # verify events in data set |
| 3506 | for p in capture: |
| 3507 | if p.haslayer(Data): |
| 3508 | data = ipfix.decode_data_set(p.getlayer(Set)) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 3509 | self.verify_ipfix_max_fragments_ip4(data, 1, |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3510 | self.pg0.remote_ip4n) |
| 3511 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 3512 | def test_multiple_outside_vrf(self): |
| 3513 | """ Multiple outside VRF """ |
| 3514 | vrf_id1 = 1 |
| 3515 | vrf_id2 = 2 |
| 3516 | |
| 3517 | self.pg1.unconfig_ip4() |
| 3518 | self.pg2.unconfig_ip4() |
| 3519 | self.vapi.ip_table_add_del(vrf_id1, is_add=1) |
| 3520 | self.vapi.ip_table_add_del(vrf_id2, is_add=1) |
| 3521 | self.pg1.set_table_ip4(vrf_id1) |
| 3522 | self.pg2.set_table_ip4(vrf_id2) |
| 3523 | self.pg1.config_ip4() |
| 3524 | self.pg2.config_ip4() |
| 3525 | self.pg1.resolve_arp() |
| 3526 | self.pg2.resolve_arp() |
| 3527 | |
| 3528 | self.nat44_add_address(self.nat_addr) |
| 3529 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3530 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3531 | is_inside=0) |
| 3532 | self.vapi.nat44_interface_add_del_feature(self.pg2.sw_if_index, |
| 3533 | is_inside=0) |
| 3534 | |
| 3535 | try: |
| 3536 | # first VRF |
| 3537 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 3538 | self.pg0.add_stream(pkts) |
| 3539 | self.pg_enable_capture(self.pg_interfaces) |
| 3540 | self.pg_start() |
| 3541 | capture = self.pg1.get_capture(len(pkts)) |
| 3542 | self.verify_capture_out(capture, self.nat_addr) |
| 3543 | |
| 3544 | pkts = self.create_stream_out(self.pg1, self.nat_addr) |
| 3545 | self.pg1.add_stream(pkts) |
| 3546 | self.pg_enable_capture(self.pg_interfaces) |
| 3547 | self.pg_start() |
| 3548 | capture = self.pg0.get_capture(len(pkts)) |
| 3549 | self.verify_capture_in(capture, self.pg0) |
| 3550 | |
| 3551 | self.tcp_port_in = 60303 |
| 3552 | self.udp_port_in = 60304 |
| 3553 | self.icmp_id_in = 60305 |
| 3554 | |
| 3555 | # second VRF |
| 3556 | pkts = self.create_stream_in(self.pg0, self.pg2) |
| 3557 | self.pg0.add_stream(pkts) |
| 3558 | self.pg_enable_capture(self.pg_interfaces) |
| 3559 | self.pg_start() |
| 3560 | capture = self.pg2.get_capture(len(pkts)) |
| 3561 | self.verify_capture_out(capture, self.nat_addr) |
| 3562 | |
| 3563 | pkts = self.create_stream_out(self.pg2, self.nat_addr) |
| 3564 | self.pg2.add_stream(pkts) |
| 3565 | self.pg_enable_capture(self.pg_interfaces) |
| 3566 | self.pg_start() |
| 3567 | capture = self.pg0.get_capture(len(pkts)) |
| 3568 | self.verify_capture_in(capture, self.pg0) |
| 3569 | |
| 3570 | finally: |
| 3571 | self.pg1.unconfig_ip4() |
| 3572 | self.pg2.unconfig_ip4() |
| 3573 | self.pg1.set_table_ip4(0) |
| 3574 | self.pg2.set_table_ip4(0) |
| 3575 | self.pg1.config_ip4() |
| 3576 | self.pg2.config_ip4() |
| 3577 | self.pg1.resolve_arp() |
| 3578 | self.pg2.resolve_arp() |
| 3579 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 3580 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 3581 | def test_session_timeout(self): |
| 3582 | """ NAT44 session timeouts """ |
| 3583 | self.nat44_add_address(self.nat_addr) |
| 3584 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3585 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3586 | is_inside=0) |
| 3587 | self.vapi.nat_set_timeouts(udp=5) |
| 3588 | |
| 3589 | max_sessions = 1000 |
| 3590 | pkts = [] |
| 3591 | for i in range(0, max_sessions): |
| 3592 | src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF) |
| 3593 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 3594 | IP(src=src, dst=self.pg1.remote_ip4) / |
| 3595 | UDP(sport=1025, dport=53)) |
| 3596 | pkts.append(p) |
| 3597 | self.pg0.add_stream(pkts) |
| 3598 | self.pg_enable_capture(self.pg_interfaces) |
| 3599 | self.pg_start() |
| 3600 | self.pg1.get_capture(max_sessions) |
| 3601 | |
| 3602 | sleep(6) |
| 3603 | |
| 3604 | pkts = [] |
| 3605 | for i in range(0, max_sessions): |
| 3606 | src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF) |
| 3607 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 3608 | IP(src=src, dst=self.pg1.remote_ip4) / |
| 3609 | UDP(sport=1026, dport=53)) |
| 3610 | pkts.append(p) |
| 3611 | self.pg0.add_stream(pkts) |
| 3612 | self.pg_enable_capture(self.pg_interfaces) |
| 3613 | self.pg_start() |
| 3614 | self.pg1.get_capture(max_sessions) |
| 3615 | |
| 3616 | nsessions = 0 |
| 3617 | users = self.vapi.nat44_user_dump() |
| 3618 | for user in users: |
| 3619 | nsessions = nsessions + user.nsessions |
| 3620 | self.assertLess(nsessions, 2 * max_sessions) |
| 3621 | |
Matus Fabian | bb4e022 | 2018-09-13 02:36:25 -0700 | [diff] [blame] | 3622 | def test_mss_clamping(self): |
| 3623 | """ TCP MSS clamping """ |
| 3624 | self.nat44_add_address(self.nat_addr) |
| 3625 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3626 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3627 | is_inside=0) |
| 3628 | |
| 3629 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 3630 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 3631 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 3632 | flags="S", options=[('MSS', 1400)])) |
| 3633 | |
| 3634 | self.vapi.nat_set_mss_clamping(enable=1, mss_value=1000) |
| 3635 | self.pg0.add_stream(p) |
| 3636 | self.pg_enable_capture(self.pg_interfaces) |
| 3637 | self.pg_start() |
| 3638 | capture = self.pg1.get_capture(1) |
| 3639 | # Negotiated MSS value greater than configured - changed |
| 3640 | self.verify_mss_value(capture[0], 1000) |
| 3641 | |
| 3642 | self.vapi.nat_set_mss_clamping(enable=0) |
| 3643 | self.pg0.add_stream(p) |
| 3644 | self.pg_enable_capture(self.pg_interfaces) |
| 3645 | self.pg_start() |
| 3646 | capture = self.pg1.get_capture(1) |
| 3647 | # MSS clamping disabled - negotiated MSS unchanged |
| 3648 | self.verify_mss_value(capture[0], 1400) |
| 3649 | |
| 3650 | self.vapi.nat_set_mss_clamping(enable=1, mss_value=1500) |
| 3651 | self.pg0.add_stream(p) |
| 3652 | self.pg_enable_capture(self.pg_interfaces) |
| 3653 | self.pg_start() |
| 3654 | capture = self.pg1.get_capture(1) |
| 3655 | # Negotiated MSS value smaller than configured - unchanged |
| 3656 | self.verify_mss_value(capture[0], 1400) |
| 3657 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3658 | def tearDown(self): |
| 3659 | super(TestNAT44, self).tearDown() |
| 3660 | if not self.vpp_dead: |
| 3661 | self.logger.info(self.vapi.cli("show nat44 addresses")) |
| 3662 | self.logger.info(self.vapi.cli("show nat44 interfaces")) |
| 3663 | self.logger.info(self.vapi.cli("show nat44 static mappings")) |
| 3664 | self.logger.info(self.vapi.cli("show nat44 interface address")) |
| 3665 | self.logger.info(self.vapi.cli("show nat44 sessions detail")) |
| 3666 | self.logger.info(self.vapi.cli("show nat virtual-reassembly")) |
| 3667 | self.logger.info(self.vapi.cli("show nat44 hash tables detail")) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 3668 | self.logger.info(self.vapi.cli("show nat timeouts")) |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 3669 | self.logger.info( |
| 3670 | self.vapi.cli("show nat addr-port-assignment-alg")) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3671 | self.clear_nat44() |
| 3672 | self.vapi.cli("clear logging") |
| 3673 | |
| 3674 | |
| 3675 | class TestNAT44EndpointDependent(MethodHolder): |
| 3676 | """ Endpoint-Dependent mapping and filtering test cases """ |
| 3677 | |
| 3678 | @classmethod |
| 3679 | def setUpConstants(cls): |
| 3680 | super(TestNAT44EndpointDependent, cls).setUpConstants() |
| 3681 | cls.vpp_cmdline.extend(["nat", "{", "endpoint-dependent", "}"]) |
| 3682 | |
| 3683 | @classmethod |
| 3684 | def setUpClass(cls): |
| 3685 | super(TestNAT44EndpointDependent, cls).setUpClass() |
| 3686 | cls.vapi.cli("set log class nat level debug") |
| 3687 | try: |
| 3688 | cls.tcp_port_in = 6303 |
| 3689 | cls.tcp_port_out = 6303 |
| 3690 | cls.udp_port_in = 6304 |
| 3691 | cls.udp_port_out = 6304 |
| 3692 | cls.icmp_id_in = 6305 |
| 3693 | cls.icmp_id_out = 6305 |
| 3694 | cls.nat_addr = '10.0.0.3' |
| 3695 | cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr) |
| 3696 | cls.ipfix_src_port = 4739 |
| 3697 | cls.ipfix_domain_id = 1 |
| 3698 | cls.tcp_external_port = 80 |
| 3699 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 3700 | cls.create_pg_interfaces(range(7)) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3701 | cls.interfaces = list(cls.pg_interfaces[0:3]) |
| 3702 | |
| 3703 | for i in cls.interfaces: |
| 3704 | i.admin_up() |
| 3705 | i.config_ip4() |
| 3706 | i.resolve_arp() |
| 3707 | |
| 3708 | cls.pg0.generate_remote_hosts(3) |
| 3709 | cls.pg0.configure_ipv4_neighbors() |
| 3710 | |
| 3711 | cls.pg3.admin_up() |
| 3712 | |
| 3713 | cls.pg4.generate_remote_hosts(2) |
| 3714 | cls.pg4.config_ip4() |
| 3715 | ip_addr_n = socket.inet_pton(socket.AF_INET, "10.0.0.1") |
| 3716 | cls.vapi.sw_interface_add_del_address(cls.pg4.sw_if_index, |
| 3717 | ip_addr_n, |
| 3718 | 24) |
| 3719 | cls.pg4.admin_up() |
| 3720 | cls.pg4.resolve_arp() |
| 3721 | cls.pg4._remote_hosts[1]._ip4 = cls.pg4._remote_hosts[0]._ip4 |
| 3722 | cls.pg4.resolve_arp() |
| 3723 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 3724 | zero_ip4n = socket.inet_pton(socket.AF_INET, "0.0.0.0") |
| 3725 | cls.vapi.ip_table_add_del(1, is_add=1) |
| 3726 | |
| 3727 | cls.pg5._local_ip4 = "10.1.1.1" |
| 3728 | cls.pg5._local_ip4n = socket.inet_pton(socket.AF_INET, |
| 3729 | cls.pg5.local_ip4) |
| 3730 | cls.pg5._remote_hosts[0]._ip4 = "10.1.1.2" |
| 3731 | cls.pg5._remote_hosts[0]._ip4n = socket.inet_pton( |
| 3732 | socket.AF_INET, cls.pg5.remote_ip4) |
| 3733 | cls.pg5.set_table_ip4(1) |
| 3734 | cls.pg5.config_ip4() |
| 3735 | cls.pg5.admin_up() |
| 3736 | cls.vapi.ip_add_del_route(dst_address=cls.pg5.remote_ip4n, |
| 3737 | dst_address_length=32, |
| 3738 | table_id=1, |
| 3739 | next_hop_sw_if_index=cls.pg5.sw_if_index, |
| 3740 | next_hop_address=zero_ip4n) |
| 3741 | |
| 3742 | cls.pg6._local_ip4 = "10.1.2.1" |
| 3743 | cls.pg6._local_ip4n = socket.inet_pton(socket.AF_INET, |
| 3744 | cls.pg6.local_ip4) |
| 3745 | cls.pg6._remote_hosts[0]._ip4 = "10.1.2.2" |
| 3746 | cls.pg6._remote_hosts[0]._ip4n = socket.inet_pton( |
| 3747 | socket.AF_INET, cls.pg6.remote_ip4) |
| 3748 | cls.pg6.set_table_ip4(1) |
| 3749 | cls.pg6.config_ip4() |
| 3750 | cls.pg6.admin_up() |
| 3751 | cls.vapi.ip_add_del_route(dst_address=cls.pg6.remote_ip4n, |
| 3752 | dst_address_length=32, |
| 3753 | table_id=1, |
| 3754 | next_hop_sw_if_index=cls.pg6.sw_if_index, |
| 3755 | next_hop_address=zero_ip4n) |
| 3756 | |
| 3757 | cls.vapi.ip_add_del_route(dst_address=cls.pg6.remote_ip4n, |
| 3758 | dst_address_length=16, |
| 3759 | next_hop_address=zero_ip4n, |
| 3760 | table_id=0, |
| 3761 | next_hop_table_id=1) |
| 3762 | cls.vapi.ip_add_del_route(dst_address=zero_ip4n, |
| 3763 | dst_address_length=0, |
| 3764 | next_hop_address=zero_ip4n, |
| 3765 | table_id=1, |
| 3766 | next_hop_table_id=0) |
| 3767 | cls.vapi.ip_add_del_route(dst_address=zero_ip4n, |
| 3768 | dst_address_length=0, |
| 3769 | table_id=0, |
| 3770 | next_hop_sw_if_index=cls.pg1.sw_if_index, |
| 3771 | next_hop_address=cls.pg1.local_ip4n) |
| 3772 | |
| 3773 | cls.pg5.resolve_arp() |
| 3774 | cls.pg6.resolve_arp() |
| 3775 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3776 | except Exception: |
| 3777 | super(TestNAT44EndpointDependent, cls).tearDownClass() |
| 3778 | raise |
| 3779 | |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 3780 | def test_frag_in_order(self): |
| 3781 | """ NAT44 translate fragments arriving in order """ |
| 3782 | self.nat44_add_address(self.nat_addr) |
| 3783 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3784 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3785 | is_inside=0) |
| 3786 | self.frag_in_order(proto=IP_PROTOS.tcp) |
| 3787 | self.frag_in_order(proto=IP_PROTOS.udp) |
| 3788 | self.frag_in_order(proto=IP_PROTOS.icmp) |
| 3789 | |
| 3790 | def test_frag_in_order_dont_translate(self): |
| 3791 | """ NAT44 don't translate fragments arriving in order """ |
| 3792 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3793 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3794 | is_inside=0) |
| 3795 | self.vapi.nat44_forwarding_enable_disable(enable=True) |
| 3796 | self.frag_in_order(proto=IP_PROTOS.tcp, dont_translate=True) |
| 3797 | |
| 3798 | def test_frag_out_of_order(self): |
| 3799 | """ NAT44 translate fragments arriving out of order """ |
| 3800 | self.nat44_add_address(self.nat_addr) |
| 3801 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3802 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3803 | is_inside=0) |
| 3804 | self.frag_out_of_order(proto=IP_PROTOS.tcp) |
| 3805 | self.frag_out_of_order(proto=IP_PROTOS.udp) |
| 3806 | self.frag_out_of_order(proto=IP_PROTOS.icmp) |
| 3807 | |
| 3808 | def test_frag_out_of_order_dont_translate(self): |
| 3809 | """ NAT44 don't translate fragments arriving out of order """ |
| 3810 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3811 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3812 | is_inside=0) |
| 3813 | self.vapi.nat44_forwarding_enable_disable(enable=True) |
| 3814 | self.frag_out_of_order(proto=IP_PROTOS.tcp, dont_translate=True) |
| 3815 | |
| 3816 | def test_frag_in_order_in_plus_out(self): |
| 3817 | """ in+out interface fragments in order """ |
| 3818 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3819 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 3820 | is_inside=0) |
| 3821 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index) |
| 3822 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3823 | is_inside=0) |
| 3824 | |
| 3825 | self.server = self.pg1.remote_hosts[0] |
| 3826 | |
| 3827 | self.server_in_addr = self.server.ip4 |
| 3828 | self.server_out_addr = '11.11.11.11' |
| 3829 | self.server_in_port = random.randint(1025, 65535) |
| 3830 | self.server_out_port = random.randint(1025, 65535) |
| 3831 | |
| 3832 | self.nat44_add_address(self.server_out_addr) |
| 3833 | |
| 3834 | # add static mappings for server |
| 3835 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3836 | self.server_out_addr, |
| 3837 | self.server_in_port, |
| 3838 | self.server_out_port, |
| 3839 | proto=IP_PROTOS.tcp) |
| 3840 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3841 | self.server_out_addr, |
| 3842 | self.server_in_port, |
| 3843 | self.server_out_port, |
| 3844 | proto=IP_PROTOS.udp) |
| 3845 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3846 | self.server_out_addr, |
| 3847 | proto=IP_PROTOS.icmp) |
| 3848 | |
| 3849 | self.vapi.nat_set_reass(timeout=10) |
| 3850 | |
| 3851 | self.frag_in_order_in_plus_out(proto=IP_PROTOS.tcp) |
| 3852 | self.frag_in_order_in_plus_out(proto=IP_PROTOS.udp) |
| 3853 | self.frag_in_order_in_plus_out(proto=IP_PROTOS.icmp) |
| 3854 | |
| 3855 | def test_frag_out_of_order_in_plus_out(self): |
| 3856 | """ in+out interface fragments out of order """ |
| 3857 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3858 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 3859 | is_inside=0) |
| 3860 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index) |
| 3861 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3862 | is_inside=0) |
| 3863 | |
| 3864 | self.server = self.pg1.remote_hosts[0] |
| 3865 | |
| 3866 | self.server_in_addr = self.server.ip4 |
| 3867 | self.server_out_addr = '11.11.11.11' |
| 3868 | self.server_in_port = random.randint(1025, 65535) |
| 3869 | self.server_out_port = random.randint(1025, 65535) |
| 3870 | |
| 3871 | self.nat44_add_address(self.server_out_addr) |
| 3872 | |
| 3873 | # add static mappings for server |
| 3874 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3875 | self.server_out_addr, |
| 3876 | self.server_in_port, |
| 3877 | self.server_out_port, |
| 3878 | proto=IP_PROTOS.tcp) |
| 3879 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3880 | self.server_out_addr, |
| 3881 | self.server_in_port, |
| 3882 | self.server_out_port, |
| 3883 | proto=IP_PROTOS.udp) |
| 3884 | self.nat44_add_static_mapping(self.server_in_addr, |
| 3885 | self.server_out_addr, |
| 3886 | proto=IP_PROTOS.icmp) |
| 3887 | |
| 3888 | self.vapi.nat_set_reass(timeout=10) |
| 3889 | |
| 3890 | self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.tcp) |
| 3891 | self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.udp) |
| 3892 | self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.icmp) |
| 3893 | |
| 3894 | def test_reass_hairpinning(self): |
| 3895 | """ NAT44 fragments hairpinning """ |
| 3896 | self.server = self.pg0.remote_hosts[1] |
| 3897 | self.host_in_port = random.randint(1025, 65535) |
| 3898 | self.server_in_port = random.randint(1025, 65535) |
| 3899 | self.server_out_port = random.randint(1025, 65535) |
| 3900 | |
| 3901 | self.nat44_add_address(self.nat_addr) |
| 3902 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3903 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3904 | is_inside=0) |
| 3905 | # add static mapping for server |
| 3906 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr, |
| 3907 | self.server_in_port, |
| 3908 | self.server_out_port, |
| 3909 | proto=IP_PROTOS.tcp) |
| 3910 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr, |
| 3911 | self.server_in_port, |
| 3912 | self.server_out_port, |
| 3913 | proto=IP_PROTOS.udp) |
Matus Fabian | da41d72 | 2018-10-19 04:01:19 -0700 | [diff] [blame] | 3914 | self.nat44_add_static_mapping(self.server.ip4, self.nat_addr) |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame] | 3915 | |
| 3916 | self.reass_hairpinning(proto=IP_PROTOS.tcp) |
| 3917 | self.reass_hairpinning(proto=IP_PROTOS.udp) |
| 3918 | self.reass_hairpinning(proto=IP_PROTOS.icmp) |
| 3919 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3920 | def test_dynamic(self): |
| 3921 | """ NAT44 dynamic translation test """ |
| 3922 | |
| 3923 | self.nat44_add_address(self.nat_addr) |
| 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 | |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 3928 | nat_config = self.vapi.nat_show_config() |
| 3929 | self.assertEqual(1, nat_config.endpoint_dependent) |
| 3930 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3931 | # in2out |
| 3932 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 3933 | self.pg0.add_stream(pkts) |
| 3934 | self.pg_enable_capture(self.pg_interfaces) |
| 3935 | self.pg_start() |
| 3936 | capture = self.pg1.get_capture(len(pkts)) |
| 3937 | self.verify_capture_out(capture) |
| 3938 | |
| 3939 | # out2in |
| 3940 | pkts = self.create_stream_out(self.pg1) |
| 3941 | self.pg1.add_stream(pkts) |
| 3942 | self.pg_enable_capture(self.pg_interfaces) |
| 3943 | self.pg_start() |
| 3944 | capture = self.pg0.get_capture(len(pkts)) |
| 3945 | self.verify_capture_in(capture, self.pg0) |
| 3946 | |
| 3947 | def test_forwarding(self): |
| 3948 | """ NAT44 forwarding test """ |
| 3949 | |
| 3950 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 3951 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 3952 | is_inside=0) |
| 3953 | self.vapi.nat44_forwarding_enable_disable(1) |
| 3954 | |
| 3955 | real_ip = self.pg0.remote_ip4n |
| 3956 | alias_ip = self.nat_addr_n |
| 3957 | self.vapi.nat44_add_del_static_mapping(local_ip=real_ip, |
| 3958 | external_ip=alias_ip) |
| 3959 | |
| 3960 | try: |
| 3961 | # in2out - static mapping match |
| 3962 | |
| 3963 | pkts = self.create_stream_out(self.pg1) |
| 3964 | self.pg1.add_stream(pkts) |
| 3965 | self.pg_enable_capture(self.pg_interfaces) |
| 3966 | self.pg_start() |
| 3967 | capture = self.pg0.get_capture(len(pkts)) |
| 3968 | self.verify_capture_in(capture, self.pg0) |
| 3969 | |
| 3970 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 3971 | self.pg0.add_stream(pkts) |
| 3972 | self.pg_enable_capture(self.pg_interfaces) |
| 3973 | self.pg_start() |
| 3974 | capture = self.pg1.get_capture(len(pkts)) |
| 3975 | self.verify_capture_out(capture, same_port=True) |
| 3976 | |
| 3977 | # in2out - no static mapping match |
| 3978 | |
| 3979 | host0 = self.pg0.remote_hosts[0] |
| 3980 | self.pg0.remote_hosts[0] = self.pg0.remote_hosts[1] |
| 3981 | try: |
| 3982 | pkts = self.create_stream_out(self.pg1, |
| 3983 | dst_ip=self.pg0.remote_ip4, |
| 3984 | use_inside_ports=True) |
| 3985 | self.pg1.add_stream(pkts) |
| 3986 | self.pg_enable_capture(self.pg_interfaces) |
| 3987 | self.pg_start() |
| 3988 | capture = self.pg0.get_capture(len(pkts)) |
| 3989 | self.verify_capture_in(capture, self.pg0) |
| 3990 | |
| 3991 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 3992 | self.pg0.add_stream(pkts) |
| 3993 | self.pg_enable_capture(self.pg_interfaces) |
| 3994 | self.pg_start() |
| 3995 | capture = self.pg1.get_capture(len(pkts)) |
| 3996 | self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4, |
| 3997 | same_port=True) |
| 3998 | finally: |
| 3999 | self.pg0.remote_hosts[0] = host0 |
| 4000 | |
| 4001 | user = self.pg0.remote_hosts[1] |
| 4002 | sessions = self.vapi.nat44_user_session_dump(user.ip4n, 0) |
| 4003 | self.assertEqual(len(sessions), 3) |
| 4004 | self.assertTrue(sessions[0].ext_host_valid) |
| 4005 | self.vapi.nat44_del_session( |
| 4006 | sessions[0].inside_ip_address, |
| 4007 | sessions[0].inside_port, |
| 4008 | sessions[0].protocol, |
| 4009 | ext_host_address=sessions[0].ext_host_address, |
| 4010 | ext_host_port=sessions[0].ext_host_port) |
| 4011 | sessions = self.vapi.nat44_user_session_dump(user.ip4n, 0) |
| 4012 | self.assertEqual(len(sessions), 2) |
| 4013 | |
| 4014 | finally: |
| 4015 | self.vapi.nat44_forwarding_enable_disable(0) |
| 4016 | self.vapi.nat44_add_del_static_mapping(local_ip=real_ip, |
| 4017 | external_ip=alias_ip, |
| 4018 | is_add=0) |
| 4019 | |
| 4020 | def test_static_lb(self): |
| 4021 | """ NAT44 local service load balancing """ |
| 4022 | external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr) |
| 4023 | external_port = 80 |
| 4024 | local_port = 8080 |
| 4025 | server1 = self.pg0.remote_hosts[0] |
| 4026 | server2 = self.pg0.remote_hosts[1] |
| 4027 | |
| 4028 | locals = [{'addr': server1.ip4n, |
| 4029 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4030 | 'probability': 70, |
| 4031 | 'vrf_id': 0}, |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4032 | {'addr': server2.ip4n, |
| 4033 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4034 | 'probability': 30, |
| 4035 | 'vrf_id': 0}] |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4036 | |
| 4037 | self.nat44_add_address(self.nat_addr) |
| 4038 | self.vapi.nat44_add_del_lb_static_mapping(external_addr_n, |
| 4039 | external_port, |
| 4040 | IP_PROTOS.tcp, |
| 4041 | local_num=len(locals), |
| 4042 | locals=locals) |
| 4043 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4044 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4045 | is_inside=0) |
| 4046 | |
| 4047 | # from client to service |
| 4048 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4049 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4050 | TCP(sport=12345, dport=external_port)) |
| 4051 | self.pg1.add_stream(p) |
| 4052 | self.pg_enable_capture(self.pg_interfaces) |
| 4053 | self.pg_start() |
| 4054 | capture = self.pg0.get_capture(1) |
| 4055 | p = capture[0] |
| 4056 | server = None |
| 4057 | try: |
| 4058 | ip = p[IP] |
| 4059 | tcp = p[TCP] |
| 4060 | self.assertIn(ip.dst, [server1.ip4, server2.ip4]) |
| 4061 | if ip.dst == server1.ip4: |
| 4062 | server = server1 |
| 4063 | else: |
| 4064 | server = server2 |
| 4065 | self.assertEqual(tcp.dport, local_port) |
| 4066 | self.assert_packet_checksums_valid(p) |
| 4067 | except: |
| 4068 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4069 | raise |
| 4070 | |
| 4071 | # from service back to client |
| 4072 | p = (Ether(src=server.mac, dst=self.pg0.local_mac) / |
| 4073 | IP(src=server.ip4, dst=self.pg1.remote_ip4) / |
| 4074 | TCP(sport=local_port, dport=12345)) |
| 4075 | self.pg0.add_stream(p) |
| 4076 | self.pg_enable_capture(self.pg_interfaces) |
| 4077 | self.pg_start() |
| 4078 | capture = self.pg1.get_capture(1) |
| 4079 | p = capture[0] |
| 4080 | try: |
| 4081 | ip = p[IP] |
| 4082 | tcp = p[TCP] |
| 4083 | self.assertEqual(ip.src, self.nat_addr) |
| 4084 | self.assertEqual(tcp.sport, external_port) |
| 4085 | self.assert_packet_checksums_valid(p) |
| 4086 | except: |
| 4087 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4088 | raise |
| 4089 | |
| 4090 | sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0) |
| 4091 | self.assertEqual(len(sessions), 1) |
| 4092 | self.assertTrue(sessions[0].ext_host_valid) |
| 4093 | self.vapi.nat44_del_session( |
| 4094 | sessions[0].inside_ip_address, |
| 4095 | sessions[0].inside_port, |
| 4096 | sessions[0].protocol, |
| 4097 | ext_host_address=sessions[0].ext_host_address, |
| 4098 | ext_host_port=sessions[0].ext_host_port) |
| 4099 | sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0) |
| 4100 | self.assertEqual(len(sessions), 0) |
| 4101 | |
| 4102 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 4103 | def test_static_lb_multi_clients(self): |
| 4104 | """ NAT44 local service load balancing - multiple clients""" |
| 4105 | |
| 4106 | external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr) |
| 4107 | external_port = 80 |
| 4108 | local_port = 8080 |
| 4109 | server1 = self.pg0.remote_hosts[0] |
| 4110 | server2 = self.pg0.remote_hosts[1] |
| 4111 | |
| 4112 | locals = [{'addr': server1.ip4n, |
| 4113 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4114 | 'probability': 90, |
| 4115 | 'vrf_id': 0}, |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4116 | {'addr': server2.ip4n, |
| 4117 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4118 | 'probability': 10, |
| 4119 | 'vrf_id': 0}] |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4120 | |
| 4121 | self.nat44_add_address(self.nat_addr) |
| 4122 | self.vapi.nat44_add_del_lb_static_mapping(external_addr_n, |
| 4123 | external_port, |
| 4124 | IP_PROTOS.tcp, |
| 4125 | local_num=len(locals), |
| 4126 | locals=locals) |
| 4127 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4128 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4129 | is_inside=0) |
| 4130 | |
| 4131 | server1_n = 0 |
| 4132 | server2_n = 0 |
| 4133 | clients = ip4_range(self.pg1.remote_ip4, 10, 50) |
| 4134 | pkts = [] |
| 4135 | for client in clients: |
| 4136 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4137 | IP(src=client, dst=self.nat_addr) / |
| 4138 | TCP(sport=12345, dport=external_port)) |
| 4139 | pkts.append(p) |
| 4140 | self.pg1.add_stream(pkts) |
| 4141 | self.pg_enable_capture(self.pg_interfaces) |
| 4142 | self.pg_start() |
| 4143 | capture = self.pg0.get_capture(len(pkts)) |
| 4144 | for p in capture: |
| 4145 | if p[IP].dst == server1.ip4: |
| 4146 | server1_n += 1 |
| 4147 | else: |
| 4148 | server2_n += 1 |
| 4149 | self.assertTrue(server1_n > server2_n) |
| 4150 | |
| 4151 | def test_static_lb_2(self): |
| 4152 | """ NAT44 local service load balancing (asymmetrical rule) """ |
| 4153 | external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr) |
| 4154 | external_port = 80 |
| 4155 | local_port = 8080 |
| 4156 | server1 = self.pg0.remote_hosts[0] |
| 4157 | server2 = self.pg0.remote_hosts[1] |
| 4158 | |
| 4159 | locals = [{'addr': server1.ip4n, |
| 4160 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4161 | 'probability': 70, |
| 4162 | 'vrf_id': 0}, |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4163 | {'addr': server2.ip4n, |
| 4164 | 'port': local_port, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4165 | 'probability': 30, |
| 4166 | 'vrf_id': 0}] |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4167 | |
| 4168 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4169 | self.vapi.nat44_add_del_lb_static_mapping(external_addr_n, |
| 4170 | external_port, |
| 4171 | IP_PROTOS.tcp, |
| 4172 | out2in_only=1, |
| 4173 | local_num=len(locals), |
| 4174 | locals=locals) |
| 4175 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4176 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4177 | is_inside=0) |
| 4178 | |
| 4179 | # from client to service |
| 4180 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4181 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4182 | TCP(sport=12345, dport=external_port)) |
| 4183 | self.pg1.add_stream(p) |
| 4184 | self.pg_enable_capture(self.pg_interfaces) |
| 4185 | self.pg_start() |
| 4186 | capture = self.pg0.get_capture(1) |
| 4187 | p = capture[0] |
| 4188 | server = None |
| 4189 | try: |
| 4190 | ip = p[IP] |
| 4191 | tcp = p[TCP] |
| 4192 | self.assertIn(ip.dst, [server1.ip4, server2.ip4]) |
| 4193 | if ip.dst == server1.ip4: |
| 4194 | server = server1 |
| 4195 | else: |
| 4196 | server = server2 |
| 4197 | self.assertEqual(tcp.dport, local_port) |
| 4198 | self.assert_packet_checksums_valid(p) |
| 4199 | except: |
| 4200 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4201 | raise |
| 4202 | |
| 4203 | # from service back to client |
| 4204 | p = (Ether(src=server.mac, dst=self.pg0.local_mac) / |
| 4205 | IP(src=server.ip4, dst=self.pg1.remote_ip4) / |
| 4206 | TCP(sport=local_port, dport=12345)) |
| 4207 | self.pg0.add_stream(p) |
| 4208 | self.pg_enable_capture(self.pg_interfaces) |
| 4209 | self.pg_start() |
| 4210 | capture = self.pg1.get_capture(1) |
| 4211 | p = capture[0] |
| 4212 | try: |
| 4213 | ip = p[IP] |
| 4214 | tcp = p[TCP] |
| 4215 | self.assertEqual(ip.src, self.nat_addr) |
| 4216 | self.assertEqual(tcp.sport, external_port) |
| 4217 | self.assert_packet_checksums_valid(p) |
| 4218 | except: |
| 4219 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4220 | raise |
| 4221 | |
| 4222 | # from client to server (no translation) |
| 4223 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4224 | IP(src=self.pg1.remote_ip4, dst=server1.ip4) / |
| 4225 | TCP(sport=12346, dport=local_port)) |
| 4226 | self.pg1.add_stream(p) |
| 4227 | self.pg_enable_capture(self.pg_interfaces) |
| 4228 | self.pg_start() |
| 4229 | capture = self.pg0.get_capture(1) |
| 4230 | p = capture[0] |
| 4231 | server = None |
| 4232 | try: |
| 4233 | ip = p[IP] |
| 4234 | tcp = p[TCP] |
| 4235 | self.assertEqual(ip.dst, server1.ip4) |
| 4236 | self.assertEqual(tcp.dport, local_port) |
| 4237 | self.assert_packet_checksums_valid(p) |
| 4238 | except: |
| 4239 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4240 | raise |
| 4241 | |
| 4242 | # from service back to client (no translation) |
| 4243 | p = (Ether(src=server1.mac, dst=self.pg0.local_mac) / |
| 4244 | IP(src=server1.ip4, dst=self.pg1.remote_ip4) / |
| 4245 | TCP(sport=local_port, dport=12346)) |
| 4246 | self.pg0.add_stream(p) |
| 4247 | self.pg_enable_capture(self.pg_interfaces) |
| 4248 | self.pg_start() |
| 4249 | capture = self.pg1.get_capture(1) |
| 4250 | p = capture[0] |
| 4251 | try: |
| 4252 | ip = p[IP] |
| 4253 | tcp = p[TCP] |
| 4254 | self.assertEqual(ip.src, server1.ip4) |
| 4255 | self.assertEqual(tcp.sport, local_port) |
| 4256 | self.assert_packet_checksums_valid(p) |
| 4257 | except: |
| 4258 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4259 | raise |
| 4260 | |
Matus Fabian | ea5b5be | 2018-09-03 05:02:23 -0700 | [diff] [blame] | 4261 | def test_lb_affinity(self): |
| 4262 | """ NAT44 local service load balancing affinity """ |
| 4263 | external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr) |
| 4264 | external_port = 80 |
| 4265 | local_port = 8080 |
| 4266 | server1 = self.pg0.remote_hosts[0] |
| 4267 | server2 = self.pg0.remote_hosts[1] |
| 4268 | |
| 4269 | locals = [{'addr': server1.ip4n, |
| 4270 | 'port': local_port, |
| 4271 | 'probability': 50, |
| 4272 | 'vrf_id': 0}, |
| 4273 | {'addr': server2.ip4n, |
| 4274 | 'port': local_port, |
| 4275 | 'probability': 50, |
| 4276 | 'vrf_id': 0}] |
| 4277 | |
| 4278 | self.nat44_add_address(self.nat_addr) |
| 4279 | self.vapi.nat44_add_del_lb_static_mapping(external_addr_n, |
| 4280 | external_port, |
| 4281 | IP_PROTOS.tcp, |
| 4282 | affinity=10800, |
| 4283 | local_num=len(locals), |
| 4284 | locals=locals) |
| 4285 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4286 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4287 | is_inside=0) |
| 4288 | |
| 4289 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 4290 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4291 | TCP(sport=1025, dport=external_port)) |
| 4292 | self.pg1.add_stream(p) |
| 4293 | self.pg_enable_capture(self.pg_interfaces) |
| 4294 | self.pg_start() |
| 4295 | capture = self.pg0.get_capture(1) |
| 4296 | backend = capture[0][IP].dst |
| 4297 | |
| 4298 | sessions = self.vapi.nat44_user_session_dump( |
| 4299 | socket.inet_pton(socket.AF_INET, backend), 0) |
| 4300 | self.assertEqual(len(sessions), 1) |
| 4301 | self.assertTrue(sessions[0].ext_host_valid) |
| 4302 | self.vapi.nat44_del_session( |
| 4303 | sessions[0].inside_ip_address, |
| 4304 | sessions[0].inside_port, |
| 4305 | sessions[0].protocol, |
| 4306 | ext_host_address=sessions[0].ext_host_address, |
| 4307 | ext_host_port=sessions[0].ext_host_port) |
| 4308 | |
| 4309 | pkts = [] |
| 4310 | for port in range(1030, 1100): |
| 4311 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 4312 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4313 | TCP(sport=port, dport=external_port)) |
| 4314 | pkts.append(p) |
| 4315 | self.pg1.add_stream(pkts) |
| 4316 | self.pg_enable_capture(self.pg_interfaces) |
| 4317 | self.pg_start() |
| 4318 | capture = self.pg0.get_capture(len(pkts)) |
| 4319 | for p in capture: |
| 4320 | self.assertEqual(p[IP].dst, backend) |
| 4321 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4322 | def test_unknown_proto(self): |
| 4323 | """ NAT44 translate packet with unknown protocol """ |
| 4324 | self.nat44_add_address(self.nat_addr) |
| 4325 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4326 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4327 | is_inside=0) |
| 4328 | |
| 4329 | # in2out |
| 4330 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 4331 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4332 | TCP(sport=self.tcp_port_in, dport=20)) |
| 4333 | self.pg0.add_stream(p) |
| 4334 | self.pg_enable_capture(self.pg_interfaces) |
| 4335 | self.pg_start() |
| 4336 | p = self.pg1.get_capture(1) |
| 4337 | |
| 4338 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 4339 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4340 | GRE() / |
| 4341 | IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) / |
| 4342 | TCP(sport=1234, dport=1234)) |
| 4343 | self.pg0.add_stream(p) |
| 4344 | self.pg_enable_capture(self.pg_interfaces) |
| 4345 | self.pg_start() |
| 4346 | p = self.pg1.get_capture(1) |
| 4347 | packet = p[0] |
| 4348 | try: |
| 4349 | self.assertEqual(packet[IP].src, self.nat_addr) |
| 4350 | self.assertEqual(packet[IP].dst, self.pg1.remote_ip4) |
| 4351 | self.assertTrue(packet.haslayer(GRE)) |
| 4352 | self.assert_packet_checksums_valid(packet) |
| 4353 | except: |
| 4354 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 4355 | raise |
| 4356 | |
| 4357 | # out2in |
| 4358 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 4359 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4360 | GRE() / |
| 4361 | IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) / |
| 4362 | TCP(sport=1234, dport=1234)) |
| 4363 | self.pg1.add_stream(p) |
| 4364 | self.pg_enable_capture(self.pg_interfaces) |
| 4365 | self.pg_start() |
| 4366 | p = self.pg0.get_capture(1) |
| 4367 | packet = p[0] |
| 4368 | try: |
| 4369 | self.assertEqual(packet[IP].src, self.pg1.remote_ip4) |
| 4370 | self.assertEqual(packet[IP].dst, self.pg0.remote_ip4) |
| 4371 | self.assertTrue(packet.haslayer(GRE)) |
| 4372 | self.assert_packet_checksums_valid(packet) |
| 4373 | except: |
| 4374 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 4375 | raise |
| 4376 | |
| 4377 | def test_hairpinning_unknown_proto(self): |
| 4378 | """ NAT44 translate packet with unknown protocol - hairpinning """ |
| 4379 | host = self.pg0.remote_hosts[0] |
| 4380 | server = self.pg0.remote_hosts[1] |
| 4381 | host_in_port = 1234 |
| 4382 | server_out_port = 8765 |
| 4383 | server_nat_ip = "10.0.0.11" |
| 4384 | |
| 4385 | self.nat44_add_address(self.nat_addr) |
| 4386 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4387 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4388 | is_inside=0) |
| 4389 | |
| 4390 | # add static mapping for server |
| 4391 | self.nat44_add_static_mapping(server.ip4, server_nat_ip) |
| 4392 | |
| 4393 | # host to server |
| 4394 | p = (Ether(src=host.mac, dst=self.pg0.local_mac) / |
| 4395 | IP(src=host.ip4, dst=server_nat_ip) / |
| 4396 | TCP(sport=host_in_port, dport=server_out_port)) |
| 4397 | self.pg0.add_stream(p) |
| 4398 | self.pg_enable_capture(self.pg_interfaces) |
| 4399 | self.pg_start() |
| 4400 | self.pg0.get_capture(1) |
| 4401 | |
| 4402 | p = (Ether(dst=self.pg0.local_mac, src=host.mac) / |
| 4403 | IP(src=host.ip4, dst=server_nat_ip) / |
| 4404 | GRE() / |
| 4405 | IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) / |
| 4406 | TCP(sport=1234, dport=1234)) |
| 4407 | self.pg0.add_stream(p) |
| 4408 | self.pg_enable_capture(self.pg_interfaces) |
| 4409 | self.pg_start() |
| 4410 | p = self.pg0.get_capture(1) |
| 4411 | packet = p[0] |
| 4412 | try: |
| 4413 | self.assertEqual(packet[IP].src, self.nat_addr) |
| 4414 | self.assertEqual(packet[IP].dst, server.ip4) |
| 4415 | self.assertTrue(packet.haslayer(GRE)) |
| 4416 | self.assert_packet_checksums_valid(packet) |
| 4417 | except: |
| 4418 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 4419 | raise |
| 4420 | |
| 4421 | # server to host |
| 4422 | p = (Ether(dst=self.pg0.local_mac, src=server.mac) / |
| 4423 | IP(src=server.ip4, dst=self.nat_addr) / |
| 4424 | GRE() / |
| 4425 | IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) / |
| 4426 | TCP(sport=1234, dport=1234)) |
| 4427 | self.pg0.add_stream(p) |
| 4428 | self.pg_enable_capture(self.pg_interfaces) |
| 4429 | self.pg_start() |
| 4430 | p = self.pg0.get_capture(1) |
| 4431 | packet = p[0] |
| 4432 | try: |
| 4433 | self.assertEqual(packet[IP].src, server_nat_ip) |
| 4434 | self.assertEqual(packet[IP].dst, host.ip4) |
| 4435 | self.assertTrue(packet.haslayer(GRE)) |
| 4436 | self.assert_packet_checksums_valid(packet) |
| 4437 | except: |
| 4438 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 4439 | raise |
| 4440 | |
| 4441 | def test_output_feature_and_service(self): |
| 4442 | """ NAT44 interface output feature and services """ |
| 4443 | external_addr = '1.2.3.4' |
| 4444 | external_port = 80 |
| 4445 | local_port = 8080 |
| 4446 | |
| 4447 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4448 | self.nat44_add_address(self.nat_addr) |
| 4449 | self.vapi.nat44_add_del_identity_mapping(ip=self.pg1.remote_ip4n) |
| 4450 | self.nat44_add_static_mapping(self.pg0.remote_ip4, external_addr, |
| 4451 | local_port, external_port, |
| 4452 | proto=IP_PROTOS.tcp, out2in_only=1) |
| 4453 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4454 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 4455 | is_inside=0) |
| 4456 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 4457 | is_inside=0) |
| 4458 | |
| 4459 | # from client to service |
| 4460 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4461 | IP(src=self.pg1.remote_ip4, dst=external_addr) / |
| 4462 | TCP(sport=12345, dport=external_port)) |
| 4463 | self.pg1.add_stream(p) |
| 4464 | self.pg_enable_capture(self.pg_interfaces) |
| 4465 | self.pg_start() |
| 4466 | capture = self.pg0.get_capture(1) |
| 4467 | p = capture[0] |
| 4468 | try: |
| 4469 | ip = p[IP] |
| 4470 | tcp = p[TCP] |
| 4471 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 4472 | self.assertEqual(tcp.dport, local_port) |
| 4473 | self.assert_packet_checksums_valid(p) |
| 4474 | except: |
| 4475 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4476 | raise |
| 4477 | |
| 4478 | # from service back to client |
| 4479 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4480 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4481 | TCP(sport=local_port, dport=12345)) |
| 4482 | self.pg0.add_stream(p) |
| 4483 | self.pg_enable_capture(self.pg_interfaces) |
| 4484 | self.pg_start() |
| 4485 | capture = self.pg1.get_capture(1) |
| 4486 | p = capture[0] |
| 4487 | try: |
| 4488 | ip = p[IP] |
| 4489 | tcp = p[TCP] |
| 4490 | self.assertEqual(ip.src, external_addr) |
| 4491 | self.assertEqual(tcp.sport, external_port) |
| 4492 | self.assert_packet_checksums_valid(p) |
| 4493 | except: |
| 4494 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4495 | raise |
| 4496 | |
| 4497 | # from local network host to external network |
| 4498 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 4499 | self.pg0.add_stream(pkts) |
| 4500 | self.pg_enable_capture(self.pg_interfaces) |
| 4501 | self.pg_start() |
| 4502 | capture = self.pg1.get_capture(len(pkts)) |
| 4503 | self.verify_capture_out(capture) |
| 4504 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 4505 | self.pg0.add_stream(pkts) |
| 4506 | self.pg_enable_capture(self.pg_interfaces) |
| 4507 | self.pg_start() |
| 4508 | capture = self.pg1.get_capture(len(pkts)) |
| 4509 | self.verify_capture_out(capture) |
| 4510 | |
| 4511 | # from external network back to local network host |
| 4512 | pkts = self.create_stream_out(self.pg1) |
| 4513 | self.pg1.add_stream(pkts) |
| 4514 | self.pg_enable_capture(self.pg_interfaces) |
| 4515 | self.pg_start() |
| 4516 | capture = self.pg0.get_capture(len(pkts)) |
| 4517 | self.verify_capture_in(capture, self.pg0) |
| 4518 | |
| 4519 | def test_output_feature_and_service2(self): |
| 4520 | """ NAT44 interface output feature and service host direct access """ |
| 4521 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4522 | self.nat44_add_address(self.nat_addr) |
| 4523 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 4524 | is_inside=0) |
| 4525 | |
| 4526 | # session initiaded from service host - translate |
| 4527 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 4528 | self.pg0.add_stream(pkts) |
| 4529 | self.pg_enable_capture(self.pg_interfaces) |
| 4530 | self.pg_start() |
| 4531 | capture = self.pg1.get_capture(len(pkts)) |
| 4532 | self.verify_capture_out(capture) |
| 4533 | |
| 4534 | pkts = self.create_stream_out(self.pg1) |
| 4535 | self.pg1.add_stream(pkts) |
| 4536 | self.pg_enable_capture(self.pg_interfaces) |
| 4537 | self.pg_start() |
| 4538 | capture = self.pg0.get_capture(len(pkts)) |
| 4539 | self.verify_capture_in(capture, self.pg0) |
| 4540 | |
| 4541 | # session initiaded from remote host - do not translate |
| 4542 | self.tcp_port_in = 60303 |
| 4543 | self.udp_port_in = 60304 |
| 4544 | self.icmp_id_in = 60305 |
| 4545 | pkts = self.create_stream_out(self.pg1, |
| 4546 | self.pg0.remote_ip4, |
| 4547 | use_inside_ports=True) |
| 4548 | self.pg1.add_stream(pkts) |
| 4549 | self.pg_enable_capture(self.pg_interfaces) |
| 4550 | self.pg_start() |
| 4551 | capture = self.pg0.get_capture(len(pkts)) |
| 4552 | self.verify_capture_in(capture, self.pg0) |
| 4553 | |
| 4554 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 4555 | self.pg0.add_stream(pkts) |
| 4556 | self.pg_enable_capture(self.pg_interfaces) |
| 4557 | self.pg_start() |
| 4558 | capture = self.pg1.get_capture(len(pkts)) |
| 4559 | self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4, |
| 4560 | same_port=True) |
| 4561 | |
| 4562 | def test_output_feature_and_service3(self): |
| 4563 | """ NAT44 interface output feature and DST NAT """ |
| 4564 | external_addr = '1.2.3.4' |
| 4565 | external_port = 80 |
| 4566 | local_port = 8080 |
| 4567 | |
| 4568 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4569 | self.nat44_add_address(self.nat_addr) |
| 4570 | self.nat44_add_static_mapping(self.pg1.remote_ip4, external_addr, |
| 4571 | local_port, external_port, |
| 4572 | proto=IP_PROTOS.tcp, out2in_only=1) |
| 4573 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4574 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 4575 | is_inside=0) |
| 4576 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 4577 | is_inside=0) |
| 4578 | |
| 4579 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4580 | IP(src=self.pg0.remote_ip4, dst=external_addr) / |
| 4581 | TCP(sport=12345, dport=external_port)) |
| 4582 | self.pg0.add_stream(p) |
| 4583 | self.pg_enable_capture(self.pg_interfaces) |
| 4584 | self.pg_start() |
| 4585 | capture = self.pg1.get_capture(1) |
| 4586 | p = capture[0] |
| 4587 | try: |
| 4588 | ip = p[IP] |
| 4589 | tcp = p[TCP] |
| 4590 | self.assertEqual(ip.src, self.pg0.remote_ip4) |
| 4591 | self.assertEqual(tcp.sport, 12345) |
| 4592 | self.assertEqual(ip.dst, self.pg1.remote_ip4) |
| 4593 | self.assertEqual(tcp.dport, local_port) |
| 4594 | self.assert_packet_checksums_valid(p) |
| 4595 | except: |
| 4596 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4597 | raise |
| 4598 | |
| 4599 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4600 | IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) / |
| 4601 | TCP(sport=local_port, dport=12345)) |
| 4602 | self.pg1.add_stream(p) |
| 4603 | self.pg_enable_capture(self.pg_interfaces) |
| 4604 | self.pg_start() |
| 4605 | capture = self.pg0.get_capture(1) |
| 4606 | p = capture[0] |
| 4607 | try: |
| 4608 | ip = p[IP] |
| 4609 | tcp = p[TCP] |
| 4610 | self.assertEqual(ip.src, external_addr) |
| 4611 | self.assertEqual(tcp.sport, external_port) |
| 4612 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 4613 | self.assertEqual(tcp.dport, 12345) |
| 4614 | self.assert_packet_checksums_valid(p) |
| 4615 | except: |
| 4616 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4617 | raise |
| 4618 | |
Matus Fabian | 182e37e | 2018-08-14 04:21:26 -0700 | [diff] [blame] | 4619 | def test_next_src_nat(self): |
| 4620 | """ On way back forward packet to nat44-in2out node. """ |
| 4621 | twice_nat_addr = '10.0.1.3' |
| 4622 | external_port = 80 |
| 4623 | local_port = 8080 |
| 4624 | post_twice_nat_port = 0 |
| 4625 | |
| 4626 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4627 | self.nat44_add_address(twice_nat_addr, twice_nat=1) |
| 4628 | self.nat44_add_static_mapping(self.pg6.remote_ip4, self.pg1.remote_ip4, |
| 4629 | local_port, external_port, |
| 4630 | proto=IP_PROTOS.tcp, out2in_only=1, |
| 4631 | self_twice_nat=1, vrf_id=1) |
| 4632 | self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index, |
| 4633 | is_inside=0) |
| 4634 | |
| 4635 | p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) / |
| 4636 | IP(src=self.pg6.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4637 | TCP(sport=12345, dport=external_port)) |
| 4638 | self.pg6.add_stream(p) |
| 4639 | self.pg_enable_capture(self.pg_interfaces) |
| 4640 | self.pg_start() |
| 4641 | capture = self.pg6.get_capture(1) |
| 4642 | p = capture[0] |
| 4643 | try: |
| 4644 | ip = p[IP] |
| 4645 | tcp = p[TCP] |
| 4646 | self.assertEqual(ip.src, twice_nat_addr) |
| 4647 | self.assertNotEqual(tcp.sport, 12345) |
| 4648 | post_twice_nat_port = tcp.sport |
| 4649 | self.assertEqual(ip.dst, self.pg6.remote_ip4) |
| 4650 | self.assertEqual(tcp.dport, local_port) |
| 4651 | self.assert_packet_checksums_valid(p) |
| 4652 | except: |
| 4653 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4654 | raise |
| 4655 | |
| 4656 | p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) / |
| 4657 | IP(src=self.pg6.remote_ip4, dst=twice_nat_addr) / |
| 4658 | TCP(sport=local_port, dport=post_twice_nat_port)) |
| 4659 | self.pg6.add_stream(p) |
| 4660 | self.pg_enable_capture(self.pg_interfaces) |
| 4661 | self.pg_start() |
| 4662 | capture = self.pg6.get_capture(1) |
| 4663 | p = capture[0] |
| 4664 | try: |
| 4665 | ip = p[IP] |
| 4666 | tcp = p[TCP] |
| 4667 | self.assertEqual(ip.src, self.pg1.remote_ip4) |
| 4668 | self.assertEqual(tcp.sport, external_port) |
| 4669 | self.assertEqual(ip.dst, self.pg6.remote_ip4) |
| 4670 | self.assertEqual(tcp.dport, 12345) |
| 4671 | self.assert_packet_checksums_valid(p) |
| 4672 | except: |
| 4673 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4674 | raise |
| 4675 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4676 | def twice_nat_common(self, self_twice_nat=False, same_pg=False, lb=False, |
| 4677 | client_id=None): |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4678 | twice_nat_addr = '10.0.1.3' |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4679 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4680 | port_in = 8080 |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4681 | if lb: |
| 4682 | if not same_pg: |
| 4683 | port_in1 = port_in |
| 4684 | port_in2 = port_in |
| 4685 | else: |
| 4686 | port_in1 = port_in+1 |
| 4687 | port_in2 = port_in+2 |
| 4688 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4689 | port_out = 80 |
| 4690 | eh_port_out = 4567 |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4691 | |
| 4692 | server1 = self.pg0.remote_hosts[0] |
| 4693 | server2 = self.pg0.remote_hosts[1] |
| 4694 | if lb and same_pg: |
| 4695 | server2 = server1 |
| 4696 | if not lb: |
| 4697 | server = server1 |
| 4698 | |
| 4699 | pg0 = self.pg0 |
| 4700 | if same_pg: |
| 4701 | pg1 = self.pg0 |
| 4702 | else: |
| 4703 | pg1 = self.pg1 |
| 4704 | |
| 4705 | eh_translate = ((not self_twice_nat) or (not lb and same_pg) or |
| 4706 | client_id == 1) |
| 4707 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4708 | self.nat44_add_address(self.nat_addr) |
| 4709 | self.nat44_add_address(twice_nat_addr, twice_nat=1) |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4710 | if not lb: |
| 4711 | self.nat44_add_static_mapping(pg0.remote_ip4, self.nat_addr, |
| 4712 | port_in, port_out, |
| 4713 | proto=IP_PROTOS.tcp, |
| 4714 | twice_nat=int(not self_twice_nat), |
| 4715 | self_twice_nat=int(self_twice_nat)) |
| 4716 | else: |
| 4717 | locals = [{'addr': server1.ip4n, |
| 4718 | 'port': port_in1, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4719 | 'probability': 50, |
| 4720 | 'vrf_id': 0}, |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4721 | {'addr': server2.ip4n, |
| 4722 | 'port': port_in2, |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 4723 | 'probability': 50, |
| 4724 | 'vrf_id': 0}] |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4725 | out_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr) |
| 4726 | self.vapi.nat44_add_del_lb_static_mapping(out_addr_n, |
| 4727 | port_out, |
| 4728 | IP_PROTOS.tcp, |
| 4729 | twice_nat=int( |
| 4730 | not self_twice_nat), |
| 4731 | self_twice_nat=int( |
| 4732 | self_twice_nat), |
| 4733 | local_num=len(locals), |
| 4734 | locals=locals) |
| 4735 | self.vapi.nat44_interface_add_del_feature(pg0.sw_if_index) |
| 4736 | self.vapi.nat44_interface_add_del_feature(pg1.sw_if_index, |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4737 | is_inside=0) |
| 4738 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4739 | if same_pg: |
| 4740 | if not lb: |
| 4741 | client = server |
| 4742 | else: |
| 4743 | assert client_id is not None |
| 4744 | if client_id == 1: |
| 4745 | client = self.pg0.remote_hosts[0] |
| 4746 | elif client_id == 2: |
| 4747 | client = self.pg0.remote_hosts[1] |
| 4748 | else: |
| 4749 | client = pg1.remote_hosts[0] |
| 4750 | p = (Ether(src=pg1.remote_mac, dst=pg1.local_mac) / |
| 4751 | IP(src=client.ip4, dst=self.nat_addr) / |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4752 | TCP(sport=eh_port_out, dport=port_out)) |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4753 | pg1.add_stream(p) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4754 | self.pg_enable_capture(self.pg_interfaces) |
| 4755 | self.pg_start() |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4756 | capture = pg0.get_capture(1) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4757 | p = capture[0] |
| 4758 | try: |
| 4759 | ip = p[IP] |
| 4760 | tcp = p[TCP] |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4761 | if lb: |
| 4762 | if ip.dst == server1.ip4: |
| 4763 | server = server1 |
| 4764 | port_in = port_in1 |
| 4765 | else: |
| 4766 | server = server2 |
| 4767 | port_in = port_in2 |
| 4768 | self.assertEqual(ip.dst, server.ip4) |
| 4769 | if lb and same_pg: |
| 4770 | self.assertIn(tcp.dport, [port_in1, port_in2]) |
| 4771 | else: |
| 4772 | self.assertEqual(tcp.dport, port_in) |
| 4773 | if eh_translate: |
| 4774 | self.assertEqual(ip.src, twice_nat_addr) |
| 4775 | self.assertNotEqual(tcp.sport, eh_port_out) |
| 4776 | else: |
| 4777 | self.assertEqual(ip.src, client.ip4) |
| 4778 | self.assertEqual(tcp.sport, eh_port_out) |
| 4779 | eh_addr_in = ip.src |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4780 | eh_port_in = tcp.sport |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4781 | saved_port_in = tcp.dport |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 4782 | self.assert_packet_checksums_valid(p) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4783 | except: |
| 4784 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4785 | raise |
| 4786 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4787 | p = (Ether(src=server.mac, dst=pg0.local_mac) / |
| 4788 | IP(src=server.ip4, dst=eh_addr_in) / |
| 4789 | TCP(sport=saved_port_in, dport=eh_port_in)) |
| 4790 | pg0.add_stream(p) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4791 | self.pg_enable_capture(self.pg_interfaces) |
| 4792 | self.pg_start() |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4793 | capture = pg1.get_capture(1) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4794 | p = capture[0] |
| 4795 | try: |
| 4796 | ip = p[IP] |
| 4797 | tcp = p[TCP] |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4798 | self.assertEqual(ip.dst, client.ip4) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4799 | self.assertEqual(ip.src, self.nat_addr) |
| 4800 | self.assertEqual(tcp.dport, eh_port_out) |
| 4801 | self.assertEqual(tcp.sport, port_out) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 4802 | self.assert_packet_checksums_valid(p) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4803 | except: |
| 4804 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 4805 | raise |
| 4806 | |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 4807 | if eh_translate: |
| 4808 | sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0) |
| 4809 | self.assertEqual(len(sessions), 1) |
| 4810 | self.assertTrue(sessions[0].ext_host_valid) |
| 4811 | self.assertTrue(sessions[0].is_twicenat) |
| 4812 | self.vapi.nat44_del_session( |
| 4813 | sessions[0].inside_ip_address, |
| 4814 | sessions[0].inside_port, |
| 4815 | sessions[0].protocol, |
| 4816 | ext_host_address=sessions[0].ext_host_nat_address, |
| 4817 | ext_host_port=sessions[0].ext_host_nat_port) |
| 4818 | sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0) |
| 4819 | self.assertEqual(len(sessions), 0) |
| 4820 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4821 | def test_twice_nat(self): |
| 4822 | """ Twice NAT44 """ |
| 4823 | self.twice_nat_common() |
| 4824 | |
| 4825 | def test_self_twice_nat_positive(self): |
| 4826 | """ Self Twice NAT44 (positive test) """ |
| 4827 | self.twice_nat_common(self_twice_nat=True, same_pg=True) |
| 4828 | |
| 4829 | def test_self_twice_nat_negative(self): |
| 4830 | """ Self Twice NAT44 (negative test) """ |
| 4831 | self.twice_nat_common(self_twice_nat=True) |
| 4832 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4833 | def test_twice_nat_lb(self): |
| 4834 | """ Twice NAT44 local service load balancing """ |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4835 | self.twice_nat_common(lb=True) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4836 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4837 | def test_self_twice_nat_lb_positive(self): |
| 4838 | """ Self Twice NAT44 local service load balancing (positive test) """ |
| 4839 | self.twice_nat_common(lb=True, self_twice_nat=True, same_pg=True, |
| 4840 | client_id=1) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4841 | |
Juraj Sloboda | 1e5c07d | 2018-04-10 13:51:54 +0200 | [diff] [blame] | 4842 | def test_self_twice_nat_lb_negative(self): |
| 4843 | """ Self Twice NAT44 local service load balancing (negative test) """ |
| 4844 | self.twice_nat_common(lb=True, self_twice_nat=True, same_pg=True, |
| 4845 | client_id=2) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4846 | |
| 4847 | def test_twice_nat_interface_addr(self): |
| 4848 | """ Acquire twice NAT44 addresses from interface """ |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4849 | 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] | 4850 | |
| 4851 | # no address in NAT pool |
| 4852 | adresses = self.vapi.nat44_address_dump() |
| 4853 | self.assertEqual(0, len(adresses)) |
| 4854 | |
| 4855 | # configure interface address and check NAT address pool |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4856 | self.pg3.config_ip4() |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4857 | adresses = self.vapi.nat44_address_dump() |
| 4858 | self.assertEqual(1, len(adresses)) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4859 | self.assertEqual(adresses[0].ip_address[0:4], self.pg3.local_ip4n) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4860 | self.assertEqual(adresses[0].twice_nat, 1) |
| 4861 | |
| 4862 | # remove interface address and check NAT address pool |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 4863 | self.pg3.unconfig_ip4() |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 4864 | adresses = self.vapi.nat44_address_dump() |
| 4865 | self.assertEqual(0, len(adresses)) |
| 4866 | |
Matus Fabian | 6c01dce | 2018-11-16 04:41:31 -0800 | [diff] [blame] | 4867 | def test_tcp_close(self): |
| 4868 | """ Close TCP session from inside network - output feature """ |
| 4869 | self.vapi.nat44_forwarding_enable_disable(1) |
| 4870 | self.nat44_add_address(self.pg1.local_ip4) |
| 4871 | twice_nat_addr = '10.0.1.3' |
| 4872 | service_ip = '192.168.16.150' |
| 4873 | self.nat44_add_address(twice_nat_addr, twice_nat=1) |
| 4874 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4875 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 4876 | is_inside=0) |
| 4877 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 4878 | is_inside=0) |
| 4879 | self.nat44_add_static_mapping(self.pg0.remote_ip4, |
| 4880 | service_ip, |
| 4881 | 80, |
| 4882 | 80, |
| 4883 | proto=IP_PROTOS.tcp, |
| 4884 | out2in_only=1, |
| 4885 | twice_nat=1) |
| 4886 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 4887 | start_sessnum = len(sessions) |
| 4888 | |
| 4889 | # SYN packet out->in |
| 4890 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4891 | IP(src=self.pg1.remote_ip4, dst=service_ip) / |
| 4892 | TCP(sport=33898, dport=80, flags="S")) |
| 4893 | self.pg1.add_stream(p) |
| 4894 | self.pg_enable_capture(self.pg_interfaces) |
| 4895 | self.pg_start() |
| 4896 | capture = self.pg0.get_capture(1) |
| 4897 | p = capture[0] |
| 4898 | tcp_port = p[TCP].sport |
| 4899 | |
| 4900 | # SYN + ACK packet in->out |
| 4901 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4902 | IP(src=self.pg0.remote_ip4, dst=twice_nat_addr) / |
| 4903 | TCP(sport=80, dport=tcp_port, flags="SA")) |
| 4904 | self.pg0.add_stream(p) |
| 4905 | self.pg_enable_capture(self.pg_interfaces) |
| 4906 | self.pg_start() |
| 4907 | self.pg1.get_capture(1) |
| 4908 | |
| 4909 | # ACK packet out->in |
| 4910 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4911 | IP(src=self.pg1.remote_ip4, dst=service_ip) / |
| 4912 | TCP(sport=33898, dport=80, flags="A")) |
| 4913 | self.pg1.add_stream(p) |
| 4914 | self.pg_enable_capture(self.pg_interfaces) |
| 4915 | self.pg_start() |
| 4916 | self.pg0.get_capture(1) |
| 4917 | |
| 4918 | # FIN packet in -> out |
| 4919 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4920 | IP(src=self.pg0.remote_ip4, dst=twice_nat_addr) / |
| 4921 | TCP(sport=80, dport=tcp_port, flags="FA", seq=100, ack=300)) |
| 4922 | self.pg0.add_stream(p) |
| 4923 | self.pg_enable_capture(self.pg_interfaces) |
| 4924 | self.pg_start() |
| 4925 | self.pg1.get_capture(1) |
| 4926 | |
| 4927 | # FIN+ACK packet out -> in |
| 4928 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4929 | IP(src=self.pg1.remote_ip4, dst=service_ip) / |
| 4930 | TCP(sport=33898, dport=80, flags="FA", seq=300, ack=101)) |
| 4931 | self.pg1.add_stream(p) |
| 4932 | self.pg_enable_capture(self.pg_interfaces) |
| 4933 | self.pg_start() |
| 4934 | self.pg0.get_capture(1) |
| 4935 | |
| 4936 | # ACK packet in -> out |
| 4937 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4938 | IP(src=self.pg0.remote_ip4, dst=twice_nat_addr) / |
| 4939 | TCP(sport=80, dport=tcp_port, flags="A", seq=101, ack=301)) |
| 4940 | self.pg0.add_stream(p) |
| 4941 | self.pg_enable_capture(self.pg_interfaces) |
| 4942 | self.pg_start() |
| 4943 | self.pg1.get_capture(1) |
| 4944 | |
| 4945 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, |
| 4946 | 0) |
| 4947 | self.assertEqual(len(sessions) - start_sessnum, 0) |
| 4948 | |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4949 | def test_tcp_session_close_in(self): |
| 4950 | """ Close TCP session from inside network """ |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4951 | self.tcp_port_out = 10505 |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4952 | self.nat44_add_address(self.nat_addr) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4953 | self.nat44_add_static_mapping(self.pg0.remote_ip4, |
| 4954 | self.nat_addr, |
| 4955 | self.tcp_port_in, |
| 4956 | self.tcp_port_out, |
| 4957 | proto=IP_PROTOS.tcp, |
| 4958 | twice_nat=1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4959 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 4960 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 4961 | is_inside=0) |
| 4962 | |
| 4963 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 4964 | start_sessnum = len(sessions) |
| 4965 | |
| 4966 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 4967 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4968 | # FIN packet in -> out |
| 4969 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 4970 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 4971 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 4972 | flags="FA", seq=100, ack=300)) |
| 4973 | self.pg0.add_stream(p) |
| 4974 | self.pg_enable_capture(self.pg_interfaces) |
| 4975 | self.pg_start() |
| 4976 | self.pg1.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4977 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4978 | pkts = [] |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4979 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4980 | # ACK packet out -> in |
| 4981 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4982 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4983 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 4984 | flags="A", seq=300, ack=101)) |
| 4985 | pkts.append(p) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4986 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4987 | # FIN packet out -> in |
| 4988 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 4989 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 4990 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 4991 | flags="FA", seq=300, ack=101)) |
| 4992 | pkts.append(p) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4993 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4994 | self.pg1.add_stream(pkts) |
| 4995 | self.pg_enable_capture(self.pg_interfaces) |
| 4996 | self.pg_start() |
| 4997 | self.pg0.get_capture(2) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 4998 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 4999 | # ACK packet in -> out |
| 5000 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5001 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5002 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 5003 | flags="A", seq=101, ack=301)) |
| 5004 | self.pg0.add_stream(p) |
| 5005 | self.pg_enable_capture(self.pg_interfaces) |
| 5006 | self.pg_start() |
| 5007 | self.pg1.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5008 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5009 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, |
| 5010 | 0) |
| 5011 | self.assertEqual(len(sessions) - start_sessnum, 0) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5012 | |
| 5013 | def test_tcp_session_close_out(self): |
| 5014 | """ Close TCP session from outside network """ |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5015 | self.tcp_port_out = 10505 |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5016 | self.nat44_add_address(self.nat_addr) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5017 | self.nat44_add_static_mapping(self.pg0.remote_ip4, |
| 5018 | self.nat_addr, |
| 5019 | self.tcp_port_in, |
| 5020 | self.tcp_port_out, |
| 5021 | proto=IP_PROTOS.tcp, |
| 5022 | twice_nat=1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5023 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5024 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 5025 | is_inside=0) |
| 5026 | |
| 5027 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 5028 | start_sessnum = len(sessions) |
| 5029 | |
| 5030 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 5031 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5032 | # FIN packet out -> in |
| 5033 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5034 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5035 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 5036 | flags="FA", seq=100, ack=300)) |
| 5037 | self.pg1.add_stream(p) |
| 5038 | self.pg_enable_capture(self.pg_interfaces) |
| 5039 | self.pg_start() |
| 5040 | self.pg0.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5041 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5042 | # FIN+ACK packet in -> out |
| 5043 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5044 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5045 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 5046 | flags="FA", seq=300, ack=101)) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5047 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5048 | self.pg0.add_stream(p) |
| 5049 | self.pg_enable_capture(self.pg_interfaces) |
| 5050 | self.pg_start() |
| 5051 | self.pg1.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5052 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5053 | # ACK packet out -> in |
| 5054 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5055 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5056 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 5057 | flags="A", seq=101, ack=301)) |
| 5058 | self.pg1.add_stream(p) |
| 5059 | self.pg_enable_capture(self.pg_interfaces) |
| 5060 | self.pg_start() |
| 5061 | self.pg0.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5062 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5063 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, |
| 5064 | 0) |
| 5065 | self.assertEqual(len(sessions) - start_sessnum, 0) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5066 | |
| 5067 | def test_tcp_session_close_simultaneous(self): |
| 5068 | """ Close TCP session from inside network """ |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5069 | self.tcp_port_out = 10505 |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5070 | self.nat44_add_address(self.nat_addr) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5071 | self.nat44_add_static_mapping(self.pg0.remote_ip4, |
| 5072 | self.nat_addr, |
| 5073 | self.tcp_port_in, |
| 5074 | self.tcp_port_out, |
| 5075 | proto=IP_PROTOS.tcp, |
| 5076 | twice_nat=1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5077 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5078 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 5079 | is_inside=0) |
| 5080 | |
| 5081 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0) |
| 5082 | start_sessnum = len(sessions) |
| 5083 | |
| 5084 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 5085 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5086 | # FIN packet in -> out |
| 5087 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5088 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5089 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 5090 | flags="FA", seq=100, ack=300)) |
| 5091 | self.pg0.add_stream(p) |
| 5092 | self.pg_enable_capture(self.pg_interfaces) |
| 5093 | self.pg_start() |
| 5094 | self.pg1.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5095 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5096 | # FIN packet out -> in |
| 5097 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5098 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5099 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 5100 | flags="FA", seq=300, ack=100)) |
| 5101 | self.pg1.add_stream(p) |
| 5102 | self.pg_enable_capture(self.pg_interfaces) |
| 5103 | self.pg_start() |
| 5104 | self.pg0.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5105 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5106 | # ACK packet in -> out |
| 5107 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5108 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5109 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 5110 | flags="A", seq=101, ack=301)) |
| 5111 | self.pg0.add_stream(p) |
| 5112 | self.pg_enable_capture(self.pg_interfaces) |
| 5113 | self.pg_start() |
| 5114 | self.pg1.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5115 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5116 | # ACK packet out -> in |
| 5117 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5118 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5119 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
| 5120 | flags="A", seq=301, ack=101)) |
| 5121 | self.pg1.add_stream(p) |
| 5122 | self.pg_enable_capture(self.pg_interfaces) |
| 5123 | self.pg_start() |
| 5124 | self.pg0.get_capture(1) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5125 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5126 | sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, |
| 5127 | 0) |
| 5128 | self.assertEqual(len(sessions) - start_sessnum, 0) |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 5129 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 5130 | def test_one_armed_nat44_static(self): |
| 5131 | """ One armed NAT44 and 1:1 NAPT asymmetrical rule """ |
| 5132 | remote_host = self.pg4.remote_hosts[0] |
| 5133 | local_host = self.pg4.remote_hosts[1] |
| 5134 | external_port = 80 |
| 5135 | local_port = 8080 |
| 5136 | eh_port_in = 0 |
| 5137 | |
| 5138 | self.vapi.nat44_forwarding_enable_disable(1) |
| 5139 | self.nat44_add_address(self.nat_addr, twice_nat=1) |
| 5140 | self.nat44_add_static_mapping(local_host.ip4, self.nat_addr, |
| 5141 | local_port, external_port, |
| 5142 | proto=IP_PROTOS.tcp, out2in_only=1, |
| 5143 | twice_nat=1) |
| 5144 | self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index) |
| 5145 | self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index, |
| 5146 | is_inside=0) |
| 5147 | |
| 5148 | # from client to service |
| 5149 | p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) / |
| 5150 | IP(src=remote_host.ip4, dst=self.nat_addr) / |
| 5151 | TCP(sport=12345, dport=external_port)) |
| 5152 | self.pg4.add_stream(p) |
| 5153 | self.pg_enable_capture(self.pg_interfaces) |
| 5154 | self.pg_start() |
| 5155 | capture = self.pg4.get_capture(1) |
| 5156 | p = capture[0] |
| 5157 | try: |
| 5158 | ip = p[IP] |
| 5159 | tcp = p[TCP] |
| 5160 | self.assertEqual(ip.dst, local_host.ip4) |
| 5161 | self.assertEqual(ip.src, self.nat_addr) |
| 5162 | self.assertEqual(tcp.dport, local_port) |
| 5163 | self.assertNotEqual(tcp.sport, 12345) |
| 5164 | eh_port_in = tcp.sport |
| 5165 | self.assert_packet_checksums_valid(p) |
| 5166 | except: |
| 5167 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5168 | raise |
| 5169 | |
| 5170 | # from service back to client |
| 5171 | p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) / |
| 5172 | IP(src=local_host.ip4, dst=self.nat_addr) / |
| 5173 | TCP(sport=local_port, dport=eh_port_in)) |
| 5174 | self.pg4.add_stream(p) |
| 5175 | self.pg_enable_capture(self.pg_interfaces) |
| 5176 | self.pg_start() |
| 5177 | capture = self.pg4.get_capture(1) |
| 5178 | p = capture[0] |
| 5179 | try: |
| 5180 | ip = p[IP] |
| 5181 | tcp = p[TCP] |
| 5182 | self.assertEqual(ip.src, self.nat_addr) |
| 5183 | self.assertEqual(ip.dst, remote_host.ip4) |
| 5184 | self.assertEqual(tcp.sport, external_port) |
| 5185 | self.assertEqual(tcp.dport, 12345) |
| 5186 | self.assert_packet_checksums_valid(p) |
| 5187 | except: |
| 5188 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5189 | raise |
| 5190 | |
| 5191 | def test_static_with_port_out2(self): |
| 5192 | """ 1:1 NAPT asymmetrical rule """ |
| 5193 | |
| 5194 | external_port = 80 |
| 5195 | local_port = 8080 |
| 5196 | |
| 5197 | self.vapi.nat44_forwarding_enable_disable(1) |
| 5198 | self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr, |
| 5199 | local_port, external_port, |
| 5200 | proto=IP_PROTOS.tcp, out2in_only=1) |
| 5201 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5202 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 5203 | is_inside=0) |
| 5204 | |
| 5205 | # from client to service |
| 5206 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5207 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5208 | TCP(sport=12345, dport=external_port)) |
| 5209 | self.pg1.add_stream(p) |
| 5210 | self.pg_enable_capture(self.pg_interfaces) |
| 5211 | self.pg_start() |
| 5212 | capture = self.pg0.get_capture(1) |
| 5213 | p = capture[0] |
| 5214 | try: |
| 5215 | ip = p[IP] |
| 5216 | tcp = p[TCP] |
| 5217 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 5218 | self.assertEqual(tcp.dport, local_port) |
| 5219 | self.assert_packet_checksums_valid(p) |
| 5220 | except: |
| 5221 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5222 | raise |
| 5223 | |
| 5224 | # ICMP error |
| 5225 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 5226 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5227 | ICMP(type=11) / capture[0][IP]) |
| 5228 | self.pg0.add_stream(p) |
| 5229 | self.pg_enable_capture(self.pg_interfaces) |
| 5230 | self.pg_start() |
| 5231 | capture = self.pg1.get_capture(1) |
| 5232 | p = capture[0] |
| 5233 | try: |
| 5234 | self.assertEqual(p[IP].src, self.nat_addr) |
| 5235 | inner = p[IPerror] |
| 5236 | self.assertEqual(inner.dst, self.nat_addr) |
| 5237 | self.assertEqual(inner[TCPerror].dport, external_port) |
| 5238 | except: |
| 5239 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5240 | raise |
| 5241 | |
| 5242 | # from service back to client |
| 5243 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5244 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5245 | TCP(sport=local_port, dport=12345)) |
| 5246 | self.pg0.add_stream(p) |
| 5247 | self.pg_enable_capture(self.pg_interfaces) |
| 5248 | self.pg_start() |
| 5249 | capture = self.pg1.get_capture(1) |
| 5250 | p = capture[0] |
| 5251 | try: |
| 5252 | ip = p[IP] |
| 5253 | tcp = p[TCP] |
| 5254 | self.assertEqual(ip.src, self.nat_addr) |
| 5255 | self.assertEqual(tcp.sport, external_port) |
| 5256 | self.assert_packet_checksums_valid(p) |
| 5257 | except: |
| 5258 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5259 | raise |
| 5260 | |
| 5261 | # ICMP error |
| 5262 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 5263 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5264 | ICMP(type=11) / capture[0][IP]) |
| 5265 | self.pg1.add_stream(p) |
| 5266 | self.pg_enable_capture(self.pg_interfaces) |
| 5267 | self.pg_start() |
| 5268 | capture = self.pg0.get_capture(1) |
| 5269 | p = capture[0] |
| 5270 | try: |
| 5271 | self.assertEqual(p[IP].dst, self.pg0.remote_ip4) |
| 5272 | inner = p[IPerror] |
| 5273 | self.assertEqual(inner.src, self.pg0.remote_ip4) |
| 5274 | self.assertEqual(inner[TCPerror].sport, local_port) |
| 5275 | except: |
| 5276 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5277 | raise |
| 5278 | |
| 5279 | # from client to server (no translation) |
| 5280 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5281 | IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) / |
| 5282 | TCP(sport=12346, dport=local_port)) |
| 5283 | self.pg1.add_stream(p) |
| 5284 | self.pg_enable_capture(self.pg_interfaces) |
| 5285 | self.pg_start() |
| 5286 | capture = self.pg0.get_capture(1) |
| 5287 | p = capture[0] |
| 5288 | try: |
| 5289 | ip = p[IP] |
| 5290 | tcp = p[TCP] |
| 5291 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 5292 | self.assertEqual(tcp.dport, local_port) |
| 5293 | self.assert_packet_checksums_valid(p) |
| 5294 | except: |
| 5295 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5296 | raise |
| 5297 | |
| 5298 | # from service back to client (no translation) |
| 5299 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5300 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5301 | TCP(sport=local_port, dport=12346)) |
| 5302 | self.pg0.add_stream(p) |
| 5303 | self.pg_enable_capture(self.pg_interfaces) |
| 5304 | self.pg_start() |
| 5305 | capture = self.pg1.get_capture(1) |
| 5306 | p = capture[0] |
| 5307 | try: |
| 5308 | ip = p[IP] |
| 5309 | tcp = p[TCP] |
| 5310 | self.assertEqual(ip.src, self.pg0.remote_ip4) |
| 5311 | self.assertEqual(tcp.sport, local_port) |
| 5312 | self.assert_packet_checksums_valid(p) |
| 5313 | except: |
| 5314 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5315 | raise |
| 5316 | |
Matus Fabian | 235a47e | 2018-06-25 16:42:36 -0700 | [diff] [blame] | 5317 | def test_output_feature(self): |
| 5318 | """ NAT44 interface output feature (in2out postrouting) """ |
| 5319 | self.vapi.nat44_forwarding_enable_disable(1) |
| 5320 | self.nat44_add_address(self.nat_addr) |
| 5321 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 5322 | is_inside=0) |
| 5323 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 5324 | is_inside=0) |
| 5325 | |
| 5326 | # in2out |
| 5327 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 5328 | self.pg0.add_stream(pkts) |
| 5329 | self.pg_enable_capture(self.pg_interfaces) |
| 5330 | self.pg_start() |
| 5331 | capture = self.pg1.get_capture(len(pkts)) |
| 5332 | self.verify_capture_out(capture) |
| 5333 | |
| 5334 | # out2in |
| 5335 | pkts = self.create_stream_out(self.pg1) |
| 5336 | self.pg1.add_stream(pkts) |
| 5337 | self.pg_enable_capture(self.pg_interfaces) |
| 5338 | self.pg_start() |
| 5339 | capture = self.pg0.get_capture(len(pkts)) |
| 5340 | self.verify_capture_in(capture, self.pg0) |
| 5341 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 5342 | def test_multiple_vrf(self): |
| 5343 | """ Multiple VRF setup """ |
| 5344 | external_addr = '1.2.3.4' |
| 5345 | external_port = 80 |
| 5346 | local_port = 8080 |
| 5347 | port = 0 |
| 5348 | |
| 5349 | self.vapi.nat44_forwarding_enable_disable(1) |
| 5350 | self.nat44_add_address(self.nat_addr) |
| 5351 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5352 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 5353 | is_inside=0) |
| 5354 | self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index, |
| 5355 | is_inside=0) |
| 5356 | self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index) |
| 5357 | self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index, |
| 5358 | is_inside=0) |
| 5359 | self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index, |
| 5360 | is_inside=0) |
| 5361 | self.nat44_add_static_mapping(self.pg5.remote_ip4, external_addr, |
| 5362 | local_port, external_port, vrf_id=1, |
| 5363 | proto=IP_PROTOS.tcp, out2in_only=1) |
| 5364 | self.nat44_add_static_mapping( |
| 5365 | self.pg0.remote_ip4, external_sw_if_index=self.pg0.sw_if_index, |
| 5366 | local_port=local_port, vrf_id=0, external_port=external_port, |
| 5367 | proto=IP_PROTOS.tcp, out2in_only=1) |
| 5368 | |
| 5369 | # from client to service (both VRF1) |
| 5370 | p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) / |
| 5371 | IP(src=self.pg6.remote_ip4, dst=external_addr) / |
| 5372 | TCP(sport=12345, dport=external_port)) |
| 5373 | self.pg6.add_stream(p) |
| 5374 | self.pg_enable_capture(self.pg_interfaces) |
| 5375 | self.pg_start() |
| 5376 | capture = self.pg5.get_capture(1) |
| 5377 | p = capture[0] |
| 5378 | try: |
| 5379 | ip = p[IP] |
| 5380 | tcp = p[TCP] |
| 5381 | self.assertEqual(ip.dst, self.pg5.remote_ip4) |
| 5382 | self.assertEqual(tcp.dport, local_port) |
| 5383 | self.assert_packet_checksums_valid(p) |
| 5384 | except: |
| 5385 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5386 | raise |
| 5387 | |
| 5388 | # from service back to client (both VRF1) |
| 5389 | p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) / |
| 5390 | IP(src=self.pg5.remote_ip4, dst=self.pg6.remote_ip4) / |
| 5391 | TCP(sport=local_port, dport=12345)) |
| 5392 | self.pg5.add_stream(p) |
| 5393 | self.pg_enable_capture(self.pg_interfaces) |
| 5394 | self.pg_start() |
| 5395 | capture = self.pg6.get_capture(1) |
| 5396 | p = capture[0] |
| 5397 | try: |
| 5398 | ip = p[IP] |
| 5399 | tcp = p[TCP] |
| 5400 | self.assertEqual(ip.src, external_addr) |
| 5401 | self.assertEqual(tcp.sport, external_port) |
| 5402 | self.assert_packet_checksums_valid(p) |
| 5403 | except: |
| 5404 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5405 | raise |
| 5406 | |
| 5407 | # dynamic NAT from VRF1 to VRF0 (output-feature) |
| 5408 | p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) / |
| 5409 | IP(src=self.pg5.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5410 | TCP(sport=2345, dport=22)) |
| 5411 | self.pg5.add_stream(p) |
| 5412 | self.pg_enable_capture(self.pg_interfaces) |
| 5413 | self.pg_start() |
| 5414 | capture = self.pg1.get_capture(1) |
| 5415 | p = capture[0] |
| 5416 | try: |
| 5417 | ip = p[IP] |
| 5418 | tcp = p[TCP] |
| 5419 | self.assertEqual(ip.src, self.nat_addr) |
| 5420 | self.assertNotEqual(tcp.sport, 2345) |
| 5421 | self.assert_packet_checksums_valid(p) |
| 5422 | port = tcp.sport |
| 5423 | except: |
| 5424 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5425 | raise |
| 5426 | |
| 5427 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 5428 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 5429 | TCP(sport=22, dport=port)) |
| 5430 | self.pg1.add_stream(p) |
| 5431 | self.pg_enable_capture(self.pg_interfaces) |
| 5432 | self.pg_start() |
| 5433 | capture = self.pg5.get_capture(1) |
| 5434 | p = capture[0] |
| 5435 | try: |
| 5436 | ip = p[IP] |
| 5437 | tcp = p[TCP] |
| 5438 | self.assertEqual(ip.dst, self.pg5.remote_ip4) |
| 5439 | self.assertEqual(tcp.dport, 2345) |
| 5440 | self.assert_packet_checksums_valid(p) |
| 5441 | except: |
| 5442 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5443 | raise |
| 5444 | |
| 5445 | # from client VRF1 to service VRF0 |
| 5446 | p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) / |
| 5447 | IP(src=self.pg6.remote_ip4, dst=self.pg0.local_ip4) / |
| 5448 | TCP(sport=12346, dport=external_port)) |
| 5449 | self.pg6.add_stream(p) |
| 5450 | self.pg_enable_capture(self.pg_interfaces) |
| 5451 | self.pg_start() |
| 5452 | capture = self.pg0.get_capture(1) |
| 5453 | p = capture[0] |
| 5454 | try: |
| 5455 | ip = p[IP] |
| 5456 | tcp = p[TCP] |
| 5457 | self.assertEqual(ip.dst, self.pg0.remote_ip4) |
| 5458 | self.assertEqual(tcp.dport, local_port) |
| 5459 | self.assert_packet_checksums_valid(p) |
| 5460 | except: |
| 5461 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5462 | raise |
| 5463 | |
| 5464 | # from service VRF0 back to client VRF1 |
| 5465 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5466 | IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) / |
| 5467 | TCP(sport=local_port, dport=12346)) |
| 5468 | self.pg0.add_stream(p) |
| 5469 | self.pg_enable_capture(self.pg_interfaces) |
| 5470 | self.pg_start() |
| 5471 | capture = self.pg6.get_capture(1) |
| 5472 | p = capture[0] |
| 5473 | try: |
| 5474 | ip = p[IP] |
| 5475 | tcp = p[TCP] |
| 5476 | self.assertEqual(ip.src, self.pg0.local_ip4) |
| 5477 | self.assertEqual(tcp.sport, external_port) |
| 5478 | self.assert_packet_checksums_valid(p) |
| 5479 | except: |
| 5480 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5481 | raise |
| 5482 | |
| 5483 | # from client VRF0 to service VRF1 |
| 5484 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5485 | IP(src=self.pg0.remote_ip4, dst=external_addr) / |
| 5486 | TCP(sport=12347, dport=external_port)) |
| 5487 | self.pg0.add_stream(p) |
| 5488 | self.pg_enable_capture(self.pg_interfaces) |
| 5489 | self.pg_start() |
| 5490 | capture = self.pg5.get_capture(1) |
| 5491 | p = capture[0] |
| 5492 | try: |
| 5493 | ip = p[IP] |
| 5494 | tcp = p[TCP] |
| 5495 | self.assertEqual(ip.dst, self.pg5.remote_ip4) |
| 5496 | self.assertEqual(tcp.dport, local_port) |
| 5497 | self.assert_packet_checksums_valid(p) |
| 5498 | except: |
| 5499 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5500 | raise |
| 5501 | |
| 5502 | # from service VRF1 back to client VRF0 |
| 5503 | p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) / |
| 5504 | IP(src=self.pg5.remote_ip4, dst=self.pg0.remote_ip4) / |
| 5505 | TCP(sport=local_port, dport=12347)) |
| 5506 | self.pg5.add_stream(p) |
| 5507 | self.pg_enable_capture(self.pg_interfaces) |
| 5508 | self.pg_start() |
| 5509 | capture = self.pg0.get_capture(1) |
| 5510 | p = capture[0] |
| 5511 | try: |
| 5512 | ip = p[IP] |
| 5513 | tcp = p[TCP] |
| 5514 | self.assertEqual(ip.src, external_addr) |
| 5515 | self.assertEqual(tcp.sport, external_port) |
| 5516 | self.assert_packet_checksums_valid(p) |
| 5517 | except: |
| 5518 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5519 | raise |
| 5520 | |
| 5521 | # from client to server (both VRF1, no translation) |
| 5522 | p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) / |
| 5523 | IP(src=self.pg6.remote_ip4, dst=self.pg5.remote_ip4) / |
| 5524 | TCP(sport=12348, dport=local_port)) |
| 5525 | self.pg6.add_stream(p) |
| 5526 | self.pg_enable_capture(self.pg_interfaces) |
| 5527 | self.pg_start() |
| 5528 | capture = self.pg5.get_capture(1) |
| 5529 | p = capture[0] |
| 5530 | try: |
| 5531 | ip = p[IP] |
| 5532 | tcp = p[TCP] |
| 5533 | self.assertEqual(ip.dst, self.pg5.remote_ip4) |
| 5534 | self.assertEqual(tcp.dport, local_port) |
| 5535 | self.assert_packet_checksums_valid(p) |
| 5536 | except: |
| 5537 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5538 | raise |
| 5539 | |
| 5540 | # from server back to client (both VRF1, no translation) |
| 5541 | p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) / |
| 5542 | IP(src=self.pg5.remote_ip4, dst=self.pg6.remote_ip4) / |
| 5543 | TCP(sport=local_port, dport=12348)) |
| 5544 | self.pg5.add_stream(p) |
| 5545 | self.pg_enable_capture(self.pg_interfaces) |
| 5546 | self.pg_start() |
| 5547 | capture = self.pg6.get_capture(1) |
| 5548 | p = capture[0] |
| 5549 | try: |
| 5550 | ip = p[IP] |
| 5551 | tcp = p[TCP] |
| 5552 | self.assertEqual(ip.src, self.pg5.remote_ip4) |
| 5553 | self.assertEqual(tcp.sport, local_port) |
| 5554 | self.assert_packet_checksums_valid(p) |
| 5555 | except: |
| 5556 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5557 | raise |
| 5558 | |
| 5559 | # from client VRF1 to server VRF0 (no translation) |
| 5560 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5561 | IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) / |
| 5562 | TCP(sport=local_port, dport=12349)) |
| 5563 | self.pg0.add_stream(p) |
| 5564 | self.pg_enable_capture(self.pg_interfaces) |
| 5565 | self.pg_start() |
| 5566 | capture = self.pg6.get_capture(1) |
| 5567 | p = capture[0] |
| 5568 | try: |
| 5569 | ip = p[IP] |
| 5570 | tcp = p[TCP] |
| 5571 | self.assertEqual(ip.src, self.pg0.remote_ip4) |
| 5572 | self.assertEqual(tcp.sport, local_port) |
| 5573 | self.assert_packet_checksums_valid(p) |
| 5574 | except: |
| 5575 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5576 | raise |
| 5577 | |
| 5578 | # from server VRF0 back to client VRF1 (no translation) |
| 5579 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5580 | IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) / |
| 5581 | TCP(sport=local_port, dport=12349)) |
| 5582 | self.pg0.add_stream(p) |
| 5583 | self.pg_enable_capture(self.pg_interfaces) |
| 5584 | self.pg_start() |
| 5585 | capture = self.pg6.get_capture(1) |
| 5586 | p = capture[0] |
| 5587 | try: |
| 5588 | ip = p[IP] |
| 5589 | tcp = p[TCP] |
| 5590 | self.assertEqual(ip.src, self.pg0.remote_ip4) |
| 5591 | self.assertEqual(tcp.sport, local_port) |
| 5592 | self.assert_packet_checksums_valid(p) |
| 5593 | except: |
| 5594 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5595 | raise |
| 5596 | |
| 5597 | # from client VRF0 to server VRF1 (no translation) |
| 5598 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5599 | IP(src=self.pg0.remote_ip4, dst=self.pg5.remote_ip4) / |
| 5600 | TCP(sport=12344, dport=local_port)) |
| 5601 | self.pg0.add_stream(p) |
| 5602 | self.pg_enable_capture(self.pg_interfaces) |
| 5603 | self.pg_start() |
| 5604 | capture = self.pg5.get_capture(1) |
| 5605 | p = capture[0] |
| 5606 | try: |
| 5607 | ip = p[IP] |
| 5608 | tcp = p[TCP] |
| 5609 | self.assertEqual(ip.dst, self.pg5.remote_ip4) |
| 5610 | self.assertEqual(tcp.dport, local_port) |
| 5611 | self.assert_packet_checksums_valid(p) |
| 5612 | except: |
| 5613 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5614 | raise |
| 5615 | |
| 5616 | # from server VRF1 back to client VRF0 (no translation) |
| 5617 | p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) / |
| 5618 | IP(src=self.pg5.remote_ip4, dst=self.pg0.remote_ip4) / |
| 5619 | TCP(sport=local_port, dport=12344)) |
| 5620 | self.pg5.add_stream(p) |
| 5621 | self.pg_enable_capture(self.pg_interfaces) |
| 5622 | self.pg_start() |
| 5623 | capture = self.pg0.get_capture(1) |
| 5624 | p = capture[0] |
| 5625 | try: |
| 5626 | ip = p[IP] |
| 5627 | tcp = p[TCP] |
| 5628 | self.assertEqual(ip.src, self.pg5.remote_ip4) |
| 5629 | self.assertEqual(tcp.sport, local_port) |
| 5630 | self.assert_packet_checksums_valid(p) |
| 5631 | except: |
| 5632 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 5633 | raise |
| 5634 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5635 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 5636 | def test_session_timeout(self): |
| 5637 | """ NAT44 session timeouts """ |
| 5638 | self.nat44_add_address(self.nat_addr) |
| 5639 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5640 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 5641 | is_inside=0) |
| 5642 | self.vapi.nat_set_timeouts(icmp=5) |
| 5643 | |
| 5644 | max_sessions = 1000 |
| 5645 | pkts = [] |
| 5646 | for i in range(0, max_sessions): |
| 5647 | src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF) |
| 5648 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 5649 | IP(src=src, dst=self.pg1.remote_ip4) / |
| 5650 | ICMP(id=1025, type='echo-request')) |
| 5651 | pkts.append(p) |
| 5652 | self.pg0.add_stream(pkts) |
| 5653 | self.pg_enable_capture(self.pg_interfaces) |
| 5654 | self.pg_start() |
| 5655 | self.pg1.get_capture(max_sessions) |
| 5656 | |
| 5657 | sleep(10) |
| 5658 | |
| 5659 | pkts = [] |
| 5660 | for i in range(0, max_sessions): |
Matus Fabian | 7f8a8db | 2018-11-22 00:12:15 -0800 | [diff] [blame] | 5661 | src = "10.11.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5662 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 5663 | IP(src=src, dst=self.pg1.remote_ip4) / |
| 5664 | ICMP(id=1026, type='echo-request')) |
| 5665 | pkts.append(p) |
| 5666 | self.pg0.add_stream(pkts) |
| 5667 | self.pg_enable_capture(self.pg_interfaces) |
| 5668 | self.pg_start() |
| 5669 | self.pg1.get_capture(max_sessions) |
| 5670 | |
| 5671 | nsessions = 0 |
| 5672 | users = self.vapi.nat44_user_dump() |
| 5673 | for user in users: |
| 5674 | nsessions = nsessions + user.nsessions |
| 5675 | self.assertLess(nsessions, 2 * max_sessions) |
| 5676 | |
| 5677 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
Matus Fabian | 15e8e68 | 2018-11-21 04:53:10 -0800 | [diff] [blame] | 5678 | def test_session_rst_timeout(self): |
| 5679 | """ NAT44 session RST timeouts """ |
| 5680 | self.nat44_add_address(self.nat_addr) |
| 5681 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5682 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 5683 | is_inside=0) |
| 5684 | self.vapi.nat_set_timeouts(tcp_transitory=5) |
| 5685 | |
Matus Fabian | 15e8e68 | 2018-11-21 04:53:10 -0800 | [diff] [blame] | 5686 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 5687 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5688 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5689 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
| 5690 | flags="R")) |
| 5691 | self.pg0.add_stream(p) |
| 5692 | self.pg_enable_capture(self.pg_interfaces) |
| 5693 | self.pg_start() |
| 5694 | self.pg1.get_capture(1) |
| 5695 | |
Matus Fabian | 15e8e68 | 2018-11-21 04:53:10 -0800 | [diff] [blame] | 5696 | sleep(6) |
| 5697 | |
| 5698 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5699 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5700 | TCP(sport=self.tcp_port_in + 1, dport=self.tcp_external_port + 1, |
| 5701 | flags="S")) |
| 5702 | self.pg0.add_stream(p) |
| 5703 | self.pg_enable_capture(self.pg_interfaces) |
| 5704 | self.pg_start() |
| 5705 | self.pg1.get_capture(1) |
| 5706 | |
| 5707 | nsessions = 0 |
| 5708 | users = self.vapi.nat44_user_dump() |
| 5709 | self.assertEqual(len(users), 1) |
| 5710 | self.assertEqual(users[0].ip_address, self.pg0.remote_ip4n) |
Matus Fabian | 7f8a8db | 2018-11-22 00:12:15 -0800 | [diff] [blame] | 5711 | self.assertEqual(users[0].nsessions, 1) |
Matus Fabian | 15e8e68 | 2018-11-21 04:53:10 -0800 | [diff] [blame] | 5712 | |
| 5713 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5714 | def test_session_limit_per_user(self): |
| 5715 | """ Maximum sessions per user limit """ |
| 5716 | self.nat44_add_address(self.nat_addr) |
| 5717 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5718 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 5719 | is_inside=0) |
| 5720 | self.vapi.set_ipfix_exporter(collector_address=self.pg2.remote_ip4n, |
| 5721 | src_address=self.pg2.local_ip4n, |
| 5722 | path_mtu=512, |
| 5723 | template_interval=10) |
Matus Fabian | 8fdc015 | 2018-09-24 04:41:28 -0700 | [diff] [blame] | 5724 | self.vapi.nat_set_timeouts(udp=5) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5725 | |
| 5726 | # get maximum number of translations per user |
| 5727 | nat44_config = self.vapi.nat_show_config() |
| 5728 | |
| 5729 | pkts = [] |
| 5730 | for port in range(0, nat44_config.max_translations_per_user): |
| 5731 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5732 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5733 | UDP(sport=1025 + port, dport=1025 + port)) |
| 5734 | pkts.append(p) |
| 5735 | |
| 5736 | self.pg0.add_stream(pkts) |
| 5737 | self.pg_enable_capture(self.pg_interfaces) |
| 5738 | self.pg_start() |
| 5739 | capture = self.pg1.get_capture(len(pkts)) |
| 5740 | |
| 5741 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 5742 | src_port=self.ipfix_src_port) |
| 5743 | |
| 5744 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5745 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5746 | UDP(sport=3001, dport=3002)) |
| 5747 | self.pg0.add_stream(p) |
| 5748 | self.pg_enable_capture(self.pg_interfaces) |
| 5749 | self.pg_start() |
| 5750 | capture = self.pg1.assert_nothing_captured() |
| 5751 | |
| 5752 | # verify IPFIX logging |
| 5753 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 5754 | sleep(1) |
| 5755 | capture = self.pg2.get_capture(10) |
| 5756 | ipfix = IPFIXDecoder() |
| 5757 | # first load template |
| 5758 | for p in capture: |
| 5759 | self.assertTrue(p.haslayer(IPFIX)) |
| 5760 | if p.haslayer(Template): |
| 5761 | ipfix.add_template(p.getlayer(Template)) |
| 5762 | # verify events in data set |
| 5763 | for p in capture: |
| 5764 | if p.haslayer(Data): |
| 5765 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 5766 | self.verify_ipfix_max_entries_per_user( |
| 5767 | data, |
| 5768 | nat44_config.max_translations_per_user, |
| 5769 | self.pg0.remote_ip4n) |
| 5770 | |
Matus Fabian | 8fdc015 | 2018-09-24 04:41:28 -0700 | [diff] [blame] | 5771 | sleep(6) |
| 5772 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 5773 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 5774 | UDP(sport=3001, dport=3002)) |
| 5775 | self.pg0.add_stream(p) |
| 5776 | self.pg_enable_capture(self.pg_interfaces) |
| 5777 | self.pg_start() |
| 5778 | self.pg1.get_capture(1) |
| 5779 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 5780 | def tearDown(self): |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 5781 | super(TestNAT44EndpointDependent, self).tearDown() |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 5782 | if not self.vpp_dead: |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 5783 | self.logger.info(self.vapi.cli("show nat44 addresses")) |
| 5784 | self.logger.info(self.vapi.cli("show nat44 interfaces")) |
| 5785 | self.logger.info(self.vapi.cli("show nat44 static mappings")) |
| 5786 | self.logger.info(self.vapi.cli("show nat44 interface address")) |
| 5787 | self.logger.info(self.vapi.cli("show nat44 sessions detail")) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5788 | self.logger.info(self.vapi.cli("show nat44 hash tables detail")) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 5789 | self.logger.info(self.vapi.cli("show nat timeouts")) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5790 | self.clear_nat44() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5791 | self.vapi.cli("clear logging") |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 5792 | |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 5793 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 5794 | class TestNAT44Out2InDPO(MethodHolder): |
| 5795 | """ NAT44 Test Cases using out2in DPO """ |
| 5796 | |
| 5797 | @classmethod |
| 5798 | def setUpConstants(cls): |
| 5799 | super(TestNAT44Out2InDPO, cls).setUpConstants() |
| 5800 | cls.vpp_cmdline.extend(["nat", "{", "out2in dpo", "}"]) |
| 5801 | |
| 5802 | @classmethod |
| 5803 | def setUpClass(cls): |
| 5804 | super(TestNAT44Out2InDPO, cls).setUpClass() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5805 | cls.vapi.cli("set log class nat level debug") |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 5806 | |
| 5807 | try: |
| 5808 | cls.tcp_port_in = 6303 |
| 5809 | cls.tcp_port_out = 6303 |
| 5810 | cls.udp_port_in = 6304 |
| 5811 | cls.udp_port_out = 6304 |
| 5812 | cls.icmp_id_in = 6305 |
| 5813 | cls.icmp_id_out = 6305 |
| 5814 | cls.nat_addr = '10.0.0.3' |
| 5815 | cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr) |
| 5816 | cls.dst_ip4 = '192.168.70.1' |
| 5817 | |
| 5818 | cls.create_pg_interfaces(range(2)) |
| 5819 | |
| 5820 | cls.pg0.admin_up() |
| 5821 | cls.pg0.config_ip4() |
| 5822 | cls.pg0.resolve_arp() |
| 5823 | |
| 5824 | cls.pg1.admin_up() |
| 5825 | cls.pg1.config_ip6() |
| 5826 | cls.pg1.resolve_ndp() |
| 5827 | |
| 5828 | cls.vapi.ip_add_del_route(is_ipv6=True, dst_address='\x00'*16, |
| 5829 | dst_address_length=0, |
| 5830 | next_hop_address=cls.pg1.remote_ip6n, |
| 5831 | next_hop_sw_if_index=cls.pg1.sw_if_index) |
| 5832 | |
| 5833 | except Exception: |
| 5834 | super(TestNAT44Out2InDPO, cls).tearDownClass() |
| 5835 | raise |
| 5836 | |
| 5837 | def configure_xlat(self): |
| 5838 | self.dst_ip6_pfx = '1:2:3::' |
| 5839 | self.dst_ip6_pfx_n = socket.inet_pton(socket.AF_INET6, |
| 5840 | self.dst_ip6_pfx) |
| 5841 | self.dst_ip6_pfx_len = 96 |
| 5842 | self.src_ip6_pfx = '4:5:6::' |
| 5843 | self.src_ip6_pfx_n = socket.inet_pton(socket.AF_INET6, |
| 5844 | self.src_ip6_pfx) |
| 5845 | self.src_ip6_pfx_len = 96 |
| 5846 | self.vapi.map_add_domain(self.dst_ip6_pfx_n, self.dst_ip6_pfx_len, |
| 5847 | self.src_ip6_pfx_n, self.src_ip6_pfx_len, |
| 5848 | '\x00\x00\x00\x00', 0, is_translation=1, |
| 5849 | is_rfc6052=1) |
| 5850 | |
Ole Troan | ffba3c3 | 2018-11-22 12:53:00 +0100 | [diff] [blame] | 5851 | @unittest.skip('Temporary disabled') |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 5852 | def test_464xlat_ce(self): |
| 5853 | """ Test 464XLAT CE with NAT44 """ |
| 5854 | |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 5855 | nat_config = self.vapi.nat_show_config() |
| 5856 | self.assertEqual(1, nat_config.out2in_dpo) |
| 5857 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 5858 | self.configure_xlat() |
| 5859 | |
| 5860 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 5861 | self.vapi.nat44_add_del_address_range(self.nat_addr_n, self.nat_addr_n) |
| 5862 | |
| 5863 | out_src_ip6 = self.compose_ip6(self.dst_ip4, self.dst_ip6_pfx, |
| 5864 | self.dst_ip6_pfx_len) |
| 5865 | out_dst_ip6 = self.compose_ip6(self.nat_addr, self.src_ip6_pfx, |
| 5866 | self.src_ip6_pfx_len) |
| 5867 | |
| 5868 | try: |
| 5869 | pkts = self.create_stream_in(self.pg0, self.pg1, self.dst_ip4) |
| 5870 | self.pg0.add_stream(pkts) |
| 5871 | self.pg_enable_capture(self.pg_interfaces) |
| 5872 | self.pg_start() |
| 5873 | capture = self.pg1.get_capture(len(pkts)) |
| 5874 | self.verify_capture_out_ip6(capture, nat_ip=out_dst_ip6, |
| 5875 | dst_ip=out_src_ip6) |
| 5876 | |
| 5877 | pkts = self.create_stream_out_ip6(self.pg1, out_src_ip6, |
| 5878 | out_dst_ip6) |
| 5879 | self.pg1.add_stream(pkts) |
| 5880 | self.pg_enable_capture(self.pg_interfaces) |
| 5881 | self.pg_start() |
| 5882 | capture = self.pg0.get_capture(len(pkts)) |
| 5883 | self.verify_capture_in(capture, self.pg0) |
| 5884 | finally: |
| 5885 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index, |
| 5886 | is_add=0) |
| 5887 | self.vapi.nat44_add_del_address_range(self.nat_addr_n, |
| 5888 | self.nat_addr_n, is_add=0) |
| 5889 | |
Ole Troan | ffba3c3 | 2018-11-22 12:53:00 +0100 | [diff] [blame] | 5890 | @unittest.skip('Temporary disabled') |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 5891 | def test_464xlat_ce_no_nat(self): |
| 5892 | """ Test 464XLAT CE without NAT44 """ |
| 5893 | |
| 5894 | self.configure_xlat() |
| 5895 | |
| 5896 | out_src_ip6 = self.compose_ip6(self.dst_ip4, self.dst_ip6_pfx, |
| 5897 | self.dst_ip6_pfx_len) |
| 5898 | out_dst_ip6 = self.compose_ip6(self.pg0.remote_ip4, self.src_ip6_pfx, |
| 5899 | self.src_ip6_pfx_len) |
| 5900 | |
| 5901 | pkts = self.create_stream_in(self.pg0, self.pg1, self.dst_ip4) |
| 5902 | self.pg0.add_stream(pkts) |
| 5903 | self.pg_enable_capture(self.pg_interfaces) |
| 5904 | self.pg_start() |
| 5905 | capture = self.pg1.get_capture(len(pkts)) |
| 5906 | self.verify_capture_out_ip6(capture, dst_ip=out_src_ip6, |
| 5907 | nat_ip=out_dst_ip6, same_port=True) |
| 5908 | |
| 5909 | pkts = self.create_stream_out_ip6(self.pg1, out_src_ip6, out_dst_ip6) |
| 5910 | self.pg1.add_stream(pkts) |
| 5911 | self.pg_enable_capture(self.pg_interfaces) |
| 5912 | self.pg_start() |
| 5913 | capture = self.pg0.get_capture(len(pkts)) |
| 5914 | self.verify_capture_in(capture, self.pg0) |
| 5915 | |
| 5916 | |
Martin Gálik | d7f75cd | 2017-03-27 06:02:47 -0700 | [diff] [blame] | 5917 | class TestDeterministicNAT(MethodHolder): |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5918 | """ Deterministic NAT Test Cases """ |
| 5919 | |
| 5920 | @classmethod |
| 5921 | def setUpConstants(cls): |
| 5922 | super(TestDeterministicNAT, cls).setUpConstants() |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5923 | cls.vpp_cmdline.extend(["nat", "{", "deterministic", "}"]) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5924 | |
| 5925 | @classmethod |
| 5926 | def setUpClass(cls): |
| 5927 | super(TestDeterministicNAT, cls).setUpClass() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 5928 | cls.vapi.cli("set log class nat level debug") |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5929 | |
| 5930 | try: |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5931 | cls.tcp_port_in = 6303 |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5932 | cls.tcp_external_port = 6303 |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5933 | cls.udp_port_in = 6304 |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5934 | cls.udp_external_port = 6304 |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5935 | cls.icmp_id_in = 6305 |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5936 | cls.nat_addr = '10.0.0.3' |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5937 | |
Matus Fabian | 2f2db1c | 2017-04-18 05:29:59 -0700 | [diff] [blame] | 5938 | cls.create_pg_interfaces(range(3)) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5939 | cls.interfaces = list(cls.pg_interfaces) |
| 5940 | |
| 5941 | for i in cls.interfaces: |
| 5942 | i.admin_up() |
| 5943 | i.config_ip4() |
| 5944 | i.resolve_arp() |
| 5945 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5946 | cls.pg0.generate_remote_hosts(2) |
| 5947 | cls.pg0.configure_ipv4_neighbors() |
| 5948 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 5949 | except Exception: |
| 5950 | super(TestDeterministicNAT, cls).tearDownClass() |
| 5951 | raise |
| 5952 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5953 | def create_stream_in(self, in_if, out_if, ttl=64): |
| 5954 | """ |
| 5955 | Create packet stream for inside network |
| 5956 | |
| 5957 | :param in_if: Inside interface |
| 5958 | :param out_if: Outside interface |
| 5959 | :param ttl: TTL of generated packets |
| 5960 | """ |
| 5961 | pkts = [] |
| 5962 | # TCP |
| 5963 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
| 5964 | 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] | 5965 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5966 | pkts.append(p) |
| 5967 | |
| 5968 | # UDP |
| 5969 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
| 5970 | 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] | 5971 | UDP(sport=self.udp_port_in, dport=self.udp_external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5972 | pkts.append(p) |
| 5973 | |
| 5974 | # ICMP |
| 5975 | p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) / |
| 5976 | IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=ttl) / |
| 5977 | ICMP(id=self.icmp_id_in, type='echo-request')) |
| 5978 | pkts.append(p) |
| 5979 | |
| 5980 | return pkts |
| 5981 | |
| 5982 | def create_stream_out(self, out_if, dst_ip=None, ttl=64): |
| 5983 | """ |
| 5984 | Create packet stream for outside network |
| 5985 | |
| 5986 | :param out_if: Outside interface |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5987 | :param dst_ip: Destination IP address (Default use global NAT address) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5988 | :param ttl: TTL of generated packets |
| 5989 | """ |
| 5990 | if dst_ip is None: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 5991 | dst_ip = self.nat_addr |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5992 | pkts = [] |
| 5993 | # TCP |
| 5994 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
| 5995 | IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 5996 | TCP(dport=self.tcp_port_out, sport=self.tcp_external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 5997 | pkts.append(p) |
| 5998 | |
| 5999 | # UDP |
| 6000 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
| 6001 | IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6002 | UDP(dport=self.udp_port_out, sport=self.udp_external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6003 | pkts.append(p) |
| 6004 | |
| 6005 | # ICMP |
| 6006 | p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) / |
| 6007 | IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) / |
| 6008 | ICMP(id=self.icmp_external_id, type='echo-reply')) |
| 6009 | pkts.append(p) |
| 6010 | |
| 6011 | return pkts |
| 6012 | |
Matus Fabian | 05ca4a3 | 2018-09-04 23:45:13 -0700 | [diff] [blame] | 6013 | def verify_capture_out(self, capture, nat_ip=None): |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6014 | """ |
| 6015 | Verify captured packets on outside network |
| 6016 | |
| 6017 | :param capture: Captured packets |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6018 | :param nat_ip: Translated IP address (Default use global NAT address) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6019 | :param same_port: Sorce port number is not translated (Default False) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6020 | """ |
| 6021 | if nat_ip is None: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6022 | nat_ip = self.nat_addr |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6023 | for packet in capture: |
| 6024 | try: |
| 6025 | self.assertEqual(packet[IP].src, nat_ip) |
| 6026 | if packet.haslayer(TCP): |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6027 | self.tcp_port_out = packet[TCP].sport |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6028 | elif packet.haslayer(UDP): |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6029 | self.udp_port_out = packet[UDP].sport |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6030 | else: |
| 6031 | self.icmp_external_id = packet[ICMP].id |
| 6032 | except: |
| 6033 | self.logger.error(ppp("Unexpected or invalid packet " |
| 6034 | "(outside network):", packet)) |
| 6035 | raise |
| 6036 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 6037 | def test_deterministic_mode(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6038 | """ NAT plugin run deterministic mode """ |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 6039 | in_addr = '172.16.255.0' |
| 6040 | out_addr = '172.17.255.50' |
| 6041 | in_addr_t = '172.16.255.20' |
| 6042 | in_addr_n = socket.inet_aton(in_addr) |
| 6043 | out_addr_n = socket.inet_aton(out_addr) |
| 6044 | in_addr_t_n = socket.inet_aton(in_addr_t) |
| 6045 | in_plen = 24 |
| 6046 | out_plen = 32 |
| 6047 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6048 | nat_config = self.vapi.nat_show_config() |
| 6049 | self.assertEqual(1, nat_config.deterministic) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 6050 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6051 | 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] | 6052 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6053 | rep1 = self.vapi.nat_det_forward(in_addr_t_n) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 6054 | self.assertEqual(rep1.out_addr[:4], out_addr_n) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6055 | 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] | 6056 | self.assertEqual(rep2.in_addr[:4], in_addr_t_n) |
| 6057 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6058 | deterministic_mappings = self.vapi.nat_det_map_dump() |
Martin | 17a75cb | 2017-03-08 05:53:20 -0800 | [diff] [blame] | 6059 | self.assertEqual(len(deterministic_mappings), 1) |
| 6060 | dsm = deterministic_mappings[0] |
| 6061 | self.assertEqual(in_addr_n, dsm.in_addr[:4]) |
| 6062 | self.assertEqual(in_plen, dsm.in_plen) |
| 6063 | self.assertEqual(out_addr_n, dsm.out_addr[:4]) |
| 6064 | self.assertEqual(out_plen, dsm.out_plen) |
| 6065 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6066 | self.clear_nat_det() |
| 6067 | deterministic_mappings = self.vapi.nat_det_map_dump() |
Martin | b616e9f | 2017-03-14 02:25:45 -0700 | [diff] [blame] | 6068 | self.assertEqual(len(deterministic_mappings), 0) |
| 6069 | |
Matus Fabian | 6a0946f | 2017-04-12 03:36:13 -0700 | [diff] [blame] | 6070 | def test_set_timeouts(self): |
| 6071 | """ Set deterministic NAT timeouts """ |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6072 | timeouts_before = self.vapi.nat_get_timeouts() |
Matus Fabian | 6a0946f | 2017-04-12 03:36:13 -0700 | [diff] [blame] | 6073 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6074 | self.vapi.nat_set_timeouts(timeouts_before.udp + 10, |
| 6075 | timeouts_before.tcp_established + 10, |
| 6076 | timeouts_before.tcp_transitory + 10, |
| 6077 | timeouts_before.icmp + 10) |
Matus Fabian | 6a0946f | 2017-04-12 03:36:13 -0700 | [diff] [blame] | 6078 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6079 | timeouts_after = self.vapi.nat_get_timeouts() |
Matus Fabian | 6a0946f | 2017-04-12 03:36:13 -0700 | [diff] [blame] | 6080 | |
| 6081 | self.assertNotEqual(timeouts_before.udp, timeouts_after.udp) |
| 6082 | self.assertNotEqual(timeouts_before.icmp, timeouts_after.icmp) |
| 6083 | self.assertNotEqual(timeouts_before.tcp_established, |
| 6084 | timeouts_after.tcp_established) |
| 6085 | self.assertNotEqual(timeouts_before.tcp_transitory, |
| 6086 | timeouts_after.tcp_transitory) |
| 6087 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6088 | def test_det_in(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6089 | """ Deterministic NAT translation test (TCP, UDP, ICMP) """ |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6090 | |
| 6091 | nat_ip = "10.0.0.10" |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6092 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6093 | self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n, |
| 6094 | 32, |
| 6095 | socket.inet_aton(nat_ip), |
| 6096 | 32) |
| 6097 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6098 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6099 | is_inside=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6100 | |
| 6101 | # in2out |
| 6102 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 6103 | self.pg0.add_stream(pkts) |
| 6104 | self.pg_enable_capture(self.pg_interfaces) |
| 6105 | self.pg_start() |
| 6106 | capture = self.pg1.get_capture(len(pkts)) |
| 6107 | self.verify_capture_out(capture, nat_ip) |
| 6108 | |
| 6109 | # out2in |
| 6110 | pkts = self.create_stream_out(self.pg1, nat_ip) |
| 6111 | self.pg1.add_stream(pkts) |
| 6112 | self.pg_enable_capture(self.pg_interfaces) |
| 6113 | self.pg_start() |
| 6114 | capture = self.pg0.get_capture(len(pkts)) |
| 6115 | self.verify_capture_in(capture, self.pg0) |
| 6116 | |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6117 | # session dump test |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6118 | sessions = self.vapi.nat_det_session_dump(self.pg0.remote_ip4n) |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6119 | self.assertEqual(len(sessions), 3) |
| 6120 | |
| 6121 | # TCP session |
| 6122 | s = sessions[0] |
| 6123 | self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n) |
| 6124 | self.assertEqual(s.in_port, self.tcp_port_in) |
| 6125 | self.assertEqual(s.out_port, self.tcp_port_out) |
| 6126 | self.assertEqual(s.ext_port, self.tcp_external_port) |
| 6127 | |
| 6128 | # UDP session |
| 6129 | s = sessions[1] |
| 6130 | self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n) |
| 6131 | self.assertEqual(s.in_port, self.udp_port_in) |
| 6132 | self.assertEqual(s.out_port, self.udp_port_out) |
| 6133 | self.assertEqual(s.ext_port, self.udp_external_port) |
| 6134 | |
| 6135 | # ICMP session |
| 6136 | s = sessions[2] |
| 6137 | self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n) |
| 6138 | self.assertEqual(s.in_port, self.icmp_id_in) |
| 6139 | self.assertEqual(s.out_port, self.icmp_external_id) |
| 6140 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6141 | def test_multiple_users(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6142 | """ Deterministic NAT multiple users """ |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6143 | |
| 6144 | nat_ip = "10.0.0.10" |
| 6145 | port_in = 80 |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6146 | external_port = 6303 |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6147 | |
| 6148 | host0 = self.pg0.remote_hosts[0] |
| 6149 | host1 = self.pg0.remote_hosts[1] |
| 6150 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6151 | self.vapi.nat_det_add_del_map(host0.ip4n, |
| 6152 | 24, |
| 6153 | socket.inet_aton(nat_ip), |
| 6154 | 32) |
| 6155 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6156 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6157 | is_inside=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6158 | |
| 6159 | # host0 to out |
| 6160 | p = (Ether(src=host0.mac, dst=self.pg0.local_mac) / |
| 6161 | IP(src=host0.ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6162 | TCP(sport=port_in, dport=external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6163 | self.pg0.add_stream(p) |
| 6164 | self.pg_enable_capture(self.pg_interfaces) |
| 6165 | self.pg_start() |
| 6166 | capture = self.pg1.get_capture(1) |
| 6167 | p = capture[0] |
| 6168 | try: |
| 6169 | ip = p[IP] |
| 6170 | tcp = p[TCP] |
| 6171 | self.assertEqual(ip.src, nat_ip) |
| 6172 | self.assertEqual(ip.dst, self.pg1.remote_ip4) |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6173 | self.assertEqual(tcp.dport, external_port) |
| 6174 | port_out0 = tcp.sport |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6175 | except: |
| 6176 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 6177 | raise |
| 6178 | |
| 6179 | # host1 to out |
| 6180 | p = (Ether(src=host1.mac, dst=self.pg0.local_mac) / |
| 6181 | IP(src=host1.ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6182 | TCP(sport=port_in, dport=external_port)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6183 | self.pg0.add_stream(p) |
| 6184 | self.pg_enable_capture(self.pg_interfaces) |
| 6185 | self.pg_start() |
| 6186 | capture = self.pg1.get_capture(1) |
| 6187 | p = capture[0] |
| 6188 | try: |
| 6189 | ip = p[IP] |
| 6190 | tcp = p[TCP] |
| 6191 | self.assertEqual(ip.src, nat_ip) |
| 6192 | self.assertEqual(ip.dst, self.pg1.remote_ip4) |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6193 | self.assertEqual(tcp.dport, external_port) |
| 6194 | port_out1 = tcp.sport |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6195 | except: |
| 6196 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 6197 | raise |
| 6198 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6199 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6200 | self.assertEqual(1, len(dms)) |
| 6201 | self.assertEqual(2, dms[0].ses_num) |
| 6202 | |
| 6203 | # out to host0 |
| 6204 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 6205 | IP(src=self.pg1.remote_ip4, dst=nat_ip) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6206 | TCP(sport=external_port, dport=port_out0)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6207 | self.pg1.add_stream(p) |
| 6208 | self.pg_enable_capture(self.pg_interfaces) |
| 6209 | self.pg_start() |
| 6210 | capture = self.pg0.get_capture(1) |
| 6211 | p = capture[0] |
| 6212 | try: |
| 6213 | ip = p[IP] |
| 6214 | tcp = p[TCP] |
| 6215 | self.assertEqual(ip.src, self.pg1.remote_ip4) |
| 6216 | self.assertEqual(ip.dst, host0.ip4) |
| 6217 | self.assertEqual(tcp.dport, port_in) |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6218 | self.assertEqual(tcp.sport, external_port) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6219 | except: |
| 6220 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 6221 | raise |
| 6222 | |
| 6223 | # out to host1 |
| 6224 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
| 6225 | IP(src=self.pg1.remote_ip4, dst=nat_ip) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6226 | TCP(sport=external_port, dport=port_out1)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6227 | self.pg1.add_stream(p) |
| 6228 | self.pg_enable_capture(self.pg_interfaces) |
| 6229 | self.pg_start() |
| 6230 | capture = self.pg0.get_capture(1) |
| 6231 | p = capture[0] |
| 6232 | try: |
| 6233 | ip = p[IP] |
| 6234 | tcp = p[TCP] |
| 6235 | self.assertEqual(ip.src, self.pg1.remote_ip4) |
| 6236 | self.assertEqual(ip.dst, host1.ip4) |
| 6237 | self.assertEqual(tcp.dport, port_in) |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6238 | self.assertEqual(tcp.sport, external_port) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6239 | except: |
| 6240 | self.logger.error(ppp("Unexpected or invalid packet", p)) |
| 6241 | raise |
| 6242 | |
Martin Gálik | 6bc8c64 | 2017-04-19 01:12:27 -0700 | [diff] [blame] | 6243 | # session close api test |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6244 | self.vapi.nat_det_close_session_out(socket.inet_aton(nat_ip), |
| 6245 | port_out1, |
Martin Gálik | 6bc8c64 | 2017-04-19 01:12:27 -0700 | [diff] [blame] | 6246 | self.pg1.remote_ip4n, |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6247 | external_port) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6248 | dms = self.vapi.nat_det_map_dump() |
| 6249 | self.assertEqual(dms[0].ses_num, 1) |
| 6250 | |
| 6251 | self.vapi.nat_det_close_session_in(host0.ip4n, |
| 6252 | port_in, |
| 6253 | self.pg1.remote_ip4n, |
| 6254 | external_port) |
| 6255 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 6bc8c64 | 2017-04-19 01:12:27 -0700 | [diff] [blame] | 6256 | self.assertEqual(dms[0].ses_num, 0) |
| 6257 | |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6258 | def test_tcp_session_close_detection_in(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6259 | """ Deterministic NAT TCP session close from inside network """ |
| 6260 | self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n, |
| 6261 | 32, |
| 6262 | socket.inet_aton(self.nat_addr), |
| 6263 | 32) |
| 6264 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6265 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6266 | is_inside=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6267 | |
| 6268 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 6269 | |
| 6270 | # close the session from inside |
| 6271 | try: |
| 6272 | # FIN packet in -> out |
| 6273 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6274 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6275 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6276 | flags="F")) |
| 6277 | self.pg0.add_stream(p) |
| 6278 | self.pg_enable_capture(self.pg_interfaces) |
| 6279 | self.pg_start() |
| 6280 | self.pg1.get_capture(1) |
| 6281 | |
| 6282 | pkts = [] |
| 6283 | |
| 6284 | # ACK packet out -> in |
| 6285 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6286 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6287 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6288 | flags="A")) |
| 6289 | pkts.append(p) |
| 6290 | |
| 6291 | # FIN packet out -> in |
| 6292 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6293 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6294 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6295 | flags="F")) |
| 6296 | pkts.append(p) |
| 6297 | |
| 6298 | self.pg1.add_stream(pkts) |
| 6299 | self.pg_enable_capture(self.pg_interfaces) |
| 6300 | self.pg_start() |
| 6301 | self.pg0.get_capture(2) |
| 6302 | |
| 6303 | # ACK packet in -> out |
| 6304 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6305 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6306 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6307 | flags="A")) |
| 6308 | self.pg0.add_stream(p) |
| 6309 | self.pg_enable_capture(self.pg_interfaces) |
| 6310 | self.pg_start() |
| 6311 | self.pg1.get_capture(1) |
| 6312 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6313 | # Check if deterministic NAT44 closed the session |
| 6314 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6315 | self.assertEqual(0, dms[0].ses_num) |
| 6316 | except: |
| 6317 | self.logger.error("TCP session termination failed") |
| 6318 | raise |
| 6319 | |
| 6320 | def test_tcp_session_close_detection_out(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6321 | """ Deterministic NAT TCP session close from outside network """ |
| 6322 | self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n, |
| 6323 | 32, |
| 6324 | socket.inet_aton(self.nat_addr), |
| 6325 | 32) |
| 6326 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6327 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6328 | is_inside=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6329 | |
| 6330 | self.initiate_tcp_session(self.pg0, self.pg1) |
| 6331 | |
| 6332 | # close the session from outside |
| 6333 | try: |
| 6334 | # FIN packet out -> in |
| 6335 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6336 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6337 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6338 | flags="F")) |
| 6339 | self.pg1.add_stream(p) |
| 6340 | self.pg_enable_capture(self.pg_interfaces) |
| 6341 | self.pg_start() |
| 6342 | self.pg0.get_capture(1) |
| 6343 | |
| 6344 | pkts = [] |
| 6345 | |
| 6346 | # ACK packet in -> out |
| 6347 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6348 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6349 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6350 | flags="A")) |
| 6351 | pkts.append(p) |
| 6352 | |
| 6353 | # ACK packet in -> out |
| 6354 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6355 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6356 | TCP(sport=self.tcp_port_in, dport=self.tcp_external_port, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6357 | flags="F")) |
| 6358 | pkts.append(p) |
| 6359 | |
| 6360 | self.pg0.add_stream(pkts) |
| 6361 | self.pg_enable_capture(self.pg_interfaces) |
| 6362 | self.pg_start() |
| 6363 | self.pg1.get_capture(2) |
| 6364 | |
| 6365 | # ACK packet out -> in |
| 6366 | p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) / |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6367 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
Martin Gálik | 9806eae | 2017-04-25 01:25:08 -0700 | [diff] [blame] | 6368 | TCP(sport=self.tcp_external_port, dport=self.tcp_port_out, |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6369 | flags="A")) |
| 6370 | self.pg1.add_stream(p) |
| 6371 | self.pg_enable_capture(self.pg_interfaces) |
| 6372 | self.pg_start() |
| 6373 | self.pg0.get_capture(1) |
| 6374 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6375 | # Check if deterministic NAT44 closed the session |
| 6376 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6377 | self.assertEqual(0, dms[0].ses_num) |
| 6378 | except: |
| 6379 | self.logger.error("TCP session termination failed") |
| 6380 | raise |
| 6381 | |
| 6382 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 6383 | def test_session_timeout(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6384 | """ Deterministic NAT session timeouts """ |
| 6385 | self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n, |
| 6386 | 32, |
| 6387 | socket.inet_aton(self.nat_addr), |
| 6388 | 32) |
| 6389 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6390 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6391 | is_inside=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6392 | |
| 6393 | self.initiate_tcp_session(self.pg0, self.pg1) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6394 | self.vapi.nat_set_timeouts(5, 5, 5, 5) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6395 | pkts = self.create_stream_in(self.pg0, self.pg1) |
| 6396 | self.pg0.add_stream(pkts) |
| 6397 | self.pg_enable_capture(self.pg_interfaces) |
| 6398 | self.pg_start() |
| 6399 | capture = self.pg1.get_capture(len(pkts)) |
| 6400 | sleep(15) |
| 6401 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6402 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6403 | self.assertEqual(0, dms[0].ses_num) |
| 6404 | |
Matus Fabian | 7c0aecc | 2017-07-03 01:21:38 -0700 | [diff] [blame] | 6405 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6406 | def test_session_limit_per_user(self): |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6407 | """ Deterministic NAT maximum sessions per user limit """ |
| 6408 | self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n, |
| 6409 | 32, |
| 6410 | socket.inet_aton(self.nat_addr), |
| 6411 | 32) |
| 6412 | self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index) |
| 6413 | self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index, |
| 6414 | is_inside=0) |
Matus Fabian | 2f2db1c | 2017-04-18 05:29:59 -0700 | [diff] [blame] | 6415 | self.vapi.set_ipfix_exporter(collector_address=self.pg2.remote_ip4n, |
| 6416 | src_address=self.pg2.local_ip4n, |
| 6417 | path_mtu=512, |
| 6418 | template_interval=10) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6419 | self.vapi.nat_ipfix() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6420 | |
| 6421 | pkts = [] |
| 6422 | for port in range(1025, 2025): |
| 6423 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6424 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
| 6425 | UDP(sport=port, dport=port)) |
| 6426 | pkts.append(p) |
| 6427 | |
| 6428 | self.pg0.add_stream(pkts) |
| 6429 | self.pg_enable_capture(self.pg_interfaces) |
| 6430 | self.pg_start() |
| 6431 | capture = self.pg1.get_capture(len(pkts)) |
| 6432 | |
| 6433 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 6434 | IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) / |
Martin Gálik | f7e655d | 2017-04-27 02:13:26 -0700 | [diff] [blame] | 6435 | UDP(sport=3001, dport=3002)) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6436 | self.pg0.add_stream(p) |
| 6437 | self.pg_enable_capture(self.pg_interfaces) |
| 6438 | self.pg_start() |
| 6439 | capture = self.pg1.assert_nothing_captured() |
| 6440 | |
Martin Gálik | f7e655d | 2017-04-27 02:13:26 -0700 | [diff] [blame] | 6441 | # verify ICMP error packet |
| 6442 | capture = self.pg0.get_capture(1) |
| 6443 | p = capture[0] |
| 6444 | self.assertTrue(p.haslayer(ICMP)) |
| 6445 | icmp = p[ICMP] |
| 6446 | self.assertEqual(icmp.type, 3) |
| 6447 | self.assertEqual(icmp.code, 1) |
| 6448 | self.assertTrue(icmp.haslayer(IPerror)) |
| 6449 | inner_ip = icmp[IPerror] |
| 6450 | self.assertEqual(inner_ip[UDPerror].sport, 3001) |
| 6451 | self.assertEqual(inner_ip[UDPerror].dport, 3002) |
| 6452 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6453 | dms = self.vapi.nat_det_map_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6454 | |
| 6455 | self.assertEqual(1000, dms[0].ses_num) |
| 6456 | |
Matus Fabian | 2f2db1c | 2017-04-18 05:29:59 -0700 | [diff] [blame] | 6457 | # verify IPFIX logging |
| 6458 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
Matus Fabian | 7c0aecc | 2017-07-03 01:21:38 -0700 | [diff] [blame] | 6459 | sleep(1) |
Matus Fabian | 2f2db1c | 2017-04-18 05:29:59 -0700 | [diff] [blame] | 6460 | capture = self.pg2.get_capture(2) |
| 6461 | ipfix = IPFIXDecoder() |
| 6462 | # first load template |
| 6463 | for p in capture: |
| 6464 | self.assertTrue(p.haslayer(IPFIX)) |
| 6465 | if p.haslayer(Template): |
| 6466 | ipfix.add_template(p.getlayer(Template)) |
| 6467 | # verify events in data set |
| 6468 | for p in capture: |
| 6469 | if p.haslayer(Data): |
| 6470 | data = ipfix.decode_data_set(p.getlayer(Set)) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6471 | self.verify_ipfix_max_entries_per_user(data, |
| 6472 | 1000, |
| 6473 | self.pg0.remote_ip4n) |
Matus Fabian | 2f2db1c | 2017-04-18 05:29:59 -0700 | [diff] [blame] | 6474 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6475 | def clear_nat_det(self): |
Martin | 17a75cb | 2017-03-08 05:53:20 -0800 | [diff] [blame] | 6476 | """ |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6477 | Clear deterministic NAT configuration. |
Martin | 17a75cb | 2017-03-08 05:53:20 -0800 | [diff] [blame] | 6478 | """ |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6479 | self.vapi.nat_ipfix(enable=0) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6480 | self.vapi.nat_set_timeouts() |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6481 | deterministic_mappings = self.vapi.nat_det_map_dump() |
Martin | 17a75cb | 2017-03-08 05:53:20 -0800 | [diff] [blame] | 6482 | for dsm in deterministic_mappings: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6483 | self.vapi.nat_det_add_del_map(dsm.in_addr, |
| 6484 | dsm.in_plen, |
| 6485 | dsm.out_addr, |
| 6486 | dsm.out_plen, |
| 6487 | is_add=0) |
Martin | 17a75cb | 2017-03-08 05:53:20 -0800 | [diff] [blame] | 6488 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6489 | interfaces = self.vapi.nat44_interface_dump() |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6490 | for intf in interfaces: |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6491 | self.vapi.nat44_interface_add_del_feature(intf.sw_if_index, |
| 6492 | intf.is_inside, |
| 6493 | is_add=0) |
Martin Gálik | 977c1cb | 2017-03-30 23:21:51 -0700 | [diff] [blame] | 6494 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 6495 | def tearDown(self): |
| 6496 | super(TestDeterministicNAT, self).tearDown() |
| 6497 | if not self.vpp_dead: |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 6498 | self.logger.info(self.vapi.cli("show nat44 interfaces")) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6499 | self.logger.info(self.vapi.cli("show nat timeouts")) |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 6500 | self.logger.info( |
| 6501 | self.vapi.cli("show nat44 deterministic mappings")) |
| 6502 | self.logger.info( |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 6503 | self.vapi.cli("show nat44 deterministic sessions")) |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 6504 | self.clear_nat_det() |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 6505 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6506 | |
| 6507 | class TestNAT64(MethodHolder): |
| 6508 | """ NAT64 Test Cases """ |
| 6509 | |
| 6510 | @classmethod |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 6511 | def setUpConstants(cls): |
| 6512 | super(TestNAT64, cls).setUpConstants() |
| 6513 | cls.vpp_cmdline.extend(["nat", "{", "nat64 bib hash buckets 128", |
| 6514 | "nat64 st hash buckets 256", "}"]) |
| 6515 | |
| 6516 | @classmethod |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6517 | def setUpClass(cls): |
| 6518 | super(TestNAT64, cls).setUpClass() |
| 6519 | |
| 6520 | try: |
| 6521 | cls.tcp_port_in = 6303 |
| 6522 | cls.tcp_port_out = 6303 |
| 6523 | cls.udp_port_in = 6304 |
| 6524 | cls.udp_port_out = 6304 |
| 6525 | cls.icmp_id_in = 6305 |
| 6526 | cls.icmp_id_out = 6305 |
| 6527 | cls.nat_addr = '10.0.0.3' |
| 6528 | 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] | 6529 | cls.vrf1_id = 10 |
| 6530 | cls.vrf1_nat_addr = '10.0.10.3' |
| 6531 | cls.vrf1_nat_addr_n = socket.inet_pton(socket.AF_INET, |
| 6532 | cls.vrf1_nat_addr) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 6533 | cls.ipfix_src_port = 4739 |
| 6534 | cls.ipfix_domain_id = 1 |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6535 | |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 6536 | cls.create_pg_interfaces(range(6)) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6537 | cls.ip6_interfaces = list(cls.pg_interfaces[0:1]) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6538 | cls.ip6_interfaces.append(cls.pg_interfaces[2]) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6539 | cls.ip4_interfaces = list(cls.pg_interfaces[1:2]) |
| 6540 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 6541 | cls.vapi.ip_table_add_del(cls.vrf1_id, is_add=1, is_ipv6=1) |
| 6542 | |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6543 | cls.pg_interfaces[2].set_table_ip6(cls.vrf1_id) |
| 6544 | |
| 6545 | cls.pg0.generate_remote_hosts(2) |
| 6546 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6547 | for i in cls.ip6_interfaces: |
| 6548 | i.admin_up() |
| 6549 | i.config_ip6() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6550 | i.configure_ipv6_neighbors() |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6551 | |
| 6552 | for i in cls.ip4_interfaces: |
| 6553 | i.admin_up() |
| 6554 | i.config_ip4() |
| 6555 | i.resolve_arp() |
| 6556 | |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 6557 | cls.pg3.admin_up() |
| 6558 | cls.pg3.config_ip4() |
| 6559 | cls.pg3.resolve_arp() |
| 6560 | cls.pg3.config_ip6() |
| 6561 | cls.pg3.configure_ipv6_neighbors() |
| 6562 | |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 6563 | cls.pg5.admin_up() |
| 6564 | cls.pg5.config_ip6() |
| 6565 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6566 | except Exception: |
| 6567 | super(TestNAT64, cls).tearDownClass() |
| 6568 | raise |
| 6569 | |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 6570 | def test_nat64_inside_interface_handles_neighbor_advertisement(self): |
| 6571 | """ NAT64 inside interface handles Neighbor Advertisement """ |
| 6572 | |
| 6573 | self.vapi.nat64_add_del_interface(self.pg5.sw_if_index) |
| 6574 | |
| 6575 | # Try to send ping |
| 6576 | ping = (Ether(dst=self.pg5.local_mac, src=self.pg5.remote_mac) / |
| 6577 | IPv6(src=self.pg5.remote_ip6, dst=self.pg5.local_ip6) / |
| 6578 | ICMPv6EchoRequest()) |
| 6579 | pkts = [ping] |
| 6580 | self.pg5.add_stream(pkts) |
| 6581 | self.pg_enable_capture(self.pg_interfaces) |
| 6582 | self.pg_start() |
| 6583 | |
| 6584 | # Wait for Neighbor Solicitation |
| 6585 | capture = self.pg5.get_capture(len(pkts)) |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 6586 | packet = capture[0] |
| 6587 | try: |
| 6588 | self.assertEqual(packet[IPv6].src, self.pg5.local_ip6) |
| 6589 | self.assertTrue(packet.haslayer(ICMPv6ND_NS)) |
| 6590 | tgt = packet[ICMPv6ND_NS].tgt |
| 6591 | except: |
| 6592 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6593 | raise |
| 6594 | |
| 6595 | # Send Neighbor Advertisement |
| 6596 | p = (Ether(dst=self.pg5.local_mac, src=self.pg5.remote_mac) / |
| 6597 | IPv6(src=self.pg5.remote_ip6, dst=self.pg5.local_ip6) / |
| 6598 | ICMPv6ND_NA(tgt=tgt) / |
| 6599 | ICMPv6NDOptDstLLAddr(lladdr=self.pg5.remote_mac)) |
| 6600 | pkts = [p] |
| 6601 | self.pg5.add_stream(pkts) |
| 6602 | self.pg_enable_capture(self.pg_interfaces) |
| 6603 | self.pg_start() |
| 6604 | |
| 6605 | # Try to send ping again |
| 6606 | pkts = [ping] |
| 6607 | self.pg5.add_stream(pkts) |
| 6608 | self.pg_enable_capture(self.pg_interfaces) |
| 6609 | self.pg_start() |
| 6610 | |
| 6611 | # Wait for ping reply |
| 6612 | capture = self.pg5.get_capture(len(pkts)) |
Juraj Sloboda | c746a15 | 2018-07-09 02:36:37 +0200 | [diff] [blame] | 6613 | packet = capture[0] |
| 6614 | try: |
| 6615 | self.assertEqual(packet[IPv6].src, self.pg5.local_ip6) |
| 6616 | self.assertEqual(packet[IPv6].dst, self.pg5.remote_ip6) |
| 6617 | self.assertTrue(packet.haslayer(ICMPv6EchoReply)) |
| 6618 | except: |
| 6619 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6620 | raise |
| 6621 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6622 | def test_pool(self): |
| 6623 | """ Add/delete address to NAT64 pool """ |
| 6624 | nat_addr = socket.inet_pton(socket.AF_INET, '1.2.3.4') |
| 6625 | |
| 6626 | self.vapi.nat64_add_del_pool_addr_range(nat_addr, nat_addr) |
| 6627 | |
| 6628 | addresses = self.vapi.nat64_pool_addr_dump() |
| 6629 | self.assertEqual(len(addresses), 1) |
| 6630 | self.assertEqual(addresses[0].address, nat_addr) |
| 6631 | |
| 6632 | self.vapi.nat64_add_del_pool_addr_range(nat_addr, nat_addr, is_add=0) |
| 6633 | |
| 6634 | addresses = self.vapi.nat64_pool_addr_dump() |
| 6635 | self.assertEqual(len(addresses), 0) |
| 6636 | |
| 6637 | def test_interface(self): |
| 6638 | """ Enable/disable NAT64 feature on the interface """ |
| 6639 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6640 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6641 | |
| 6642 | interfaces = self.vapi.nat64_interface_dump() |
| 6643 | self.assertEqual(len(interfaces), 2) |
| 6644 | pg0_found = False |
| 6645 | pg1_found = False |
| 6646 | for intf in interfaces: |
| 6647 | if intf.sw_if_index == self.pg0.sw_if_index: |
| 6648 | self.assertEqual(intf.is_inside, 1) |
| 6649 | pg0_found = True |
| 6650 | elif intf.sw_if_index == self.pg1.sw_if_index: |
| 6651 | self.assertEqual(intf.is_inside, 0) |
| 6652 | pg1_found = True |
| 6653 | self.assertTrue(pg0_found) |
| 6654 | self.assertTrue(pg1_found) |
| 6655 | |
| 6656 | features = self.vapi.cli("show interface features pg0") |
| 6657 | self.assertNotEqual(features.find('nat64-in2out'), -1) |
| 6658 | features = self.vapi.cli("show interface features pg1") |
| 6659 | self.assertNotEqual(features.find('nat64-out2in'), -1) |
| 6660 | |
| 6661 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index, is_add=0) |
| 6662 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_add=0) |
| 6663 | |
| 6664 | interfaces = self.vapi.nat64_interface_dump() |
| 6665 | self.assertEqual(len(interfaces), 0) |
| 6666 | |
| 6667 | def test_static_bib(self): |
| 6668 | """ Add/delete static BIB entry """ |
| 6669 | in_addr = socket.inet_pton(socket.AF_INET6, |
| 6670 | '2001:db8:85a3::8a2e:370:7334') |
| 6671 | out_addr = socket.inet_pton(socket.AF_INET, '10.1.1.3') |
| 6672 | in_port = 1234 |
| 6673 | out_port = 5678 |
| 6674 | proto = IP_PROTOS.tcp |
| 6675 | |
| 6676 | self.vapi.nat64_add_del_static_bib(in_addr, |
| 6677 | out_addr, |
| 6678 | in_port, |
| 6679 | out_port, |
| 6680 | proto) |
| 6681 | bib = self.vapi.nat64_bib_dump(IP_PROTOS.tcp) |
| 6682 | static_bib_num = 0 |
| 6683 | for bibe in bib: |
| 6684 | if bibe.is_static: |
| 6685 | static_bib_num += 1 |
| 6686 | self.assertEqual(bibe.i_addr, in_addr) |
| 6687 | self.assertEqual(bibe.o_addr, out_addr) |
| 6688 | self.assertEqual(bibe.i_port, in_port) |
| 6689 | self.assertEqual(bibe.o_port, out_port) |
| 6690 | self.assertEqual(static_bib_num, 1) |
| 6691 | |
| 6692 | self.vapi.nat64_add_del_static_bib(in_addr, |
| 6693 | out_addr, |
| 6694 | in_port, |
| 6695 | out_port, |
| 6696 | proto, |
| 6697 | is_add=0) |
| 6698 | bib = self.vapi.nat64_bib_dump(IP_PROTOS.tcp) |
| 6699 | static_bib_num = 0 |
| 6700 | for bibe in bib: |
| 6701 | if bibe.is_static: |
| 6702 | static_bib_num += 1 |
| 6703 | self.assertEqual(static_bib_num, 0) |
| 6704 | |
| 6705 | def test_set_timeouts(self): |
| 6706 | """ Set NAT64 timeouts """ |
| 6707 | # verify default values |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6708 | timeouts = self.vapi.nat_get_timeouts() |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6709 | self.assertEqual(timeouts.udp, 300) |
| 6710 | self.assertEqual(timeouts.icmp, 60) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6711 | self.assertEqual(timeouts.tcp_transitory, 240) |
| 6712 | self.assertEqual(timeouts.tcp_established, 7440) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6713 | |
| 6714 | # set and verify custom values |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6715 | self.vapi.nat_set_timeouts(udp=200, icmp=30, tcp_transitory=250, |
| 6716 | tcp_established=7450) |
| 6717 | timeouts = self.vapi.nat_get_timeouts() |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6718 | self.assertEqual(timeouts.udp, 200) |
| 6719 | self.assertEqual(timeouts.icmp, 30) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 6720 | self.assertEqual(timeouts.tcp_transitory, 250) |
| 6721 | self.assertEqual(timeouts.tcp_established, 7450) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6722 | |
| 6723 | def test_dynamic(self): |
| 6724 | """ NAT64 dynamic translation test """ |
| 6725 | self.tcp_port_in = 6303 |
| 6726 | self.udp_port_in = 6304 |
| 6727 | self.icmp_id_in = 6305 |
| 6728 | |
| 6729 | ses_num_start = self.nat64_get_ses_num() |
| 6730 | |
| 6731 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6732 | self.nat_addr_n) |
| 6733 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6734 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6735 | |
| 6736 | # in2out |
| 6737 | pkts = self.create_stream_in_ip6(self.pg0, self.pg1) |
| 6738 | self.pg0.add_stream(pkts) |
| 6739 | self.pg_enable_capture(self.pg_interfaces) |
| 6740 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6741 | capture = self.pg1.get_capture(len(pkts)) |
| 6742 | self.verify_capture_out(capture, nat_ip=self.nat_addr, |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6743 | dst_ip=self.pg1.remote_ip4) |
| 6744 | |
| 6745 | # out2in |
| 6746 | pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr) |
| 6747 | self.pg1.add_stream(pkts) |
| 6748 | self.pg_enable_capture(self.pg_interfaces) |
| 6749 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6750 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6751 | ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4])) |
| 6752 | self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6) |
| 6753 | |
| 6754 | # in2out |
| 6755 | pkts = self.create_stream_in_ip6(self.pg0, self.pg1) |
| 6756 | self.pg0.add_stream(pkts) |
| 6757 | self.pg_enable_capture(self.pg_interfaces) |
| 6758 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6759 | capture = self.pg1.get_capture(len(pkts)) |
| 6760 | self.verify_capture_out(capture, nat_ip=self.nat_addr, |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6761 | dst_ip=self.pg1.remote_ip4) |
| 6762 | |
| 6763 | # out2in |
| 6764 | pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr) |
| 6765 | self.pg1.add_stream(pkts) |
| 6766 | self.pg_enable_capture(self.pg_interfaces) |
| 6767 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6768 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6769 | self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6) |
| 6770 | |
| 6771 | ses_num_end = self.nat64_get_ses_num() |
| 6772 | |
| 6773 | self.assertEqual(ses_num_end - ses_num_start, 3) |
| 6774 | |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6775 | # tenant with specific VRF |
| 6776 | self.vapi.nat64_add_del_pool_addr_range(self.vrf1_nat_addr_n, |
| 6777 | self.vrf1_nat_addr_n, |
| 6778 | vrf_id=self.vrf1_id) |
| 6779 | self.vapi.nat64_add_del_interface(self.pg2.sw_if_index) |
| 6780 | |
| 6781 | pkts = self.create_stream_in_ip6(self.pg2, self.pg1) |
| 6782 | self.pg2.add_stream(pkts) |
| 6783 | self.pg_enable_capture(self.pg_interfaces) |
| 6784 | self.pg_start() |
| 6785 | capture = self.pg1.get_capture(len(pkts)) |
| 6786 | self.verify_capture_out(capture, nat_ip=self.vrf1_nat_addr, |
| 6787 | dst_ip=self.pg1.remote_ip4) |
| 6788 | |
| 6789 | pkts = self.create_stream_out(self.pg1, dst_ip=self.vrf1_nat_addr) |
| 6790 | self.pg1.add_stream(pkts) |
| 6791 | self.pg_enable_capture(self.pg_interfaces) |
| 6792 | self.pg_start() |
| 6793 | capture = self.pg2.get_capture(len(pkts)) |
| 6794 | self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg2.remote_ip6) |
| 6795 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6796 | def test_static(self): |
| 6797 | """ NAT64 static translation test """ |
| 6798 | self.tcp_port_in = 60303 |
| 6799 | self.udp_port_in = 60304 |
| 6800 | self.icmp_id_in = 60305 |
| 6801 | self.tcp_port_out = 60303 |
| 6802 | self.udp_port_out = 60304 |
| 6803 | self.icmp_id_out = 60305 |
| 6804 | |
| 6805 | ses_num_start = self.nat64_get_ses_num() |
| 6806 | |
| 6807 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6808 | self.nat_addr_n) |
| 6809 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6810 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6811 | |
| 6812 | self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n, |
| 6813 | self.nat_addr_n, |
| 6814 | self.tcp_port_in, |
| 6815 | self.tcp_port_out, |
| 6816 | IP_PROTOS.tcp) |
| 6817 | self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n, |
| 6818 | self.nat_addr_n, |
| 6819 | self.udp_port_in, |
| 6820 | self.udp_port_out, |
| 6821 | IP_PROTOS.udp) |
| 6822 | self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n, |
| 6823 | self.nat_addr_n, |
| 6824 | self.icmp_id_in, |
| 6825 | self.icmp_id_out, |
| 6826 | IP_PROTOS.icmp) |
| 6827 | |
| 6828 | # in2out |
| 6829 | pkts = self.create_stream_in_ip6(self.pg0, self.pg1) |
| 6830 | self.pg0.add_stream(pkts) |
| 6831 | self.pg_enable_capture(self.pg_interfaces) |
| 6832 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6833 | capture = self.pg1.get_capture(len(pkts)) |
| 6834 | self.verify_capture_out(capture, nat_ip=self.nat_addr, |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6835 | dst_ip=self.pg1.remote_ip4, same_port=True) |
| 6836 | |
| 6837 | # out2in |
| 6838 | pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr) |
| 6839 | self.pg1.add_stream(pkts) |
| 6840 | self.pg_enable_capture(self.pg_interfaces) |
| 6841 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6842 | capture = self.pg0.get_capture(len(pkts)) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6843 | ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4])) |
| 6844 | self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6) |
| 6845 | |
| 6846 | ses_num_end = self.nat64_get_ses_num() |
| 6847 | |
| 6848 | self.assertEqual(ses_num_end - ses_num_start, 3) |
| 6849 | |
| 6850 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 6851 | def test_session_timeout(self): |
| 6852 | """ NAT64 session timeout """ |
| 6853 | self.icmp_id_in = 1234 |
| 6854 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6855 | self.nat_addr_n) |
| 6856 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6857 | 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] | 6858 | 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] | 6859 | |
| 6860 | pkts = self.create_stream_in_ip6(self.pg0, self.pg1) |
| 6861 | self.pg0.add_stream(pkts) |
| 6862 | self.pg_enable_capture(self.pg_interfaces) |
| 6863 | self.pg_start() |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6864 | capture = self.pg1.get_capture(len(pkts)) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6865 | |
| 6866 | ses_num_before_timeout = self.nat64_get_ses_num() |
| 6867 | |
| 6868 | sleep(15) |
| 6869 | |
Matus Fabian | 8fed424 | 2018-08-14 05:14:55 -0700 | [diff] [blame] | 6870 | # ICMP and TCP session after timeout |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6871 | ses_num_after_timeout = self.nat64_get_ses_num() |
Matus Fabian | 8fed424 | 2018-08-14 05:14:55 -0700 | [diff] [blame] | 6872 | self.assertEqual(ses_num_before_timeout - ses_num_after_timeout, 2) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 6873 | |
Matus Fabian | 732036d | 2017-06-08 05:24:28 -0700 | [diff] [blame] | 6874 | def test_icmp_error(self): |
| 6875 | """ NAT64 ICMP Error message translation """ |
| 6876 | self.tcp_port_in = 6303 |
| 6877 | self.udp_port_in = 6304 |
| 6878 | self.icmp_id_in = 6305 |
| 6879 | |
Matus Fabian | 732036d | 2017-06-08 05:24:28 -0700 | [diff] [blame] | 6880 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6881 | self.nat_addr_n) |
| 6882 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6883 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6884 | |
| 6885 | # send some packets to create sessions |
| 6886 | pkts = self.create_stream_in_ip6(self.pg0, self.pg1) |
| 6887 | self.pg0.add_stream(pkts) |
| 6888 | self.pg_enable_capture(self.pg_interfaces) |
| 6889 | self.pg_start() |
| 6890 | capture_ip4 = self.pg1.get_capture(len(pkts)) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6891 | self.verify_capture_out(capture_ip4, |
Matus Fabian | 732036d | 2017-06-08 05:24:28 -0700 | [diff] [blame] | 6892 | nat_ip=self.nat_addr, |
| 6893 | dst_ip=self.pg1.remote_ip4) |
| 6894 | |
| 6895 | pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr) |
| 6896 | self.pg1.add_stream(pkts) |
| 6897 | self.pg_enable_capture(self.pg_interfaces) |
| 6898 | self.pg_start() |
| 6899 | capture_ip6 = self.pg0.get_capture(len(pkts)) |
| 6900 | ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4])) |
| 6901 | self.verify_capture_in_ip6(capture_ip6, ip[IPv6].src, |
| 6902 | self.pg0.remote_ip6) |
| 6903 | |
| 6904 | # in2out |
| 6905 | pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 6906 | IPv6(src=self.pg0.remote_ip6, dst=ip[IPv6].src) / |
| 6907 | ICMPv6DestUnreach(code=1) / |
| 6908 | packet[IPv6] for packet in capture_ip6] |
| 6909 | self.pg0.add_stream(pkts) |
| 6910 | self.pg_enable_capture(self.pg_interfaces) |
| 6911 | self.pg_start() |
| 6912 | capture = self.pg1.get_capture(len(pkts)) |
| 6913 | for packet in capture: |
| 6914 | try: |
| 6915 | self.assertEqual(packet[IP].src, self.nat_addr) |
| 6916 | self.assertEqual(packet[IP].dst, self.pg1.remote_ip4) |
| 6917 | self.assertEqual(packet[ICMP].type, 3) |
| 6918 | self.assertEqual(packet[ICMP].code, 13) |
| 6919 | inner = packet[IPerror] |
| 6920 | self.assertEqual(inner.src, self.pg1.remote_ip4) |
| 6921 | self.assertEqual(inner.dst, self.nat_addr) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 6922 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 732036d | 2017-06-08 05:24:28 -0700 | [diff] [blame] | 6923 | if inner.haslayer(TCPerror): |
| 6924 | self.assertEqual(inner[TCPerror].dport, self.tcp_port_out) |
| 6925 | elif inner.haslayer(UDPerror): |
| 6926 | self.assertEqual(inner[UDPerror].dport, self.udp_port_out) |
| 6927 | else: |
| 6928 | self.assertEqual(inner[ICMPerror].id, self.icmp_id_out) |
| 6929 | except: |
| 6930 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6931 | raise |
| 6932 | |
| 6933 | # out2in |
| 6934 | pkts = [Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 6935 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 6936 | ICMP(type=3, code=13) / |
| 6937 | packet[IP] for packet in capture_ip4] |
| 6938 | self.pg1.add_stream(pkts) |
| 6939 | self.pg_enable_capture(self.pg_interfaces) |
| 6940 | self.pg_start() |
| 6941 | capture = self.pg0.get_capture(len(pkts)) |
| 6942 | for packet in capture: |
| 6943 | try: |
| 6944 | self.assertEqual(packet[IPv6].src, ip.src) |
| 6945 | self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6) |
| 6946 | icmp = packet[ICMPv6DestUnreach] |
| 6947 | self.assertEqual(icmp.code, 1) |
| 6948 | inner = icmp[IPerror6] |
| 6949 | self.assertEqual(inner.src, self.pg0.remote_ip6) |
| 6950 | self.assertEqual(inner.dst, ip.src) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 6951 | self.assert_icmpv6_checksum_valid(packet) |
Matus Fabian | 732036d | 2017-06-08 05:24:28 -0700 | [diff] [blame] | 6952 | if inner.haslayer(TCPerror): |
| 6953 | self.assertEqual(inner[TCPerror].sport, self.tcp_port_in) |
| 6954 | elif inner.haslayer(UDPerror): |
| 6955 | self.assertEqual(inner[UDPerror].sport, self.udp_port_in) |
| 6956 | else: |
| 6957 | self.assertEqual(inner[ICMPv6EchoRequest].id, |
| 6958 | self.icmp_id_in) |
| 6959 | except: |
| 6960 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 6961 | raise |
| 6962 | |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 6963 | def test_hairpinning(self): |
| 6964 | """ NAT64 hairpinning """ |
| 6965 | |
| 6966 | client = self.pg0.remote_hosts[0] |
| 6967 | server = self.pg0.remote_hosts[1] |
| 6968 | server_tcp_in_port = 22 |
| 6969 | server_tcp_out_port = 4022 |
| 6970 | server_udp_in_port = 23 |
| 6971 | server_udp_out_port = 4023 |
| 6972 | client_tcp_in_port = 1234 |
| 6973 | client_udp_in_port = 1235 |
| 6974 | client_tcp_out_port = 0 |
| 6975 | client_udp_out_port = 0 |
| 6976 | ip = IPv6(src=''.join(['64:ff9b::', self.nat_addr])) |
| 6977 | nat_addr_ip6 = ip.src |
| 6978 | |
| 6979 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 6980 | self.nat_addr_n) |
| 6981 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 6982 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 6983 | |
| 6984 | self.vapi.nat64_add_del_static_bib(server.ip6n, |
| 6985 | self.nat_addr_n, |
| 6986 | server_tcp_in_port, |
| 6987 | server_tcp_out_port, |
| 6988 | IP_PROTOS.tcp) |
| 6989 | self.vapi.nat64_add_del_static_bib(server.ip6n, |
| 6990 | self.nat_addr_n, |
| 6991 | server_udp_in_port, |
| 6992 | server_udp_out_port, |
| 6993 | IP_PROTOS.udp) |
| 6994 | |
| 6995 | # client to server |
| 6996 | pkts = [] |
| 6997 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 6998 | IPv6(src=client.ip6, dst=nat_addr_ip6) / |
| 6999 | TCP(sport=client_tcp_in_port, dport=server_tcp_out_port)) |
| 7000 | pkts.append(p) |
| 7001 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7002 | IPv6(src=client.ip6, dst=nat_addr_ip6) / |
| 7003 | UDP(sport=client_udp_in_port, dport=server_udp_out_port)) |
| 7004 | pkts.append(p) |
| 7005 | self.pg0.add_stream(pkts) |
| 7006 | self.pg_enable_capture(self.pg_interfaces) |
| 7007 | self.pg_start() |
| 7008 | capture = self.pg0.get_capture(len(pkts)) |
| 7009 | for packet in capture: |
| 7010 | try: |
| 7011 | self.assertEqual(packet[IPv6].src, nat_addr_ip6) |
| 7012 | self.assertEqual(packet[IPv6].dst, server.ip6) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7013 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 7014 | if packet.haslayer(TCP): |
| 7015 | self.assertNotEqual(packet[TCP].sport, client_tcp_in_port) |
| 7016 | self.assertEqual(packet[TCP].dport, server_tcp_in_port) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 7017 | client_tcp_out_port = packet[TCP].sport |
| 7018 | else: |
| 7019 | self.assertNotEqual(packet[UDP].sport, client_udp_in_port) |
| 7020 | self.assertEqual(packet[UDP].dport, server_udp_in_port) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 7021 | client_udp_out_port = packet[UDP].sport |
| 7022 | except: |
| 7023 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7024 | raise |
| 7025 | |
| 7026 | # server to client |
| 7027 | pkts = [] |
| 7028 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7029 | IPv6(src=server.ip6, dst=nat_addr_ip6) / |
| 7030 | TCP(sport=server_tcp_in_port, dport=client_tcp_out_port)) |
| 7031 | pkts.append(p) |
| 7032 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7033 | IPv6(src=server.ip6, dst=nat_addr_ip6) / |
| 7034 | UDP(sport=server_udp_in_port, dport=client_udp_out_port)) |
| 7035 | pkts.append(p) |
| 7036 | self.pg0.add_stream(pkts) |
| 7037 | self.pg_enable_capture(self.pg_interfaces) |
| 7038 | self.pg_start() |
| 7039 | capture = self.pg0.get_capture(len(pkts)) |
| 7040 | for packet in capture: |
| 7041 | try: |
| 7042 | self.assertEqual(packet[IPv6].src, nat_addr_ip6) |
| 7043 | self.assertEqual(packet[IPv6].dst, client.ip6) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7044 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 7045 | if packet.haslayer(TCP): |
| 7046 | self.assertEqual(packet[TCP].sport, server_tcp_out_port) |
| 7047 | self.assertEqual(packet[TCP].dport, client_tcp_in_port) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 7048 | else: |
| 7049 | self.assertEqual(packet[UDP].sport, server_udp_out_port) |
| 7050 | self.assertEqual(packet[UDP].dport, client_udp_in_port) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 7051 | except: |
| 7052 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7053 | raise |
| 7054 | |
| 7055 | # ICMP error |
| 7056 | pkts = [] |
| 7057 | pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7058 | IPv6(src=client.ip6, dst=nat_addr_ip6) / |
| 7059 | ICMPv6DestUnreach(code=1) / |
| 7060 | packet[IPv6] for packet in capture] |
| 7061 | self.pg0.add_stream(pkts) |
| 7062 | self.pg_enable_capture(self.pg_interfaces) |
| 7063 | self.pg_start() |
| 7064 | capture = self.pg0.get_capture(len(pkts)) |
| 7065 | for packet in capture: |
| 7066 | try: |
| 7067 | self.assertEqual(packet[IPv6].src, nat_addr_ip6) |
| 7068 | self.assertEqual(packet[IPv6].dst, server.ip6) |
| 7069 | icmp = packet[ICMPv6DestUnreach] |
| 7070 | self.assertEqual(icmp.code, 1) |
| 7071 | inner = icmp[IPerror6] |
| 7072 | self.assertEqual(inner.src, server.ip6) |
| 7073 | self.assertEqual(inner.dst, nat_addr_ip6) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7074 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 7075 | if inner.haslayer(TCPerror): |
| 7076 | self.assertEqual(inner[TCPerror].sport, server_tcp_in_port) |
| 7077 | self.assertEqual(inner[TCPerror].dport, |
| 7078 | client_tcp_out_port) |
| 7079 | else: |
| 7080 | self.assertEqual(inner[UDPerror].sport, server_udp_in_port) |
| 7081 | self.assertEqual(inner[UDPerror].dport, |
| 7082 | client_udp_out_port) |
| 7083 | except: |
| 7084 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7085 | raise |
| 7086 | |
Matus Fabian | 428dc91 | 2017-06-21 06:15:18 -0700 | [diff] [blame] | 7087 | def test_prefix(self): |
| 7088 | """ NAT64 Network-Specific Prefix """ |
| 7089 | |
| 7090 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7091 | self.nat_addr_n) |
| 7092 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7093 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7094 | self.vapi.nat64_add_del_pool_addr_range(self.vrf1_nat_addr_n, |
| 7095 | self.vrf1_nat_addr_n, |
| 7096 | vrf_id=self.vrf1_id) |
| 7097 | self.vapi.nat64_add_del_interface(self.pg2.sw_if_index) |
| 7098 | |
| 7099 | # Add global prefix |
| 7100 | global_pref64 = "2001:db8::" |
| 7101 | global_pref64_n = socket.inet_pton(socket.AF_INET6, global_pref64) |
| 7102 | global_pref64_len = 32 |
| 7103 | self.vapi.nat64_add_del_prefix(global_pref64_n, global_pref64_len) |
| 7104 | |
| 7105 | prefix = self.vapi.nat64_prefix_dump() |
| 7106 | self.assertEqual(len(prefix), 1) |
| 7107 | self.assertEqual(prefix[0].prefix, global_pref64_n) |
| 7108 | self.assertEqual(prefix[0].prefix_len, global_pref64_len) |
| 7109 | self.assertEqual(prefix[0].vrf_id, 0) |
| 7110 | |
| 7111 | # Add tenant specific prefix |
| 7112 | vrf1_pref64 = "2001:db8:122:300::" |
| 7113 | vrf1_pref64_n = socket.inet_pton(socket.AF_INET6, vrf1_pref64) |
| 7114 | vrf1_pref64_len = 56 |
| 7115 | self.vapi.nat64_add_del_prefix(vrf1_pref64_n, |
| 7116 | vrf1_pref64_len, |
| 7117 | vrf_id=self.vrf1_id) |
| 7118 | prefix = self.vapi.nat64_prefix_dump() |
| 7119 | self.assertEqual(len(prefix), 2) |
| 7120 | |
| 7121 | # Global prefix |
| 7122 | pkts = self.create_stream_in_ip6(self.pg0, |
| 7123 | self.pg1, |
| 7124 | pref=global_pref64, |
| 7125 | plen=global_pref64_len) |
| 7126 | self.pg0.add_stream(pkts) |
| 7127 | self.pg_enable_capture(self.pg_interfaces) |
| 7128 | self.pg_start() |
| 7129 | capture = self.pg1.get_capture(len(pkts)) |
| 7130 | self.verify_capture_out(capture, nat_ip=self.nat_addr, |
| 7131 | dst_ip=self.pg1.remote_ip4) |
| 7132 | |
| 7133 | pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr) |
| 7134 | self.pg1.add_stream(pkts) |
| 7135 | self.pg_enable_capture(self.pg_interfaces) |
| 7136 | self.pg_start() |
| 7137 | capture = self.pg0.get_capture(len(pkts)) |
| 7138 | dst_ip = self.compose_ip6(self.pg1.remote_ip4, |
| 7139 | global_pref64, |
| 7140 | global_pref64_len) |
| 7141 | self.verify_capture_in_ip6(capture, dst_ip, self.pg0.remote_ip6) |
| 7142 | |
| 7143 | # Tenant specific prefix |
| 7144 | pkts = self.create_stream_in_ip6(self.pg2, |
| 7145 | self.pg1, |
| 7146 | pref=vrf1_pref64, |
| 7147 | plen=vrf1_pref64_len) |
| 7148 | self.pg2.add_stream(pkts) |
| 7149 | self.pg_enable_capture(self.pg_interfaces) |
| 7150 | self.pg_start() |
| 7151 | capture = self.pg1.get_capture(len(pkts)) |
| 7152 | self.verify_capture_out(capture, nat_ip=self.vrf1_nat_addr, |
| 7153 | dst_ip=self.pg1.remote_ip4) |
| 7154 | |
| 7155 | pkts = self.create_stream_out(self.pg1, dst_ip=self.vrf1_nat_addr) |
| 7156 | self.pg1.add_stream(pkts) |
| 7157 | self.pg_enable_capture(self.pg_interfaces) |
| 7158 | self.pg_start() |
| 7159 | capture = self.pg2.get_capture(len(pkts)) |
| 7160 | dst_ip = self.compose_ip6(self.pg1.remote_ip4, |
| 7161 | vrf1_pref64, |
| 7162 | vrf1_pref64_len) |
| 7163 | self.verify_capture_in_ip6(capture, dst_ip, self.pg2.remote_ip6) |
| 7164 | |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7165 | def test_unknown_proto(self): |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7166 | """ NAT64 translate packet with unknown protocol """ |
| 7167 | |
| 7168 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7169 | self.nat_addr_n) |
| 7170 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7171 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7172 | remote_ip6 = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96) |
| 7173 | |
| 7174 | # in2out |
| 7175 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7176 | IPv6(src=self.pg0.remote_ip6, dst=remote_ip6) / |
| 7177 | TCP(sport=self.tcp_port_in, dport=20)) |
| 7178 | self.pg0.add_stream(p) |
| 7179 | self.pg_enable_capture(self.pg_interfaces) |
| 7180 | self.pg_start() |
| 7181 | p = self.pg1.get_capture(1) |
| 7182 | |
| 7183 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7184 | IPv6(src=self.pg0.remote_ip6, dst=remote_ip6, nh=47) / |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7185 | GRE() / |
| 7186 | IP(src=self.pg2.local_ip4, dst=self.pg2.remote_ip4) / |
| 7187 | TCP(sport=1234, dport=1234)) |
| 7188 | self.pg0.add_stream(p) |
| 7189 | self.pg_enable_capture(self.pg_interfaces) |
| 7190 | self.pg_start() |
| 7191 | p = self.pg1.get_capture(1) |
| 7192 | packet = p[0] |
| 7193 | try: |
| 7194 | self.assertEqual(packet[IP].src, self.nat_addr) |
| 7195 | self.assertEqual(packet[IP].dst, self.pg1.remote_ip4) |
| 7196 | self.assertTrue(packet.haslayer(GRE)) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7197 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7198 | except: |
| 7199 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7200 | raise |
| 7201 | |
| 7202 | # out2in |
| 7203 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7204 | IP(src=self.pg1.remote_ip4, dst=self.nat_addr) / |
| 7205 | GRE() / |
| 7206 | IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) / |
| 7207 | TCP(sport=1234, dport=1234)) |
| 7208 | self.pg1.add_stream(p) |
| 7209 | self.pg_enable_capture(self.pg_interfaces) |
| 7210 | self.pg_start() |
| 7211 | p = self.pg0.get_capture(1) |
| 7212 | packet = p[0] |
| 7213 | try: |
| 7214 | self.assertEqual(packet[IPv6].src, remote_ip6) |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7215 | self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6) |
| 7216 | self.assertEqual(packet[IPv6].nh, 47) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7217 | except: |
| 7218 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7219 | raise |
| 7220 | |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7221 | def test_hairpinning_unknown_proto(self): |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7222 | """ NAT64 translate packet with unknown protocol - hairpinning """ |
| 7223 | |
| 7224 | client = self.pg0.remote_hosts[0] |
| 7225 | server = self.pg0.remote_hosts[1] |
| 7226 | server_tcp_in_port = 22 |
| 7227 | server_tcp_out_port = 4022 |
| 7228 | client_tcp_in_port = 1234 |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7229 | client_tcp_out_port = 1235 |
| 7230 | server_nat_ip = "10.0.0.100" |
| 7231 | client_nat_ip = "10.0.0.110" |
| 7232 | server_nat_ip_n = socket.inet_pton(socket.AF_INET, server_nat_ip) |
| 7233 | client_nat_ip_n = socket.inet_pton(socket.AF_INET, client_nat_ip) |
| 7234 | server_nat_ip6 = self.compose_ip6(server_nat_ip, '64:ff9b::', 96) |
| 7235 | client_nat_ip6 = self.compose_ip6(client_nat_ip, '64:ff9b::', 96) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7236 | |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7237 | self.vapi.nat64_add_del_pool_addr_range(server_nat_ip_n, |
| 7238 | client_nat_ip_n) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7239 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7240 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7241 | |
| 7242 | self.vapi.nat64_add_del_static_bib(server.ip6n, |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7243 | server_nat_ip_n, |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7244 | server_tcp_in_port, |
| 7245 | server_tcp_out_port, |
| 7246 | IP_PROTOS.tcp) |
| 7247 | |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7248 | self.vapi.nat64_add_del_static_bib(server.ip6n, |
| 7249 | server_nat_ip_n, |
| 7250 | 0, |
| 7251 | 0, |
| 7252 | IP_PROTOS.gre) |
| 7253 | |
| 7254 | self.vapi.nat64_add_del_static_bib(client.ip6n, |
| 7255 | client_nat_ip_n, |
| 7256 | client_tcp_in_port, |
| 7257 | client_tcp_out_port, |
| 7258 | IP_PROTOS.tcp) |
| 7259 | |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7260 | # client to server |
| 7261 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7262 | IPv6(src=client.ip6, dst=server_nat_ip6) / |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7263 | TCP(sport=client_tcp_in_port, dport=server_tcp_out_port)) |
| 7264 | self.pg0.add_stream(p) |
| 7265 | self.pg_enable_capture(self.pg_interfaces) |
| 7266 | self.pg_start() |
| 7267 | p = self.pg0.get_capture(1) |
| 7268 | |
| 7269 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7270 | IPv6(src=client.ip6, dst=server_nat_ip6, nh=IP_PROTOS.gre) / |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7271 | GRE() / |
| 7272 | IP(src=self.pg2.local_ip4, dst=self.pg2.remote_ip4) / |
| 7273 | TCP(sport=1234, dport=1234)) |
| 7274 | self.pg0.add_stream(p) |
| 7275 | self.pg_enable_capture(self.pg_interfaces) |
| 7276 | self.pg_start() |
| 7277 | p = self.pg0.get_capture(1) |
| 7278 | packet = p[0] |
| 7279 | try: |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7280 | self.assertEqual(packet[IPv6].src, client_nat_ip6) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7281 | self.assertEqual(packet[IPv6].dst, server.ip6) |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7282 | self.assertEqual(packet[IPv6].nh, IP_PROTOS.gre) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7283 | except: |
| 7284 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7285 | raise |
| 7286 | |
| 7287 | # server to client |
| 7288 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7289 | IPv6(src=server.ip6, dst=client_nat_ip6, nh=IP_PROTOS.gre) / |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7290 | GRE() / |
| 7291 | IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) / |
| 7292 | TCP(sport=1234, dport=1234)) |
| 7293 | self.pg0.add_stream(p) |
| 7294 | self.pg_enable_capture(self.pg_interfaces) |
| 7295 | self.pg_start() |
| 7296 | p = self.pg0.get_capture(1) |
| 7297 | packet = p[0] |
| 7298 | try: |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7299 | self.assertEqual(packet[IPv6].src, server_nat_ip6) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7300 | self.assertEqual(packet[IPv6].dst, client.ip6) |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 7301 | self.assertEqual(packet[IPv6].nh, IP_PROTOS.gre) |
Matus Fabian | 7968e6c | 2017-07-06 05:37:49 -0700 | [diff] [blame] | 7302 | except: |
| 7303 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 7304 | raise |
| 7305 | |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 7306 | def test_one_armed_nat64(self): |
| 7307 | """ One armed NAT64 """ |
| 7308 | external_port = 0 |
| 7309 | remote_host_ip6 = self.compose_ip6(self.pg3.remote_ip4, |
| 7310 | '64:ff9b::', |
| 7311 | 96) |
| 7312 | |
| 7313 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7314 | self.nat_addr_n) |
| 7315 | self.vapi.nat64_add_del_interface(self.pg3.sw_if_index) |
| 7316 | self.vapi.nat64_add_del_interface(self.pg3.sw_if_index, is_inside=0) |
| 7317 | |
| 7318 | # in2out |
| 7319 | p = (Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) / |
| 7320 | IPv6(src=self.pg3.remote_ip6, dst=remote_host_ip6) / |
| 7321 | TCP(sport=12345, dport=80)) |
| 7322 | self.pg3.add_stream(p) |
| 7323 | self.pg_enable_capture(self.pg_interfaces) |
| 7324 | self.pg_start() |
| 7325 | capture = self.pg3.get_capture(1) |
| 7326 | p = capture[0] |
| 7327 | try: |
| 7328 | ip = p[IP] |
| 7329 | tcp = p[TCP] |
| 7330 | self.assertEqual(ip.src, self.nat_addr) |
| 7331 | self.assertEqual(ip.dst, self.pg3.remote_ip4) |
| 7332 | self.assertNotEqual(tcp.sport, 12345) |
| 7333 | external_port = tcp.sport |
| 7334 | self.assertEqual(tcp.dport, 80) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7335 | self.assert_packet_checksums_valid(p) |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 7336 | except: |
| 7337 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 7338 | raise |
| 7339 | |
| 7340 | # out2in |
| 7341 | p = (Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) / |
| 7342 | IP(src=self.pg3.remote_ip4, dst=self.nat_addr) / |
| 7343 | TCP(sport=80, dport=external_port)) |
| 7344 | self.pg3.add_stream(p) |
| 7345 | self.pg_enable_capture(self.pg_interfaces) |
| 7346 | self.pg_start() |
| 7347 | capture = self.pg3.get_capture(1) |
| 7348 | p = capture[0] |
| 7349 | try: |
| 7350 | ip = p[IPv6] |
| 7351 | tcp = p[TCP] |
| 7352 | self.assertEqual(ip.src, remote_host_ip6) |
| 7353 | self.assertEqual(ip.dst, self.pg3.remote_ip6) |
| 7354 | self.assertEqual(tcp.sport, 80) |
| 7355 | self.assertEqual(tcp.dport, 12345) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7356 | self.assert_packet_checksums_valid(p) |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 7357 | except: |
| 7358 | self.logger.error(ppp("Unexpected or invalid packet:", p)) |
| 7359 | raise |
| 7360 | |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 7361 | def test_frag_in_order(self): |
| 7362 | """ NAT64 translate fragments arriving in order """ |
| 7363 | self.tcp_port_in = random.randint(1025, 65535) |
| 7364 | |
| 7365 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7366 | self.nat_addr_n) |
| 7367 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7368 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7369 | |
| 7370 | reass = self.vapi.nat_reass_dump() |
| 7371 | reass_n_start = len(reass) |
| 7372 | |
| 7373 | # in2out |
| 7374 | data = 'a' * 200 |
| 7375 | pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4, |
| 7376 | self.tcp_port_in, 20, data) |
| 7377 | self.pg0.add_stream(pkts) |
| 7378 | self.pg_enable_capture(self.pg_interfaces) |
| 7379 | self.pg_start() |
| 7380 | frags = self.pg1.get_capture(len(pkts)) |
| 7381 | p = self.reass_frags_and_verify(frags, |
| 7382 | self.nat_addr, |
| 7383 | self.pg1.remote_ip4) |
| 7384 | self.assertEqual(p[TCP].dport, 20) |
| 7385 | self.assertNotEqual(p[TCP].sport, self.tcp_port_in) |
| 7386 | self.tcp_port_out = p[TCP].sport |
| 7387 | self.assertEqual(data, p[Raw].load) |
| 7388 | |
| 7389 | # out2in |
| 7390 | data = "A" * 4 + "b" * 16 + "C" * 3 |
| 7391 | pkts = self.create_stream_frag(self.pg1, |
| 7392 | self.nat_addr, |
| 7393 | 20, |
| 7394 | self.tcp_port_out, |
| 7395 | data) |
| 7396 | self.pg1.add_stream(pkts) |
| 7397 | self.pg_enable_capture(self.pg_interfaces) |
| 7398 | self.pg_start() |
| 7399 | frags = self.pg0.get_capture(len(pkts)) |
| 7400 | src = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96) |
| 7401 | p = self.reass_frags_and_verify_ip6(frags, src, self.pg0.remote_ip6) |
| 7402 | self.assertEqual(p[TCP].sport, 20) |
| 7403 | self.assertEqual(p[TCP].dport, self.tcp_port_in) |
| 7404 | self.assertEqual(data, p[Raw].load) |
| 7405 | |
| 7406 | reass = self.vapi.nat_reass_dump() |
| 7407 | reass_n_end = len(reass) |
| 7408 | |
| 7409 | self.assertEqual(reass_n_end - reass_n_start, 2) |
| 7410 | |
| 7411 | def test_reass_hairpinning(self): |
| 7412 | """ NAT64 fragments hairpinning """ |
| 7413 | data = 'a' * 200 |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 7414 | server = self.pg0.remote_hosts[1] |
| 7415 | server_in_port = random.randint(1025, 65535) |
| 7416 | server_out_port = random.randint(1025, 65535) |
| 7417 | client_in_port = random.randint(1025, 65535) |
| 7418 | ip = IPv6(src=''.join(['64:ff9b::', self.nat_addr])) |
| 7419 | nat_addr_ip6 = ip.src |
| 7420 | |
| 7421 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7422 | self.nat_addr_n) |
| 7423 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7424 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7425 | |
| 7426 | # add static BIB entry for server |
| 7427 | self.vapi.nat64_add_del_static_bib(server.ip6n, |
| 7428 | self.nat_addr_n, |
| 7429 | server_in_port, |
| 7430 | server_out_port, |
| 7431 | IP_PROTOS.tcp) |
| 7432 | |
| 7433 | # send packet from host to server |
| 7434 | pkts = self.create_stream_frag_ip6(self.pg0, |
| 7435 | self.nat_addr, |
| 7436 | client_in_port, |
| 7437 | server_out_port, |
| 7438 | data) |
| 7439 | self.pg0.add_stream(pkts) |
| 7440 | self.pg_enable_capture(self.pg_interfaces) |
| 7441 | self.pg_start() |
| 7442 | frags = self.pg0.get_capture(len(pkts)) |
| 7443 | p = self.reass_frags_and_verify_ip6(frags, nat_addr_ip6, server.ip6) |
| 7444 | self.assertNotEqual(p[TCP].sport, client_in_port) |
| 7445 | self.assertEqual(p[TCP].dport, server_in_port) |
| 7446 | self.assertEqual(data, p[Raw].load) |
| 7447 | |
| 7448 | def test_frag_out_of_order(self): |
| 7449 | """ NAT64 translate fragments arriving out of order """ |
| 7450 | self.tcp_port_in = random.randint(1025, 65535) |
| 7451 | |
| 7452 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7453 | self.nat_addr_n) |
| 7454 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7455 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7456 | |
| 7457 | # in2out |
| 7458 | data = 'a' * 200 |
| 7459 | pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4, |
| 7460 | self.tcp_port_in, 20, data) |
| 7461 | pkts.reverse() |
| 7462 | self.pg0.add_stream(pkts) |
| 7463 | self.pg_enable_capture(self.pg_interfaces) |
| 7464 | self.pg_start() |
| 7465 | frags = self.pg1.get_capture(len(pkts)) |
| 7466 | p = self.reass_frags_and_verify(frags, |
| 7467 | self.nat_addr, |
| 7468 | self.pg1.remote_ip4) |
| 7469 | self.assertEqual(p[TCP].dport, 20) |
| 7470 | self.assertNotEqual(p[TCP].sport, self.tcp_port_in) |
| 7471 | self.tcp_port_out = p[TCP].sport |
| 7472 | self.assertEqual(data, p[Raw].load) |
| 7473 | |
| 7474 | # out2in |
| 7475 | data = "A" * 4 + "B" * 16 + "C" * 3 |
| 7476 | pkts = self.create_stream_frag(self.pg1, |
| 7477 | self.nat_addr, |
| 7478 | 20, |
| 7479 | self.tcp_port_out, |
| 7480 | data) |
| 7481 | pkts.reverse() |
| 7482 | self.pg1.add_stream(pkts) |
| 7483 | self.pg_enable_capture(self.pg_interfaces) |
| 7484 | self.pg_start() |
| 7485 | frags = self.pg0.get_capture(len(pkts)) |
| 7486 | src = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96) |
| 7487 | p = self.reass_frags_and_verify_ip6(frags, src, self.pg0.remote_ip6) |
| 7488 | self.assertEqual(p[TCP].sport, 20) |
| 7489 | self.assertEqual(p[TCP].dport, self.tcp_port_in) |
| 7490 | self.assertEqual(data, p[Raw].load) |
| 7491 | |
Matus Fabian | 0938dcf | 2017-11-08 01:59:38 -0800 | [diff] [blame] | 7492 | def test_interface_addr(self): |
| 7493 | """ Acquire NAT64 pool addresses from interface """ |
| 7494 | self.vapi.nat64_add_interface_addr(self.pg4.sw_if_index) |
| 7495 | |
| 7496 | # no address in NAT64 pool |
| 7497 | adresses = self.vapi.nat44_address_dump() |
| 7498 | self.assertEqual(0, len(adresses)) |
| 7499 | |
| 7500 | # configure interface address and check NAT64 address pool |
| 7501 | self.pg4.config_ip4() |
| 7502 | addresses = self.vapi.nat64_pool_addr_dump() |
| 7503 | self.assertEqual(len(addresses), 1) |
| 7504 | self.assertEqual(addresses[0].address, self.pg4.local_ip4n) |
| 7505 | |
| 7506 | # remove interface address and check NAT64 address pool |
| 7507 | self.pg4.unconfig_ip4() |
| 7508 | addresses = self.vapi.nat64_pool_addr_dump() |
| 7509 | self.assertEqual(0, len(adresses)) |
| 7510 | |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7511 | @unittest.skipUnless(running_extended_tests(), "part of extended tests") |
| 7512 | def test_ipfix_max_bibs_sessions(self): |
| 7513 | """ IPFIX logging maximum session and BIB entries exceeded """ |
| 7514 | max_bibs = 1280 |
| 7515 | max_sessions = 2560 |
| 7516 | remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4, |
| 7517 | '64:ff9b::', |
| 7518 | 96) |
| 7519 | |
| 7520 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7521 | self.nat_addr_n) |
| 7522 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7523 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7524 | |
| 7525 | pkts = [] |
| 7526 | src = "" |
| 7527 | for i in range(0, max_bibs): |
| 7528 | src = "fd01:aa::%x" % (i) |
| 7529 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 7530 | IPv6(src=src, dst=remote_host_ip6) / |
| 7531 | TCP(sport=12345, dport=80)) |
| 7532 | pkts.append(p) |
| 7533 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 7534 | IPv6(src=src, dst=remote_host_ip6) / |
| 7535 | TCP(sport=12345, dport=22)) |
| 7536 | pkts.append(p) |
| 7537 | self.pg0.add_stream(pkts) |
| 7538 | self.pg_enable_capture(self.pg_interfaces) |
| 7539 | self.pg_start() |
| 7540 | self.pg1.get_capture(max_sessions) |
| 7541 | |
| 7542 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 7543 | src_address=self.pg3.local_ip4n, |
| 7544 | path_mtu=512, |
| 7545 | template_interval=10) |
| 7546 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 7547 | src_port=self.ipfix_src_port) |
| 7548 | |
| 7549 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 7550 | IPv6(src=src, dst=remote_host_ip6) / |
| 7551 | TCP(sport=12345, dport=25)) |
| 7552 | self.pg0.add_stream(p) |
| 7553 | self.pg_enable_capture(self.pg_interfaces) |
| 7554 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 7555 | self.pg1.assert_nothing_captured() |
| 7556 | sleep(1) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7557 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 7558 | capture = self.pg3.get_capture(9) |
| 7559 | ipfix = IPFIXDecoder() |
| 7560 | # first load template |
| 7561 | for p in capture: |
| 7562 | self.assertTrue(p.haslayer(IPFIX)) |
| 7563 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 7564 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 7565 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 7566 | self.assertEqual(p[UDP].dport, 4739) |
| 7567 | self.assertEqual(p[IPFIX].observationDomainID, |
| 7568 | self.ipfix_domain_id) |
| 7569 | if p.haslayer(Template): |
| 7570 | ipfix.add_template(p.getlayer(Template)) |
| 7571 | # verify events in data set |
| 7572 | for p in capture: |
| 7573 | if p.haslayer(Data): |
| 7574 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 7575 | self.verify_ipfix_max_sessions(data, max_sessions) |
| 7576 | |
| 7577 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 7578 | IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6) / |
| 7579 | TCP(sport=12345, dport=80)) |
| 7580 | self.pg0.add_stream(p) |
| 7581 | self.pg_enable_capture(self.pg_interfaces) |
| 7582 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 7583 | self.pg1.assert_nothing_captured() |
| 7584 | sleep(1) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7585 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 7586 | capture = self.pg3.get_capture(1) |
| 7587 | # verify events in data set |
| 7588 | for p in capture: |
| 7589 | self.assertTrue(p.haslayer(IPFIX)) |
| 7590 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 7591 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 7592 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 7593 | self.assertEqual(p[UDP].dport, 4739) |
| 7594 | self.assertEqual(p[IPFIX].observationDomainID, |
| 7595 | self.ipfix_domain_id) |
| 7596 | if p.haslayer(Data): |
| 7597 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 7598 | self.verify_ipfix_max_bibs(data, max_bibs) |
| 7599 | |
| 7600 | def test_ipfix_max_frags(self): |
| 7601 | """ IPFIX logging maximum fragments pending reassembly exceeded """ |
| 7602 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7603 | self.nat_addr_n) |
| 7604 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7605 | 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] | 7606 | self.vapi.nat_set_reass(max_frag=1, is_ip6=1) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7607 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 7608 | src_address=self.pg3.local_ip4n, |
| 7609 | path_mtu=512, |
| 7610 | template_interval=10) |
| 7611 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 7612 | src_port=self.ipfix_src_port) |
| 7613 | |
| 7614 | data = 'a' * 200 |
| 7615 | pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4, |
| 7616 | self.tcp_port_in, 20, data) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 7617 | pkts.reverse() |
| 7618 | self.pg0.add_stream(pkts) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7619 | self.pg_enable_capture(self.pg_interfaces) |
| 7620 | self.pg_start() |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 7621 | self.pg1.assert_nothing_captured() |
| 7622 | sleep(1) |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7623 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 7624 | capture = self.pg3.get_capture(9) |
| 7625 | ipfix = IPFIXDecoder() |
| 7626 | # first load template |
| 7627 | for p in capture: |
| 7628 | self.assertTrue(p.haslayer(IPFIX)) |
| 7629 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 7630 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 7631 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 7632 | self.assertEqual(p[UDP].dport, 4739) |
| 7633 | self.assertEqual(p[IPFIX].observationDomainID, |
| 7634 | self.ipfix_domain_id) |
| 7635 | if p.haslayer(Template): |
| 7636 | ipfix.add_template(p.getlayer(Template)) |
| 7637 | # verify events in data set |
| 7638 | for p in capture: |
| 7639 | if p.haslayer(Data): |
| 7640 | data = ipfix.decode_data_set(p.getlayer(Set)) |
Matus Fabian | a7f8b22 | 2018-09-05 06:01:55 -0700 | [diff] [blame] | 7641 | self.verify_ipfix_max_fragments_ip6(data, 1, |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7642 | self.pg0.remote_ip6n) |
| 7643 | |
| 7644 | def test_ipfix_bib_ses(self): |
| 7645 | """ IPFIX logging NAT64 BIB/session create and delete events """ |
| 7646 | self.tcp_port_in = random.randint(1025, 65535) |
| 7647 | remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4, |
| 7648 | '64:ff9b::', |
| 7649 | 96) |
| 7650 | |
| 7651 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7652 | self.nat_addr_n) |
| 7653 | self.vapi.nat64_add_del_interface(self.pg0.sw_if_index) |
| 7654 | self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 7655 | self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n, |
| 7656 | src_address=self.pg3.local_ip4n, |
| 7657 | path_mtu=512, |
| 7658 | template_interval=10) |
| 7659 | self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id, |
| 7660 | src_port=self.ipfix_src_port) |
| 7661 | |
| 7662 | # Create |
| 7663 | p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 7664 | IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6) / |
| 7665 | TCP(sport=self.tcp_port_in, dport=25)) |
| 7666 | self.pg0.add_stream(p) |
| 7667 | self.pg_enable_capture(self.pg_interfaces) |
| 7668 | self.pg_start() |
| 7669 | p = self.pg1.get_capture(1) |
| 7670 | self.tcp_port_out = p[0][TCP].sport |
| 7671 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 7672 | capture = self.pg3.get_capture(10) |
| 7673 | ipfix = IPFIXDecoder() |
| 7674 | # first load template |
| 7675 | for p in capture: |
| 7676 | self.assertTrue(p.haslayer(IPFIX)) |
| 7677 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 7678 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 7679 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 7680 | self.assertEqual(p[UDP].dport, 4739) |
| 7681 | self.assertEqual(p[IPFIX].observationDomainID, |
| 7682 | self.ipfix_domain_id) |
| 7683 | if p.haslayer(Template): |
| 7684 | ipfix.add_template(p.getlayer(Template)) |
| 7685 | # verify events in data set |
| 7686 | for p in capture: |
| 7687 | if p.haslayer(Data): |
| 7688 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 7689 | if ord(data[0][230]) == 10: |
| 7690 | self.verify_ipfix_bib(data, 1, self.pg0.remote_ip6n) |
| 7691 | elif ord(data[0][230]) == 6: |
| 7692 | self.verify_ipfix_nat64_ses(data, |
| 7693 | 1, |
| 7694 | self.pg0.remote_ip6n, |
| 7695 | self.pg1.remote_ip4, |
| 7696 | 25) |
| 7697 | else: |
| 7698 | self.logger.error(ppp("Unexpected or invalid packet: ", p)) |
| 7699 | |
| 7700 | # Delete |
| 7701 | self.pg_enable_capture(self.pg_interfaces) |
| 7702 | self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, |
| 7703 | self.nat_addr_n, |
| 7704 | is_add=0) |
| 7705 | self.vapi.cli("ipfix flush") # FIXME this should be an API call |
| 7706 | capture = self.pg3.get_capture(2) |
| 7707 | # verify events in data set |
| 7708 | for p in capture: |
| 7709 | self.assertTrue(p.haslayer(IPFIX)) |
| 7710 | self.assertEqual(p[IP].src, self.pg3.local_ip4) |
| 7711 | self.assertEqual(p[IP].dst, self.pg3.remote_ip4) |
| 7712 | self.assertEqual(p[UDP].sport, self.ipfix_src_port) |
| 7713 | self.assertEqual(p[UDP].dport, 4739) |
| 7714 | self.assertEqual(p[IPFIX].observationDomainID, |
| 7715 | self.ipfix_domain_id) |
| 7716 | if p.haslayer(Data): |
| 7717 | data = ipfix.decode_data_set(p.getlayer(Set)) |
| 7718 | if ord(data[0][230]) == 11: |
| 7719 | self.verify_ipfix_bib(data, 0, self.pg0.remote_ip6n) |
| 7720 | elif ord(data[0][230]) == 7: |
| 7721 | self.verify_ipfix_nat64_ses(data, |
| 7722 | 0, |
| 7723 | self.pg0.remote_ip6n, |
| 7724 | self.pg1.remote_ip4, |
| 7725 | 25) |
| 7726 | else: |
| 7727 | self.logger.error(ppp("Unexpected or invalid packet: ", p)) |
| 7728 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7729 | def nat64_get_ses_num(self): |
| 7730 | """ |
| 7731 | Return number of active NAT64 sessions. |
| 7732 | """ |
Matus Fabian | ab9a59c | 2017-08-16 05:37:36 -0700 | [diff] [blame] | 7733 | st = self.vapi.nat64_st_dump() |
| 7734 | return len(st) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7735 | |
| 7736 | def clear_nat64(self): |
| 7737 | """ |
| 7738 | Clear NAT64 configuration. |
| 7739 | """ |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7740 | self.vapi.nat_ipfix(enable=0, src_port=self.ipfix_src_port, |
| 7741 | domain_id=self.ipfix_domain_id) |
| 7742 | self.ipfix_src_port = 4739 |
| 7743 | self.ipfix_domain_id = 1 |
| 7744 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 7745 | self.vapi.nat_set_timeouts() |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7746 | |
| 7747 | interfaces = self.vapi.nat64_interface_dump() |
| 7748 | for intf in interfaces: |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 7749 | if intf.is_inside > 1: |
| 7750 | self.vapi.nat64_add_del_interface(intf.sw_if_index, |
| 7751 | 0, |
| 7752 | is_add=0) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7753 | self.vapi.nat64_add_del_interface(intf.sw_if_index, |
| 7754 | intf.is_inside, |
| 7755 | is_add=0) |
| 7756 | |
Matus Fabian | a431ad1 | 2018-01-04 04:03:14 -0800 | [diff] [blame] | 7757 | bib = self.vapi.nat64_bib_dump(255) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7758 | for bibe in bib: |
| 7759 | if bibe.is_static: |
| 7760 | self.vapi.nat64_add_del_static_bib(bibe.i_addr, |
| 7761 | bibe.o_addr, |
| 7762 | bibe.i_port, |
| 7763 | bibe.o_port, |
| 7764 | bibe.proto, |
| 7765 | bibe.vrf_id, |
| 7766 | is_add=0) |
| 7767 | |
| 7768 | adresses = self.vapi.nat64_pool_addr_dump() |
| 7769 | for addr in adresses: |
| 7770 | self.vapi.nat64_add_del_pool_addr_range(addr.address, |
| 7771 | addr.address, |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 7772 | vrf_id=addr.vrf_id, |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7773 | is_add=0) |
| 7774 | |
Matus Fabian | 428dc91 | 2017-06-21 06:15:18 -0700 | [diff] [blame] | 7775 | prefixes = self.vapi.nat64_prefix_dump() |
| 7776 | for prefix in prefixes: |
| 7777 | self.vapi.nat64_add_del_prefix(prefix.prefix, |
| 7778 | prefix.prefix_len, |
| 7779 | vrf_id=prefix.vrf_id, |
| 7780 | is_add=0) |
| 7781 | |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7782 | def tearDown(self): |
| 7783 | super(TestNAT64, self).tearDown() |
| 7784 | if not self.vpp_dead: |
| 7785 | self.logger.info(self.vapi.cli("show nat64 pool")) |
| 7786 | self.logger.info(self.vapi.cli("show nat64 interfaces")) |
Matus Fabian | 428dc91 | 2017-06-21 06:15:18 -0700 | [diff] [blame] | 7787 | self.logger.info(self.vapi.cli("show nat64 prefix")) |
Matus Fabian | ab9a59c | 2017-08-16 05:37:36 -0700 | [diff] [blame] | 7788 | self.logger.info(self.vapi.cli("show nat64 bib all")) |
| 7789 | self.logger.info(self.vapi.cli("show nat64 session table all")) |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 7790 | self.logger.info(self.vapi.cli("show nat virtual-reassembly")) |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 7791 | self.clear_nat64() |
| 7792 | |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7793 | |
| 7794 | class TestDSlite(MethodHolder): |
| 7795 | """ DS-Lite Test Cases """ |
| 7796 | |
| 7797 | @classmethod |
| 7798 | def setUpClass(cls): |
| 7799 | super(TestDSlite, cls).setUpClass() |
| 7800 | |
| 7801 | try: |
| 7802 | cls.nat_addr = '10.0.0.3' |
| 7803 | cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr) |
| 7804 | |
| 7805 | cls.create_pg_interfaces(range(2)) |
| 7806 | cls.pg0.admin_up() |
| 7807 | cls.pg0.config_ip4() |
| 7808 | cls.pg0.resolve_arp() |
| 7809 | cls.pg1.admin_up() |
| 7810 | cls.pg1.config_ip6() |
| 7811 | cls.pg1.generate_remote_hosts(2) |
| 7812 | cls.pg1.configure_ipv6_neighbors() |
| 7813 | |
| 7814 | except Exception: |
| 7815 | super(TestDSlite, cls).tearDownClass() |
| 7816 | raise |
| 7817 | |
| 7818 | def test_dslite(self): |
| 7819 | """ Test DS-Lite """ |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 7820 | nat_config = self.vapi.nat_show_config() |
| 7821 | self.assertEqual(0, nat_config.dslite_ce) |
| 7822 | |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7823 | self.vapi.dslite_add_del_pool_addr_range(self.nat_addr_n, |
| 7824 | self.nat_addr_n) |
| 7825 | aftr_ip4 = '192.0.0.1' |
| 7826 | aftr_ip4_n = socket.inet_pton(socket.AF_INET, aftr_ip4) |
| 7827 | aftr_ip6 = '2001:db8:85a3::8a2e:370:1' |
| 7828 | aftr_ip6_n = socket.inet_pton(socket.AF_INET6, aftr_ip6) |
| 7829 | self.vapi.dslite_set_aftr_addr(aftr_ip6_n, aftr_ip4_n) |
| 7830 | |
| 7831 | # UDP |
| 7832 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7833 | IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[0].ip6) / |
| 7834 | IP(dst=self.pg0.remote_ip4, src='192.168.1.1') / |
| 7835 | UDP(sport=20000, dport=10000)) |
| 7836 | self.pg1.add_stream(p) |
| 7837 | self.pg_enable_capture(self.pg_interfaces) |
| 7838 | self.pg_start() |
| 7839 | capture = self.pg0.get_capture(1) |
| 7840 | capture = capture[0] |
| 7841 | self.assertFalse(capture.haslayer(IPv6)) |
| 7842 | self.assertEqual(capture[IP].src, self.nat_addr) |
| 7843 | self.assertEqual(capture[IP].dst, self.pg0.remote_ip4) |
| 7844 | self.assertNotEqual(capture[UDP].sport, 20000) |
| 7845 | self.assertEqual(capture[UDP].dport, 10000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7846 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7847 | out_port = capture[UDP].sport |
| 7848 | |
| 7849 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7850 | IP(dst=self.nat_addr, src=self.pg0.remote_ip4) / |
| 7851 | UDP(sport=10000, dport=out_port)) |
| 7852 | self.pg0.add_stream(p) |
| 7853 | self.pg_enable_capture(self.pg_interfaces) |
| 7854 | self.pg_start() |
| 7855 | capture = self.pg1.get_capture(1) |
| 7856 | capture = capture[0] |
| 7857 | self.assertEqual(capture[IPv6].src, aftr_ip6) |
| 7858 | self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[0].ip6) |
| 7859 | self.assertEqual(capture[IP].src, self.pg0.remote_ip4) |
| 7860 | self.assertEqual(capture[IP].dst, '192.168.1.1') |
| 7861 | self.assertEqual(capture[UDP].sport, 10000) |
| 7862 | self.assertEqual(capture[UDP].dport, 20000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7863 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7864 | |
| 7865 | # TCP |
| 7866 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7867 | IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[1].ip6) / |
| 7868 | IP(dst=self.pg0.remote_ip4, src='192.168.1.1') / |
| 7869 | TCP(sport=20001, dport=10001)) |
| 7870 | self.pg1.add_stream(p) |
| 7871 | self.pg_enable_capture(self.pg_interfaces) |
| 7872 | self.pg_start() |
| 7873 | capture = self.pg0.get_capture(1) |
| 7874 | capture = capture[0] |
| 7875 | self.assertFalse(capture.haslayer(IPv6)) |
| 7876 | self.assertEqual(capture[IP].src, self.nat_addr) |
| 7877 | self.assertEqual(capture[IP].dst, self.pg0.remote_ip4) |
| 7878 | self.assertNotEqual(capture[TCP].sport, 20001) |
| 7879 | self.assertEqual(capture[TCP].dport, 10001) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7880 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7881 | out_port = capture[TCP].sport |
| 7882 | |
| 7883 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7884 | IP(dst=self.nat_addr, src=self.pg0.remote_ip4) / |
| 7885 | TCP(sport=10001, dport=out_port)) |
| 7886 | self.pg0.add_stream(p) |
| 7887 | self.pg_enable_capture(self.pg_interfaces) |
| 7888 | self.pg_start() |
| 7889 | capture = self.pg1.get_capture(1) |
| 7890 | capture = capture[0] |
| 7891 | self.assertEqual(capture[IPv6].src, aftr_ip6) |
| 7892 | self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6) |
| 7893 | self.assertEqual(capture[IP].src, self.pg0.remote_ip4) |
| 7894 | self.assertEqual(capture[IP].dst, '192.168.1.1') |
| 7895 | self.assertEqual(capture[TCP].sport, 10001) |
| 7896 | self.assertEqual(capture[TCP].dport, 20001) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7897 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7898 | |
| 7899 | # ICMP |
| 7900 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7901 | IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[1].ip6) / |
| 7902 | IP(dst=self.pg0.remote_ip4, src='192.168.1.1') / |
| 7903 | ICMP(id=4000, type='echo-request')) |
| 7904 | self.pg1.add_stream(p) |
| 7905 | self.pg_enable_capture(self.pg_interfaces) |
| 7906 | self.pg_start() |
| 7907 | capture = self.pg0.get_capture(1) |
| 7908 | capture = capture[0] |
| 7909 | self.assertFalse(capture.haslayer(IPv6)) |
| 7910 | self.assertEqual(capture[IP].src, self.nat_addr) |
| 7911 | self.assertEqual(capture[IP].dst, self.pg0.remote_ip4) |
| 7912 | self.assertNotEqual(capture[ICMP].id, 4000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7913 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7914 | out_id = capture[ICMP].id |
| 7915 | |
| 7916 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 7917 | IP(dst=self.nat_addr, src=self.pg0.remote_ip4) / |
| 7918 | ICMP(id=out_id, type='echo-reply')) |
| 7919 | self.pg0.add_stream(p) |
| 7920 | self.pg_enable_capture(self.pg_interfaces) |
| 7921 | self.pg_start() |
| 7922 | capture = self.pg1.get_capture(1) |
| 7923 | capture = capture[0] |
| 7924 | self.assertEqual(capture[IPv6].src, aftr_ip6) |
| 7925 | self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6) |
| 7926 | self.assertEqual(capture[IP].src, self.pg0.remote_ip4) |
| 7927 | self.assertEqual(capture[IP].dst, '192.168.1.1') |
| 7928 | self.assertEqual(capture[ICMP].id, 4000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 7929 | self.assert_packet_checksums_valid(capture) |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7930 | |
Matus Fabian | 331acc6 | 2017-12-08 03:38:51 -0800 | [diff] [blame] | 7931 | # ping DS-Lite AFTR tunnel endpoint address |
| 7932 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 7933 | IPv6(src=self.pg1.remote_hosts[1].ip6, dst=aftr_ip6) / |
| 7934 | ICMPv6EchoRequest()) |
| 7935 | self.pg1.add_stream(p) |
| 7936 | self.pg_enable_capture(self.pg_interfaces) |
| 7937 | self.pg_start() |
| 7938 | capture = self.pg1.get_capture(1) |
Matus Fabian | 331acc6 | 2017-12-08 03:38:51 -0800 | [diff] [blame] | 7939 | capture = capture[0] |
| 7940 | self.assertEqual(capture[IPv6].src, aftr_ip6) |
| 7941 | self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6) |
| 7942 | self.assertTrue(capture.haslayer(ICMPv6EchoReply)) |
| 7943 | |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 7944 | def tearDown(self): |
| 7945 | super(TestDSlite, self).tearDown() |
| 7946 | if not self.vpp_dead: |
| 7947 | self.logger.info(self.vapi.cli("show dslite pool")) |
| 7948 | self.logger.info( |
| 7949 | self.vapi.cli("show dslite aftr-tunnel-endpoint-address")) |
| 7950 | self.logger.info(self.vapi.cli("show dslite sessions")) |
| 7951 | |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 7952 | |
| 7953 | class TestDSliteCE(MethodHolder): |
| 7954 | """ DS-Lite CE Test Cases """ |
| 7955 | |
| 7956 | @classmethod |
| 7957 | def setUpConstants(cls): |
| 7958 | super(TestDSliteCE, cls).setUpConstants() |
| 7959 | cls.vpp_cmdline.extend(["nat", "{", "dslite ce", "}"]) |
| 7960 | |
| 7961 | @classmethod |
| 7962 | def setUpClass(cls): |
| 7963 | super(TestDSliteCE, cls).setUpClass() |
| 7964 | |
| 7965 | try: |
| 7966 | cls.create_pg_interfaces(range(2)) |
| 7967 | cls.pg0.admin_up() |
| 7968 | cls.pg0.config_ip4() |
| 7969 | cls.pg0.resolve_arp() |
| 7970 | cls.pg1.admin_up() |
| 7971 | cls.pg1.config_ip6() |
| 7972 | cls.pg1.generate_remote_hosts(1) |
| 7973 | cls.pg1.configure_ipv6_neighbors() |
| 7974 | |
| 7975 | except Exception: |
| 7976 | super(TestDSliteCE, cls).tearDownClass() |
| 7977 | raise |
| 7978 | |
| 7979 | def test_dslite_ce(self): |
| 7980 | """ Test DS-Lite CE """ |
| 7981 | |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 7982 | nat_config = self.vapi.nat_show_config() |
| 7983 | self.assertEqual(1, nat_config.dslite_ce) |
| 7984 | |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 7985 | b4_ip4 = '192.0.0.2' |
| 7986 | b4_ip4_n = socket.inet_pton(socket.AF_INET, b4_ip4) |
| 7987 | b4_ip6 = '2001:db8:62aa::375e:f4c1:1' |
| 7988 | b4_ip6_n = socket.inet_pton(socket.AF_INET6, b4_ip6) |
| 7989 | self.vapi.dslite_set_b4_addr(b4_ip6_n, b4_ip4_n) |
| 7990 | |
| 7991 | aftr_ip4 = '192.0.0.1' |
| 7992 | aftr_ip4_n = socket.inet_pton(socket.AF_INET, aftr_ip4) |
| 7993 | aftr_ip6 = '2001:db8:85a3::8a2e:370:1' |
| 7994 | aftr_ip6_n = socket.inet_pton(socket.AF_INET6, aftr_ip6) |
| 7995 | self.vapi.dslite_set_aftr_addr(aftr_ip6_n, aftr_ip4_n) |
| 7996 | |
| 7997 | self.vapi.ip_add_del_route(dst_address=aftr_ip6_n, |
| 7998 | dst_address_length=128, |
| 7999 | next_hop_address=self.pg1.remote_ip6n, |
| 8000 | next_hop_sw_if_index=self.pg1.sw_if_index, |
| 8001 | is_ipv6=1) |
| 8002 | |
| 8003 | # UDP encapsulation |
| 8004 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 8005 | IP(dst=self.pg1.remote_ip4, src=self.pg0.remote_ip4) / |
| 8006 | UDP(sport=10000, dport=20000)) |
| 8007 | self.pg0.add_stream(p) |
| 8008 | self.pg_enable_capture(self.pg_interfaces) |
| 8009 | self.pg_start() |
| 8010 | capture = self.pg1.get_capture(1) |
| 8011 | capture = capture[0] |
| 8012 | self.assertEqual(capture[IPv6].src, b4_ip6) |
| 8013 | self.assertEqual(capture[IPv6].dst, aftr_ip6) |
| 8014 | self.assertEqual(capture[IP].src, self.pg0.remote_ip4) |
| 8015 | self.assertEqual(capture[IP].dst, self.pg1.remote_ip4) |
| 8016 | self.assertEqual(capture[UDP].sport, 10000) |
| 8017 | self.assertEqual(capture[UDP].dport, 20000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 8018 | self.assert_packet_checksums_valid(capture) |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 8019 | |
| 8020 | # UDP decapsulation |
| 8021 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 8022 | IPv6(dst=b4_ip6, src=aftr_ip6) / |
| 8023 | IP(dst=self.pg0.remote_ip4, src=self.pg1.remote_ip4) / |
| 8024 | UDP(sport=20000, dport=10000)) |
| 8025 | self.pg1.add_stream(p) |
| 8026 | self.pg_enable_capture(self.pg_interfaces) |
| 8027 | self.pg_start() |
| 8028 | capture = self.pg0.get_capture(1) |
| 8029 | capture = capture[0] |
| 8030 | self.assertFalse(capture.haslayer(IPv6)) |
| 8031 | self.assertEqual(capture[IP].src, self.pg1.remote_ip4) |
| 8032 | self.assertEqual(capture[IP].dst, self.pg0.remote_ip4) |
| 8033 | self.assertEqual(capture[UDP].sport, 20000) |
| 8034 | self.assertEqual(capture[UDP].dport, 10000) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 8035 | self.assert_packet_checksums_valid(capture) |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 8036 | |
| 8037 | # ping DS-Lite B4 tunnel endpoint address |
| 8038 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 8039 | IPv6(src=self.pg1.remote_hosts[0].ip6, dst=b4_ip6) / |
| 8040 | ICMPv6EchoRequest()) |
| 8041 | self.pg1.add_stream(p) |
| 8042 | self.pg_enable_capture(self.pg_interfaces) |
| 8043 | self.pg_start() |
| 8044 | capture = self.pg1.get_capture(1) |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 8045 | capture = capture[0] |
| 8046 | self.assertEqual(capture[IPv6].src, b4_ip6) |
| 8047 | self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[0].ip6) |
| 8048 | self.assertTrue(capture.haslayer(ICMPv6EchoReply)) |
| 8049 | |
| 8050 | def tearDown(self): |
| 8051 | super(TestDSliteCE, self).tearDown() |
| 8052 | if not self.vpp_dead: |
| 8053 | self.logger.info( |
| 8054 | self.vapi.cli("show dslite aftr-tunnel-endpoint-address")) |
| 8055 | self.logger.info( |
| 8056 | self.vapi.cli("show dslite b4-tunnel-endpoint-address")) |
| 8057 | |
Matus Fabian | f2a23cc | 2018-01-22 03:41:53 -0800 | [diff] [blame] | 8058 | |
| 8059 | class TestNAT66(MethodHolder): |
| 8060 | """ NAT66 Test Cases """ |
| 8061 | |
| 8062 | @classmethod |
| 8063 | def setUpClass(cls): |
| 8064 | super(TestNAT66, cls).setUpClass() |
| 8065 | |
| 8066 | try: |
| 8067 | cls.nat_addr = 'fd01:ff::2' |
| 8068 | cls.nat_addr_n = socket.inet_pton(socket.AF_INET6, cls.nat_addr) |
| 8069 | |
| 8070 | cls.create_pg_interfaces(range(2)) |
| 8071 | cls.interfaces = list(cls.pg_interfaces) |
| 8072 | |
| 8073 | for i in cls.interfaces: |
| 8074 | i.admin_up() |
| 8075 | i.config_ip6() |
| 8076 | i.configure_ipv6_neighbors() |
| 8077 | |
| 8078 | except Exception: |
| 8079 | super(TestNAT66, cls).tearDownClass() |
| 8080 | raise |
| 8081 | |
| 8082 | def test_static(self): |
| 8083 | """ 1:1 NAT66 test """ |
| 8084 | self.vapi.nat66_add_del_interface(self.pg0.sw_if_index) |
| 8085 | self.vapi.nat66_add_del_interface(self.pg1.sw_if_index, is_inside=0) |
| 8086 | self.vapi.nat66_add_del_static_mapping(self.pg0.remote_ip6n, |
| 8087 | self.nat_addr_n) |
| 8088 | |
| 8089 | # in2out |
| 8090 | pkts = [] |
| 8091 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 8092 | IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) / |
| 8093 | TCP()) |
| 8094 | pkts.append(p) |
| 8095 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 8096 | IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) / |
| 8097 | UDP()) |
| 8098 | pkts.append(p) |
| 8099 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 8100 | IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) / |
| 8101 | ICMPv6EchoRequest()) |
| 8102 | pkts.append(p) |
| 8103 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 8104 | IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) / |
| 8105 | GRE() / IP() / TCP()) |
| 8106 | pkts.append(p) |
| 8107 | self.pg0.add_stream(pkts) |
| 8108 | self.pg_enable_capture(self.pg_interfaces) |
| 8109 | self.pg_start() |
| 8110 | capture = self.pg1.get_capture(len(pkts)) |
| 8111 | for packet in capture: |
| 8112 | try: |
| 8113 | self.assertEqual(packet[IPv6].src, self.nat_addr) |
| 8114 | self.assertEqual(packet[IPv6].dst, self.pg1.remote_ip6) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 8115 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | f2a23cc | 2018-01-22 03:41:53 -0800 | [diff] [blame] | 8116 | except: |
| 8117 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 8118 | raise |
| 8119 | |
| 8120 | # out2in |
| 8121 | pkts = [] |
| 8122 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 8123 | IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) / |
| 8124 | TCP()) |
| 8125 | pkts.append(p) |
| 8126 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 8127 | IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) / |
| 8128 | UDP()) |
| 8129 | pkts.append(p) |
| 8130 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 8131 | IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) / |
| 8132 | ICMPv6EchoReply()) |
| 8133 | pkts.append(p) |
| 8134 | p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / |
| 8135 | IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) / |
| 8136 | GRE() / IP() / TCP()) |
| 8137 | pkts.append(p) |
| 8138 | self.pg1.add_stream(pkts) |
| 8139 | self.pg_enable_capture(self.pg_interfaces) |
| 8140 | self.pg_start() |
| 8141 | capture = self.pg0.get_capture(len(pkts)) |
| 8142 | for packet in capture: |
| 8143 | try: |
| 8144 | self.assertEqual(packet[IPv6].src, self.pg1.remote_ip6) |
| 8145 | self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6) |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 8146 | self.assert_packet_checksums_valid(packet) |
Matus Fabian | f2a23cc | 2018-01-22 03:41:53 -0800 | [diff] [blame] | 8147 | except: |
| 8148 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 8149 | raise |
| 8150 | |
| 8151 | sm = self.vapi.nat66_static_mapping_dump() |
| 8152 | self.assertEqual(len(sm), 1) |
| 8153 | self.assertEqual(sm[0].total_pkts, 8) |
| 8154 | |
Juraj Sloboda | 9341e34 | 2018-04-13 12:00:46 +0200 | [diff] [blame] | 8155 | def test_check_no_translate(self): |
| 8156 | """ NAT66 translate only when egress interface is outside interface """ |
| 8157 | self.vapi.nat66_add_del_interface(self.pg0.sw_if_index) |
| 8158 | self.vapi.nat66_add_del_interface(self.pg1.sw_if_index) |
| 8159 | self.vapi.nat66_add_del_static_mapping(self.pg0.remote_ip6n, |
| 8160 | self.nat_addr_n) |
| 8161 | |
| 8162 | # in2out |
| 8163 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 8164 | IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) / |
| 8165 | UDP()) |
| 8166 | self.pg0.add_stream([p]) |
| 8167 | self.pg_enable_capture(self.pg_interfaces) |
| 8168 | self.pg_start() |
| 8169 | capture = self.pg1.get_capture(1) |
| 8170 | packet = capture[0] |
| 8171 | try: |
| 8172 | self.assertEqual(packet[IPv6].src, self.pg0.remote_ip6) |
| 8173 | self.assertEqual(packet[IPv6].dst, self.pg1.remote_ip6) |
| 8174 | except: |
| 8175 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
| 8176 | raise |
| 8177 | |
Matus Fabian | f2a23cc | 2018-01-22 03:41:53 -0800 | [diff] [blame] | 8178 | def clear_nat66(self): |
| 8179 | """ |
| 8180 | Clear NAT66 configuration. |
| 8181 | """ |
| 8182 | interfaces = self.vapi.nat66_interface_dump() |
| 8183 | for intf in interfaces: |
| 8184 | self.vapi.nat66_add_del_interface(intf.sw_if_index, |
| 8185 | intf.is_inside, |
| 8186 | is_add=0) |
| 8187 | |
| 8188 | static_mappings = self.vapi.nat66_static_mapping_dump() |
| 8189 | for sm in static_mappings: |
| 8190 | self.vapi.nat66_add_del_static_mapping(sm.local_ip_address, |
| 8191 | sm.external_ip_address, |
| 8192 | sm.vrf_id, |
| 8193 | is_add=0) |
| 8194 | |
| 8195 | def tearDown(self): |
| 8196 | super(TestNAT66, self).tearDown() |
| 8197 | if not self.vpp_dead: |
| 8198 | self.logger.info(self.vapi.cli("show nat66 interfaces")) |
| 8199 | self.logger.info(self.vapi.cli("show nat66 static mappings")) |
| 8200 | self.clear_nat66() |
| 8201 | |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 8202 | |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 8203 | if __name__ == '__main__': |
| 8204 | unittest.main(testRunner=VppTestRunner) |