blob: 0d74cb60f87a3834832bcd518b04737bf1e5fd52 [file] [log] [blame]
Matus Fabiande886752016-12-07 03:38:19 -08001#!/usr/bin/env python
2
3import socket
4import unittest
Matus Fabianeea28d72017-01-13 04:15:54 -08005import struct
Matus Fabianefcd1e92017-08-15 06:59:19 -07006import random
Matus Fabiande886752016-12-07 03:38:19 -08007
Martin Gálik977c1cb2017-03-30 23:21:51 -07008from framework import VppTestCase, VppTestRunner, running_extended_tests
Matus Fabiande886752016-12-07 03:38:19 -08009from scapy.layers.inet import IP, TCP, UDP, ICMP
Juraj Slobodab33f4132017-02-08 23:54:21 -080010from scapy.layers.inet import IPerror, TCPerror, UDPerror, ICMPerror
Juraj Slobodac746a152018-07-09 02:36:37 +020011from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest, ICMPv6EchoReply, \
Paul Vinciguerra978aa642018-11-24 22:19:12 -080012 ICMPv6ND_NS, ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, fragment6
Matus Fabianefcd1e92017-08-15 06:59:19 -070013from scapy.layers.inet6 import ICMPv6DestUnreach, IPerror6, IPv6ExtHdrFragment
Matus Fabian328dbc82017-06-19 04:28:04 -070014from scapy.layers.l2 import Ether, ARP, GRE
Matus Fabianeea28d72017-01-13 04:15:54 -080015from scapy.data import IP_PROTOS
Matus Fabianefcd1e92017-08-15 06:59:19 -070016from scapy.packet import bind_layers, Raw
Klement Sekera9225dee2016-12-12 08:36:58 +010017from util import ppp
Matus Fabianeea28d72017-01-13 04:15:54 -080018from ipfix import IPFIX, Set, Template, Data, IPFIXDecoder
Martin Gálik977c1cb2017-03-30 23:21:51 -070019from time import sleep
Matus Fabian704018c2017-09-04 02:17:18 -070020from util import ip4_range
Ole Troan8006c6a2018-12-17 12:02:26 +010021from vpp_papi import mac_pton
Matus Fabianad1f3e12018-11-28 21:26:34 -080022from syslog_rfc5424_parser import SyslogMessage, ParseError
23from syslog_rfc5424_parser.constants import SyslogFacility, SyslogSeverity
24from vpp_papi_provider import SYSLOG_SEVERITY
Ole Troan7f991832018-12-06 17:35:12 +010025from io import BytesIO
Neale Ranns37029302018-08-10 05:30:06 -070026from vpp_papi import VppEnum
Matus Fabiande886752016-12-07 03:38:19 -080027
28
Martin Gálikd7f75cd2017-03-27 06:02:47 -070029class MethodHolder(VppTestCase):
Matus Fabian2ba92e32017-08-21 07:05:03 -070030 """ NAT create capture and verify method holder """
Matus Fabiande886752016-12-07 03:38:19 -080031
Matus Fabiana6110b62018-06-13 05:39:07 -070032 def clear_nat44(self):
33 """
34 Clear NAT44 configuration.
35 """
36 if hasattr(self, 'pg7') and hasattr(self, 'pg8'):
37 # I found no elegant way to do this
38 self.vapi.ip_add_del_route(
39 dst_address=self.pg7.remote_ip4n,
40 dst_address_length=32,
41 next_hop_address=self.pg7.remote_ip4n,
42 next_hop_sw_if_index=self.pg7.sw_if_index,
43 is_add=0)
44 self.vapi.ip_add_del_route(
45 dst_address=self.pg8.remote_ip4n,
46 dst_address_length=32,
47 next_hop_address=self.pg8.remote_ip4n,
48 next_hop_sw_if_index=self.pg8.sw_if_index,
49 is_add=0)
50
51 for intf in [self.pg7, self.pg8]:
Neale Ranns37029302018-08-10 05:30:06 -070052 self.vapi.ip_neighbor_add_del(
53 intf.sw_if_index,
54 intf.remote_mac,
55 intf.remote_ip4,
56 flags=(VppEnum.vl_api_ip_neighbor_flags_t.
57 IP_API_NEIGHBOR_FLAG_STATIC),
58 is_add=0)
Matus Fabiana6110b62018-06-13 05:39:07 -070059
60 if self.pg7.has_ip4_config:
61 self.pg7.unconfig_ip4()
62
63 self.vapi.nat44_forwarding_enable_disable(0)
64
65 interfaces = self.vapi.nat44_interface_addr_dump()
66 for intf in interfaces:
67 self.vapi.nat44_add_interface_addr(intf.sw_if_index,
68 twice_nat=intf.twice_nat,
69 is_add=0)
70
71 self.vapi.nat_ipfix(enable=0, src_port=self.ipfix_src_port,
72 domain_id=self.ipfix_domain_id)
73 self.ipfix_src_port = 4739
74 self.ipfix_domain_id = 1
75
Matus Fabianad1f3e12018-11-28 21:26:34 -080076 self.vapi.syslog_set_filter(SYSLOG_SEVERITY.EMERG)
77
Matus Fabiana6110b62018-06-13 05:39:07 -070078 interfaces = self.vapi.nat44_interface_dump()
79 for intf in interfaces:
80 if intf.is_inside > 1:
81 self.vapi.nat44_interface_add_del_feature(intf.sw_if_index,
82 0,
83 is_add=0)
84 self.vapi.nat44_interface_add_del_feature(intf.sw_if_index,
85 intf.is_inside,
86 is_add=0)
87
88 interfaces = self.vapi.nat44_interface_output_feature_dump()
89 for intf in interfaces:
90 self.vapi.nat44_interface_add_del_output_feature(intf.sw_if_index,
91 intf.is_inside,
92 is_add=0)
93
94 static_mappings = self.vapi.nat44_static_mapping_dump()
95 for sm in static_mappings:
96 self.vapi.nat44_add_del_static_mapping(
97 sm.local_ip_address,
98 sm.external_ip_address,
99 local_port=sm.local_port,
100 external_port=sm.external_port,
101 addr_only=sm.addr_only,
102 vrf_id=sm.vrf_id,
103 protocol=sm.protocol,
104 twice_nat=sm.twice_nat,
105 self_twice_nat=sm.self_twice_nat,
106 out2in_only=sm.out2in_only,
107 tag=sm.tag,
108 external_sw_if_index=sm.external_sw_if_index,
109 is_add=0)
110
111 lb_static_mappings = self.vapi.nat44_lb_static_mapping_dump()
112 for lb_sm in lb_static_mappings:
113 self.vapi.nat44_add_del_lb_static_mapping(
114 lb_sm.external_addr,
115 lb_sm.external_port,
116 lb_sm.protocol,
Matus Fabiana6110b62018-06-13 05:39:07 -0700117 twice_nat=lb_sm.twice_nat,
118 self_twice_nat=lb_sm.self_twice_nat,
119 out2in_only=lb_sm.out2in_only,
120 tag=lb_sm.tag,
121 is_add=0,
122 local_num=0,
123 locals=[])
124
125 identity_mappings = self.vapi.nat44_identity_mapping_dump()
126 for id_m in identity_mappings:
127 self.vapi.nat44_add_del_identity_mapping(
128 addr_only=id_m.addr_only,
129 ip=id_m.ip_address,
130 port=id_m.port,
131 sw_if_index=id_m.sw_if_index,
132 vrf_id=id_m.vrf_id,
133 protocol=id_m.protocol,
134 is_add=0)
135
136 adresses = self.vapi.nat44_address_dump()
137 for addr in adresses:
138 self.vapi.nat44_add_del_address_range(addr.ip_address,
139 addr.ip_address,
140 twice_nat=addr.twice_nat,
141 is_add=0)
142
143 self.vapi.nat_set_reass()
144 self.vapi.nat_set_reass(is_ip6=1)
Matus Fabian68ba8802018-08-08 05:52:47 -0700145 self.verify_no_nat44_user()
Matus Fabian878c6462018-08-23 00:33:35 -0700146 self.vapi.nat_set_timeouts()
Matus Fabian5d28c7a2018-09-04 03:55:45 -0700147 self.vapi.nat_set_addr_and_port_alloc_alg()
Matus Fabianbb4e0222018-09-13 02:36:25 -0700148 self.vapi.nat_set_mss_clamping()
Matus Fabiana6110b62018-06-13 05:39:07 -0700149
150 def nat44_add_static_mapping(self, local_ip, external_ip='0.0.0.0',
151 local_port=0, external_port=0, vrf_id=0,
152 is_add=1, external_sw_if_index=0xFFFFFFFF,
153 proto=0, twice_nat=0, self_twice_nat=0,
154 out2in_only=0, tag=""):
155 """
156 Add/delete NAT44 static mapping
157
158 :param local_ip: Local IP address
159 :param external_ip: External IP address
160 :param local_port: Local port number (Optional)
161 :param external_port: External port number (Optional)
162 :param vrf_id: VRF ID (Default 0)
163 :param is_add: 1 if add, 0 if delete (Default add)
164 :param external_sw_if_index: External interface instead of IP address
165 :param proto: IP protocol (Mandatory if port specified)
166 :param twice_nat: 1 if translate external host address and port
167 :param self_twice_nat: 1 if translate external host address and port
168 whenever external host address equals
169 local address of internal host
170 :param out2in_only: if 1 rule is matching only out2in direction
171 :param tag: Opaque string tag
172 """
173 addr_only = 1
174 if local_port and external_port:
175 addr_only = 0
176 l_ip = socket.inet_pton(socket.AF_INET, local_ip)
177 e_ip = socket.inet_pton(socket.AF_INET, external_ip)
178 self.vapi.nat44_add_del_static_mapping(
179 l_ip,
180 e_ip,
181 external_sw_if_index,
182 local_port,
183 external_port,
184 addr_only,
185 vrf_id,
186 proto,
187 twice_nat,
188 self_twice_nat,
189 out2in_only,
190 tag,
191 is_add)
192
193 def nat44_add_address(self, ip, is_add=1, vrf_id=0xFFFFFFFF, twice_nat=0):
194 """
195 Add/delete NAT44 address
196
197 :param ip: IP address
198 :param is_add: 1 if add, 0 if delete (Default add)
199 :param twice_nat: twice NAT address for extenal hosts
200 """
201 nat_addr = socket.inet_pton(socket.AF_INET, ip)
202 self.vapi.nat44_add_del_address_range(nat_addr, nat_addr, is_add,
203 vrf_id=vrf_id,
204 twice_nat=twice_nat)
205
Juraj Slobodacba69362017-12-19 02:09:32 +0100206 def create_stream_in(self, in_if, out_if, dst_ip=None, ttl=64):
Matus Fabiande886752016-12-07 03:38:19 -0800207 """
208 Create packet stream for inside network
209
210 :param in_if: Inside interface
211 :param out_if: Outside interface
Juraj Slobodacba69362017-12-19 02:09:32 +0100212 :param dst_ip: Destination address
Juraj Slobodab33f4132017-02-08 23:54:21 -0800213 :param ttl: TTL of generated packets
Matus Fabiande886752016-12-07 03:38:19 -0800214 """
Juraj Slobodacba69362017-12-19 02:09:32 +0100215 if dst_ip is None:
216 dst_ip = out_if.remote_ip4
217
Matus Fabiande886752016-12-07 03:38:19 -0800218 pkts = []
219 # TCP
220 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
Juraj Slobodacba69362017-12-19 02:09:32 +0100221 IP(src=in_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Matus Fabian06596c52017-06-06 04:53:28 -0700222 TCP(sport=self.tcp_port_in, dport=20))
Matus Fabiande886752016-12-07 03:38:19 -0800223 pkts.append(p)
224
225 # UDP
226 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
Juraj Slobodacba69362017-12-19 02:09:32 +0100227 IP(src=in_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Matus Fabian06596c52017-06-06 04:53:28 -0700228 UDP(sport=self.udp_port_in, dport=20))
Matus Fabiande886752016-12-07 03:38:19 -0800229 pkts.append(p)
230
231 # ICMP
232 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
Juraj Slobodacba69362017-12-19 02:09:32 +0100233 IP(src=in_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Matus Fabiande886752016-12-07 03:38:19 -0800234 ICMP(id=self.icmp_id_in, type='echo-request'))
235 pkts.append(p)
236
237 return pkts
238
Matus Fabian428dc912017-06-21 06:15:18 -0700239 def compose_ip6(self, ip4, pref, plen):
240 """
241 Compose IPv4-embedded IPv6 addresses
242
243 :param ip4: IPv4 address
244 :param pref: IPv6 prefix
245 :param plen: IPv6 prefix length
246 :returns: IPv4-embedded IPv6 addresses
247 """
248 pref_n = list(socket.inet_pton(socket.AF_INET6, pref))
249 ip4_n = list(socket.inet_pton(socket.AF_INET, ip4))
250 if plen == 32:
251 pref_n[4] = ip4_n[0]
252 pref_n[5] = ip4_n[1]
253 pref_n[6] = ip4_n[2]
254 pref_n[7] = ip4_n[3]
255 elif plen == 40:
256 pref_n[5] = ip4_n[0]
257 pref_n[6] = ip4_n[1]
258 pref_n[7] = ip4_n[2]
259 pref_n[9] = ip4_n[3]
260 elif plen == 48:
261 pref_n[6] = ip4_n[0]
262 pref_n[7] = ip4_n[1]
263 pref_n[9] = ip4_n[2]
264 pref_n[10] = ip4_n[3]
265 elif plen == 56:
266 pref_n[7] = ip4_n[0]
267 pref_n[9] = ip4_n[1]
268 pref_n[10] = ip4_n[2]
269 pref_n[11] = ip4_n[3]
270 elif plen == 64:
271 pref_n[9] = ip4_n[0]
272 pref_n[10] = ip4_n[1]
273 pref_n[11] = ip4_n[2]
274 pref_n[12] = ip4_n[3]
275 elif plen == 96:
276 pref_n[12] = ip4_n[0]
277 pref_n[13] = ip4_n[1]
278 pref_n[14] = ip4_n[2]
279 pref_n[15] = ip4_n[3]
280 return socket.inet_ntop(socket.AF_INET6, ''.join(pref_n))
281
Juraj Slobodacba69362017-12-19 02:09:32 +0100282 def extract_ip4(self, ip6, plen):
283 """
284 Extract IPv4 address embedded in IPv6 addresses
285
286 :param ip6: IPv6 address
287 :param plen: IPv6 prefix length
288 :returns: extracted IPv4 address
289 """
290 ip6_n = list(socket.inet_pton(socket.AF_INET6, ip6))
291 ip4_n = [None] * 4
292 if plen == 32:
293 ip4_n[0] = ip6_n[4]
294 ip4_n[1] = ip6_n[5]
295 ip4_n[2] = ip6_n[6]
296 ip4_n[3] = ip6_n[7]
297 elif plen == 40:
298 ip4_n[0] = ip6_n[5]
299 ip4_n[1] = ip6_n[6]
300 ip4_n[2] = ip6_n[7]
301 ip4_n[3] = ip6_n[9]
302 elif plen == 48:
303 ip4_n[0] = ip6_n[6]
304 ip4_n[1] = ip6_n[7]
305 ip4_n[2] = ip6_n[9]
306 ip4_n[3] = ip6_n[10]
307 elif plen == 56:
308 ip4_n[0] = ip6_n[7]
309 ip4_n[1] = ip6_n[9]
310 ip4_n[2] = ip6_n[10]
311 ip4_n[3] = ip6_n[11]
312 elif plen == 64:
313 ip4_n[0] = ip6_n[9]
314 ip4_n[1] = ip6_n[10]
315 ip4_n[2] = ip6_n[11]
316 ip4_n[3] = ip6_n[12]
317 elif plen == 96:
318 ip4_n[0] = ip6_n[12]
319 ip4_n[1] = ip6_n[13]
320 ip4_n[2] = ip6_n[14]
321 ip4_n[3] = ip6_n[15]
322 return socket.inet_ntop(socket.AF_INET, ''.join(ip4_n))
323
Matus Fabian428dc912017-06-21 06:15:18 -0700324 def create_stream_in_ip6(self, in_if, out_if, hlim=64, pref=None, plen=0):
Matus Fabian06596c52017-06-06 04:53:28 -0700325 """
326 Create IPv6 packet stream for inside network
327
328 :param in_if: Inside interface
329 :param out_if: Outside interface
330 :param ttl: Hop Limit of generated packets
Matus Fabian428dc912017-06-21 06:15:18 -0700331 :param pref: NAT64 prefix
332 :param plen: NAT64 prefix length
Matus Fabian06596c52017-06-06 04:53:28 -0700333 """
334 pkts = []
Matus Fabian428dc912017-06-21 06:15:18 -0700335 if pref is None:
336 dst = ''.join(['64:ff9b::', out_if.remote_ip4])
337 else:
338 dst = self.compose_ip6(out_if.remote_ip4, pref, plen)
339
Matus Fabian06596c52017-06-06 04:53:28 -0700340 # TCP
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 TCP(sport=self.tcp_port_in, dport=20))
344 pkts.append(p)
345
346 # UDP
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 UDP(sport=self.udp_port_in, dport=20))
350 pkts.append(p)
351
352 # ICMP
353 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
354 IPv6(src=in_if.remote_ip6, dst=dst, hlim=hlim) /
355 ICMPv6EchoRequest(id=self.icmp_id_in))
356 pkts.append(p)
357
358 return pkts
359
Juraj Sloboda7b929792017-11-23 13:20:48 +0100360 def create_stream_out(self, out_if, dst_ip=None, ttl=64,
361 use_inside_ports=False):
Matus Fabiande886752016-12-07 03:38:19 -0800362 """
363 Create packet stream for outside network
364
365 :param out_if: Outside interface
Matus Fabian2ba92e32017-08-21 07:05:03 -0700366 :param dst_ip: Destination IP address (Default use global NAT address)
Juraj Slobodab33f4132017-02-08 23:54:21 -0800367 :param ttl: TTL of generated packets
Juraj Sloboda7b929792017-11-23 13:20:48 +0100368 :param use_inside_ports: Use inside NAT ports as destination ports
369 instead of outside ports
Matus Fabiande886752016-12-07 03:38:19 -0800370 """
371 if dst_ip is None:
Matus Fabian2ba92e32017-08-21 07:05:03 -0700372 dst_ip = self.nat_addr
Juraj Sloboda7b929792017-11-23 13:20:48 +0100373 if not use_inside_ports:
374 tcp_port = self.tcp_port_out
375 udp_port = self.udp_port_out
376 icmp_id = self.icmp_id_out
377 else:
378 tcp_port = self.tcp_port_in
379 udp_port = self.udp_port_in
380 icmp_id = self.icmp_id_in
Matus Fabiande886752016-12-07 03:38:19 -0800381 pkts = []
382 # TCP
383 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
Juraj Slobodab33f4132017-02-08 23:54:21 -0800384 IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Juraj Sloboda7b929792017-11-23 13:20:48 +0100385 TCP(dport=tcp_port, sport=20))
Matus Fabiande886752016-12-07 03:38:19 -0800386 pkts.append(p)
387
388 # UDP
389 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
Juraj Slobodab33f4132017-02-08 23:54:21 -0800390 IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Juraj Sloboda7b929792017-11-23 13:20:48 +0100391 UDP(dport=udp_port, sport=20))
Matus Fabiande886752016-12-07 03:38:19 -0800392 pkts.append(p)
393
394 # ICMP
395 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
Juraj Slobodab33f4132017-02-08 23:54:21 -0800396 IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Juraj Sloboda7b929792017-11-23 13:20:48 +0100397 ICMP(id=icmp_id, type='echo-reply'))
Matus Fabiande886752016-12-07 03:38:19 -0800398 pkts.append(p)
399
400 return pkts
401
Juraj Slobodacba69362017-12-19 02:09:32 +0100402 def create_stream_out_ip6(self, out_if, src_ip, dst_ip, hl=64):
403 """
404 Create packet stream for outside network
405
406 :param out_if: Outside interface
407 :param dst_ip: Destination IP address (Default use global NAT address)
408 :param hl: HL of generated packets
409 """
410 pkts = []
411 # TCP
412 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
413 IPv6(src=src_ip, dst=dst_ip, hlim=hl) /
414 TCP(dport=self.tcp_port_out, sport=20))
415 pkts.append(p)
416
417 # UDP
418 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
419 IPv6(src=src_ip, dst=dst_ip, hlim=hl) /
420 UDP(dport=self.udp_port_out, sport=20))
421 pkts.append(p)
422
423 # ICMP
424 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
425 IPv6(src=src_ip, dst=dst_ip, hlim=hl) /
426 ICMPv6EchoReply(id=self.icmp_id_out))
427 pkts.append(p)
428
429 return pkts
430
Matus Fabiande886752016-12-07 03:38:19 -0800431 def verify_capture_out(self, capture, nat_ip=None, same_port=False,
Matus Fabian05ca4a32018-09-04 23:45:13 -0700432 dst_ip=None, is_ip6=False):
Matus Fabiande886752016-12-07 03:38:19 -0800433 """
434 Verify captured packets on outside network
435
436 :param capture: Captured packets
Matus Fabian2ba92e32017-08-21 07:05:03 -0700437 :param nat_ip: Translated IP address (Default use global NAT address)
Matus Fabiande886752016-12-07 03:38:19 -0800438 :param same_port: Sorce port number is not translated (Default False)
Matus Fabian06596c52017-06-06 04:53:28 -0700439 :param dst_ip: Destination IP address (Default do not verify)
Juraj Slobodacba69362017-12-19 02:09:32 +0100440 :param is_ip6: If L3 protocol is IPv6 (Default False)
Matus Fabiande886752016-12-07 03:38:19 -0800441 """
Juraj Slobodacba69362017-12-19 02:09:32 +0100442 if is_ip6:
443 IP46 = IPv6
444 ICMP46 = ICMPv6EchoRequest
445 else:
446 IP46 = IP
447 ICMP46 = ICMP
Matus Fabiande886752016-12-07 03:38:19 -0800448 if nat_ip is None:
Matus Fabian2ba92e32017-08-21 07:05:03 -0700449 nat_ip = self.nat_addr
Matus Fabiande886752016-12-07 03:38:19 -0800450 for packet in capture:
451 try:
Juraj Slobodacba69362017-12-19 02:09:32 +0100452 if not is_ip6:
Klement Sekerad81ae412018-05-16 10:52:54 +0200453 self.assert_packet_checksums_valid(packet)
Juraj Slobodacba69362017-12-19 02:09:32 +0100454 self.assertEqual(packet[IP46].src, nat_ip)
Matus Fabian06596c52017-06-06 04:53:28 -0700455 if dst_ip is not None:
Juraj Slobodacba69362017-12-19 02:09:32 +0100456 self.assertEqual(packet[IP46].dst, dst_ip)
Matus Fabiande886752016-12-07 03:38:19 -0800457 if packet.haslayer(TCP):
458 if same_port:
459 self.assertEqual(packet[TCP].sport, self.tcp_port_in)
460 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100461 self.assertNotEqual(
462 packet[TCP].sport, self.tcp_port_in)
Matus Fabiande886752016-12-07 03:38:19 -0800463 self.tcp_port_out = packet[TCP].sport
Klement Sekerad81ae412018-05-16 10:52:54 +0200464 self.assert_packet_checksums_valid(packet)
Matus Fabiande886752016-12-07 03:38:19 -0800465 elif packet.haslayer(UDP):
466 if same_port:
467 self.assertEqual(packet[UDP].sport, self.udp_port_in)
468 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100469 self.assertNotEqual(
470 packet[UDP].sport, self.udp_port_in)
Matus Fabiande886752016-12-07 03:38:19 -0800471 self.udp_port_out = packet[UDP].sport
472 else:
473 if same_port:
Juraj Slobodacba69362017-12-19 02:09:32 +0100474 self.assertEqual(packet[ICMP46].id, self.icmp_id_in)
Matus Fabiande886752016-12-07 03:38:19 -0800475 else:
Juraj Slobodacba69362017-12-19 02:09:32 +0100476 self.assertNotEqual(packet[ICMP46].id, self.icmp_id_in)
477 self.icmp_id_out = packet[ICMP46].id
Klement Sekerad81ae412018-05-16 10:52:54 +0200478 self.assert_packet_checksums_valid(packet)
Matus Fabiande886752016-12-07 03:38:19 -0800479 except:
Klement Sekera9225dee2016-12-12 08:36:58 +0100480 self.logger.error(ppp("Unexpected or invalid packet "
481 "(outside network):", packet))
Matus Fabiande886752016-12-07 03:38:19 -0800482 raise
483
Juraj Slobodacba69362017-12-19 02:09:32 +0100484 def verify_capture_out_ip6(self, capture, nat_ip, same_port=False,
Matus Fabian05ca4a32018-09-04 23:45:13 -0700485 dst_ip=None):
Juraj Slobodacba69362017-12-19 02:09:32 +0100486 """
487 Verify captured packets on outside network
488
489 :param capture: Captured packets
490 :param nat_ip: Translated IP address
491 :param same_port: Sorce port number is not translated (Default False)
Juraj Slobodacba69362017-12-19 02:09:32 +0100492 :param dst_ip: Destination IP address (Default do not verify)
493 """
Matus Fabian05ca4a32018-09-04 23:45:13 -0700494 return self.verify_capture_out(capture, nat_ip, same_port, dst_ip,
495 True)
Juraj Slobodacba69362017-12-19 02:09:32 +0100496
Matus Fabian05ca4a32018-09-04 23:45:13 -0700497 def verify_capture_in(self, capture, in_if):
Matus Fabiande886752016-12-07 03:38:19 -0800498 """
499 Verify captured packets on inside network
500
501 :param capture: Captured packets
502 :param in_if: Inside interface
Matus Fabiande886752016-12-07 03:38:19 -0800503 """
Matus Fabiande886752016-12-07 03:38:19 -0800504 for packet in capture:
505 try:
Klement Sekerad81ae412018-05-16 10:52:54 +0200506 self.assert_packet_checksums_valid(packet)
Matus Fabiande886752016-12-07 03:38:19 -0800507 self.assertEqual(packet[IP].dst, in_if.remote_ip4)
508 if packet.haslayer(TCP):
509 self.assertEqual(packet[TCP].dport, self.tcp_port_in)
510 elif packet.haslayer(UDP):
511 self.assertEqual(packet[UDP].dport, self.udp_port_in)
512 else:
513 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
514 except:
Klement Sekera9225dee2016-12-12 08:36:58 +0100515 self.logger.error(ppp("Unexpected or invalid packet "
516 "(inside network):", packet))
Matus Fabiande886752016-12-07 03:38:19 -0800517 raise
518
Matus Fabian05ca4a32018-09-04 23:45:13 -0700519 def verify_capture_in_ip6(self, capture, src_ip, dst_ip):
Matus Fabian06596c52017-06-06 04:53:28 -0700520 """
521 Verify captured IPv6 packets on inside network
522
523 :param capture: Captured packets
524 :param src_ip: Source IP
525 :param dst_ip: Destination IP address
Matus Fabian06596c52017-06-06 04:53:28 -0700526 """
Matus Fabian06596c52017-06-06 04:53:28 -0700527 for packet in capture:
528 try:
529 self.assertEqual(packet[IPv6].src, src_ip)
530 self.assertEqual(packet[IPv6].dst, dst_ip)
Klement Sekerad81ae412018-05-16 10:52:54 +0200531 self.assert_packet_checksums_valid(packet)
Matus Fabian06596c52017-06-06 04:53:28 -0700532 if packet.haslayer(TCP):
533 self.assertEqual(packet[TCP].dport, self.tcp_port_in)
534 elif packet.haslayer(UDP):
535 self.assertEqual(packet[UDP].dport, self.udp_port_in)
536 else:
537 self.assertEqual(packet[ICMPv6EchoReply].id,
538 self.icmp_id_in)
539 except:
540 self.logger.error(ppp("Unexpected or invalid packet "
541 "(inside network):", packet))
542 raise
543
Matus Fabian675a69c2017-01-18 01:46:01 -0800544 def verify_capture_no_translation(self, capture, ingress_if, egress_if):
545 """
546 Verify captured packet that don't have to be translated
547
548 :param capture: Captured packets
549 :param ingress_if: Ingress interface
550 :param egress_if: Egress interface
551 """
552 for packet in capture:
553 try:
554 self.assertEqual(packet[IP].src, ingress_if.remote_ip4)
555 self.assertEqual(packet[IP].dst, egress_if.remote_ip4)
556 if packet.haslayer(TCP):
557 self.assertEqual(packet[TCP].sport, self.tcp_port_in)
558 elif packet.haslayer(UDP):
559 self.assertEqual(packet[UDP].sport, self.udp_port_in)
560 else:
561 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
562 except:
563 self.logger.error(ppp("Unexpected or invalid packet "
564 "(inside network):", packet))
565 raise
566
Juraj Slobodab33f4132017-02-08 23:54:21 -0800567 def verify_capture_out_with_icmp_errors(self, capture, src_ip=None,
Matus Fabian05ca4a32018-09-04 23:45:13 -0700568 icmp_type=11):
Juraj Slobodab33f4132017-02-08 23:54:21 -0800569 """
570 Verify captured packets with ICMP errors on outside network
571
572 :param capture: Captured packets
573 :param src_ip: Translated IP address or IP address of VPP
Matus Fabian2ba92e32017-08-21 07:05:03 -0700574 (Default use global NAT address)
Juraj Slobodab33f4132017-02-08 23:54:21 -0800575 :param icmp_type: Type of error ICMP packet
576 we are expecting (Default 11)
577 """
578 if src_ip is None:
Matus Fabian2ba92e32017-08-21 07:05:03 -0700579 src_ip = self.nat_addr
Juraj Slobodab33f4132017-02-08 23:54:21 -0800580 for packet in capture:
581 try:
582 self.assertEqual(packet[IP].src, src_ip)
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800583 self.assertEqual(packet.haslayer(ICMP), 1)
Juraj Slobodab33f4132017-02-08 23:54:21 -0800584 icmp = packet[ICMP]
585 self.assertEqual(icmp.type, icmp_type)
586 self.assertTrue(icmp.haslayer(IPerror))
587 inner_ip = icmp[IPerror]
588 if inner_ip.haslayer(TCPerror):
589 self.assertEqual(inner_ip[TCPerror].dport,
590 self.tcp_port_out)
591 elif inner_ip.haslayer(UDPerror):
592 self.assertEqual(inner_ip[UDPerror].dport,
593 self.udp_port_out)
594 else:
595 self.assertEqual(inner_ip[ICMPerror].id, self.icmp_id_out)
596 except:
597 self.logger.error(ppp("Unexpected or invalid packet "
598 "(outside network):", packet))
599 raise
600
Matus Fabian05ca4a32018-09-04 23:45:13 -0700601 def verify_capture_in_with_icmp_errors(self, capture, in_if, icmp_type=11):
Juraj Slobodab33f4132017-02-08 23:54:21 -0800602 """
603 Verify captured packets with ICMP errors on inside network
604
605 :param capture: Captured packets
606 :param in_if: Inside interface
Juraj Slobodab33f4132017-02-08 23:54:21 -0800607 :param icmp_type: Type of error ICMP packet
608 we are expecting (Default 11)
609 """
Juraj Slobodab33f4132017-02-08 23:54:21 -0800610 for packet in capture:
611 try:
612 self.assertEqual(packet[IP].dst, in_if.remote_ip4)
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800613 self.assertEqual(packet.haslayer(ICMP), 1)
Juraj Slobodab33f4132017-02-08 23:54:21 -0800614 icmp = packet[ICMP]
615 self.assertEqual(icmp.type, icmp_type)
616 self.assertTrue(icmp.haslayer(IPerror))
617 inner_ip = icmp[IPerror]
618 if inner_ip.haslayer(TCPerror):
619 self.assertEqual(inner_ip[TCPerror].sport,
620 self.tcp_port_in)
621 elif inner_ip.haslayer(UDPerror):
622 self.assertEqual(inner_ip[UDPerror].sport,
623 self.udp_port_in)
624 else:
625 self.assertEqual(inner_ip[ICMPerror].id, self.icmp_id_in)
626 except:
627 self.logger.error(ppp("Unexpected or invalid packet "
628 "(inside network):", packet))
629 raise
630
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200631 def create_stream_frag(self, src_if, dst, sport, dport, data,
632 proto=IP_PROTOS.tcp, echo_reply=False):
Matus Fabianefcd1e92017-08-15 06:59:19 -0700633 """
634 Create fragmented packet stream
635
636 :param src_if: Source interface
637 :param dst: Destination IPv4 address
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200638 :param sport: Source port
639 :param dport: Destination port
Matus Fabianefcd1e92017-08-15 06:59:19 -0700640 :param data: Payload data
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200641 :param proto: protocol (TCP, UDP, ICMP)
642 :param echo_reply: use echo_reply if protocol is ICMP
Matus Fabianefcd1e92017-08-15 06:59:19 -0700643 :returns: Fragmets
644 """
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200645 if proto == IP_PROTOS.tcp:
646 p = (IP(src=src_if.remote_ip4, dst=dst) /
647 TCP(sport=sport, dport=dport) /
648 Raw(data))
649 p = p.__class__(str(p))
650 chksum = p['TCP'].chksum
651 proto_header = TCP(sport=sport, dport=dport, chksum=chksum)
652 elif proto == IP_PROTOS.udp:
653 proto_header = UDP(sport=sport, dport=dport)
654 elif proto == IP_PROTOS.icmp:
655 if not echo_reply:
656 proto_header = ICMP(id=sport, type='echo-request')
657 else:
658 proto_header = ICMP(id=sport, type='echo-reply')
659 else:
660 raise Exception("Unsupported protocol")
Matus Fabianefcd1e92017-08-15 06:59:19 -0700661 id = random.randint(0, 65535)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700662 pkts = []
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200663 if proto == IP_PROTOS.tcp:
664 raw = Raw(data[0:4])
665 else:
666 raw = Raw(data[0:16])
Matus Fabianefcd1e92017-08-15 06:59:19 -0700667 p = (Ether(src=src_if.remote_mac, dst=src_if.local_mac) /
668 IP(src=src_if.remote_ip4, dst=dst, flags="MF", frag=0, id=id) /
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200669 proto_header /
670 raw)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700671 pkts.append(p)
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200672 if proto == IP_PROTOS.tcp:
673 raw = Raw(data[4:20])
674 else:
675 raw = Raw(data[16:32])
Matus Fabianefcd1e92017-08-15 06:59:19 -0700676 p = (Ether(src=src_if.remote_mac, dst=src_if.local_mac) /
677 IP(src=src_if.remote_ip4, dst=dst, flags="MF", frag=3, id=id,
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200678 proto=proto) /
679 raw)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700680 pkts.append(p)
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200681 if proto == IP_PROTOS.tcp:
682 raw = Raw(data[20:])
683 else:
684 raw = Raw(data[32:])
Matus Fabianefcd1e92017-08-15 06:59:19 -0700685 p = (Ether(src=src_if.remote_mac, dst=src_if.local_mac) /
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200686 IP(src=src_if.remote_ip4, dst=dst, frag=5, proto=proto,
Matus Fabianefcd1e92017-08-15 06:59:19 -0700687 id=id) /
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200688 raw)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700689 pkts.append(p)
690 return pkts
691
692 def create_stream_frag_ip6(self, src_if, dst, sport, dport, data,
693 pref=None, plen=0, frag_size=128):
694 """
695 Create fragmented packet stream
696
697 :param src_if: Source interface
698 :param dst: Destination IPv4 address
699 :param sport: Source TCP port
700 :param dport: Destination TCP port
701 :param data: Payload data
702 :param pref: NAT64 prefix
703 :param plen: NAT64 prefix length
704 :param fragsize: size of fragments
705 :returns: Fragmets
706 """
707 if pref is None:
708 dst_ip6 = ''.join(['64:ff9b::', dst])
709 else:
710 dst_ip6 = self.compose_ip6(dst, pref, plen)
711
712 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
713 IPv6(src=src_if.remote_ip6, dst=dst_ip6) /
714 IPv6ExtHdrFragment(id=random.randint(0, 65535)) /
715 TCP(sport=sport, dport=dport) /
716 Raw(data))
717
718 return fragment6(p, frag_size)
719
720 def reass_frags_and_verify(self, frags, src, dst):
721 """
722 Reassemble and verify fragmented packet
723
724 :param frags: Captured fragments
725 :param src: Source IPv4 address to verify
726 :param dst: Destination IPv4 address to verify
727
728 :returns: Reassembled IPv4 packet
729 """
Ole Troan7f991832018-12-06 17:35:12 +0100730 buffer = BytesIO()
Matus Fabianefcd1e92017-08-15 06:59:19 -0700731 for p in frags:
732 self.assertEqual(p[IP].src, src)
733 self.assertEqual(p[IP].dst, dst)
Klement Sekerad81ae412018-05-16 10:52:54 +0200734 self.assert_ip_checksum_valid(p)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700735 buffer.seek(p[IP].frag * 8)
Ole Troan7f991832018-12-06 17:35:12 +0100736 buffer.write(bytes(p[IP].payload))
Matus Fabianefcd1e92017-08-15 06:59:19 -0700737 ip = IP(src=frags[0][IP].src, dst=frags[0][IP].dst,
738 proto=frags[0][IP].proto)
739 if ip.proto == IP_PROTOS.tcp:
740 p = (ip / TCP(buffer.getvalue()))
Klement Sekerad81ae412018-05-16 10:52:54 +0200741 self.assert_tcp_checksum_valid(p)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700742 elif ip.proto == IP_PROTOS.udp:
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200743 p = (ip / UDP(buffer.getvalue()[:8]) /
744 Raw(buffer.getvalue()[8:]))
745 elif ip.proto == IP_PROTOS.icmp:
746 p = (ip / ICMP(buffer.getvalue()))
Matus Fabianefcd1e92017-08-15 06:59:19 -0700747 return p
748
749 def reass_frags_and_verify_ip6(self, frags, src, dst):
750 """
751 Reassemble and verify fragmented packet
752
753 :param frags: Captured fragments
754 :param src: Source IPv6 address to verify
755 :param dst: Destination IPv6 address to verify
756
757 :returns: Reassembled IPv6 packet
758 """
Ole Troan7f991832018-12-06 17:35:12 +0100759 buffer = BytesIO()
Matus Fabianefcd1e92017-08-15 06:59:19 -0700760 for p in frags:
761 self.assertEqual(p[IPv6].src, src)
762 self.assertEqual(p[IPv6].dst, dst)
763 buffer.seek(p[IPv6ExtHdrFragment].offset * 8)
Ole Troan7f991832018-12-06 17:35:12 +0100764 buffer.write(bytes(p[IPv6ExtHdrFragment].payload))
Matus Fabianefcd1e92017-08-15 06:59:19 -0700765 ip = IPv6(src=frags[0][IPv6].src, dst=frags[0][IPv6].dst,
766 nh=frags[0][IPv6ExtHdrFragment].nh)
767 if ip.nh == IP_PROTOS.tcp:
768 p = (ip / TCP(buffer.getvalue()))
Matus Fabianefcd1e92017-08-15 06:59:19 -0700769 elif ip.nh == IP_PROTOS.udp:
770 p = (ip / UDP(buffer.getvalue()))
Klement Sekerad81ae412018-05-16 10:52:54 +0200771 self.assert_packet_checksums_valid(p)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700772 return p
773
Matus Fabianebdf1902018-05-04 03:57:42 -0700774 def initiate_tcp_session(self, in_if, out_if):
775 """
776 Initiates TCP session
777
778 :param in_if: Inside interface
779 :param out_if: Outside interface
780 """
781 try:
782 # SYN packet in->out
783 p = (Ether(src=in_if.remote_mac, dst=in_if.local_mac) /
784 IP(src=in_if.remote_ip4, dst=out_if.remote_ip4) /
785 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
786 flags="S"))
787 in_if.add_stream(p)
788 self.pg_enable_capture(self.pg_interfaces)
789 self.pg_start()
790 capture = out_if.get_capture(1)
791 p = capture[0]
792 self.tcp_port_out = p[TCP].sport
793
794 # SYN + ACK packet out->in
795 p = (Ether(src=out_if.remote_mac, dst=out_if.local_mac) /
796 IP(src=out_if.remote_ip4, dst=self.nat_addr) /
797 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
798 flags="SA"))
799 out_if.add_stream(p)
800 self.pg_enable_capture(self.pg_interfaces)
801 self.pg_start()
802 in_if.get_capture(1)
803
804 # ACK packet in->out
805 p = (Ether(src=in_if.remote_mac, dst=in_if.local_mac) /
806 IP(src=in_if.remote_ip4, dst=out_if.remote_ip4) /
807 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
808 flags="A"))
809 in_if.add_stream(p)
810 self.pg_enable_capture(self.pg_interfaces)
811 self.pg_start()
812 out_if.get_capture(1)
813
814 except:
815 self.logger.error("TCP 3 way handshake failed")
816 raise
817
Matus Fabianeea28d72017-01-13 04:15:54 -0800818 def verify_ipfix_nat44_ses(self, data):
819 """
820 Verify IPFIX NAT44 session create/delete event
821
822 :param data: Decoded IPFIX data records
823 """
824 nat44_ses_create_num = 0
825 nat44_ses_delete_num = 0
826 self.assertEqual(6, len(data))
827 for record in data:
828 # natEvent
829 self.assertIn(ord(record[230]), [4, 5])
830 if ord(record[230]) == 4:
831 nat44_ses_create_num += 1
832 else:
833 nat44_ses_delete_num += 1
834 # sourceIPv4Address
835 self.assertEqual(self.pg0.remote_ip4n, record[8])
836 # postNATSourceIPv4Address
Matus Fabian2ba92e32017-08-21 07:05:03 -0700837 self.assertEqual(socket.inet_pton(socket.AF_INET, self.nat_addr),
Matus Fabianeea28d72017-01-13 04:15:54 -0800838 record[225])
839 # ingressVRFID
840 self.assertEqual(struct.pack("!I", 0), record[234])
841 # protocolIdentifier/sourceTransportPort/postNAPTSourceTransportPort
842 if IP_PROTOS.icmp == ord(record[4]):
843 self.assertEqual(struct.pack("!H", self.icmp_id_in), record[7])
844 self.assertEqual(struct.pack("!H", self.icmp_id_out),
845 record[227])
846 elif IP_PROTOS.tcp == ord(record[4]):
847 self.assertEqual(struct.pack("!H", self.tcp_port_in),
848 record[7])
849 self.assertEqual(struct.pack("!H", self.tcp_port_out),
850 record[227])
851 elif IP_PROTOS.udp == ord(record[4]):
852 self.assertEqual(struct.pack("!H", self.udp_port_in),
853 record[7])
854 self.assertEqual(struct.pack("!H", self.udp_port_out),
855 record[227])
856 else:
857 self.fail("Invalid protocol")
858 self.assertEqual(3, nat44_ses_create_num)
859 self.assertEqual(3, nat44_ses_delete_num)
860
861 def verify_ipfix_addr_exhausted(self, data):
862 """
863 Verify IPFIX NAT addresses event
864
865 :param data: Decoded IPFIX data records
866 """
867 self.assertEqual(1, len(data))
868 record = data[0]
869 # natEvent
870 self.assertEqual(ord(record[230]), 3)
871 # natPoolID
872 self.assertEqual(struct.pack("!I", 0), record[283])
873
Matus Fabiana431ad12018-01-04 04:03:14 -0800874 def verify_ipfix_max_sessions(self, data, limit):
875 """
876 Verify IPFIX maximum session entries exceeded event
877
878 :param data: Decoded IPFIX data records
879 :param limit: Number of maximum session entries that can be created.
880 """
881 self.assertEqual(1, len(data))
882 record = data[0]
883 # natEvent
884 self.assertEqual(ord(record[230]), 13)
885 # natQuotaExceededEvent
886 self.assertEqual(struct.pack("I", 1), record[466])
887 # maxSessionEntries
888 self.assertEqual(struct.pack("I", limit), record[471])
889
890 def verify_ipfix_max_bibs(self, data, limit):
891 """
892 Verify IPFIX maximum BIB entries exceeded event
893
894 :param data: Decoded IPFIX data records
895 :param limit: Number of maximum BIB entries that can be created.
896 """
897 self.assertEqual(1, len(data))
898 record = data[0]
899 # natEvent
900 self.assertEqual(ord(record[230]), 13)
901 # natQuotaExceededEvent
902 self.assertEqual(struct.pack("I", 2), record[466])
903 # maxBIBEntries
904 self.assertEqual(struct.pack("I", limit), record[472])
905
906 def verify_ipfix_max_fragments_ip6(self, data, limit, src_addr):
907 """
908 Verify IPFIX maximum IPv6 fragments pending reassembly exceeded event
909
910 :param data: Decoded IPFIX data records
911 :param limit: Number of maximum fragments pending reassembly
912 :param src_addr: IPv6 source address
913 """
914 self.assertEqual(1, len(data))
915 record = data[0]
916 # natEvent
917 self.assertEqual(ord(record[230]), 13)
918 # natQuotaExceededEvent
919 self.assertEqual(struct.pack("I", 5), record[466])
920 # maxFragmentsPendingReassembly
921 self.assertEqual(struct.pack("I", limit), record[475])
922 # sourceIPv6Address
923 self.assertEqual(src_addr, record[27])
924
925 def verify_ipfix_max_fragments_ip4(self, data, limit, src_addr):
926 """
927 Verify IPFIX maximum IPv4 fragments pending reassembly exceeded event
928
929 :param data: Decoded IPFIX data records
930 :param limit: Number of maximum fragments pending reassembly
931 :param src_addr: IPv4 source address
932 """
933 self.assertEqual(1, len(data))
934 record = data[0]
935 # natEvent
936 self.assertEqual(ord(record[230]), 13)
937 # natQuotaExceededEvent
938 self.assertEqual(struct.pack("I", 5), record[466])
939 # maxFragmentsPendingReassembly
940 self.assertEqual(struct.pack("I", limit), record[475])
941 # sourceIPv4Address
942 self.assertEqual(src_addr, record[8])
943
944 def verify_ipfix_bib(self, data, is_create, src_addr):
945 """
946 Verify IPFIX NAT64 BIB create and delete events
947
948 :param data: Decoded IPFIX data records
949 :param is_create: Create event if nonzero value otherwise delete event
950 :param src_addr: IPv6 source address
951 """
952 self.assertEqual(1, len(data))
953 record = data[0]
954 # natEvent
955 if is_create:
956 self.assertEqual(ord(record[230]), 10)
957 else:
958 self.assertEqual(ord(record[230]), 11)
959 # sourceIPv6Address
960 self.assertEqual(src_addr, record[27])
961 # postNATSourceIPv4Address
962 self.assertEqual(self.nat_addr_n, record[225])
963 # protocolIdentifier
964 self.assertEqual(IP_PROTOS.tcp, ord(record[4]))
965 # ingressVRFID
966 self.assertEqual(struct.pack("!I", 0), record[234])
967 # sourceTransportPort
968 self.assertEqual(struct.pack("!H", self.tcp_port_in), record[7])
969 # postNAPTSourceTransportPort
970 self.assertEqual(struct.pack("!H", self.tcp_port_out), record[227])
971
972 def verify_ipfix_nat64_ses(self, data, is_create, src_addr, dst_addr,
973 dst_port):
974 """
975 Verify IPFIX NAT64 session create and delete events
976
977 :param data: Decoded IPFIX data records
978 :param is_create: Create event if nonzero value otherwise delete event
979 :param src_addr: IPv6 source address
980 :param dst_addr: IPv4 destination address
981 :param dst_port: destination TCP port
982 """
983 self.assertEqual(1, len(data))
984 record = data[0]
985 # natEvent
986 if is_create:
987 self.assertEqual(ord(record[230]), 6)
988 else:
989 self.assertEqual(ord(record[230]), 7)
990 # sourceIPv6Address
991 self.assertEqual(src_addr, record[27])
992 # destinationIPv6Address
993 self.assertEqual(socket.inet_pton(socket.AF_INET6,
994 self.compose_ip6(dst_addr,
995 '64:ff9b::',
996 96)),
997 record[28])
998 # postNATSourceIPv4Address
999 self.assertEqual(self.nat_addr_n, record[225])
1000 # postNATDestinationIPv4Address
1001 self.assertEqual(socket.inet_pton(socket.AF_INET, dst_addr),
1002 record[226])
1003 # protocolIdentifier
1004 self.assertEqual(IP_PROTOS.tcp, ord(record[4]))
1005 # ingressVRFID
1006 self.assertEqual(struct.pack("!I", 0), record[234])
1007 # sourceTransportPort
1008 self.assertEqual(struct.pack("!H", self.tcp_port_in), record[7])
1009 # postNAPTSourceTransportPort
1010 self.assertEqual(struct.pack("!H", self.tcp_port_out), record[227])
1011 # destinationTransportPort
1012 self.assertEqual(struct.pack("!H", dst_port), record[11])
1013 # postNAPTDestinationTransportPort
1014 self.assertEqual(struct.pack("!H", dst_port), record[228])
1015
Matus Fabian68ba8802018-08-08 05:52:47 -07001016 def verify_no_nat44_user(self):
1017 """ Verify that there is no NAT44 user """
1018 users = self.vapi.nat44_user_dump()
1019 self.assertEqual(len(users), 0)
Matus Fabianfd0d5082018-12-18 01:08:51 -08001020 users = self.statistics.get_counter('/nat44/total-users')
1021 self.assertEqual(users[0][0], 0)
1022 sessions = self.statistics.get_counter('/nat44/total-sessions')
1023 self.assertEqual(sessions[0][0], 0)
Matus Fabian68ba8802018-08-08 05:52:47 -07001024
Matus Fabian878c6462018-08-23 00:33:35 -07001025 def verify_ipfix_max_entries_per_user(self, data, limit, src_addr):
1026 """
1027 Verify IPFIX maximum entries per user exceeded event
1028
1029 :param data: Decoded IPFIX data records
1030 :param limit: Number of maximum entries per user
1031 :param src_addr: IPv4 source address
1032 """
1033 self.assertEqual(1, len(data))
1034 record = data[0]
1035 # natEvent
1036 self.assertEqual(ord(record[230]), 13)
1037 # natQuotaExceededEvent
1038 self.assertEqual(struct.pack("I", 3), record[466])
1039 # maxEntriesPerUser
1040 self.assertEqual(struct.pack("I", limit), record[473])
1041 # sourceIPv4Address
1042 self.assertEqual(src_addr, record[8])
1043
Matus Fabianad1f3e12018-11-28 21:26:34 -08001044 def verify_syslog_apmap(self, data, is_add=True):
1045 message = data.decode('utf-8')
1046 try:
1047 message = SyslogMessage.parse(message)
Paul Vinciguerrad7532092019-01-14 08:51:17 -08001048 except ParseError as e:
1049 self.logger.error(e)
1050 raise
1051 else:
Matus Fabianad1f3e12018-11-28 21:26:34 -08001052 self.assertEqual(message.severity, SyslogSeverity.info)
1053 self.assertEqual(message.appname, 'NAT')
1054 self.assertEqual(message.msgid, 'APMADD' if is_add else 'APMDEL')
1055 sd_params = message.sd.get('napmap')
1056 self.assertTrue(sd_params is not None)
1057 self.assertEqual(sd_params.get('IATYP'), 'IPv4')
1058 self.assertEqual(sd_params.get('ISADDR'), self.pg0.remote_ip4)
1059 self.assertEqual(sd_params.get('ISPORT'), "%d" % self.tcp_port_in)
1060 self.assertEqual(sd_params.get('XATYP'), 'IPv4')
1061 self.assertEqual(sd_params.get('XSADDR'), self.nat_addr)
1062 self.assertEqual(sd_params.get('XSPORT'), "%d" % self.tcp_port_out)
1063 self.assertEqual(sd_params.get('PROTO'), "%d" % IP_PROTOS.tcp)
1064 self.assertTrue(sd_params.get('SSUBIX') is not None)
1065 self.assertEqual(sd_params.get('SVLAN'), '0')
Matus Fabianad1f3e12018-11-28 21:26:34 -08001066
1067 def verify_syslog_sess(self, data, is_add=True, is_ip6=False):
1068 message = data.decode('utf-8')
1069 try:
1070 message = SyslogMessage.parse(message)
Paul Vinciguerrad7532092019-01-14 08:51:17 -08001071 except ParseError as e:
1072 self.logger.error(e)
1073 raise
1074 else:
Matus Fabianad1f3e12018-11-28 21:26:34 -08001075 self.assertEqual(message.severity, SyslogSeverity.info)
1076 self.assertEqual(message.appname, 'NAT')
1077 self.assertEqual(message.msgid, 'SADD' if is_add else 'SDEL')
1078 sd_params = message.sd.get('nsess')
1079 self.assertTrue(sd_params is not None)
1080 if is_ip6:
1081 self.assertEqual(sd_params.get('IATYP'), 'IPv6')
1082 self.assertEqual(sd_params.get('ISADDR'), self.pg0.remote_ip6)
1083 else:
1084 self.assertEqual(sd_params.get('IATYP'), 'IPv4')
1085 self.assertEqual(sd_params.get('ISADDR'), self.pg0.remote_ip4)
1086 self.assertTrue(sd_params.get('SSUBIX') is not None)
1087 self.assertEqual(sd_params.get('ISPORT'), "%d" % self.tcp_port_in)
1088 self.assertEqual(sd_params.get('XATYP'), 'IPv4')
1089 self.assertEqual(sd_params.get('XSADDR'), self.nat_addr)
1090 self.assertEqual(sd_params.get('XSPORT'), "%d" % self.tcp_port_out)
1091 self.assertEqual(sd_params.get('PROTO'), "%d" % IP_PROTOS.tcp)
1092 self.assertEqual(sd_params.get('SVLAN'), '0')
1093 self.assertEqual(sd_params.get('XDADDR'), self.pg1.remote_ip4)
1094 self.assertEqual(sd_params.get('XDPORT'),
1095 "%d" % self.tcp_external_port)
Matus Fabianad1f3e12018-11-28 21:26:34 -08001096
Matus Fabianbb4e0222018-09-13 02:36:25 -07001097 def verify_mss_value(self, pkt, mss):
1098 """
1099 Verify TCP MSS value
1100
1101 :param pkt:
1102 :param mss:
1103 """
1104 if not pkt.haslayer(IP) or not pkt.haslayer(TCP):
1105 raise TypeError("Not a TCP/IP packet")
1106
1107 for option in pkt[TCP].options:
1108 if option[0] == 'MSS':
1109 self.assertEqual(option[1], mss)
1110 self.assert_tcp_checksum_valid(pkt)
1111
Juraj Slobodafe0aa762018-07-23 12:22:54 +02001112 @staticmethod
1113 def proto2layer(proto):
1114 if proto == IP_PROTOS.tcp:
1115 return TCP
1116 elif proto == IP_PROTOS.udp:
1117 return UDP
1118 elif proto == IP_PROTOS.icmp:
1119 return ICMP
1120 else:
1121 raise Exception("Unsupported protocol")
1122
1123 def frag_in_order(self, proto=IP_PROTOS.tcp, dont_translate=False):
1124 layer = self.proto2layer(proto)
1125
1126 if proto == IP_PROTOS.tcp:
1127 data = "A" * 4 + "B" * 16 + "C" * 3
1128 else:
1129 data = "A" * 16 + "B" * 16 + "C" * 3
1130 self.port_in = random.randint(1025, 65535)
1131
1132 reass = self.vapi.nat_reass_dump()
1133 reass_n_start = len(reass)
1134
1135 # in2out
1136 pkts = self.create_stream_frag(self.pg0,
1137 self.pg1.remote_ip4,
1138 self.port_in,
1139 20,
1140 data,
1141 proto)
1142 self.pg0.add_stream(pkts)
1143 self.pg_enable_capture(self.pg_interfaces)
1144 self.pg_start()
1145 frags = self.pg1.get_capture(len(pkts))
1146 if not dont_translate:
1147 p = self.reass_frags_and_verify(frags,
1148 self.nat_addr,
1149 self.pg1.remote_ip4)
1150 else:
1151 p = self.reass_frags_and_verify(frags,
1152 self.pg0.remote_ip4,
1153 self.pg1.remote_ip4)
1154 if proto != IP_PROTOS.icmp:
1155 if not dont_translate:
1156 self.assertEqual(p[layer].dport, 20)
1157 self.assertNotEqual(p[layer].sport, self.port_in)
1158 else:
1159 self.assertEqual(p[layer].sport, self.port_in)
1160 else:
1161 if not dont_translate:
1162 self.assertNotEqual(p[layer].id, self.port_in)
1163 else:
1164 self.assertEqual(p[layer].id, self.port_in)
1165 self.assertEqual(data, p[Raw].load)
1166
1167 # out2in
1168 if not dont_translate:
1169 dst_addr = self.nat_addr
1170 else:
1171 dst_addr = self.pg0.remote_ip4
1172 if proto != IP_PROTOS.icmp:
1173 sport = 20
1174 dport = p[layer].sport
1175 else:
1176 sport = p[layer].id
1177 dport = 0
1178 pkts = self.create_stream_frag(self.pg1,
1179 dst_addr,
1180 sport,
1181 dport,
1182 data,
1183 proto,
1184 echo_reply=True)
1185 self.pg1.add_stream(pkts)
1186 self.pg_enable_capture(self.pg_interfaces)
1187 self.pg_start()
1188 frags = self.pg0.get_capture(len(pkts))
1189 p = self.reass_frags_and_verify(frags,
1190 self.pg1.remote_ip4,
1191 self.pg0.remote_ip4)
1192 if proto != IP_PROTOS.icmp:
1193 self.assertEqual(p[layer].sport, 20)
1194 self.assertEqual(p[layer].dport, self.port_in)
1195 else:
1196 self.assertEqual(p[layer].id, self.port_in)
1197 self.assertEqual(data, p[Raw].load)
1198
1199 reass = self.vapi.nat_reass_dump()
1200 reass_n_end = len(reass)
1201
1202 self.assertEqual(reass_n_end - reass_n_start, 2)
1203
1204 def frag_in_order_in_plus_out(self, proto=IP_PROTOS.tcp):
1205 layer = self.proto2layer(proto)
1206
1207 if proto == IP_PROTOS.tcp:
1208 data = "A" * 4 + "B" * 16 + "C" * 3
1209 else:
1210 data = "A" * 16 + "B" * 16 + "C" * 3
1211 self.port_in = random.randint(1025, 65535)
1212
1213 for i in range(2):
1214 reass = self.vapi.nat_reass_dump()
1215 reass_n_start = len(reass)
1216
1217 # out2in
1218 pkts = self.create_stream_frag(self.pg0,
1219 self.server_out_addr,
1220 self.port_in,
1221 self.server_out_port,
1222 data,
1223 proto)
1224 self.pg0.add_stream(pkts)
1225 self.pg_enable_capture(self.pg_interfaces)
1226 self.pg_start()
1227 frags = self.pg1.get_capture(len(pkts))
1228 p = self.reass_frags_and_verify(frags,
1229 self.pg0.remote_ip4,
1230 self.server_in_addr)
1231 if proto != IP_PROTOS.icmp:
1232 self.assertEqual(p[layer].sport, self.port_in)
1233 self.assertEqual(p[layer].dport, self.server_in_port)
1234 else:
1235 self.assertEqual(p[layer].id, self.port_in)
1236 self.assertEqual(data, p[Raw].load)
1237
1238 # in2out
1239 if proto != IP_PROTOS.icmp:
1240 pkts = self.create_stream_frag(self.pg1,
1241 self.pg0.remote_ip4,
1242 self.server_in_port,
1243 p[layer].sport,
1244 data,
1245 proto)
1246 else:
1247 pkts = self.create_stream_frag(self.pg1,
1248 self.pg0.remote_ip4,
1249 p[layer].id,
1250 0,
1251 data,
1252 proto,
1253 echo_reply=True)
1254 self.pg1.add_stream(pkts)
1255 self.pg_enable_capture(self.pg_interfaces)
1256 self.pg_start()
1257 frags = self.pg0.get_capture(len(pkts))
1258 p = self.reass_frags_and_verify(frags,
1259 self.server_out_addr,
1260 self.pg0.remote_ip4)
1261 if proto != IP_PROTOS.icmp:
1262 self.assertEqual(p[layer].sport, self.server_out_port)
1263 self.assertEqual(p[layer].dport, self.port_in)
1264 else:
1265 self.assertEqual(p[layer].id, self.port_in)
1266 self.assertEqual(data, p[Raw].load)
1267
1268 reass = self.vapi.nat_reass_dump()
1269 reass_n_end = len(reass)
1270
1271 self.assertEqual(reass_n_end - reass_n_start, 2)
1272
1273 def reass_hairpinning(self, proto=IP_PROTOS.tcp):
1274 layer = self.proto2layer(proto)
1275
1276 if proto == IP_PROTOS.tcp:
1277 data = "A" * 4 + "B" * 16 + "C" * 3
1278 else:
1279 data = "A" * 16 + "B" * 16 + "C" * 3
1280
1281 # send packet from host to server
1282 pkts = self.create_stream_frag(self.pg0,
1283 self.nat_addr,
1284 self.host_in_port,
1285 self.server_out_port,
1286 data,
1287 proto)
1288 self.pg0.add_stream(pkts)
1289 self.pg_enable_capture(self.pg_interfaces)
1290 self.pg_start()
1291 frags = self.pg0.get_capture(len(pkts))
1292 p = self.reass_frags_and_verify(frags,
1293 self.nat_addr,
1294 self.server.ip4)
1295 if proto != IP_PROTOS.icmp:
1296 self.assertNotEqual(p[layer].sport, self.host_in_port)
1297 self.assertEqual(p[layer].dport, self.server_in_port)
1298 else:
1299 self.assertNotEqual(p[layer].id, self.host_in_port)
1300 self.assertEqual(data, p[Raw].load)
1301
1302 def frag_out_of_order(self, proto=IP_PROTOS.tcp, dont_translate=False):
1303 layer = self.proto2layer(proto)
1304
1305 if proto == IP_PROTOS.tcp:
1306 data = "A" * 4 + "B" * 16 + "C" * 3
1307 else:
1308 data = "A" * 16 + "B" * 16 + "C" * 3
1309 self.port_in = random.randint(1025, 65535)
1310
1311 for i in range(2):
1312 # in2out
1313 pkts = self.create_stream_frag(self.pg0,
1314 self.pg1.remote_ip4,
1315 self.port_in,
1316 20,
1317 data,
1318 proto)
1319 pkts.reverse()
1320 self.pg0.add_stream(pkts)
1321 self.pg_enable_capture(self.pg_interfaces)
1322 self.pg_start()
1323 frags = self.pg1.get_capture(len(pkts))
1324 if not dont_translate:
1325 p = self.reass_frags_and_verify(frags,
1326 self.nat_addr,
1327 self.pg1.remote_ip4)
1328 else:
1329 p = self.reass_frags_and_verify(frags,
1330 self.pg0.remote_ip4,
1331 self.pg1.remote_ip4)
1332 if proto != IP_PROTOS.icmp:
1333 if not dont_translate:
1334 self.assertEqual(p[layer].dport, 20)
1335 self.assertNotEqual(p[layer].sport, self.port_in)
1336 else:
1337 self.assertEqual(p[layer].sport, self.port_in)
1338 else:
1339 if not dont_translate:
1340 self.assertNotEqual(p[layer].id, self.port_in)
1341 else:
1342 self.assertEqual(p[layer].id, self.port_in)
1343 self.assertEqual(data, p[Raw].load)
1344
1345 # out2in
1346 if not dont_translate:
1347 dst_addr = self.nat_addr
1348 else:
1349 dst_addr = self.pg0.remote_ip4
1350 if proto != IP_PROTOS.icmp:
1351 sport = 20
1352 dport = p[layer].sport
1353 else:
1354 sport = p[layer].id
1355 dport = 0
1356 pkts = self.create_stream_frag(self.pg1,
1357 dst_addr,
1358 sport,
1359 dport,
1360 data,
1361 proto,
1362 echo_reply=True)
1363 pkts.reverse()
1364 self.pg1.add_stream(pkts)
1365 self.pg_enable_capture(self.pg_interfaces)
1366 self.pg_start()
1367 frags = self.pg0.get_capture(len(pkts))
1368 p = self.reass_frags_and_verify(frags,
1369 self.pg1.remote_ip4,
1370 self.pg0.remote_ip4)
1371 if proto != IP_PROTOS.icmp:
1372 self.assertEqual(p[layer].sport, 20)
1373 self.assertEqual(p[layer].dport, self.port_in)
1374 else:
1375 self.assertEqual(p[layer].id, self.port_in)
1376 self.assertEqual(data, p[Raw].load)
1377
1378 def frag_out_of_order_in_plus_out(self, proto=IP_PROTOS.tcp):
1379 layer = self.proto2layer(proto)
1380
1381 if proto == IP_PROTOS.tcp:
1382 data = "A" * 4 + "B" * 16 + "C" * 3
1383 else:
1384 data = "A" * 16 + "B" * 16 + "C" * 3
1385 self.port_in = random.randint(1025, 65535)
1386
1387 for i in range(2):
1388 # out2in
1389 pkts = self.create_stream_frag(self.pg0,
1390 self.server_out_addr,
1391 self.port_in,
1392 self.server_out_port,
1393 data,
1394 proto)
1395 pkts.reverse()
1396 self.pg0.add_stream(pkts)
1397 self.pg_enable_capture(self.pg_interfaces)
1398 self.pg_start()
1399 frags = self.pg1.get_capture(len(pkts))
1400 p = self.reass_frags_and_verify(frags,
1401 self.pg0.remote_ip4,
1402 self.server_in_addr)
1403 if proto != IP_PROTOS.icmp:
1404 self.assertEqual(p[layer].dport, self.server_in_port)
1405 self.assertEqual(p[layer].sport, self.port_in)
1406 self.assertEqual(p[layer].dport, self.server_in_port)
1407 else:
1408 self.assertEqual(p[layer].id, self.port_in)
1409 self.assertEqual(data, p[Raw].load)
1410
1411 # in2out
1412 if proto != IP_PROTOS.icmp:
1413 pkts = self.create_stream_frag(self.pg1,
1414 self.pg0.remote_ip4,
1415 self.server_in_port,
1416 p[layer].sport,
1417 data,
1418 proto)
1419 else:
1420 pkts = self.create_stream_frag(self.pg1,
1421 self.pg0.remote_ip4,
1422 p[layer].id,
1423 0,
1424 data,
1425 proto,
1426 echo_reply=True)
1427 pkts.reverse()
1428 self.pg1.add_stream(pkts)
1429 self.pg_enable_capture(self.pg_interfaces)
1430 self.pg_start()
1431 frags = self.pg0.get_capture(len(pkts))
1432 p = self.reass_frags_and_verify(frags,
1433 self.server_out_addr,
1434 self.pg0.remote_ip4)
1435 if proto != IP_PROTOS.icmp:
1436 self.assertEqual(p[layer].sport, self.server_out_port)
1437 self.assertEqual(p[layer].dport, self.port_in)
1438 else:
1439 self.assertEqual(p[layer].id, self.port_in)
1440 self.assertEqual(data, p[Raw].load)
1441
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001442
Matus Fabian2ba92e32017-08-21 07:05:03 -07001443class TestNAT44(MethodHolder):
1444 """ NAT44 Test Cases """
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001445
1446 @classmethod
1447 def setUpClass(cls):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001448 super(TestNAT44, cls).setUpClass()
Matus Fabian229c1aa2018-05-28 04:09:52 -07001449 cls.vapi.cli("set log class nat level debug")
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001450
1451 try:
1452 cls.tcp_port_in = 6303
1453 cls.tcp_port_out = 6303
1454 cls.udp_port_in = 6304
1455 cls.udp_port_out = 6304
1456 cls.icmp_id_in = 6305
1457 cls.icmp_id_out = 6305
Matus Fabian2ba92e32017-08-21 07:05:03 -07001458 cls.nat_addr = '10.0.0.3'
Juraj Sloboda7b929792017-11-23 13:20:48 +01001459 cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
Matus Fabian6631e9c2017-05-24 01:52:20 -07001460 cls.ipfix_src_port = 4739
1461 cls.ipfix_domain_id = 1
Matus Fabianebdf1902018-05-04 03:57:42 -07001462 cls.tcp_external_port = 80
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001463
Matus Fabian36ea2d62017-10-24 04:13:49 -07001464 cls.create_pg_interfaces(range(10))
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001465 cls.interfaces = list(cls.pg_interfaces[0:4])
1466
1467 for i in cls.interfaces:
1468 i.admin_up()
1469 i.config_ip4()
1470 i.resolve_arp()
1471
Matus Fabian6fa74c62017-06-05 05:55:48 -07001472 cls.pg0.generate_remote_hosts(3)
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001473 cls.pg0.configure_ipv4_neighbors()
1474
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02001475 cls.pg1.generate_remote_hosts(1)
1476 cls.pg1.configure_ipv4_neighbors()
1477
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001478 cls.overlapping_interfaces = list(list(cls.pg_interfaces[4:7]))
Neale Ranns15002542017-09-10 04:39:11 -07001479 cls.vapi.ip_table_add_del(10, is_add=1)
1480 cls.vapi.ip_table_add_del(20, is_add=1)
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001481
1482 cls.pg4._local_ip4 = "172.16.255.1"
1483 cls.pg4._local_ip4n = socket.inet_pton(socket.AF_INET, i.local_ip4)
1484 cls.pg4._remote_hosts[0]._ip4 = "172.16.255.2"
1485 cls.pg4.set_table_ip4(10)
Matus Fabian69126282017-08-14 23:39:58 -07001486 cls.pg5._local_ip4 = "172.17.255.3"
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001487 cls.pg5._local_ip4n = socket.inet_pton(socket.AF_INET, i.local_ip4)
Matus Fabian69126282017-08-14 23:39:58 -07001488 cls.pg5._remote_hosts[0]._ip4 = "172.17.255.4"
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001489 cls.pg5.set_table_ip4(10)
1490 cls.pg6._local_ip4 = "172.16.255.1"
1491 cls.pg6._local_ip4n = socket.inet_pton(socket.AF_INET, i.local_ip4)
1492 cls.pg6._remote_hosts[0]._ip4 = "172.16.255.2"
1493 cls.pg6.set_table_ip4(20)
1494 for i in cls.overlapping_interfaces:
1495 i.config_ip4()
1496 i.admin_up()
1497 i.resolve_arp()
1498
1499 cls.pg7.admin_up()
Martin Gálik406eb1d2017-05-04 04:35:04 -07001500 cls.pg8.admin_up()
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001501
Matus Fabian36ea2d62017-10-24 04:13:49 -07001502 cls.pg9.generate_remote_hosts(2)
1503 cls.pg9.config_ip4()
1504 ip_addr_n = socket.inet_pton(socket.AF_INET, "10.0.0.1")
1505 cls.vapi.sw_interface_add_del_address(cls.pg9.sw_if_index,
1506 ip_addr_n,
1507 24)
1508 cls.pg9.admin_up()
1509 cls.pg9.resolve_arp()
1510 cls.pg9._remote_hosts[1]._ip4 = cls.pg9._remote_hosts[0]._ip4
1511 cls.pg4._remote_ip4 = cls.pg9._remote_hosts[0]._ip4 = "10.0.0.2"
1512 cls.pg9.resolve_arp()
1513
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001514 except Exception:
Matus Fabian2ba92e32017-08-21 07:05:03 -07001515 super(TestNAT44, cls).tearDownClass()
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001516 raise
1517
Matus Fabiande886752016-12-07 03:38:19 -08001518 def test_dynamic(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001519 """ NAT44 dynamic translation test """
Matus Fabian2ba92e32017-08-21 07:05:03 -07001520 self.nat44_add_address(self.nat_addr)
1521 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1522 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1523 is_inside=0)
Matus Fabiande886752016-12-07 03:38:19 -08001524
1525 # in2out
Matus Fabiana5e73762018-12-14 01:55:16 -08001526 tcpn = self.statistics.get_counter(
1527 '/err/nat44-in2out-slowpath/TCP packets')
1528 udpn = self.statistics.get_counter(
1529 '/err/nat44-in2out-slowpath/UDP packets')
1530 icmpn = self.statistics.get_counter(
1531 '/err/nat44-in2out-slowpath/ICMP packets')
1532 totaln = self.statistics.get_counter(
1533 '/err/nat44-in2out-slowpath/good in2out packets processed')
1534
Matus Fabiande886752016-12-07 03:38:19 -08001535 pkts = self.create_stream_in(self.pg0, self.pg1)
1536 self.pg0.add_stream(pkts)
1537 self.pg_enable_capture(self.pg_interfaces)
1538 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001539 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001540 self.verify_capture_out(capture)
1541
Matus Fabiana5e73762018-12-14 01:55:16 -08001542 err = self.statistics.get_counter(
1543 '/err/nat44-in2out-slowpath/TCP packets')
1544 self.assertEqual(err - tcpn, 1)
1545 err = self.statistics.get_counter(
1546 '/err/nat44-in2out-slowpath/UDP packets')
1547 self.assertEqual(err - udpn, 1)
1548 err = self.statistics.get_counter(
1549 '/err/nat44-in2out-slowpath/ICMP packets')
1550 self.assertEqual(err - icmpn, 1)
1551 err = self.statistics.get_counter(
1552 '/err/nat44-in2out-slowpath/good in2out packets processed')
1553 self.assertEqual(err - totaln, 3)
1554
Matus Fabiande886752016-12-07 03:38:19 -08001555 # out2in
Matus Fabiana5e73762018-12-14 01:55:16 -08001556 tcpn = self.statistics.get_counter('/err/nat44-out2in/TCP packets')
1557 udpn = self.statistics.get_counter('/err/nat44-out2in/UDP packets')
1558 icmpn = self.statistics.get_counter('/err/nat44-out2in/ICMP packets')
1559 totaln = self.statistics.get_counter(
1560 '/err/nat44-out2in/good out2in packets processed')
1561
Matus Fabiande886752016-12-07 03:38:19 -08001562 pkts = self.create_stream_out(self.pg1)
1563 self.pg1.add_stream(pkts)
1564 self.pg_enable_capture(self.pg_interfaces)
1565 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001566 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001567 self.verify_capture_in(capture, self.pg0)
1568
Matus Fabiana5e73762018-12-14 01:55:16 -08001569 err = self.statistics.get_counter('/err/nat44-out2in/TCP packets')
1570 self.assertEqual(err - tcpn, 1)
1571 err = self.statistics.get_counter('/err/nat44-out2in/UDP packets')
1572 self.assertEqual(err - udpn, 1)
1573 err = self.statistics.get_counter('/err/nat44-out2in/ICMP packets')
1574 self.assertEqual(err - icmpn, 1)
1575 err = self.statistics.get_counter(
1576 '/err/nat44-out2in/good out2in packets processed')
1577 self.assertEqual(err - totaln, 3)
1578
Matus Fabianfd0d5082018-12-18 01:08:51 -08001579 users = self.statistics.get_counter('/nat44/total-users')
1580 self.assertEqual(users[0][0], 1)
1581 sessions = self.statistics.get_counter('/nat44/total-sessions')
1582 self.assertEqual(sessions[0][0], 3)
1583
Juraj Slobodab33f4132017-02-08 23:54:21 -08001584 def test_dynamic_icmp_errors_in2out_ttl_1(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001585 """ NAT44 handling of client packets with TTL=1 """
Juraj Slobodab33f4132017-02-08 23:54:21 -08001586
Matus Fabian2ba92e32017-08-21 07:05:03 -07001587 self.nat44_add_address(self.nat_addr)
1588 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1589 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1590 is_inside=0)
Juraj Slobodab33f4132017-02-08 23:54:21 -08001591
1592 # Client side - generate traffic
1593 pkts = self.create_stream_in(self.pg0, self.pg1, ttl=1)
1594 self.pg0.add_stream(pkts)
1595 self.pg_enable_capture(self.pg_interfaces)
1596 self.pg_start()
1597
1598 # Client side - verify ICMP type 11 packets
1599 capture = self.pg0.get_capture(len(pkts))
1600 self.verify_capture_in_with_icmp_errors(capture, self.pg0)
1601
1602 def test_dynamic_icmp_errors_out2in_ttl_1(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001603 """ NAT44 handling of server packets with TTL=1 """
Juraj Slobodab33f4132017-02-08 23:54:21 -08001604
Matus Fabian2ba92e32017-08-21 07:05:03 -07001605 self.nat44_add_address(self.nat_addr)
1606 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1607 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1608 is_inside=0)
Juraj Slobodab33f4132017-02-08 23:54:21 -08001609
1610 # Client side - create sessions
1611 pkts = self.create_stream_in(self.pg0, self.pg1)
1612 self.pg0.add_stream(pkts)
1613 self.pg_enable_capture(self.pg_interfaces)
1614 self.pg_start()
1615
1616 # Server side - generate traffic
1617 capture = self.pg1.get_capture(len(pkts))
1618 self.verify_capture_out(capture)
1619 pkts = self.create_stream_out(self.pg1, ttl=1)
1620 self.pg1.add_stream(pkts)
1621 self.pg_enable_capture(self.pg_interfaces)
1622 self.pg_start()
1623
1624 # Server side - verify ICMP type 11 packets
1625 capture = self.pg1.get_capture(len(pkts))
1626 self.verify_capture_out_with_icmp_errors(capture,
1627 src_ip=self.pg1.local_ip4)
1628
1629 def test_dynamic_icmp_errors_in2out_ttl_2(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001630 """ NAT44 handling of error responses to client packets with TTL=2 """
Juraj Slobodab33f4132017-02-08 23:54:21 -08001631
Matus Fabian2ba92e32017-08-21 07:05:03 -07001632 self.nat44_add_address(self.nat_addr)
1633 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1634 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1635 is_inside=0)
Juraj Slobodab33f4132017-02-08 23:54:21 -08001636
1637 # Client side - generate traffic
1638 pkts = self.create_stream_in(self.pg0, self.pg1, ttl=2)
1639 self.pg0.add_stream(pkts)
1640 self.pg_enable_capture(self.pg_interfaces)
1641 self.pg_start()
1642
1643 # Server side - simulate ICMP type 11 response
1644 capture = self.pg1.get_capture(len(pkts))
1645 pkts = [Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07001646 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
Juraj Slobodab33f4132017-02-08 23:54:21 -08001647 ICMP(type=11) / packet[IP] for packet in capture]
1648 self.pg1.add_stream(pkts)
1649 self.pg_enable_capture(self.pg_interfaces)
1650 self.pg_start()
1651
1652 # Client side - verify ICMP type 11 packets
1653 capture = self.pg0.get_capture(len(pkts))
1654 self.verify_capture_in_with_icmp_errors(capture, self.pg0)
1655
1656 def test_dynamic_icmp_errors_out2in_ttl_2(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001657 """ NAT44 handling of error responses to server packets with TTL=2 """
Juraj Slobodab33f4132017-02-08 23:54:21 -08001658
Matus Fabian2ba92e32017-08-21 07:05:03 -07001659 self.nat44_add_address(self.nat_addr)
1660 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1661 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1662 is_inside=0)
Juraj Slobodab33f4132017-02-08 23:54:21 -08001663
1664 # Client side - create sessions
1665 pkts = self.create_stream_in(self.pg0, self.pg1)
1666 self.pg0.add_stream(pkts)
1667 self.pg_enable_capture(self.pg_interfaces)
1668 self.pg_start()
1669
1670 # Server side - generate traffic
1671 capture = self.pg1.get_capture(len(pkts))
1672 self.verify_capture_out(capture)
1673 pkts = self.create_stream_out(self.pg1, ttl=2)
1674 self.pg1.add_stream(pkts)
1675 self.pg_enable_capture(self.pg_interfaces)
1676 self.pg_start()
1677
1678 # Client side - simulate ICMP type 11 response
1679 capture = self.pg0.get_capture(len(pkts))
1680 pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1681 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
1682 ICMP(type=11) / packet[IP] for packet in capture]
1683 self.pg0.add_stream(pkts)
1684 self.pg_enable_capture(self.pg_interfaces)
1685 self.pg_start()
1686
1687 # Server side - verify ICMP type 11 packets
1688 capture = self.pg1.get_capture(len(pkts))
1689 self.verify_capture_out_with_icmp_errors(capture)
1690
Juraj Sloboda665e4822017-02-16 17:17:19 -08001691 def test_ping_out_interface_from_outside(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001692 """ Ping NAT44 out interface from outside network """
Juraj Sloboda665e4822017-02-16 17:17:19 -08001693
Matus Fabian2ba92e32017-08-21 07:05:03 -07001694 self.nat44_add_address(self.nat_addr)
1695 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1696 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1697 is_inside=0)
Juraj Sloboda665e4822017-02-16 17:17:19 -08001698
1699 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1700 IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
1701 ICMP(id=self.icmp_id_out, type='echo-request'))
1702 pkts = [p]
1703 self.pg1.add_stream(pkts)
1704 self.pg_enable_capture(self.pg_interfaces)
1705 self.pg_start()
1706 capture = self.pg1.get_capture(len(pkts))
Juraj Sloboda665e4822017-02-16 17:17:19 -08001707 packet = capture[0]
1708 try:
1709 self.assertEqual(packet[IP].src, self.pg1.local_ip4)
1710 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
1711 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
1712 self.assertEqual(packet[ICMP].type, 0) # echo reply
1713 except:
1714 self.logger.error(ppp("Unexpected or invalid packet "
1715 "(outside network):", packet))
1716 raise
1717
Juraj Slobodad3677682017-04-14 03:24:45 +02001718 def test_ping_internal_host_from_outside(self):
1719 """ Ping internal host from outside network """
1720
Matus Fabian2ba92e32017-08-21 07:05:03 -07001721 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr)
1722 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1723 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1724 is_inside=0)
Juraj Slobodad3677682017-04-14 03:24:45 +02001725
1726 # out2in
1727 pkt = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07001728 IP(src=self.pg1.remote_ip4, dst=self.nat_addr, ttl=64) /
Juraj Slobodad3677682017-04-14 03:24:45 +02001729 ICMP(id=self.icmp_id_out, type='echo-request'))
1730 self.pg1.add_stream(pkt)
1731 self.pg_enable_capture(self.pg_interfaces)
1732 self.pg_start()
1733 capture = self.pg0.get_capture(1)
Matus Fabian05ca4a32018-09-04 23:45:13 -07001734 self.verify_capture_in(capture, self.pg0)
Juraj Slobodad3677682017-04-14 03:24:45 +02001735 self.assert_equal(capture[0][IP].proto, IP_PROTOS.icmp)
1736
1737 # in2out
1738 pkt = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1739 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, ttl=64) /
1740 ICMP(id=self.icmp_id_in, type='echo-reply'))
1741 self.pg0.add_stream(pkt)
1742 self.pg_enable_capture(self.pg_interfaces)
1743 self.pg_start()
1744 capture = self.pg1.get_capture(1)
Matus Fabian05ca4a32018-09-04 23:45:13 -07001745 self.verify_capture_out(capture, same_port=True)
Juraj Slobodad3677682017-04-14 03:24:45 +02001746 self.assert_equal(capture[0][IP].proto, IP_PROTOS.icmp)
1747
Matus Fabianc79396e2018-07-23 00:05:22 -07001748 def test_forwarding(self):
Juraj Sloboda7b929792017-11-23 13:20:48 +01001749 """ NAT44 forwarding test """
1750
1751 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1752 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1753 is_inside=0)
1754 self.vapi.nat44_forwarding_enable_disable(1)
1755
1756 real_ip = self.pg0.remote_ip4n
1757 alias_ip = self.nat_addr_n
1758 self.vapi.nat44_add_del_static_mapping(local_ip=real_ip,
1759 external_ip=alias_ip)
1760
1761 try:
Matus Fabianc79396e2018-07-23 00:05:22 -07001762 # static mapping match
Juraj Sloboda7b929792017-11-23 13:20:48 +01001763
1764 pkts = self.create_stream_out(self.pg1)
1765 self.pg1.add_stream(pkts)
1766 self.pg_enable_capture(self.pg_interfaces)
1767 self.pg_start()
1768 capture = self.pg0.get_capture(len(pkts))
1769 self.verify_capture_in(capture, self.pg0)
1770
1771 pkts = self.create_stream_in(self.pg0, self.pg1)
1772 self.pg0.add_stream(pkts)
1773 self.pg_enable_capture(self.pg_interfaces)
1774 self.pg_start()
1775 capture = self.pg1.get_capture(len(pkts))
1776 self.verify_capture_out(capture, same_port=True)
1777
Matus Fabianc79396e2018-07-23 00:05:22 -07001778 # no static mapping match
Juraj Sloboda7b929792017-11-23 13:20:48 +01001779
1780 host0 = self.pg0.remote_hosts[0]
1781 self.pg0.remote_hosts[0] = self.pg0.remote_hosts[1]
1782 try:
1783 pkts = self.create_stream_out(self.pg1,
1784 dst_ip=self.pg0.remote_ip4,
1785 use_inside_ports=True)
1786 self.pg1.add_stream(pkts)
1787 self.pg_enable_capture(self.pg_interfaces)
1788 self.pg_start()
1789 capture = self.pg0.get_capture(len(pkts))
1790 self.verify_capture_in(capture, self.pg0)
1791
1792 pkts = self.create_stream_in(self.pg0, self.pg1)
1793 self.pg0.add_stream(pkts)
1794 self.pg_enable_capture(self.pg_interfaces)
1795 self.pg_start()
1796 capture = self.pg1.get_capture(len(pkts))
1797 self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4,
1798 same_port=True)
1799 finally:
1800 self.pg0.remote_hosts[0] = host0
1801
1802 finally:
1803 self.vapi.nat44_forwarding_enable_disable(0)
1804 self.vapi.nat44_add_del_static_mapping(local_ip=real_ip,
1805 external_ip=alias_ip,
1806 is_add=0)
1807
Matus Fabiande886752016-12-07 03:38:19 -08001808 def test_static_in(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001809 """ 1:1 NAT initialized from inside network """
Matus Fabiande886752016-12-07 03:38:19 -08001810
1811 nat_ip = "10.0.0.10"
1812 self.tcp_port_out = 6303
1813 self.udp_port_out = 6304
1814 self.icmp_id_out = 6305
1815
Matus Fabian2ba92e32017-08-21 07:05:03 -07001816 self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip)
1817 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1818 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1819 is_inside=0)
Matus Fabian5f224992018-01-25 21:59:16 -08001820 sm = self.vapi.nat44_static_mapping_dump()
1821 self.assertEqual(len(sm), 1)
1822 self.assertEqual((sm[0].tag).split('\0', 1)[0], '')
Matus Fabian9dba7812018-01-31 01:13:23 -08001823 self.assertEqual(sm[0].protocol, 0)
1824 self.assertEqual(sm[0].local_port, 0)
1825 self.assertEqual(sm[0].external_port, 0)
Matus Fabiande886752016-12-07 03:38:19 -08001826
1827 # in2out
1828 pkts = self.create_stream_in(self.pg0, self.pg1)
1829 self.pg0.add_stream(pkts)
1830 self.pg_enable_capture(self.pg_interfaces)
1831 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001832 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001833 self.verify_capture_out(capture, nat_ip, True)
1834
1835 # out2in
1836 pkts = self.create_stream_out(self.pg1, nat_ip)
1837 self.pg1.add_stream(pkts)
1838 self.pg_enable_capture(self.pg_interfaces)
1839 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001840 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001841 self.verify_capture_in(capture, self.pg0)
1842
1843 def test_static_out(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001844 """ 1:1 NAT initialized from outside network """
Matus Fabiande886752016-12-07 03:38:19 -08001845
1846 nat_ip = "10.0.0.20"
1847 self.tcp_port_out = 6303
1848 self.udp_port_out = 6304
1849 self.icmp_id_out = 6305
Matus Fabian5f224992018-01-25 21:59:16 -08001850 tag = "testTAG"
Matus Fabiande886752016-12-07 03:38:19 -08001851
Matus Fabian5f224992018-01-25 21:59:16 -08001852 self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip, tag=tag)
Matus Fabian2ba92e32017-08-21 07:05:03 -07001853 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1854 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1855 is_inside=0)
Matus Fabian5f224992018-01-25 21:59:16 -08001856 sm = self.vapi.nat44_static_mapping_dump()
1857 self.assertEqual(len(sm), 1)
1858 self.assertEqual((sm[0].tag).split('\0', 1)[0], tag)
Matus Fabiande886752016-12-07 03:38:19 -08001859
1860 # out2in
1861 pkts = self.create_stream_out(self.pg1, nat_ip)
1862 self.pg1.add_stream(pkts)
1863 self.pg_enable_capture(self.pg_interfaces)
1864 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001865 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001866 self.verify_capture_in(capture, self.pg0)
1867
1868 # in2out
1869 pkts = self.create_stream_in(self.pg0, self.pg1)
1870 self.pg0.add_stream(pkts)
1871 self.pg_enable_capture(self.pg_interfaces)
1872 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001873 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001874 self.verify_capture_out(capture, nat_ip, True)
1875
1876 def test_static_with_port_in(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001877 """ 1:1 NAPT initialized from inside network """
Matus Fabiande886752016-12-07 03:38:19 -08001878
1879 self.tcp_port_out = 3606
1880 self.udp_port_out = 3607
1881 self.icmp_id_out = 3608
1882
Matus Fabian2ba92e32017-08-21 07:05:03 -07001883 self.nat44_add_address(self.nat_addr)
1884 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1885 self.tcp_port_in, self.tcp_port_out,
1886 proto=IP_PROTOS.tcp)
1887 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1888 self.udp_port_in, self.udp_port_out,
1889 proto=IP_PROTOS.udp)
1890 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1891 self.icmp_id_in, self.icmp_id_out,
1892 proto=IP_PROTOS.icmp)
1893 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1894 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1895 is_inside=0)
Matus Fabiande886752016-12-07 03:38:19 -08001896
1897 # in2out
1898 pkts = self.create_stream_in(self.pg0, self.pg1)
1899 self.pg0.add_stream(pkts)
1900 self.pg_enable_capture(self.pg_interfaces)
1901 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001902 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001903 self.verify_capture_out(capture)
1904
1905 # out2in
1906 pkts = self.create_stream_out(self.pg1)
1907 self.pg1.add_stream(pkts)
1908 self.pg_enable_capture(self.pg_interfaces)
1909 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001910 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001911 self.verify_capture_in(capture, self.pg0)
1912
1913 def test_static_with_port_out(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001914 """ 1:1 NAPT initialized from outside network """
Matus Fabiande886752016-12-07 03:38:19 -08001915
1916 self.tcp_port_out = 30606
1917 self.udp_port_out = 30607
1918 self.icmp_id_out = 30608
1919
Matus Fabian2ba92e32017-08-21 07:05:03 -07001920 self.nat44_add_address(self.nat_addr)
1921 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1922 self.tcp_port_in, self.tcp_port_out,
1923 proto=IP_PROTOS.tcp)
1924 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1925 self.udp_port_in, self.udp_port_out,
1926 proto=IP_PROTOS.udp)
1927 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1928 self.icmp_id_in, self.icmp_id_out,
1929 proto=IP_PROTOS.icmp)
1930 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1931 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1932 is_inside=0)
Matus Fabiande886752016-12-07 03:38:19 -08001933
1934 # out2in
1935 pkts = self.create_stream_out(self.pg1)
1936 self.pg1.add_stream(pkts)
1937 self.pg_enable_capture(self.pg_interfaces)
1938 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001939 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001940 self.verify_capture_in(capture, self.pg0)
1941
1942 # in2out
1943 pkts = self.create_stream_in(self.pg0, self.pg1)
1944 self.pg0.add_stream(pkts)
1945 self.pg_enable_capture(self.pg_interfaces)
1946 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001947 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001948 self.verify_capture_out(capture)
1949
1950 def test_static_vrf_aware(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001951 """ 1:1 NAT VRF awareness """
Matus Fabiande886752016-12-07 03:38:19 -08001952
1953 nat_ip1 = "10.0.0.30"
1954 nat_ip2 = "10.0.0.40"
1955 self.tcp_port_out = 6303
1956 self.udp_port_out = 6304
1957 self.icmp_id_out = 6305
1958
Matus Fabian2ba92e32017-08-21 07:05:03 -07001959 self.nat44_add_static_mapping(self.pg4.remote_ip4, nat_ip1,
1960 vrf_id=10)
1961 self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip2,
1962 vrf_id=10)
1963 self.vapi.nat44_interface_add_del_feature(self.pg3.sw_if_index,
1964 is_inside=0)
1965 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1966 self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index)
Matus Fabiande886752016-12-07 03:38:19 -08001967
Matus Fabian2ba92e32017-08-21 07:05:03 -07001968 # inside interface VRF match NAT44 static mapping VRF
Matus Fabiande886752016-12-07 03:38:19 -08001969 pkts = self.create_stream_in(self.pg4, self.pg3)
1970 self.pg4.add_stream(pkts)
1971 self.pg_enable_capture(self.pg_interfaces)
1972 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001973 capture = self.pg3.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001974 self.verify_capture_out(capture, nat_ip1, True)
1975
Matus Fabian2ba92e32017-08-21 07:05:03 -07001976 # inside interface VRF don't match NAT44 static mapping VRF (packets
Matus Fabiande886752016-12-07 03:38:19 -08001977 # are dropped)
1978 pkts = self.create_stream_in(self.pg0, self.pg3)
1979 self.pg0.add_stream(pkts)
1980 self.pg_enable_capture(self.pg_interfaces)
1981 self.pg_start()
Klement Sekera9225dee2016-12-12 08:36:58 +01001982 self.pg3.assert_nothing_captured()
Matus Fabiande886752016-12-07 03:38:19 -08001983
Matus Fabianb793d092018-01-31 05:50:21 -08001984 def test_dynamic_to_static(self):
1985 """ Switch from dynamic translation to 1:1NAT """
1986 nat_ip = "10.0.0.10"
1987 self.tcp_port_out = 6303
1988 self.udp_port_out = 6304
1989 self.icmp_id_out = 6305
1990
1991 self.nat44_add_address(self.nat_addr)
1992 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1993 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1994 is_inside=0)
1995
1996 # dynamic
1997 pkts = self.create_stream_in(self.pg0, self.pg1)
1998 self.pg0.add_stream(pkts)
1999 self.pg_enable_capture(self.pg_interfaces)
2000 self.pg_start()
2001 capture = self.pg1.get_capture(len(pkts))
2002 self.verify_capture_out(capture)
2003
2004 # 1:1NAT
2005 self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip)
2006 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
2007 self.assertEqual(len(sessions), 0)
2008 pkts = self.create_stream_in(self.pg0, self.pg1)
2009 self.pg0.add_stream(pkts)
2010 self.pg_enable_capture(self.pg_interfaces)
2011 self.pg_start()
2012 capture = self.pg1.get_capture(len(pkts))
2013 self.verify_capture_out(capture, nat_ip, True)
2014
Matus Fabianab7a8052017-11-28 04:29:41 -08002015 def test_identity_nat(self):
2016 """ Identity NAT """
2017
2018 self.vapi.nat44_add_del_identity_mapping(ip=self.pg0.remote_ip4n)
2019 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2020 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2021 is_inside=0)
2022
2023 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
2024 IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) /
2025 TCP(sport=12345, dport=56789))
2026 self.pg1.add_stream(p)
2027 self.pg_enable_capture(self.pg_interfaces)
2028 self.pg_start()
2029 capture = self.pg0.get_capture(1)
2030 p = capture[0]
2031 try:
2032 ip = p[IP]
2033 tcp = p[TCP]
2034 self.assertEqual(ip.dst, self.pg0.remote_ip4)
2035 self.assertEqual(ip.src, self.pg1.remote_ip4)
2036 self.assertEqual(tcp.dport, 56789)
2037 self.assertEqual(tcp.sport, 12345)
Klement Sekerad81ae412018-05-16 10:52:54 +02002038 self.assert_packet_checksums_valid(p)
Matus Fabianab7a8052017-11-28 04:29:41 -08002039 except:
2040 self.logger.error(ppp("Unexpected or invalid packet:", p))
2041 raise
2042
Matus Fabiane2f4e2f2018-10-07 21:28:23 -07002043 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
2044 self.assertEqual(len(sessions), 0)
Matus Fabian82b4ceb2018-10-11 04:28:48 -07002045 self.vapi.nat44_add_del_identity_mapping(ip=self.pg0.remote_ip4n,
2046 vrf_id=1)
2047 identity_mappings = self.vapi.nat44_identity_mapping_dump()
2048 self.assertEqual(len(identity_mappings), 2)
Matus Fabiane2f4e2f2018-10-07 21:28:23 -07002049
Matus Fabiande886752016-12-07 03:38:19 -08002050 def test_multiple_inside_interfaces(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002051 """ NAT44 multiple non-overlapping address space inside interfaces """
Matus Fabiande886752016-12-07 03:38:19 -08002052
Matus Fabian2ba92e32017-08-21 07:05:03 -07002053 self.nat44_add_address(self.nat_addr)
2054 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2055 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index)
2056 self.vapi.nat44_interface_add_del_feature(self.pg3.sw_if_index,
2057 is_inside=0)
Matus Fabiande886752016-12-07 03:38:19 -08002058
Matus Fabian2ba92e32017-08-21 07:05:03 -07002059 # between two NAT44 inside interfaces (no translation)
Matus Fabian675a69c2017-01-18 01:46:01 -08002060 pkts = self.create_stream_in(self.pg0, self.pg1)
2061 self.pg0.add_stream(pkts)
2062 self.pg_enable_capture(self.pg_interfaces)
2063 self.pg_start()
2064 capture = self.pg1.get_capture(len(pkts))
2065 self.verify_capture_no_translation(capture, self.pg0, self.pg1)
2066
Matus Fabian2ba92e32017-08-21 07:05:03 -07002067 # from NAT44 inside to interface without NAT44 feature (no translation)
Matus Fabian675a69c2017-01-18 01:46:01 -08002068 pkts = self.create_stream_in(self.pg0, self.pg2)
2069 self.pg0.add_stream(pkts)
2070 self.pg_enable_capture(self.pg_interfaces)
2071 self.pg_start()
2072 capture = self.pg2.get_capture(len(pkts))
2073 self.verify_capture_no_translation(capture, self.pg0, self.pg2)
2074
Matus Fabiande886752016-12-07 03:38:19 -08002075 # in2out 1st interface
2076 pkts = self.create_stream_in(self.pg0, self.pg3)
2077 self.pg0.add_stream(pkts)
2078 self.pg_enable_capture(self.pg_interfaces)
2079 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002080 capture = self.pg3.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002081 self.verify_capture_out(capture)
2082
2083 # out2in 1st interface
2084 pkts = self.create_stream_out(self.pg3)
2085 self.pg3.add_stream(pkts)
2086 self.pg_enable_capture(self.pg_interfaces)
2087 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002088 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002089 self.verify_capture_in(capture, self.pg0)
2090
2091 # in2out 2nd interface
2092 pkts = self.create_stream_in(self.pg1, self.pg3)
2093 self.pg1.add_stream(pkts)
2094 self.pg_enable_capture(self.pg_interfaces)
2095 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002096 capture = self.pg3.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002097 self.verify_capture_out(capture)
2098
2099 # out2in 2nd interface
2100 pkts = self.create_stream_out(self.pg3)
2101 self.pg3.add_stream(pkts)
2102 self.pg_enable_capture(self.pg_interfaces)
2103 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002104 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002105 self.verify_capture_in(capture, self.pg1)
2106
Matus Fabiande886752016-12-07 03:38:19 -08002107 def test_inside_overlapping_interfaces(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002108 """ NAT44 multiple inside interfaces with overlapping address space """
Matus Fabiande886752016-12-07 03:38:19 -08002109
Matus Fabian675a69c2017-01-18 01:46:01 -08002110 static_nat_ip = "10.0.0.10"
Matus Fabian2ba92e32017-08-21 07:05:03 -07002111 self.nat44_add_address(self.nat_addr)
2112 self.vapi.nat44_interface_add_del_feature(self.pg3.sw_if_index,
2113 is_inside=0)
2114 self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index)
2115 self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index)
2116 self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index)
2117 self.nat44_add_static_mapping(self.pg6.remote_ip4, static_nat_ip,
2118 vrf_id=20)
Matus Fabian675a69c2017-01-18 01:46:01 -08002119
Matus Fabian2ba92e32017-08-21 07:05:03 -07002120 # between NAT44 inside interfaces with same VRF (no translation)
Matus Fabian675a69c2017-01-18 01:46:01 -08002121 pkts = self.create_stream_in(self.pg4, self.pg5)
2122 self.pg4.add_stream(pkts)
2123 self.pg_enable_capture(self.pg_interfaces)
2124 self.pg_start()
2125 capture = self.pg5.get_capture(len(pkts))
2126 self.verify_capture_no_translation(capture, self.pg4, self.pg5)
2127
Matus Fabian2ba92e32017-08-21 07:05:03 -07002128 # between NAT44 inside interfaces with different VRF (hairpinning)
Matus Fabian675a69c2017-01-18 01:46:01 -08002129 p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) /
2130 IP(src=self.pg4.remote_ip4, dst=static_nat_ip) /
2131 TCP(sport=1234, dport=5678))
2132 self.pg4.add_stream(p)
2133 self.pg_enable_capture(self.pg_interfaces)
2134 self.pg_start()
2135 capture = self.pg6.get_capture(1)
2136 p = capture[0]
2137 try:
2138 ip = p[IP]
2139 tcp = p[TCP]
Matus Fabian2ba92e32017-08-21 07:05:03 -07002140 self.assertEqual(ip.src, self.nat_addr)
Matus Fabian675a69c2017-01-18 01:46:01 -08002141 self.assertEqual(ip.dst, self.pg6.remote_ip4)
2142 self.assertNotEqual(tcp.sport, 1234)
2143 self.assertEqual(tcp.dport, 5678)
2144 except:
2145 self.logger.error(ppp("Unexpected or invalid packet:", p))
2146 raise
Matus Fabiande886752016-12-07 03:38:19 -08002147
2148 # in2out 1st interface
2149 pkts = self.create_stream_in(self.pg4, self.pg3)
2150 self.pg4.add_stream(pkts)
2151 self.pg_enable_capture(self.pg_interfaces)
2152 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002153 capture = self.pg3.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002154 self.verify_capture_out(capture)
2155
2156 # out2in 1st interface
2157 pkts = self.create_stream_out(self.pg3)
2158 self.pg3.add_stream(pkts)
2159 self.pg_enable_capture(self.pg_interfaces)
2160 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002161 capture = self.pg4.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002162 self.verify_capture_in(capture, self.pg4)
2163
2164 # in2out 2nd interface
2165 pkts = self.create_stream_in(self.pg5, self.pg3)
2166 self.pg5.add_stream(pkts)
2167 self.pg_enable_capture(self.pg_interfaces)
2168 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002169 capture = self.pg3.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002170 self.verify_capture_out(capture)
2171
2172 # out2in 2nd interface
2173 pkts = self.create_stream_out(self.pg3)
2174 self.pg3.add_stream(pkts)
2175 self.pg_enable_capture(self.pg_interfaces)
2176 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002177 capture = self.pg5.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002178 self.verify_capture_in(capture, self.pg5)
2179
magalik23caa882017-02-08 23:25:45 -08002180 # pg5 session dump
Matus Fabian2ba92e32017-08-21 07:05:03 -07002181 addresses = self.vapi.nat44_address_dump()
magalik23caa882017-02-08 23:25:45 -08002182 self.assertEqual(len(addresses), 1)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002183 sessions = self.vapi.nat44_user_session_dump(self.pg5.remote_ip4n, 10)
magalik23caa882017-02-08 23:25:45 -08002184 self.assertEqual(len(sessions), 3)
2185 for session in sessions:
2186 self.assertFalse(session.is_static)
2187 self.assertEqual(session.inside_ip_address[0:4],
2188 self.pg5.remote_ip4n)
2189 self.assertEqual(session.outside_ip_address,
2190 addresses[0].ip_address)
2191 self.assertEqual(sessions[0].protocol, IP_PROTOS.tcp)
2192 self.assertEqual(sessions[1].protocol, IP_PROTOS.udp)
2193 self.assertEqual(sessions[2].protocol, IP_PROTOS.icmp)
2194 self.assertEqual(sessions[0].inside_port, self.tcp_port_in)
2195 self.assertEqual(sessions[1].inside_port, self.udp_port_in)
2196 self.assertEqual(sessions[2].inside_port, self.icmp_id_in)
2197 self.assertEqual(sessions[0].outside_port, self.tcp_port_out)
2198 self.assertEqual(sessions[1].outside_port, self.udp_port_out)
2199 self.assertEqual(sessions[2].outside_port, self.icmp_id_out)
2200
Matus Fabiande886752016-12-07 03:38:19 -08002201 # in2out 3rd interface
2202 pkts = self.create_stream_in(self.pg6, self.pg3)
2203 self.pg6.add_stream(pkts)
2204 self.pg_enable_capture(self.pg_interfaces)
2205 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002206 capture = self.pg3.get_capture(len(pkts))
Matus Fabian675a69c2017-01-18 01:46:01 -08002207 self.verify_capture_out(capture, static_nat_ip, True)
Matus Fabiande886752016-12-07 03:38:19 -08002208
2209 # out2in 3rd interface
Matus Fabian675a69c2017-01-18 01:46:01 -08002210 pkts = self.create_stream_out(self.pg3, static_nat_ip)
Matus Fabiande886752016-12-07 03:38:19 -08002211 self.pg3.add_stream(pkts)
2212 self.pg_enable_capture(self.pg_interfaces)
2213 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002214 capture = self.pg6.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002215 self.verify_capture_in(capture, self.pg6)
2216
magalik23caa882017-02-08 23:25:45 -08002217 # general user and session dump verifications
Matus Fabian2ba92e32017-08-21 07:05:03 -07002218 users = self.vapi.nat44_user_dump()
Paul Vinciguerra3d2df212018-11-24 23:19:53 -08002219 self.assertGreaterEqual(len(users), 3)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002220 addresses = self.vapi.nat44_address_dump()
magalik23caa882017-02-08 23:25:45 -08002221 self.assertEqual(len(addresses), 1)
2222 for user in users:
Matus Fabian2ba92e32017-08-21 07:05:03 -07002223 sessions = self.vapi.nat44_user_session_dump(user.ip_address,
2224 user.vrf_id)
magalik23caa882017-02-08 23:25:45 -08002225 for session in sessions:
2226 self.assertEqual(user.ip_address, session.inside_ip_address)
2227 self.assertTrue(session.total_bytes > session.total_pkts > 0)
2228 self.assertTrue(session.protocol in
2229 [IP_PROTOS.tcp, IP_PROTOS.udp,
2230 IP_PROTOS.icmp])
Matus Fabian70a26ac2018-05-14 06:20:28 -07002231 self.assertFalse(session.ext_host_valid)
magalik23caa882017-02-08 23:25:45 -08002232
2233 # pg4 session dump
Matus Fabian2ba92e32017-08-21 07:05:03 -07002234 sessions = self.vapi.nat44_user_session_dump(self.pg4.remote_ip4n, 10)
Paul Vinciguerra3d2df212018-11-24 23:19:53 -08002235 self.assertGreaterEqual(len(sessions), 4)
magalik23caa882017-02-08 23:25:45 -08002236 for session in sessions:
2237 self.assertFalse(session.is_static)
2238 self.assertEqual(session.inside_ip_address[0:4],
2239 self.pg4.remote_ip4n)
2240 self.assertEqual(session.outside_ip_address,
2241 addresses[0].ip_address)
2242
2243 # pg6 session dump
Matus Fabian2ba92e32017-08-21 07:05:03 -07002244 sessions = self.vapi.nat44_user_session_dump(self.pg6.remote_ip4n, 20)
Paul Vinciguerra3d2df212018-11-24 23:19:53 -08002245 self.assertGreaterEqual(len(sessions), 3)
magalik23caa882017-02-08 23:25:45 -08002246 for session in sessions:
2247 self.assertTrue(session.is_static)
2248 self.assertEqual(session.inside_ip_address[0:4],
2249 self.pg6.remote_ip4n)
2250 self.assertEqual(map(ord, session.outside_ip_address[0:4]),
2251 map(int, static_nat_ip.split('.')))
2252 self.assertTrue(session.inside_port in
2253 [self.tcp_port_in, self.udp_port_in,
2254 self.icmp_id_in])
2255
Matus Fabianf78a70d2016-12-12 04:30:39 -08002256 def test_hairpinning(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002257 """ NAT44 hairpinning - 1:1 NAPT """
Matus Fabianf78a70d2016-12-12 04:30:39 -08002258
2259 host = self.pg0.remote_hosts[0]
2260 server = self.pg0.remote_hosts[1]
2261 host_in_port = 1234
2262 host_out_port = 0
2263 server_in_port = 5678
2264 server_out_port = 8765
2265
Matus Fabian2ba92e32017-08-21 07:05:03 -07002266 self.nat44_add_address(self.nat_addr)
2267 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2268 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2269 is_inside=0)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002270 # add static mapping for server
Matus Fabian2ba92e32017-08-21 07:05:03 -07002271 self.nat44_add_static_mapping(server.ip4, self.nat_addr,
2272 server_in_port, server_out_port,
2273 proto=IP_PROTOS.tcp)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002274
2275 # send packet from host to server
2276 p = (Ether(src=host.mac, dst=self.pg0.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002277 IP(src=host.ip4, dst=self.nat_addr) /
Matus Fabianf78a70d2016-12-12 04:30:39 -08002278 TCP(sport=host_in_port, dport=server_out_port))
2279 self.pg0.add_stream(p)
2280 self.pg_enable_capture(self.pg_interfaces)
2281 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002282 capture = self.pg0.get_capture(1)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002283 p = capture[0]
2284 try:
2285 ip = p[IP]
2286 tcp = p[TCP]
Matus Fabian2ba92e32017-08-21 07:05:03 -07002287 self.assertEqual(ip.src, self.nat_addr)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002288 self.assertEqual(ip.dst, server.ip4)
2289 self.assertNotEqual(tcp.sport, host_in_port)
2290 self.assertEqual(tcp.dport, server_in_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02002291 self.assert_packet_checksums_valid(p)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002292 host_out_port = tcp.sport
2293 except:
Klement Sekera9225dee2016-12-12 08:36:58 +01002294 self.logger.error(ppp("Unexpected or invalid packet:", p))
Matus Fabianf78a70d2016-12-12 04:30:39 -08002295 raise
2296
2297 # send reply from server to host
2298 p = (Ether(src=server.mac, dst=self.pg0.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002299 IP(src=server.ip4, dst=self.nat_addr) /
Matus Fabianf78a70d2016-12-12 04:30:39 -08002300 TCP(sport=server_in_port, dport=host_out_port))
2301 self.pg0.add_stream(p)
2302 self.pg_enable_capture(self.pg_interfaces)
2303 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002304 capture = self.pg0.get_capture(1)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002305 p = capture[0]
2306 try:
2307 ip = p[IP]
2308 tcp = p[TCP]
Matus Fabian2ba92e32017-08-21 07:05:03 -07002309 self.assertEqual(ip.src, self.nat_addr)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002310 self.assertEqual(ip.dst, host.ip4)
2311 self.assertEqual(tcp.sport, server_out_port)
2312 self.assertEqual(tcp.dport, host_in_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02002313 self.assert_packet_checksums_valid(p)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002314 except:
Matus Fabiane82488f2018-01-18 03:38:45 -08002315 self.logger.error(ppp("Unexpected or invalid packet:", p))
Matus Fabianf78a70d2016-12-12 04:30:39 -08002316 raise
2317
Matus Fabian6fa74c62017-06-05 05:55:48 -07002318 def test_hairpinning2(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002319 """ NAT44 hairpinning - 1:1 NAT"""
Matus Fabian6fa74c62017-06-05 05:55:48 -07002320
2321 server1_nat_ip = "10.0.0.10"
2322 server2_nat_ip = "10.0.0.11"
2323 host = self.pg0.remote_hosts[0]
2324 server1 = self.pg0.remote_hosts[1]
2325 server2 = self.pg0.remote_hosts[2]
2326 server_tcp_port = 22
2327 server_udp_port = 20
2328
Matus Fabian2ba92e32017-08-21 07:05:03 -07002329 self.nat44_add_address(self.nat_addr)
2330 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2331 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2332 is_inside=0)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002333
2334 # add static mapping for servers
Matus Fabian2ba92e32017-08-21 07:05:03 -07002335 self.nat44_add_static_mapping(server1.ip4, server1_nat_ip)
2336 self.nat44_add_static_mapping(server2.ip4, server2_nat_ip)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002337
2338 # host to server1
2339 pkts = []
2340 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2341 IP(src=host.ip4, dst=server1_nat_ip) /
2342 TCP(sport=self.tcp_port_in, dport=server_tcp_port))
2343 pkts.append(p)
2344 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2345 IP(src=host.ip4, dst=server1_nat_ip) /
2346 UDP(sport=self.udp_port_in, dport=server_udp_port))
2347 pkts.append(p)
2348 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2349 IP(src=host.ip4, dst=server1_nat_ip) /
2350 ICMP(id=self.icmp_id_in, type='echo-request'))
2351 pkts.append(p)
2352 self.pg0.add_stream(pkts)
2353 self.pg_enable_capture(self.pg_interfaces)
2354 self.pg_start()
2355 capture = self.pg0.get_capture(len(pkts))
2356 for packet in capture:
2357 try:
Matus Fabian2ba92e32017-08-21 07:05:03 -07002358 self.assertEqual(packet[IP].src, self.nat_addr)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002359 self.assertEqual(packet[IP].dst, server1.ip4)
2360 if packet.haslayer(TCP):
2361 self.assertNotEqual(packet[TCP].sport, self.tcp_port_in)
2362 self.assertEqual(packet[TCP].dport, server_tcp_port)
2363 self.tcp_port_out = packet[TCP].sport
Klement Sekerad81ae412018-05-16 10:52:54 +02002364 self.assert_packet_checksums_valid(packet)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002365 elif packet.haslayer(UDP):
2366 self.assertNotEqual(packet[UDP].sport, self.udp_port_in)
2367 self.assertEqual(packet[UDP].dport, server_udp_port)
2368 self.udp_port_out = packet[UDP].sport
2369 else:
2370 self.assertNotEqual(packet[ICMP].id, self.icmp_id_in)
2371 self.icmp_id_out = packet[ICMP].id
2372 except:
2373 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2374 raise
2375
2376 # server1 to host
2377 pkts = []
2378 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002379 IP(src=server1.ip4, dst=self.nat_addr) /
Matus Fabian6fa74c62017-06-05 05:55:48 -07002380 TCP(sport=server_tcp_port, dport=self.tcp_port_out))
2381 pkts.append(p)
2382 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002383 IP(src=server1.ip4, dst=self.nat_addr) /
Matus Fabian6fa74c62017-06-05 05:55:48 -07002384 UDP(sport=server_udp_port, dport=self.udp_port_out))
2385 pkts.append(p)
2386 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002387 IP(src=server1.ip4, dst=self.nat_addr) /
Matus Fabian6fa74c62017-06-05 05:55:48 -07002388 ICMP(id=self.icmp_id_out, type='echo-reply'))
2389 pkts.append(p)
2390 self.pg0.add_stream(pkts)
2391 self.pg_enable_capture(self.pg_interfaces)
2392 self.pg_start()
2393 capture = self.pg0.get_capture(len(pkts))
2394 for packet in capture:
2395 try:
2396 self.assertEqual(packet[IP].src, server1_nat_ip)
2397 self.assertEqual(packet[IP].dst, host.ip4)
2398 if packet.haslayer(TCP):
2399 self.assertEqual(packet[TCP].dport, self.tcp_port_in)
2400 self.assertEqual(packet[TCP].sport, server_tcp_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02002401 self.assert_packet_checksums_valid(packet)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002402 elif packet.haslayer(UDP):
2403 self.assertEqual(packet[UDP].dport, self.udp_port_in)
2404 self.assertEqual(packet[UDP].sport, server_udp_port)
2405 else:
2406 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
2407 except:
2408 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2409 raise
2410
2411 # server2 to server1
2412 pkts = []
2413 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2414 IP(src=server2.ip4, dst=server1_nat_ip) /
2415 TCP(sport=self.tcp_port_in, dport=server_tcp_port))
2416 pkts.append(p)
2417 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2418 IP(src=server2.ip4, dst=server1_nat_ip) /
2419 UDP(sport=self.udp_port_in, dport=server_udp_port))
2420 pkts.append(p)
2421 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2422 IP(src=server2.ip4, dst=server1_nat_ip) /
2423 ICMP(id=self.icmp_id_in, type='echo-request'))
2424 pkts.append(p)
2425 self.pg0.add_stream(pkts)
2426 self.pg_enable_capture(self.pg_interfaces)
2427 self.pg_start()
2428 capture = self.pg0.get_capture(len(pkts))
2429 for packet in capture:
2430 try:
2431 self.assertEqual(packet[IP].src, server2_nat_ip)
2432 self.assertEqual(packet[IP].dst, server1.ip4)
2433 if packet.haslayer(TCP):
2434 self.assertEqual(packet[TCP].sport, self.tcp_port_in)
2435 self.assertEqual(packet[TCP].dport, server_tcp_port)
2436 self.tcp_port_out = packet[TCP].sport
Klement Sekerad81ae412018-05-16 10:52:54 +02002437 self.assert_packet_checksums_valid(packet)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002438 elif packet.haslayer(UDP):
2439 self.assertEqual(packet[UDP].sport, self.udp_port_in)
2440 self.assertEqual(packet[UDP].dport, server_udp_port)
2441 self.udp_port_out = packet[UDP].sport
2442 else:
2443 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
2444 self.icmp_id_out = packet[ICMP].id
2445 except:
2446 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2447 raise
2448
2449 # server1 to server2
2450 pkts = []
2451 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2452 IP(src=server1.ip4, dst=server2_nat_ip) /
2453 TCP(sport=server_tcp_port, dport=self.tcp_port_out))
2454 pkts.append(p)
2455 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2456 IP(src=server1.ip4, dst=server2_nat_ip) /
2457 UDP(sport=server_udp_port, dport=self.udp_port_out))
2458 pkts.append(p)
2459 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2460 IP(src=server1.ip4, dst=server2_nat_ip) /
2461 ICMP(id=self.icmp_id_out, type='echo-reply'))
2462 pkts.append(p)
2463 self.pg0.add_stream(pkts)
2464 self.pg_enable_capture(self.pg_interfaces)
2465 self.pg_start()
2466 capture = self.pg0.get_capture(len(pkts))
2467 for packet in capture:
2468 try:
2469 self.assertEqual(packet[IP].src, server1_nat_ip)
2470 self.assertEqual(packet[IP].dst, server2.ip4)
2471 if packet.haslayer(TCP):
2472 self.assertEqual(packet[TCP].dport, self.tcp_port_in)
2473 self.assertEqual(packet[TCP].sport, server_tcp_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02002474 self.assert_packet_checksums_valid(packet)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002475 elif packet.haslayer(UDP):
2476 self.assertEqual(packet[UDP].dport, self.udp_port_in)
2477 self.assertEqual(packet[UDP].sport, server_udp_port)
2478 else:
2479 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
2480 except:
2481 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2482 raise
2483
Matus Fabian9902fcd2016-12-21 23:58:46 -08002484 def test_max_translations_per_user(self):
2485 """ MAX translations per user - recycle the least recently used """
2486
Matus Fabian2ba92e32017-08-21 07:05:03 -07002487 self.nat44_add_address(self.nat_addr)
2488 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2489 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2490 is_inside=0)
Matus Fabian9902fcd2016-12-21 23:58:46 -08002491
2492 # get maximum number of translations per user
Matus Fabian2ba92e32017-08-21 07:05:03 -07002493 nat44_config = self.vapi.nat_show_config()
Matus Fabian9902fcd2016-12-21 23:58:46 -08002494
2495 # send more than maximum number of translations per user packets
Matus Fabian2ba92e32017-08-21 07:05:03 -07002496 pkts_num = nat44_config.max_translations_per_user + 5
Matus Fabian9902fcd2016-12-21 23:58:46 -08002497 pkts = []
2498 for port in range(0, pkts_num):
2499 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2500 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
2501 TCP(sport=1025 + port))
2502 pkts.append(p)
2503 self.pg0.add_stream(pkts)
2504 self.pg_enable_capture(self.pg_interfaces)
2505 self.pg_start()
2506
2507 # verify number of translated packet
Klement Sekeradab231a2016-12-21 08:50:14 +01002508 self.pg1.get_capture(pkts_num)
Matus Fabian9902fcd2016-12-21 23:58:46 -08002509
Matus Fabian132dc492018-05-09 04:51:03 -07002510 users = self.vapi.nat44_user_dump()
2511 for user in users:
2512 if user.ip_address == self.pg0.remote_ip4n:
2513 self.assertEqual(user.nsessions,
2514 nat44_config.max_translations_per_user)
2515 self.assertEqual(user.nstaticsessions, 0)
2516
2517 tcp_port = 22
2518 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
2519 tcp_port, tcp_port,
2520 proto=IP_PROTOS.tcp)
2521 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2522 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
2523 TCP(sport=tcp_port))
2524 self.pg0.add_stream(p)
2525 self.pg_enable_capture(self.pg_interfaces)
2526 self.pg_start()
2527 self.pg1.get_capture(1)
2528 users = self.vapi.nat44_user_dump()
2529 for user in users:
2530 if user.ip_address == self.pg0.remote_ip4n:
2531 self.assertEqual(user.nsessions,
2532 nat44_config.max_translations_per_user - 1)
2533 self.assertEqual(user.nstaticsessions, 1)
2534
Matus Fabian8bf68e82017-01-12 04:24:35 -08002535 def test_interface_addr(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002536 """ Acquire NAT44 addresses from interface """
2537 self.vapi.nat44_add_interface_addr(self.pg7.sw_if_index)
Matus Fabian8bf68e82017-01-12 04:24:35 -08002538
2539 # no address in NAT pool
Matus Fabian2ba92e32017-08-21 07:05:03 -07002540 adresses = self.vapi.nat44_address_dump()
Matus Fabian8bf68e82017-01-12 04:24:35 -08002541 self.assertEqual(0, len(adresses))
2542
2543 # configure interface address and check NAT address pool
2544 self.pg7.config_ip4()
Matus Fabian2ba92e32017-08-21 07:05:03 -07002545 adresses = self.vapi.nat44_address_dump()
Matus Fabian8bf68e82017-01-12 04:24:35 -08002546 self.assertEqual(1, len(adresses))
Matus Fabian36532bd2017-01-23 23:42:28 -08002547 self.assertEqual(adresses[0].ip_address[0:4], self.pg7.local_ip4n)
Matus Fabian8bf68e82017-01-12 04:24:35 -08002548
2549 # remove interface address and check NAT address pool
2550 self.pg7.unconfig_ip4()
Matus Fabian2ba92e32017-08-21 07:05:03 -07002551 adresses = self.vapi.nat44_address_dump()
Matus Fabian8bf68e82017-01-12 04:24:35 -08002552 self.assertEqual(0, len(adresses))
2553
Matus Fabian36532bd2017-01-23 23:42:28 -08002554 def test_interface_addr_static_mapping(self):
2555 """ Static mapping with addresses from interface """
Matus Fabian5f224992018-01-25 21:59:16 -08002556 tag = "testTAG"
2557
Matus Fabian2ba92e32017-08-21 07:05:03 -07002558 self.vapi.nat44_add_interface_addr(self.pg7.sw_if_index)
2559 self.nat44_add_static_mapping(
2560 '1.2.3.4',
Matus Fabian5f224992018-01-25 21:59:16 -08002561 external_sw_if_index=self.pg7.sw_if_index,
2562 tag=tag)
Matus Fabian36532bd2017-01-23 23:42:28 -08002563
Matus Fabiane22e5462017-02-14 23:33:43 -08002564 # static mappings with external interface
Matus Fabian2ba92e32017-08-21 07:05:03 -07002565 static_mappings = self.vapi.nat44_static_mapping_dump()
Matus Fabiane22e5462017-02-14 23:33:43 -08002566 self.assertEqual(1, len(static_mappings))
2567 self.assertEqual(self.pg7.sw_if_index,
2568 static_mappings[0].external_sw_if_index)
Matus Fabian5f224992018-01-25 21:59:16 -08002569 self.assertEqual((static_mappings[0].tag).split('\0', 1)[0], tag)
Matus Fabian36532bd2017-01-23 23:42:28 -08002570
2571 # configure interface address and check static mappings
2572 self.pg7.config_ip4()
Matus Fabian2ba92e32017-08-21 07:05:03 -07002573 static_mappings = self.vapi.nat44_static_mapping_dump()
Matus Fabianea2600a2018-03-28 04:06:26 -07002574 self.assertEqual(2, len(static_mappings))
2575 resolved = False
2576 for sm in static_mappings:
2577 if sm.external_sw_if_index == 0xFFFFFFFF:
2578 self.assertEqual(sm.external_ip_address[0:4],
2579 self.pg7.local_ip4n)
2580 self.assertEqual((sm.tag).split('\0', 1)[0], tag)
2581 resolved = True
2582 self.assertTrue(resolved)
Matus Fabian36532bd2017-01-23 23:42:28 -08002583
2584 # remove interface address and check static mappings
2585 self.pg7.unconfig_ip4()
Matus Fabian2ba92e32017-08-21 07:05:03 -07002586 static_mappings = self.vapi.nat44_static_mapping_dump()
Matus Fabianea2600a2018-03-28 04:06:26 -07002587 self.assertEqual(1, len(static_mappings))
2588 self.assertEqual(self.pg7.sw_if_index,
2589 static_mappings[0].external_sw_if_index)
2590 self.assertEqual((static_mappings[0].tag).split('\0', 1)[0], tag)
2591
2592 # configure interface address again and check static mappings
2593 self.pg7.config_ip4()
2594 static_mappings = self.vapi.nat44_static_mapping_dump()
2595 self.assertEqual(2, len(static_mappings))
2596 resolved = False
2597 for sm in static_mappings:
2598 if sm.external_sw_if_index == 0xFFFFFFFF:
2599 self.assertEqual(sm.external_ip_address[0:4],
2600 self.pg7.local_ip4n)
2601 self.assertEqual((sm.tag).split('\0', 1)[0], tag)
2602 resolved = True
2603 self.assertTrue(resolved)
2604
2605 # remove static mapping
2606 self.nat44_add_static_mapping(
2607 '1.2.3.4',
2608 external_sw_if_index=self.pg7.sw_if_index,
2609 tag=tag,
2610 is_add=0)
2611 static_mappings = self.vapi.nat44_static_mapping_dump()
Matus Fabian36532bd2017-01-23 23:42:28 -08002612 self.assertEqual(0, len(static_mappings))
2613
Matus Fabianab7a8052017-11-28 04:29:41 -08002614 def test_interface_addr_identity_nat(self):
2615 """ Identity NAT with addresses from interface """
2616
2617 port = 53053
2618 self.vapi.nat44_add_interface_addr(self.pg7.sw_if_index)
2619 self.vapi.nat44_add_del_identity_mapping(
2620 sw_if_index=self.pg7.sw_if_index,
2621 port=port,
2622 protocol=IP_PROTOS.tcp,
2623 addr_only=0)
2624
2625 # identity mappings with external interface
2626 identity_mappings = self.vapi.nat44_identity_mapping_dump()
2627 self.assertEqual(1, len(identity_mappings))
2628 self.assertEqual(self.pg7.sw_if_index,
2629 identity_mappings[0].sw_if_index)
2630
2631 # configure interface address and check identity mappings
2632 self.pg7.config_ip4()
2633 identity_mappings = self.vapi.nat44_identity_mapping_dump()
Matus Fabianea2600a2018-03-28 04:06:26 -07002634 resolved = False
2635 self.assertEqual(2, len(identity_mappings))
2636 for sm in identity_mappings:
2637 if sm.sw_if_index == 0xFFFFFFFF:
2638 self.assertEqual(identity_mappings[0].ip_address,
2639 self.pg7.local_ip4n)
2640 self.assertEqual(port, identity_mappings[0].port)
2641 self.assertEqual(IP_PROTOS.tcp, identity_mappings[0].protocol)
2642 resolved = True
2643 self.assertTrue(resolved)
Matus Fabianab7a8052017-11-28 04:29:41 -08002644
2645 # remove interface address and check identity mappings
2646 self.pg7.unconfig_ip4()
2647 identity_mappings = self.vapi.nat44_identity_mapping_dump()
Matus Fabianea2600a2018-03-28 04:06:26 -07002648 self.assertEqual(1, len(identity_mappings))
2649 self.assertEqual(self.pg7.sw_if_index,
2650 identity_mappings[0].sw_if_index)
Matus Fabianab7a8052017-11-28 04:29:41 -08002651
Matus Fabianeea28d72017-01-13 04:15:54 -08002652 def test_ipfix_nat44_sess(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002653 """ IPFIX logging NAT44 session created/delted """
Matus Fabian6631e9c2017-05-24 01:52:20 -07002654 self.ipfix_domain_id = 10
2655 self.ipfix_src_port = 20202
2656 colector_port = 30303
2657 bind_layers(UDP, IPFIX, dport=30303)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002658 self.nat44_add_address(self.nat_addr)
2659 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2660 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2661 is_inside=0)
Matus Fabianeea28d72017-01-13 04:15:54 -08002662 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
2663 src_address=self.pg3.local_ip4n,
2664 path_mtu=512,
Matus Fabian6631e9c2017-05-24 01:52:20 -07002665 template_interval=10,
2666 collector_port=colector_port)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002667 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
2668 src_port=self.ipfix_src_port)
Matus Fabianeea28d72017-01-13 04:15:54 -08002669
2670 pkts = self.create_stream_in(self.pg0, self.pg1)
2671 self.pg0.add_stream(pkts)
2672 self.pg_enable_capture(self.pg_interfaces)
2673 self.pg_start()
2674 capture = self.pg1.get_capture(len(pkts))
2675 self.verify_capture_out(capture)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002676 self.nat44_add_address(self.nat_addr, is_add=0)
Matus Fabianeea28d72017-01-13 04:15:54 -08002677 self.vapi.cli("ipfix flush") # FIXME this should be an API call
Matus Fabiana431ad12018-01-04 04:03:14 -08002678 capture = self.pg3.get_capture(9)
Matus Fabianeea28d72017-01-13 04:15:54 -08002679 ipfix = IPFIXDecoder()
2680 # first load template
2681 for p in capture:
2682 self.assertTrue(p.haslayer(IPFIX))
Matus Fabian6631e9c2017-05-24 01:52:20 -07002683 self.assertEqual(p[IP].src, self.pg3.local_ip4)
2684 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
2685 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
2686 self.assertEqual(p[UDP].dport, colector_port)
2687 self.assertEqual(p[IPFIX].observationDomainID,
2688 self.ipfix_domain_id)
Matus Fabianeea28d72017-01-13 04:15:54 -08002689 if p.haslayer(Template):
2690 ipfix.add_template(p.getlayer(Template))
2691 # verify events in data set
2692 for p in capture:
2693 if p.haslayer(Data):
2694 data = ipfix.decode_data_set(p.getlayer(Set))
2695 self.verify_ipfix_nat44_ses(data)
2696
2697 def test_ipfix_addr_exhausted(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002698 """ IPFIX logging NAT addresses exhausted """
2699 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2700 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2701 is_inside=0)
Matus Fabianeea28d72017-01-13 04:15:54 -08002702 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
2703 src_address=self.pg3.local_ip4n,
2704 path_mtu=512,
2705 template_interval=10)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002706 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
2707 src_port=self.ipfix_src_port)
Matus Fabianeea28d72017-01-13 04:15:54 -08002708
2709 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
2710 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
2711 TCP(sport=3025))
2712 self.pg0.add_stream(p)
2713 self.pg_enable_capture(self.pg_interfaces)
2714 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07002715 self.pg1.assert_nothing_captured()
2716 sleep(1)
Matus Fabianeea28d72017-01-13 04:15:54 -08002717 self.vapi.cli("ipfix flush") # FIXME this should be an API call
Matus Fabiana431ad12018-01-04 04:03:14 -08002718 capture = self.pg3.get_capture(9)
Matus Fabianeea28d72017-01-13 04:15:54 -08002719 ipfix = IPFIXDecoder()
2720 # first load template
2721 for p in capture:
2722 self.assertTrue(p.haslayer(IPFIX))
Matus Fabian6631e9c2017-05-24 01:52:20 -07002723 self.assertEqual(p[IP].src, self.pg3.local_ip4)
2724 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
2725 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
2726 self.assertEqual(p[UDP].dport, 4739)
2727 self.assertEqual(p[IPFIX].observationDomainID,
2728 self.ipfix_domain_id)
Matus Fabianeea28d72017-01-13 04:15:54 -08002729 if p.haslayer(Template):
2730 ipfix.add_template(p.getlayer(Template))
2731 # verify events in data set
2732 for p in capture:
2733 if p.haslayer(Data):
2734 data = ipfix.decode_data_set(p.getlayer(Set))
2735 self.verify_ipfix_addr_exhausted(data)
2736
Paul Vinciguerradefde0f2018-12-06 07:46:13 -08002737 @unittest.skipUnless(running_extended_tests, "part of extended tests")
Matus Fabiana431ad12018-01-04 04:03:14 -08002738 def test_ipfix_max_sessions(self):
2739 """ IPFIX logging maximum session entries exceeded """
2740 self.nat44_add_address(self.nat_addr)
2741 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2742 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2743 is_inside=0)
2744
2745 nat44_config = self.vapi.nat_show_config()
2746 max_sessions = 10 * nat44_config.translation_buckets
2747
2748 pkts = []
2749 for i in range(0, max_sessions):
2750 src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF)
2751 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2752 IP(src=src, dst=self.pg1.remote_ip4) /
2753 TCP(sport=1025))
2754 pkts.append(p)
2755 self.pg0.add_stream(pkts)
2756 self.pg_enable_capture(self.pg_interfaces)
2757 self.pg_start()
2758
2759 self.pg1.get_capture(max_sessions)
2760 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
2761 src_address=self.pg3.local_ip4n,
2762 path_mtu=512,
2763 template_interval=10)
2764 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
2765 src_port=self.ipfix_src_port)
2766
2767 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2768 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
2769 TCP(sport=1025))
2770 self.pg0.add_stream(p)
2771 self.pg_enable_capture(self.pg_interfaces)
2772 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07002773 self.pg1.assert_nothing_captured()
2774 sleep(1)
Matus Fabiana431ad12018-01-04 04:03:14 -08002775 self.vapi.cli("ipfix flush") # FIXME this should be an API call
2776 capture = self.pg3.get_capture(9)
2777 ipfix = IPFIXDecoder()
2778 # first load template
2779 for p in capture:
2780 self.assertTrue(p.haslayer(IPFIX))
2781 self.assertEqual(p[IP].src, self.pg3.local_ip4)
2782 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
2783 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
2784 self.assertEqual(p[UDP].dport, 4739)
2785 self.assertEqual(p[IPFIX].observationDomainID,
2786 self.ipfix_domain_id)
2787 if p.haslayer(Template):
2788 ipfix.add_template(p.getlayer(Template))
2789 # verify events in data set
2790 for p in capture:
2791 if p.haslayer(Data):
2792 data = ipfix.decode_data_set(p.getlayer(Set))
2793 self.verify_ipfix_max_sessions(data, max_sessions)
2794
Matus Fabianad1f3e12018-11-28 21:26:34 -08002795 def test_syslog_apmap(self):
2796 """ Test syslog address and port mapping creation and deletion """
2797 self.vapi.syslog_set_filter(SYSLOG_SEVERITY.INFO)
2798 self.vapi.syslog_set_sender(self.pg3.remote_ip4n, self.pg3.local_ip4n)
2799 self.nat44_add_address(self.nat_addr)
2800 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2801 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2802 is_inside=0)
2803
2804 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2805 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
2806 TCP(sport=self.tcp_port_in, dport=20))
2807 self.pg0.add_stream(p)
2808 self.pg_enable_capture(self.pg_interfaces)
2809 self.pg_start()
2810 capture = self.pg1.get_capture(1)
2811 self.tcp_port_out = capture[0][TCP].sport
2812 capture = self.pg3.get_capture(1)
2813 self.verify_syslog_apmap(capture[0][Raw].load)
2814
2815 self.pg_enable_capture(self.pg_interfaces)
2816 self.pg_start()
2817 self.nat44_add_address(self.nat_addr, is_add=0)
2818 capture = self.pg3.get_capture(1)
2819 self.verify_syslog_apmap(capture[0][Raw].load, False)
2820
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002821 def test_pool_addr_fib(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002822 """ NAT44 add pool addresses to FIB """
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002823 static_addr = '10.0.0.10'
Matus Fabian2ba92e32017-08-21 07:05:03 -07002824 self.nat44_add_address(self.nat_addr)
2825 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2826 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2827 is_inside=0)
2828 self.nat44_add_static_mapping(self.pg0.remote_ip4, static_addr)
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002829
Matus Fabian2ba92e32017-08-21 07:05:03 -07002830 # NAT44 address
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002831 p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002832 ARP(op=ARP.who_has, pdst=self.nat_addr,
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002833 psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac))
2834 self.pg1.add_stream(p)
2835 self.pg_enable_capture(self.pg_interfaces)
2836 self.pg_start()
2837 capture = self.pg1.get_capture(1)
2838 self.assertTrue(capture[0].haslayer(ARP))
2839 self.assertTrue(capture[0][ARP].op, ARP.is_at)
2840
2841 # 1:1 NAT address
2842 p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') /
2843 ARP(op=ARP.who_has, pdst=static_addr,
2844 psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac))
2845 self.pg1.add_stream(p)
2846 self.pg_enable_capture(self.pg_interfaces)
2847 self.pg_start()
2848 capture = self.pg1.get_capture(1)
2849 self.assertTrue(capture[0].haslayer(ARP))
2850 self.assertTrue(capture[0][ARP].op, ARP.is_at)
2851
Matus Fabian2ba92e32017-08-21 07:05:03 -07002852 # send ARP to non-NAT44 interface
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002853 p = (Ether(src=self.pg2.remote_mac, dst='ff:ff:ff:ff:ff:ff') /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002854 ARP(op=ARP.who_has, pdst=self.nat_addr,
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002855 psrc=self.pg2.remote_ip4, hwsrc=self.pg2.remote_mac))
2856 self.pg2.add_stream(p)
2857 self.pg_enable_capture(self.pg_interfaces)
2858 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07002859 self.pg1.assert_nothing_captured()
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002860
2861 # remove addresses and verify
Matus Fabian2ba92e32017-08-21 07:05:03 -07002862 self.nat44_add_address(self.nat_addr, is_add=0)
2863 self.nat44_add_static_mapping(self.pg0.remote_ip4, static_addr,
2864 is_add=0)
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002865
2866 p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002867 ARP(op=ARP.who_has, pdst=self.nat_addr,
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002868 psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac))
2869 self.pg1.add_stream(p)
2870 self.pg_enable_capture(self.pg_interfaces)
2871 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07002872 self.pg1.assert_nothing_captured()
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002873
2874 p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') /
2875 ARP(op=ARP.who_has, pdst=static_addr,
2876 psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac))
2877 self.pg1.add_stream(p)
2878 self.pg_enable_capture(self.pg_interfaces)
2879 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07002880 self.pg1.assert_nothing_captured()
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002881
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002882 def test_vrf_mode(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002883 """ NAT44 tenant VRF aware address pool mode """
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002884
2885 vrf_id1 = 1
2886 vrf_id2 = 2
2887 nat_ip1 = "10.0.0.10"
2888 nat_ip2 = "10.0.0.11"
2889
2890 self.pg0.unconfig_ip4()
2891 self.pg1.unconfig_ip4()
Neale Ranns15002542017-09-10 04:39:11 -07002892 self.vapi.ip_table_add_del(vrf_id1, is_add=1)
2893 self.vapi.ip_table_add_del(vrf_id2, is_add=1)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002894 self.pg0.set_table_ip4(vrf_id1)
2895 self.pg1.set_table_ip4(vrf_id2)
2896 self.pg0.config_ip4()
2897 self.pg1.config_ip4()
Matus Fabian8008d7c2018-07-09 01:34:20 -07002898 self.pg0.resolve_arp()
2899 self.pg1.resolve_arp()
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002900
Matus Fabian2ba92e32017-08-21 07:05:03 -07002901 self.nat44_add_address(nat_ip1, vrf_id=vrf_id1)
2902 self.nat44_add_address(nat_ip2, vrf_id=vrf_id2)
2903 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2904 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index)
2905 self.vapi.nat44_interface_add_del_feature(self.pg2.sw_if_index,
2906 is_inside=0)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002907
Matus Fabian8008d7c2018-07-09 01:34:20 -07002908 try:
2909 # first VRF
2910 pkts = self.create_stream_in(self.pg0, self.pg2)
2911 self.pg0.add_stream(pkts)
2912 self.pg_enable_capture(self.pg_interfaces)
2913 self.pg_start()
2914 capture = self.pg2.get_capture(len(pkts))
2915 self.verify_capture_out(capture, nat_ip1)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002916
Matus Fabian8008d7c2018-07-09 01:34:20 -07002917 # second VRF
2918 pkts = self.create_stream_in(self.pg1, self.pg2)
2919 self.pg1.add_stream(pkts)
2920 self.pg_enable_capture(self.pg_interfaces)
2921 self.pg_start()
2922 capture = self.pg2.get_capture(len(pkts))
2923 self.verify_capture_out(capture, nat_ip2)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002924
Matus Fabian8008d7c2018-07-09 01:34:20 -07002925 finally:
2926 self.pg0.unconfig_ip4()
2927 self.pg1.unconfig_ip4()
2928 self.pg0.set_table_ip4(0)
2929 self.pg1.set_table_ip4(0)
2930 self.pg0.config_ip4()
2931 self.pg1.config_ip4()
2932 self.pg0.resolve_arp()
2933 self.pg1.resolve_arp()
2934 self.vapi.ip_table_add_del(vrf_id1, is_add=0)
2935 self.vapi.ip_table_add_del(vrf_id2, is_add=0)
Neale Ranns15002542017-09-10 04:39:11 -07002936
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002937 def test_vrf_feature_independent(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002938 """ NAT44 tenant VRF independent address pool mode """
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002939
2940 nat_ip1 = "10.0.0.10"
2941 nat_ip2 = "10.0.0.11"
2942
Matus Fabian2ba92e32017-08-21 07:05:03 -07002943 self.nat44_add_address(nat_ip1)
Matus Fabian51e759f2017-12-07 23:22:51 -08002944 self.nat44_add_address(nat_ip2, vrf_id=99)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002945 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2946 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index)
2947 self.vapi.nat44_interface_add_del_feature(self.pg2.sw_if_index,
2948 is_inside=0)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002949
2950 # first VRF
2951 pkts = self.create_stream_in(self.pg0, self.pg2)
2952 self.pg0.add_stream(pkts)
2953 self.pg_enable_capture(self.pg_interfaces)
2954 self.pg_start()
2955 capture = self.pg2.get_capture(len(pkts))
2956 self.verify_capture_out(capture, nat_ip1)
2957
2958 # second VRF
2959 pkts = self.create_stream_in(self.pg1, self.pg2)
2960 self.pg1.add_stream(pkts)
2961 self.pg_enable_capture(self.pg_interfaces)
2962 self.pg_start()
2963 capture = self.pg2.get_capture(len(pkts))
2964 self.verify_capture_out(capture, nat_ip1)
2965
Martin Gálik406eb1d2017-05-04 04:35:04 -07002966 def test_dynamic_ipless_interfaces(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002967 """ NAT44 interfaces without configured IP address """
Martin Gálik406eb1d2017-05-04 04:35:04 -07002968
Neale Ranns37029302018-08-10 05:30:06 -07002969 self.vapi.ip_neighbor_add_del(
2970 self.pg7.sw_if_index,
2971 self.pg7.remote_mac,
2972 self.pg7.remote_ip4,
2973 flags=(VppEnum.vl_api_ip_neighbor_flags_t.
2974 IP_API_NEIGHBOR_FLAG_STATIC))
2975 self.vapi.ip_neighbor_add_del(
2976 self.pg8.sw_if_index,
2977 self.pg8.remote_mac,
2978 self.pg8.remote_ip4,
2979 flags=(VppEnum.vl_api_ip_neighbor_flags_t.
2980 IP_API_NEIGHBOR_FLAG_STATIC))
Martin Gálik406eb1d2017-05-04 04:35:04 -07002981
2982 self.vapi.ip_add_del_route(dst_address=self.pg7.remote_ip4n,
2983 dst_address_length=32,
2984 next_hop_address=self.pg7.remote_ip4n,
2985 next_hop_sw_if_index=self.pg7.sw_if_index)
2986 self.vapi.ip_add_del_route(dst_address=self.pg8.remote_ip4n,
2987 dst_address_length=32,
2988 next_hop_address=self.pg8.remote_ip4n,
2989 next_hop_sw_if_index=self.pg8.sw_if_index)
2990
Matus Fabian2ba92e32017-08-21 07:05:03 -07002991 self.nat44_add_address(self.nat_addr)
2992 self.vapi.nat44_interface_add_del_feature(self.pg7.sw_if_index)
2993 self.vapi.nat44_interface_add_del_feature(self.pg8.sw_if_index,
2994 is_inside=0)
Martin Gálik406eb1d2017-05-04 04:35:04 -07002995
2996 # in2out
2997 pkts = self.create_stream_in(self.pg7, self.pg8)
2998 self.pg7.add_stream(pkts)
2999 self.pg_enable_capture(self.pg_interfaces)
3000 self.pg_start()
3001 capture = self.pg8.get_capture(len(pkts))
3002 self.verify_capture_out(capture)
3003
3004 # out2in
Matus Fabian2ba92e32017-08-21 07:05:03 -07003005 pkts = self.create_stream_out(self.pg8, self.nat_addr)
Martin Gálik406eb1d2017-05-04 04:35:04 -07003006 self.pg8.add_stream(pkts)
3007 self.pg_enable_capture(self.pg_interfaces)
3008 self.pg_start()
3009 capture = self.pg7.get_capture(len(pkts))
3010 self.verify_capture_in(capture, self.pg7)
3011
3012 def test_static_ipless_interfaces(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07003013 """ NAT44 interfaces without configured IP address - 1:1 NAT """
Martin Gálik406eb1d2017-05-04 04:35:04 -07003014
Neale Ranns37029302018-08-10 05:30:06 -07003015 self.vapi.ip_neighbor_add_del(
3016 self.pg7.sw_if_index,
3017 self.pg7.remote_mac,
3018 self.pg7.remote_ip4,
3019 flags=(VppEnum.vl_api_ip_neighbor_flags_t.
3020 IP_API_NEIGHBOR_FLAG_STATIC))
3021 self.vapi.ip_neighbor_add_del(
3022 self.pg8.sw_if_index,
3023 self.pg8.remote_mac,
3024 self.pg8.remote_ip4,
3025 flags=(VppEnum.vl_api_ip_neighbor_flags_t.
3026 IP_API_NEIGHBOR_FLAG_STATIC))
Martin Gálik406eb1d2017-05-04 04:35:04 -07003027
3028 self.vapi.ip_add_del_route(dst_address=self.pg7.remote_ip4n,
3029 dst_address_length=32,
3030 next_hop_address=self.pg7.remote_ip4n,
3031 next_hop_sw_if_index=self.pg7.sw_if_index)
3032 self.vapi.ip_add_del_route(dst_address=self.pg8.remote_ip4n,
3033 dst_address_length=32,
3034 next_hop_address=self.pg8.remote_ip4n,
3035 next_hop_sw_if_index=self.pg8.sw_if_index)
3036
Matus Fabian2ba92e32017-08-21 07:05:03 -07003037 self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr)
3038 self.vapi.nat44_interface_add_del_feature(self.pg7.sw_if_index)
3039 self.vapi.nat44_interface_add_del_feature(self.pg8.sw_if_index,
3040 is_inside=0)
Martin Gálik406eb1d2017-05-04 04:35:04 -07003041
3042 # out2in
3043 pkts = self.create_stream_out(self.pg8)
3044 self.pg8.add_stream(pkts)
3045 self.pg_enable_capture(self.pg_interfaces)
3046 self.pg_start()
3047 capture = self.pg7.get_capture(len(pkts))
3048 self.verify_capture_in(capture, self.pg7)
3049
3050 # in2out
3051 pkts = self.create_stream_in(self.pg7, self.pg8)
3052 self.pg7.add_stream(pkts)
3053 self.pg_enable_capture(self.pg_interfaces)
3054 self.pg_start()
3055 capture = self.pg8.get_capture(len(pkts))
Matus Fabian2ba92e32017-08-21 07:05:03 -07003056 self.verify_capture_out(capture, self.nat_addr, True)
Martin Gálik406eb1d2017-05-04 04:35:04 -07003057
3058 def test_static_with_port_ipless_interfaces(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07003059 """ NAT44 interfaces without configured IP address - 1:1 NAPT """
Martin Gálik406eb1d2017-05-04 04:35:04 -07003060
3061 self.tcp_port_out = 30606
3062 self.udp_port_out = 30607
3063 self.icmp_id_out = 30608
3064
Neale Ranns37029302018-08-10 05:30:06 -07003065 self.vapi.ip_neighbor_add_del(
3066 self.pg7.sw_if_index,
3067 self.pg7.remote_mac,
3068 self.pg7.remote_ip4,
3069 flags=(VppEnum.vl_api_ip_neighbor_flags_t.
3070 IP_API_NEIGHBOR_FLAG_STATIC))
3071 self.vapi.ip_neighbor_add_del(
3072 self.pg8.sw_if_index,
3073 self.pg8.remote_mac,
3074 self.pg8.remote_ip4,
3075 flags=(VppEnum.vl_api_ip_neighbor_flags_t.
3076 IP_API_NEIGHBOR_FLAG_STATIC))
Martin Gálik406eb1d2017-05-04 04:35:04 -07003077
3078 self.vapi.ip_add_del_route(dst_address=self.pg7.remote_ip4n,
3079 dst_address_length=32,
3080 next_hop_address=self.pg7.remote_ip4n,
3081 next_hop_sw_if_index=self.pg7.sw_if_index)
3082 self.vapi.ip_add_del_route(dst_address=self.pg8.remote_ip4n,
3083 dst_address_length=32,
3084 next_hop_address=self.pg8.remote_ip4n,
3085 next_hop_sw_if_index=self.pg8.sw_if_index)
3086
Matus Fabian2ba92e32017-08-21 07:05:03 -07003087 self.nat44_add_address(self.nat_addr)
3088 self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr,
3089 self.tcp_port_in, self.tcp_port_out,
3090 proto=IP_PROTOS.tcp)
3091 self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr,
3092 self.udp_port_in, self.udp_port_out,
3093 proto=IP_PROTOS.udp)
3094 self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr,
3095 self.icmp_id_in, self.icmp_id_out,
3096 proto=IP_PROTOS.icmp)
3097 self.vapi.nat44_interface_add_del_feature(self.pg7.sw_if_index)
3098 self.vapi.nat44_interface_add_del_feature(self.pg8.sw_if_index,
3099 is_inside=0)
Martin Gálik406eb1d2017-05-04 04:35:04 -07003100
3101 # out2in
3102 pkts = self.create_stream_out(self.pg8)
3103 self.pg8.add_stream(pkts)
3104 self.pg_enable_capture(self.pg_interfaces)
3105 self.pg_start()
3106 capture = self.pg7.get_capture(len(pkts))
3107 self.verify_capture_in(capture, self.pg7)
3108
3109 # in2out
3110 pkts = self.create_stream_in(self.pg7, self.pg8)
3111 self.pg7.add_stream(pkts)
3112 self.pg_enable_capture(self.pg_interfaces)
3113 self.pg_start()
3114 capture = self.pg8.get_capture(len(pkts))
3115 self.verify_capture_out(capture)
3116
Matus Fabian328dbc82017-06-19 04:28:04 -07003117 def test_static_unknown_proto(self):
3118 """ 1:1 NAT translate packet with unknown protocol """
3119 nat_ip = "10.0.0.10"
Matus Fabian2ba92e32017-08-21 07:05:03 -07003120 self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip)
3121 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3122 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3123 is_inside=0)
Matus Fabian328dbc82017-06-19 04:28:04 -07003124
3125 # in2out
3126 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
3127 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
3128 GRE() /
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003129 IP(src=self.pg2.remote_ip4, dst=self.pg3.remote_ip4) /
Matus Fabian328dbc82017-06-19 04:28:04 -07003130 TCP(sport=1234, dport=1234))
3131 self.pg0.add_stream(p)
3132 self.pg_enable_capture(self.pg_interfaces)
3133 self.pg_start()
3134 p = self.pg1.get_capture(1)
3135 packet = p[0]
3136 try:
3137 self.assertEqual(packet[IP].src, nat_ip)
3138 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08003139 self.assertEqual(packet.haslayer(GRE), 1)
Klement Sekerad81ae412018-05-16 10:52:54 +02003140 self.assert_packet_checksums_valid(packet)
Matus Fabian328dbc82017-06-19 04:28:04 -07003141 except:
3142 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3143 raise
3144
3145 # out2in
3146 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
3147 IP(src=self.pg1.remote_ip4, dst=nat_ip) /
3148 GRE() /
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003149 IP(src=self.pg3.remote_ip4, dst=self.pg2.remote_ip4) /
Matus Fabian328dbc82017-06-19 04:28:04 -07003150 TCP(sport=1234, dport=1234))
3151 self.pg1.add_stream(p)
3152 self.pg_enable_capture(self.pg_interfaces)
3153 self.pg_start()
3154 p = self.pg0.get_capture(1)
3155 packet = p[0]
3156 try:
3157 self.assertEqual(packet[IP].src, self.pg1.remote_ip4)
3158 self.assertEqual(packet[IP].dst, self.pg0.remote_ip4)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08003159 self.assertEqual(packet.haslayer(GRE), 1)
Klement Sekerad81ae412018-05-16 10:52:54 +02003160 self.assert_packet_checksums_valid(packet)
Matus Fabian328dbc82017-06-19 04:28:04 -07003161 except:
3162 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3163 raise
3164
Matus Fabian7968e6c2017-07-06 05:37:49 -07003165 def test_hairpinning_static_unknown_proto(self):
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003166 """ 1:1 NAT translate packet with unknown protocol - hairpinning """
3167
3168 host = self.pg0.remote_hosts[0]
3169 server = self.pg0.remote_hosts[1]
3170
3171 host_nat_ip = "10.0.0.10"
3172 server_nat_ip = "10.0.0.11"
3173
Matus Fabian2ba92e32017-08-21 07:05:03 -07003174 self.nat44_add_static_mapping(host.ip4, host_nat_ip)
3175 self.nat44_add_static_mapping(server.ip4, server_nat_ip)
3176 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3177 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3178 is_inside=0)
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003179
3180 # host to server
3181 p = (Ether(dst=self.pg0.local_mac, src=host.mac) /
3182 IP(src=host.ip4, dst=server_nat_ip) /
3183 GRE() /
3184 IP(src=self.pg2.remote_ip4, dst=self.pg3.remote_ip4) /
3185 TCP(sport=1234, dport=1234))
3186 self.pg0.add_stream(p)
3187 self.pg_enable_capture(self.pg_interfaces)
3188 self.pg_start()
3189 p = self.pg0.get_capture(1)
3190 packet = p[0]
3191 try:
3192 self.assertEqual(packet[IP].src, host_nat_ip)
3193 self.assertEqual(packet[IP].dst, server.ip4)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08003194 self.assertEqual(packet.haslayer(GRE), 1)
Klement Sekerad81ae412018-05-16 10:52:54 +02003195 self.assert_packet_checksums_valid(packet)
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003196 except:
3197 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3198 raise
3199
3200 # server to host
3201 p = (Ether(dst=self.pg0.local_mac, src=server.mac) /
3202 IP(src=server.ip4, dst=host_nat_ip) /
3203 GRE() /
3204 IP(src=self.pg3.remote_ip4, dst=self.pg2.remote_ip4) /
3205 TCP(sport=1234, dport=1234))
3206 self.pg0.add_stream(p)
3207 self.pg_enable_capture(self.pg_interfaces)
3208 self.pg_start()
3209 p = self.pg0.get_capture(1)
3210 packet = p[0]
3211 try:
3212 self.assertEqual(packet[IP].src, server_nat_ip)
3213 self.assertEqual(packet[IP].dst, host.ip4)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08003214 self.assertEqual(packet.haslayer(GRE), 1)
Klement Sekerad81ae412018-05-16 10:52:54 +02003215 self.assert_packet_checksums_valid(packet)
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003216 except:
3217 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3218 raise
3219
Matus Fabian93d84c92017-07-19 08:06:01 -07003220 def test_output_feature(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07003221 """ NAT44 interface output feature (in2out postrouting) """
3222 self.nat44_add_address(self.nat_addr)
3223 self.vapi.nat44_interface_add_del_output_feature(self.pg0.sw_if_index)
Matus Fabianb2d2fc72017-09-10 22:17:47 -07003224 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index)
3225 self.vapi.nat44_interface_add_del_output_feature(self.pg3.sw_if_index,
Matus Fabian2ba92e32017-08-21 07:05:03 -07003226 is_inside=0)
Matus Fabian93d84c92017-07-19 08:06:01 -07003227
3228 # in2out
Matus Fabianb2d2fc72017-09-10 22:17:47 -07003229 pkts = self.create_stream_in(self.pg0, self.pg3)
Matus Fabian93d84c92017-07-19 08:06:01 -07003230 self.pg0.add_stream(pkts)
3231 self.pg_enable_capture(self.pg_interfaces)
3232 self.pg_start()
Matus Fabianb2d2fc72017-09-10 22:17:47 -07003233 capture = self.pg3.get_capture(len(pkts))
Matus Fabian93d84c92017-07-19 08:06:01 -07003234 self.verify_capture_out(capture)
3235
3236 # out2in
Matus Fabianb2d2fc72017-09-10 22:17:47 -07003237 pkts = self.create_stream_out(self.pg3)
3238 self.pg3.add_stream(pkts)
Matus Fabian93d84c92017-07-19 08:06:01 -07003239 self.pg_enable_capture(self.pg_interfaces)
3240 self.pg_start()
3241 capture = self.pg0.get_capture(len(pkts))
3242 self.verify_capture_in(capture, self.pg0)
3243
Matus Fabianb2d2fc72017-09-10 22:17:47 -07003244 # from non-NAT interface to NAT inside interface
3245 pkts = self.create_stream_in(self.pg2, self.pg0)
3246 self.pg2.add_stream(pkts)
3247 self.pg_enable_capture(self.pg_interfaces)
3248 self.pg_start()
3249 capture = self.pg0.get_capture(len(pkts))
3250 self.verify_capture_no_translation(capture, self.pg2, self.pg0)
3251
Matus Fabian93d84c92017-07-19 08:06:01 -07003252 def test_output_feature_vrf_aware(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07003253 """ NAT44 interface output feature VRF aware (in2out postrouting) """
Matus Fabian93d84c92017-07-19 08:06:01 -07003254 nat_ip_vrf10 = "10.0.0.10"
3255 nat_ip_vrf20 = "10.0.0.20"
3256
3257 self.vapi.ip_add_del_route(dst_address=self.pg3.remote_ip4n,
3258 dst_address_length=32,
3259 next_hop_address=self.pg3.remote_ip4n,
3260 next_hop_sw_if_index=self.pg3.sw_if_index,
3261 table_id=10)
3262 self.vapi.ip_add_del_route(dst_address=self.pg3.remote_ip4n,
3263 dst_address_length=32,
3264 next_hop_address=self.pg3.remote_ip4n,
3265 next_hop_sw_if_index=self.pg3.sw_if_index,
3266 table_id=20)
3267
Matus Fabian2ba92e32017-08-21 07:05:03 -07003268 self.nat44_add_address(nat_ip_vrf10, vrf_id=10)
3269 self.nat44_add_address(nat_ip_vrf20, vrf_id=20)
3270 self.vapi.nat44_interface_add_del_output_feature(self.pg4.sw_if_index)
3271 self.vapi.nat44_interface_add_del_output_feature(self.pg6.sw_if_index)
3272 self.vapi.nat44_interface_add_del_output_feature(self.pg3.sw_if_index,
3273 is_inside=0)
Matus Fabian93d84c92017-07-19 08:06:01 -07003274
3275 # in2out VRF 10
3276 pkts = self.create_stream_in(self.pg4, self.pg3)
3277 self.pg4.add_stream(pkts)
3278 self.pg_enable_capture(self.pg_interfaces)
3279 self.pg_start()
3280 capture = self.pg3.get_capture(len(pkts))
3281 self.verify_capture_out(capture, nat_ip=nat_ip_vrf10)
3282
3283 # out2in VRF 10
3284 pkts = self.create_stream_out(self.pg3, dst_ip=nat_ip_vrf10)
3285 self.pg3.add_stream(pkts)
3286 self.pg_enable_capture(self.pg_interfaces)
3287 self.pg_start()
3288 capture = self.pg4.get_capture(len(pkts))
3289 self.verify_capture_in(capture, self.pg4)
3290
3291 # in2out VRF 20
3292 pkts = self.create_stream_in(self.pg6, self.pg3)
3293 self.pg6.add_stream(pkts)
3294 self.pg_enable_capture(self.pg_interfaces)
3295 self.pg_start()
3296 capture = self.pg3.get_capture(len(pkts))
3297 self.verify_capture_out(capture, nat_ip=nat_ip_vrf20)
3298
3299 # out2in VRF 20
3300 pkts = self.create_stream_out(self.pg3, dst_ip=nat_ip_vrf20)
3301 self.pg3.add_stream(pkts)
3302 self.pg_enable_capture(self.pg_interfaces)
3303 self.pg_start()
3304 capture = self.pg6.get_capture(len(pkts))
3305 self.verify_capture_in(capture, self.pg6)
3306
Matus Fabian161c59c2017-07-21 03:46:03 -07003307 def test_output_feature_hairpinning(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07003308 """ NAT44 interface output feature hairpinning (in2out postrouting) """
Matus Fabian93d84c92017-07-19 08:06:01 -07003309 host = self.pg0.remote_hosts[0]
3310 server = self.pg0.remote_hosts[1]
3311 host_in_port = 1234
3312 host_out_port = 0
3313 server_in_port = 5678
3314 server_out_port = 8765
3315
Matus Fabian2ba92e32017-08-21 07:05:03 -07003316 self.nat44_add_address(self.nat_addr)
3317 self.vapi.nat44_interface_add_del_output_feature(self.pg0.sw_if_index)
3318 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
3319 is_inside=0)
Matus Fabian93d84c92017-07-19 08:06:01 -07003320
3321 # add static mapping for server
Matus Fabian2ba92e32017-08-21 07:05:03 -07003322 self.nat44_add_static_mapping(server.ip4, self.nat_addr,
3323 server_in_port, server_out_port,
3324 proto=IP_PROTOS.tcp)
Matus Fabian93d84c92017-07-19 08:06:01 -07003325
3326 # send packet from host to server
3327 p = (Ether(src=host.mac, dst=self.pg0.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07003328 IP(src=host.ip4, dst=self.nat_addr) /
Matus Fabian93d84c92017-07-19 08:06:01 -07003329 TCP(sport=host_in_port, dport=server_out_port))
3330 self.pg0.add_stream(p)
3331 self.pg_enable_capture(self.pg_interfaces)
3332 self.pg_start()
3333 capture = self.pg0.get_capture(1)
3334 p = capture[0]
3335 try:
3336 ip = p[IP]
3337 tcp = p[TCP]
Matus Fabian2ba92e32017-08-21 07:05:03 -07003338 self.assertEqual(ip.src, self.nat_addr)
Matus Fabian93d84c92017-07-19 08:06:01 -07003339 self.assertEqual(ip.dst, server.ip4)
3340 self.assertNotEqual(tcp.sport, host_in_port)
3341 self.assertEqual(tcp.dport, server_in_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02003342 self.assert_packet_checksums_valid(p)
Matus Fabian93d84c92017-07-19 08:06:01 -07003343 host_out_port = tcp.sport
3344 except:
3345 self.logger.error(ppp("Unexpected or invalid packet:", p))
3346 raise
3347
3348 # send reply from server to host
3349 p = (Ether(src=server.mac, dst=self.pg0.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07003350 IP(src=server.ip4, dst=self.nat_addr) /
Matus Fabian93d84c92017-07-19 08:06:01 -07003351 TCP(sport=server_in_port, dport=host_out_port))
3352 self.pg0.add_stream(p)
3353 self.pg_enable_capture(self.pg_interfaces)
3354 self.pg_start()
3355 capture = self.pg0.get_capture(1)
3356 p = capture[0]
3357 try:
3358 ip = p[IP]
3359 tcp = p[TCP]
Matus Fabian2ba92e32017-08-21 07:05:03 -07003360 self.assertEqual(ip.src, self.nat_addr)
Matus Fabian93d84c92017-07-19 08:06:01 -07003361 self.assertEqual(ip.dst, host.ip4)
3362 self.assertEqual(tcp.sport, server_out_port)
3363 self.assertEqual(tcp.dport, host_in_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02003364 self.assert_packet_checksums_valid(p)
Matus Fabian93d84c92017-07-19 08:06:01 -07003365 except:
Matus Fabiane82488f2018-01-18 03:38:45 -08003366 self.logger.error(ppp("Unexpected or invalid packet:", p))
Matus Fabian93d84c92017-07-19 08:06:01 -07003367 raise
3368
Matus Fabian36ea2d62017-10-24 04:13:49 -07003369 def test_one_armed_nat44(self):
3370 """ One armed NAT44 """
3371 remote_host = self.pg9.remote_hosts[0]
3372 local_host = self.pg9.remote_hosts[1]
3373 external_port = 0
3374
3375 self.nat44_add_address(self.nat_addr)
3376 self.vapi.nat44_interface_add_del_feature(self.pg9.sw_if_index)
3377 self.vapi.nat44_interface_add_del_feature(self.pg9.sw_if_index,
3378 is_inside=0)
3379
3380 # in2out
3381 p = (Ether(src=self.pg9.remote_mac, dst=self.pg9.local_mac) /
3382 IP(src=local_host.ip4, dst=remote_host.ip4) /
3383 TCP(sport=12345, dport=80))
3384 self.pg9.add_stream(p)
3385 self.pg_enable_capture(self.pg_interfaces)
3386 self.pg_start()
3387 capture = self.pg9.get_capture(1)
3388 p = capture[0]
3389 try:
3390 ip = p[IP]
3391 tcp = p[TCP]
3392 self.assertEqual(ip.src, self.nat_addr)
3393 self.assertEqual(ip.dst, remote_host.ip4)
3394 self.assertNotEqual(tcp.sport, 12345)
3395 external_port = tcp.sport
3396 self.assertEqual(tcp.dport, 80)
Klement Sekerad81ae412018-05-16 10:52:54 +02003397 self.assert_packet_checksums_valid(p)
Matus Fabian36ea2d62017-10-24 04:13:49 -07003398 except:
3399 self.logger.error(ppp("Unexpected or invalid packet:", p))
3400 raise
3401
3402 # out2in
3403 p = (Ether(src=self.pg9.remote_mac, dst=self.pg9.local_mac) /
3404 IP(src=remote_host.ip4, dst=self.nat_addr) /
3405 TCP(sport=80, dport=external_port))
3406 self.pg9.add_stream(p)
3407 self.pg_enable_capture(self.pg_interfaces)
3408 self.pg_start()
3409 capture = self.pg9.get_capture(1)
3410 p = capture[0]
3411 try:
3412 ip = p[IP]
3413 tcp = p[TCP]
3414 self.assertEqual(ip.src, remote_host.ip4)
3415 self.assertEqual(ip.dst, local_host.ip4)
3416 self.assertEqual(tcp.sport, 80)
3417 self.assertEqual(tcp.dport, 12345)
Klement Sekerad81ae412018-05-16 10:52:54 +02003418 self.assert_packet_checksums_valid(p)
Matus Fabian36ea2d62017-10-24 04:13:49 -07003419 except:
3420 self.logger.error(ppp("Unexpected or invalid packet:", p))
3421 raise
3422
Matus Fabiana5e73762018-12-14 01:55:16 -08003423 err = self.statistics.get_counter('/err/nat44-classify/next in2out')
3424 self.assertEqual(err, 1)
3425 err = self.statistics.get_counter('/err/nat44-classify/next out2in')
3426 self.assertEqual(err, 1)
3427
Matus Fabian5ba86f72017-10-26 03:37:38 -07003428 def test_del_session(self):
3429 """ Delete NAT44 session """
3430 self.nat44_add_address(self.nat_addr)
3431 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3432 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3433 is_inside=0)
3434
3435 pkts = self.create_stream_in(self.pg0, self.pg1)
3436 self.pg0.add_stream(pkts)
3437 self.pg_enable_capture(self.pg_interfaces)
3438 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07003439 self.pg1.get_capture(len(pkts))
Matus Fabian5ba86f72017-10-26 03:37:38 -07003440
3441 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
3442 nsessions = len(sessions)
3443
3444 self.vapi.nat44_del_session(sessions[0].inside_ip_address,
3445 sessions[0].inside_port,
3446 sessions[0].protocol)
3447 self.vapi.nat44_del_session(sessions[1].outside_ip_address,
3448 sessions[1].outside_port,
3449 sessions[1].protocol,
3450 is_in=0)
3451
3452 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
3453 self.assertEqual(nsessions - len(sessions), 2)
3454
Matus Fabian68ba8802018-08-08 05:52:47 -07003455 self.vapi.nat44_del_session(sessions[0].inside_ip_address,
3456 sessions[0].inside_port,
3457 sessions[0].protocol)
3458
3459 self.verify_no_nat44_user()
3460
Matus Fabianefcd1e92017-08-15 06:59:19 -07003461 def test_set_get_reass(self):
3462 """ NAT44 set/get virtual fragmentation reassembly """
3463 reas_cfg1 = self.vapi.nat_get_reass()
3464
3465 self.vapi.nat_set_reass(timeout=reas_cfg1.ip4_timeout + 5,
3466 max_reass=reas_cfg1.ip4_max_reass * 2,
3467 max_frag=reas_cfg1.ip4_max_frag * 2)
3468
3469 reas_cfg2 = self.vapi.nat_get_reass()
3470
3471 self.assertEqual(reas_cfg1.ip4_timeout + 5, reas_cfg2.ip4_timeout)
3472 self.assertEqual(reas_cfg1.ip4_max_reass * 2, reas_cfg2.ip4_max_reass)
3473 self.assertEqual(reas_cfg1.ip4_max_frag * 2, reas_cfg2.ip4_max_frag)
3474
3475 self.vapi.nat_set_reass(drop_frag=1)
3476 self.assertTrue(self.vapi.nat_get_reass().ip4_drop_frag)
3477
3478 def test_frag_in_order(self):
3479 """ NAT44 translate fragments arriving in order """
Juraj Slobodafe0aa762018-07-23 12:22:54 +02003480
Matus Fabianefcd1e92017-08-15 06:59:19 -07003481 self.nat44_add_address(self.nat_addr)
3482 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3483 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3484 is_inside=0)
3485
Matus Fabianda41d722018-10-19 04:01:19 -07003486 self.frag_in_order(proto=IP_PROTOS.tcp)
3487 self.frag_in_order(proto=IP_PROTOS.udp)
3488 self.frag_in_order(proto=IP_PROTOS.icmp)
Matus Fabianefcd1e92017-08-15 06:59:19 -07003489
Matus Fabian111add72018-11-20 02:19:05 -08003490 def test_frag_forwarding(self):
3491 """ NAT44 forwarding fragment test """
3492 self.vapi.nat44_add_interface_addr(self.pg1.sw_if_index)
3493 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3494 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3495 is_inside=0)
3496 self.vapi.nat44_forwarding_enable_disable(1)
3497
3498 data = "A" * 16 + "B" * 16 + "C" * 3
3499 pkts = self.create_stream_frag(self.pg1,
3500 self.pg0.remote_ip4,
3501 4789,
3502 4789,
3503 data,
3504 proto=IP_PROTOS.udp)
3505 self.pg1.add_stream(pkts)
3506 self.pg_enable_capture(self.pg_interfaces)
3507 self.pg_start()
3508 frags = self.pg0.get_capture(len(pkts))
3509 p = self.reass_frags_and_verify(frags,
3510 self.pg1.remote_ip4,
3511 self.pg0.remote_ip4)
3512 self.assertEqual(p[UDP].sport, 4789)
3513 self.assertEqual(p[UDP].dport, 4789)
3514 self.assertEqual(data, p[Raw].load)
3515
Matus Fabianefcd1e92017-08-15 06:59:19 -07003516 def test_reass_hairpinning(self):
3517 """ NAT44 fragments hairpinning """
Juraj Slobodafe0aa762018-07-23 12:22:54 +02003518
Matus Fabianda41d722018-10-19 04:01:19 -07003519 self.server = self.pg0.remote_hosts[1]
3520 self.host_in_port = random.randint(1025, 65535)
3521 self.server_in_port = random.randint(1025, 65535)
3522 self.server_out_port = random.randint(1025, 65535)
Matus Fabianefcd1e92017-08-15 06:59:19 -07003523
3524 self.nat44_add_address(self.nat_addr)
3525 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3526 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3527 is_inside=0)
3528 # add static mapping for server
Matus Fabianda41d722018-10-19 04:01:19 -07003529 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr,
3530 self.server_in_port,
3531 self.server_out_port,
Matus Fabianefcd1e92017-08-15 06:59:19 -07003532 proto=IP_PROTOS.tcp)
Matus Fabianda41d722018-10-19 04:01:19 -07003533 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr,
3534 self.server_in_port,
3535 self.server_out_port,
3536 proto=IP_PROTOS.udp)
3537 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr)
Matus Fabianefcd1e92017-08-15 06:59:19 -07003538
Matus Fabianda41d722018-10-19 04:01:19 -07003539 self.reass_hairpinning(proto=IP_PROTOS.tcp)
3540 self.reass_hairpinning(proto=IP_PROTOS.udp)
3541 self.reass_hairpinning(proto=IP_PROTOS.icmp)
Matus Fabianefcd1e92017-08-15 06:59:19 -07003542
3543 def test_frag_out_of_order(self):
3544 """ NAT44 translate fragments arriving out of order """
Juraj Slobodafe0aa762018-07-23 12:22:54 +02003545
Matus Fabianefcd1e92017-08-15 06:59:19 -07003546 self.nat44_add_address(self.nat_addr)
3547 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3548 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3549 is_inside=0)
3550
Matus Fabianda41d722018-10-19 04:01:19 -07003551 self.frag_out_of_order(proto=IP_PROTOS.tcp)
3552 self.frag_out_of_order(proto=IP_PROTOS.udp)
3553 self.frag_out_of_order(proto=IP_PROTOS.icmp)
Matus Fabianefcd1e92017-08-15 06:59:19 -07003554
Matus Fabian27697102017-11-09 01:43:47 -08003555 def test_port_restricted(self):
3556 """ Port restricted NAT44 (MAP-E CE) """
3557 self.nat44_add_address(self.nat_addr)
3558 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3559 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3560 is_inside=0)
Matus Fabian5d28c7a2018-09-04 03:55:45 -07003561 self.vapi.nat_set_addr_and_port_alloc_alg(alg=1,
3562 psid_offset=6,
3563 psid_length=6,
3564 psid=10)
Matus Fabian27697102017-11-09 01:43:47 -08003565
3566 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
3567 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
3568 TCP(sport=4567, dport=22))
3569 self.pg0.add_stream(p)
3570 self.pg_enable_capture(self.pg_interfaces)
3571 self.pg_start()
3572 capture = self.pg1.get_capture(1)
3573 p = capture[0]
3574 try:
3575 ip = p[IP]
3576 tcp = p[TCP]
3577 self.assertEqual(ip.dst, self.pg1.remote_ip4)
3578 self.assertEqual(ip.src, self.nat_addr)
3579 self.assertEqual(tcp.dport, 22)
3580 self.assertNotEqual(tcp.sport, 4567)
3581 self.assertEqual((tcp.sport >> 6) & 63, 10)
Klement Sekerad81ae412018-05-16 10:52:54 +02003582 self.assert_packet_checksums_valid(p)
Matus Fabian27697102017-11-09 01:43:47 -08003583 except:
3584 self.logger.error(ppp("Unexpected or invalid packet:", p))
3585 raise
3586
Matus Fabian5d28c7a2018-09-04 03:55:45 -07003587 def test_port_range(self):
3588 """ External address port range """
3589 self.nat44_add_address(self.nat_addr)
3590 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3591 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3592 is_inside=0)
3593 self.vapi.nat_set_addr_and_port_alloc_alg(alg=2,
3594 start_port=1025,
3595 end_port=1027)
3596
3597 pkts = []
3598 for port in range(0, 5):
3599 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
3600 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
3601 TCP(sport=1125 + port))
3602 pkts.append(p)
3603 self.pg0.add_stream(pkts)
3604 self.pg_enable_capture(self.pg_interfaces)
3605 self.pg_start()
3606 capture = self.pg1.get_capture(3)
3607 for p in capture:
3608 tcp = p[TCP]
3609 self.assertGreaterEqual(tcp.sport, 1025)
3610 self.assertLessEqual(tcp.sport, 1027)
3611
Matus Fabiana6110b62018-06-13 05:39:07 -07003612 def test_ipfix_max_frags(self):
3613 """ IPFIX logging maximum fragments pending reassembly exceeded """
3614 self.nat44_add_address(self.nat_addr)
3615 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3616 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3617 is_inside=0)
Matus Fabiana7f8b222018-09-05 06:01:55 -07003618 self.vapi.nat_set_reass(max_frag=1)
Matus Fabiana6110b62018-06-13 05:39:07 -07003619 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
3620 src_address=self.pg3.local_ip4n,
3621 path_mtu=512,
3622 template_interval=10)
3623 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
3624 src_port=self.ipfix_src_port)
3625
3626 data = "A" * 4 + "B" * 16 + "C" * 3
3627 self.tcp_port_in = random.randint(1025, 65535)
3628 pkts = self.create_stream_frag(self.pg0,
3629 self.pg1.remote_ip4,
3630 self.tcp_port_in,
3631 20,
3632 data)
Matus Fabiana7f8b222018-09-05 06:01:55 -07003633 pkts.reverse()
3634 self.pg0.add_stream(pkts)
Matus Fabiana6110b62018-06-13 05:39:07 -07003635 self.pg_enable_capture(self.pg_interfaces)
3636 self.pg_start()
3637 self.pg1.assert_nothing_captured()
3638 sleep(1)
3639 self.vapi.cli("ipfix flush") # FIXME this should be an API call
3640 capture = self.pg3.get_capture(9)
3641 ipfix = IPFIXDecoder()
3642 # first load template
3643 for p in capture:
3644 self.assertTrue(p.haslayer(IPFIX))
3645 self.assertEqual(p[IP].src, self.pg3.local_ip4)
3646 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
3647 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
3648 self.assertEqual(p[UDP].dport, 4739)
3649 self.assertEqual(p[IPFIX].observationDomainID,
3650 self.ipfix_domain_id)
3651 if p.haslayer(Template):
3652 ipfix.add_template(p.getlayer(Template))
3653 # verify events in data set
3654 for p in capture:
3655 if p.haslayer(Data):
3656 data = ipfix.decode_data_set(p.getlayer(Set))
Matus Fabiana7f8b222018-09-05 06:01:55 -07003657 self.verify_ipfix_max_fragments_ip4(data, 1,
Matus Fabiana6110b62018-06-13 05:39:07 -07003658 self.pg0.remote_ip4n)
3659
Matus Fabian8008d7c2018-07-09 01:34:20 -07003660 def test_multiple_outside_vrf(self):
3661 """ Multiple outside VRF """
3662 vrf_id1 = 1
3663 vrf_id2 = 2
3664
3665 self.pg1.unconfig_ip4()
3666 self.pg2.unconfig_ip4()
3667 self.vapi.ip_table_add_del(vrf_id1, is_add=1)
3668 self.vapi.ip_table_add_del(vrf_id2, is_add=1)
3669 self.pg1.set_table_ip4(vrf_id1)
3670 self.pg2.set_table_ip4(vrf_id2)
3671 self.pg1.config_ip4()
3672 self.pg2.config_ip4()
3673 self.pg1.resolve_arp()
3674 self.pg2.resolve_arp()
3675
3676 self.nat44_add_address(self.nat_addr)
3677 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3678 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3679 is_inside=0)
3680 self.vapi.nat44_interface_add_del_feature(self.pg2.sw_if_index,
3681 is_inside=0)
3682
3683 try:
3684 # first VRF
3685 pkts = self.create_stream_in(self.pg0, self.pg1)
3686 self.pg0.add_stream(pkts)
3687 self.pg_enable_capture(self.pg_interfaces)
3688 self.pg_start()
3689 capture = self.pg1.get_capture(len(pkts))
3690 self.verify_capture_out(capture, self.nat_addr)
3691
3692 pkts = self.create_stream_out(self.pg1, self.nat_addr)
3693 self.pg1.add_stream(pkts)
3694 self.pg_enable_capture(self.pg_interfaces)
3695 self.pg_start()
3696 capture = self.pg0.get_capture(len(pkts))
3697 self.verify_capture_in(capture, self.pg0)
3698
3699 self.tcp_port_in = 60303
3700 self.udp_port_in = 60304
3701 self.icmp_id_in = 60305
3702
3703 # second VRF
3704 pkts = self.create_stream_in(self.pg0, self.pg2)
3705 self.pg0.add_stream(pkts)
3706 self.pg_enable_capture(self.pg_interfaces)
3707 self.pg_start()
3708 capture = self.pg2.get_capture(len(pkts))
3709 self.verify_capture_out(capture, self.nat_addr)
3710
3711 pkts = self.create_stream_out(self.pg2, self.nat_addr)
3712 self.pg2.add_stream(pkts)
3713 self.pg_enable_capture(self.pg_interfaces)
3714 self.pg_start()
3715 capture = self.pg0.get_capture(len(pkts))
3716 self.verify_capture_in(capture, self.pg0)
3717
3718 finally:
3719 self.pg1.unconfig_ip4()
3720 self.pg2.unconfig_ip4()
3721 self.pg1.set_table_ip4(0)
3722 self.pg2.set_table_ip4(0)
3723 self.pg1.config_ip4()
3724 self.pg2.config_ip4()
3725 self.pg1.resolve_arp()
3726 self.pg2.resolve_arp()
3727
Paul Vinciguerradefde0f2018-12-06 07:46:13 -08003728 @unittest.skipUnless(running_extended_tests, "part of extended tests")
Matus Fabian878c6462018-08-23 00:33:35 -07003729 def test_session_timeout(self):
3730 """ NAT44 session timeouts """
3731 self.nat44_add_address(self.nat_addr)
3732 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3733 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3734 is_inside=0)
3735 self.vapi.nat_set_timeouts(udp=5)
3736
3737 max_sessions = 1000
3738 pkts = []
3739 for i in range(0, max_sessions):
3740 src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF)
3741 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
3742 IP(src=src, dst=self.pg1.remote_ip4) /
3743 UDP(sport=1025, dport=53))
3744 pkts.append(p)
3745 self.pg0.add_stream(pkts)
3746 self.pg_enable_capture(self.pg_interfaces)
3747 self.pg_start()
3748 self.pg1.get_capture(max_sessions)
3749
3750 sleep(6)
3751
3752 pkts = []
3753 for i in range(0, max_sessions):
3754 src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF)
3755 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
3756 IP(src=src, dst=self.pg1.remote_ip4) /
3757 UDP(sport=1026, dport=53))
3758 pkts.append(p)
3759 self.pg0.add_stream(pkts)
3760 self.pg_enable_capture(self.pg_interfaces)
3761 self.pg_start()
3762 self.pg1.get_capture(max_sessions)
3763
3764 nsessions = 0
3765 users = self.vapi.nat44_user_dump()
3766 for user in users:
3767 nsessions = nsessions + user.nsessions
3768 self.assertLess(nsessions, 2 * max_sessions)
3769
Matus Fabianbb4e0222018-09-13 02:36:25 -07003770 def test_mss_clamping(self):
3771 """ TCP MSS clamping """
3772 self.nat44_add_address(self.nat_addr)
3773 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3774 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3775 is_inside=0)
3776
3777 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
3778 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
3779 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
3780 flags="S", options=[('MSS', 1400)]))
3781
3782 self.vapi.nat_set_mss_clamping(enable=1, mss_value=1000)
3783 self.pg0.add_stream(p)
3784 self.pg_enable_capture(self.pg_interfaces)
3785 self.pg_start()
3786 capture = self.pg1.get_capture(1)
3787 # Negotiated MSS value greater than configured - changed
3788 self.verify_mss_value(capture[0], 1000)
3789
3790 self.vapi.nat_set_mss_clamping(enable=0)
3791 self.pg0.add_stream(p)
3792 self.pg_enable_capture(self.pg_interfaces)
3793 self.pg_start()
3794 capture = self.pg1.get_capture(1)
3795 # MSS clamping disabled - negotiated MSS unchanged
3796 self.verify_mss_value(capture[0], 1400)
3797
3798 self.vapi.nat_set_mss_clamping(enable=1, mss_value=1500)
3799 self.pg0.add_stream(p)
3800 self.pg_enable_capture(self.pg_interfaces)
3801 self.pg_start()
3802 capture = self.pg1.get_capture(1)
3803 # Negotiated MSS value smaller than configured - unchanged
3804 self.verify_mss_value(capture[0], 1400)
3805
Matus Fabiana6110b62018-06-13 05:39:07 -07003806 def tearDown(self):
3807 super(TestNAT44, self).tearDown()
3808 if not self.vpp_dead:
3809 self.logger.info(self.vapi.cli("show nat44 addresses"))
3810 self.logger.info(self.vapi.cli("show nat44 interfaces"))
3811 self.logger.info(self.vapi.cli("show nat44 static mappings"))
3812 self.logger.info(self.vapi.cli("show nat44 interface address"))
3813 self.logger.info(self.vapi.cli("show nat44 sessions detail"))
3814 self.logger.info(self.vapi.cli("show nat virtual-reassembly"))
3815 self.logger.info(self.vapi.cli("show nat44 hash tables detail"))
Matus Fabian878c6462018-08-23 00:33:35 -07003816 self.logger.info(self.vapi.cli("show nat timeouts"))
Matus Fabian5d28c7a2018-09-04 03:55:45 -07003817 self.logger.info(
3818 self.vapi.cli("show nat addr-port-assignment-alg"))
Matus Fabiana6110b62018-06-13 05:39:07 -07003819 self.clear_nat44()
3820 self.vapi.cli("clear logging")
3821
3822
3823class TestNAT44EndpointDependent(MethodHolder):
3824 """ Endpoint-Dependent mapping and filtering test cases """
3825
3826 @classmethod
3827 def setUpConstants(cls):
3828 super(TestNAT44EndpointDependent, cls).setUpConstants()
3829 cls.vpp_cmdline.extend(["nat", "{", "endpoint-dependent", "}"])
3830
3831 @classmethod
3832 def setUpClass(cls):
3833 super(TestNAT44EndpointDependent, cls).setUpClass()
3834 cls.vapi.cli("set log class nat level debug")
3835 try:
3836 cls.tcp_port_in = 6303
3837 cls.tcp_port_out = 6303
3838 cls.udp_port_in = 6304
3839 cls.udp_port_out = 6304
3840 cls.icmp_id_in = 6305
3841 cls.icmp_id_out = 6305
3842 cls.nat_addr = '10.0.0.3'
3843 cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
3844 cls.ipfix_src_port = 4739
3845 cls.ipfix_domain_id = 1
3846 cls.tcp_external_port = 80
3847
Matus Fabian8008d7c2018-07-09 01:34:20 -07003848 cls.create_pg_interfaces(range(7))
Matus Fabiana6110b62018-06-13 05:39:07 -07003849 cls.interfaces = list(cls.pg_interfaces[0:3])
3850
3851 for i in cls.interfaces:
3852 i.admin_up()
3853 i.config_ip4()
3854 i.resolve_arp()
3855
3856 cls.pg0.generate_remote_hosts(3)
3857 cls.pg0.configure_ipv4_neighbors()
3858
3859 cls.pg3.admin_up()
3860
3861 cls.pg4.generate_remote_hosts(2)
3862 cls.pg4.config_ip4()
3863 ip_addr_n = socket.inet_pton(socket.AF_INET, "10.0.0.1")
3864 cls.vapi.sw_interface_add_del_address(cls.pg4.sw_if_index,
3865 ip_addr_n,
3866 24)
3867 cls.pg4.admin_up()
3868 cls.pg4.resolve_arp()
3869 cls.pg4._remote_hosts[1]._ip4 = cls.pg4._remote_hosts[0]._ip4
3870 cls.pg4.resolve_arp()
3871
Matus Fabian8008d7c2018-07-09 01:34:20 -07003872 zero_ip4n = socket.inet_pton(socket.AF_INET, "0.0.0.0")
3873 cls.vapi.ip_table_add_del(1, is_add=1)
3874
3875 cls.pg5._local_ip4 = "10.1.1.1"
3876 cls.pg5._local_ip4n = socket.inet_pton(socket.AF_INET,
3877 cls.pg5.local_ip4)
3878 cls.pg5._remote_hosts[0]._ip4 = "10.1.1.2"
3879 cls.pg5._remote_hosts[0]._ip4n = socket.inet_pton(
3880 socket.AF_INET, cls.pg5.remote_ip4)
3881 cls.pg5.set_table_ip4(1)
3882 cls.pg5.config_ip4()
3883 cls.pg5.admin_up()
3884 cls.vapi.ip_add_del_route(dst_address=cls.pg5.remote_ip4n,
3885 dst_address_length=32,
3886 table_id=1,
3887 next_hop_sw_if_index=cls.pg5.sw_if_index,
3888 next_hop_address=zero_ip4n)
3889
3890 cls.pg6._local_ip4 = "10.1.2.1"
3891 cls.pg6._local_ip4n = socket.inet_pton(socket.AF_INET,
3892 cls.pg6.local_ip4)
3893 cls.pg6._remote_hosts[0]._ip4 = "10.1.2.2"
3894 cls.pg6._remote_hosts[0]._ip4n = socket.inet_pton(
3895 socket.AF_INET, cls.pg6.remote_ip4)
3896 cls.pg6.set_table_ip4(1)
3897 cls.pg6.config_ip4()
3898 cls.pg6.admin_up()
3899 cls.vapi.ip_add_del_route(dst_address=cls.pg6.remote_ip4n,
3900 dst_address_length=32,
3901 table_id=1,
3902 next_hop_sw_if_index=cls.pg6.sw_if_index,
3903 next_hop_address=zero_ip4n)
3904
3905 cls.vapi.ip_add_del_route(dst_address=cls.pg6.remote_ip4n,
3906 dst_address_length=16,
3907 next_hop_address=zero_ip4n,
3908 table_id=0,
3909 next_hop_table_id=1)
3910 cls.vapi.ip_add_del_route(dst_address=zero_ip4n,
3911 dst_address_length=0,
3912 next_hop_address=zero_ip4n,
3913 table_id=1,
3914 next_hop_table_id=0)
3915 cls.vapi.ip_add_del_route(dst_address=zero_ip4n,
3916 dst_address_length=0,
3917 table_id=0,
3918 next_hop_sw_if_index=cls.pg1.sw_if_index,
3919 next_hop_address=cls.pg1.local_ip4n)
3920
3921 cls.pg5.resolve_arp()
3922 cls.pg6.resolve_arp()
3923
Matus Fabiana6110b62018-06-13 05:39:07 -07003924 except Exception:
3925 super(TestNAT44EndpointDependent, cls).tearDownClass()
3926 raise
3927
Juraj Slobodafe0aa762018-07-23 12:22:54 +02003928 def test_frag_in_order(self):
3929 """ NAT44 translate fragments arriving in order """
3930 self.nat44_add_address(self.nat_addr)
3931 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3932 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3933 is_inside=0)
3934 self.frag_in_order(proto=IP_PROTOS.tcp)
3935 self.frag_in_order(proto=IP_PROTOS.udp)
3936 self.frag_in_order(proto=IP_PROTOS.icmp)
3937
3938 def test_frag_in_order_dont_translate(self):
3939 """ NAT44 don't translate fragments arriving in order """
3940 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3941 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3942 is_inside=0)
3943 self.vapi.nat44_forwarding_enable_disable(enable=True)
3944 self.frag_in_order(proto=IP_PROTOS.tcp, dont_translate=True)
3945
3946 def test_frag_out_of_order(self):
3947 """ NAT44 translate fragments arriving out of order """
3948 self.nat44_add_address(self.nat_addr)
3949 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3950 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3951 is_inside=0)
3952 self.frag_out_of_order(proto=IP_PROTOS.tcp)
3953 self.frag_out_of_order(proto=IP_PROTOS.udp)
3954 self.frag_out_of_order(proto=IP_PROTOS.icmp)
3955
3956 def test_frag_out_of_order_dont_translate(self):
3957 """ NAT44 don't translate fragments arriving out of order """
3958 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3959 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3960 is_inside=0)
3961 self.vapi.nat44_forwarding_enable_disable(enable=True)
3962 self.frag_out_of_order(proto=IP_PROTOS.tcp, dont_translate=True)
3963
3964 def test_frag_in_order_in_plus_out(self):
3965 """ in+out interface fragments in order """
3966 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3967 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
3968 is_inside=0)
3969 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index)
3970 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3971 is_inside=0)
3972
3973 self.server = self.pg1.remote_hosts[0]
3974
3975 self.server_in_addr = self.server.ip4
3976 self.server_out_addr = '11.11.11.11'
3977 self.server_in_port = random.randint(1025, 65535)
3978 self.server_out_port = random.randint(1025, 65535)
3979
3980 self.nat44_add_address(self.server_out_addr)
3981
3982 # add static mappings for server
3983 self.nat44_add_static_mapping(self.server_in_addr,
3984 self.server_out_addr,
3985 self.server_in_port,
3986 self.server_out_port,
3987 proto=IP_PROTOS.tcp)
3988 self.nat44_add_static_mapping(self.server_in_addr,
3989 self.server_out_addr,
3990 self.server_in_port,
3991 self.server_out_port,
3992 proto=IP_PROTOS.udp)
3993 self.nat44_add_static_mapping(self.server_in_addr,
3994 self.server_out_addr,
3995 proto=IP_PROTOS.icmp)
3996
3997 self.vapi.nat_set_reass(timeout=10)
3998
3999 self.frag_in_order_in_plus_out(proto=IP_PROTOS.tcp)
4000 self.frag_in_order_in_plus_out(proto=IP_PROTOS.udp)
4001 self.frag_in_order_in_plus_out(proto=IP_PROTOS.icmp)
4002
4003 def test_frag_out_of_order_in_plus_out(self):
4004 """ in+out interface fragments out of order """
4005 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4006 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
4007 is_inside=0)
4008 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index)
4009 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4010 is_inside=0)
4011
4012 self.server = self.pg1.remote_hosts[0]
4013
4014 self.server_in_addr = self.server.ip4
4015 self.server_out_addr = '11.11.11.11'
4016 self.server_in_port = random.randint(1025, 65535)
4017 self.server_out_port = random.randint(1025, 65535)
4018
4019 self.nat44_add_address(self.server_out_addr)
4020
4021 # add static mappings for server
4022 self.nat44_add_static_mapping(self.server_in_addr,
4023 self.server_out_addr,
4024 self.server_in_port,
4025 self.server_out_port,
4026 proto=IP_PROTOS.tcp)
4027 self.nat44_add_static_mapping(self.server_in_addr,
4028 self.server_out_addr,
4029 self.server_in_port,
4030 self.server_out_port,
4031 proto=IP_PROTOS.udp)
4032 self.nat44_add_static_mapping(self.server_in_addr,
4033 self.server_out_addr,
4034 proto=IP_PROTOS.icmp)
4035
4036 self.vapi.nat_set_reass(timeout=10)
4037
4038 self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.tcp)
4039 self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.udp)
4040 self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.icmp)
4041
4042 def test_reass_hairpinning(self):
4043 """ NAT44 fragments hairpinning """
4044 self.server = self.pg0.remote_hosts[1]
4045 self.host_in_port = random.randint(1025, 65535)
4046 self.server_in_port = random.randint(1025, 65535)
4047 self.server_out_port = random.randint(1025, 65535)
4048
4049 self.nat44_add_address(self.nat_addr)
4050 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4051 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4052 is_inside=0)
4053 # add static mapping for server
4054 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr,
4055 self.server_in_port,
4056 self.server_out_port,
4057 proto=IP_PROTOS.tcp)
4058 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr,
4059 self.server_in_port,
4060 self.server_out_port,
4061 proto=IP_PROTOS.udp)
Matus Fabianda41d722018-10-19 04:01:19 -07004062 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr)
Juraj Slobodafe0aa762018-07-23 12:22:54 +02004063
4064 self.reass_hairpinning(proto=IP_PROTOS.tcp)
4065 self.reass_hairpinning(proto=IP_PROTOS.udp)
4066 self.reass_hairpinning(proto=IP_PROTOS.icmp)
4067
Matus Fabiana6110b62018-06-13 05:39:07 -07004068 def test_dynamic(self):
4069 """ NAT44 dynamic translation test """
4070
4071 self.nat44_add_address(self.nat_addr)
4072 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4073 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4074 is_inside=0)
4075
Matus Fabian69ce30d2018-08-22 01:27:10 -07004076 nat_config = self.vapi.nat_show_config()
4077 self.assertEqual(1, nat_config.endpoint_dependent)
4078
Matus Fabiana6110b62018-06-13 05:39:07 -07004079 # in2out
Matus Fabiana5e73762018-12-14 01:55:16 -08004080 tcpn = self.statistics.get_counter(
4081 '/err/nat44-ed-in2out-slowpath/TCP packets')
4082 udpn = self.statistics.get_counter(
4083 '/err/nat44-ed-in2out-slowpath/UDP packets')
4084 icmpn = self.statistics.get_counter(
4085 '/err/nat44-ed-in2out-slowpath/ICMP packets')
4086 totaln = self.statistics.get_counter(
4087 '/err/nat44-ed-in2out-slowpath/good in2out packets processed')
4088
Matus Fabiana6110b62018-06-13 05:39:07 -07004089 pkts = self.create_stream_in(self.pg0, self.pg1)
4090 self.pg0.add_stream(pkts)
4091 self.pg_enable_capture(self.pg_interfaces)
4092 self.pg_start()
4093 capture = self.pg1.get_capture(len(pkts))
4094 self.verify_capture_out(capture)
4095
Matus Fabiana5e73762018-12-14 01:55:16 -08004096 err = self.statistics.get_counter(
4097 '/err/nat44-ed-in2out-slowpath/TCP packets')
4098 self.assertEqual(err - tcpn, 1)
4099 err = self.statistics.get_counter(
4100 '/err/nat44-ed-in2out-slowpath/UDP packets')
4101 self.assertEqual(err - udpn, 1)
4102 err = self.statistics.get_counter(
4103 '/err/nat44-ed-in2out-slowpath/ICMP packets')
4104 self.assertEqual(err - icmpn, 1)
4105 err = self.statistics.get_counter(
4106 '/err/nat44-ed-in2out-slowpath/good in2out packets processed')
4107 self.assertEqual(err - totaln, 3)
4108
Matus Fabiana6110b62018-06-13 05:39:07 -07004109 # out2in
Matus Fabiana5e73762018-12-14 01:55:16 -08004110 tcpn = self.statistics.get_counter('/err/nat44-ed-out2in/TCP packets')
4111 udpn = self.statistics.get_counter('/err/nat44-ed-out2in/UDP packets')
4112 icmpn = self.statistics.get_counter(
4113 '/err/nat44-ed-out2in-slowpath/ICMP packets')
4114 totaln = self.statistics.get_counter(
4115 '/err/nat44-ed-out2in/good out2in packets processed')
4116
Matus Fabiana6110b62018-06-13 05:39:07 -07004117 pkts = self.create_stream_out(self.pg1)
4118 self.pg1.add_stream(pkts)
4119 self.pg_enable_capture(self.pg_interfaces)
4120 self.pg_start()
4121 capture = self.pg0.get_capture(len(pkts))
4122 self.verify_capture_in(capture, self.pg0)
4123
Matus Fabiana5e73762018-12-14 01:55:16 -08004124 err = self.statistics.get_counter('/err/nat44-ed-out2in/TCP packets')
4125 self.assertEqual(err - tcpn, 1)
4126 err = self.statistics.get_counter('/err/nat44-ed-out2in/UDP packets')
4127 self.assertEqual(err - udpn, 1)
4128 err = self.statistics.get_counter(
4129 '/err/nat44-ed-out2in-slowpath/ICMP packets')
4130 self.assertEqual(err - icmpn, 1)
4131 err = self.statistics.get_counter(
4132 '/err/nat44-ed-out2in/good out2in packets processed')
4133 self.assertEqual(err - totaln, 2)
4134
Matus Fabianfd0d5082018-12-18 01:08:51 -08004135 users = self.statistics.get_counter('/nat44/total-users')
4136 self.assertEqual(users[0][0], 1)
4137 sessions = self.statistics.get_counter('/nat44/total-sessions')
4138 self.assertEqual(sessions[0][0], 3)
4139
Matus Fabiana6110b62018-06-13 05:39:07 -07004140 def test_forwarding(self):
4141 """ NAT44 forwarding test """
4142
4143 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4144 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4145 is_inside=0)
4146 self.vapi.nat44_forwarding_enable_disable(1)
4147
4148 real_ip = self.pg0.remote_ip4n
4149 alias_ip = self.nat_addr_n
4150 self.vapi.nat44_add_del_static_mapping(local_ip=real_ip,
4151 external_ip=alias_ip)
4152
4153 try:
4154 # in2out - static mapping match
4155
4156 pkts = self.create_stream_out(self.pg1)
4157 self.pg1.add_stream(pkts)
4158 self.pg_enable_capture(self.pg_interfaces)
4159 self.pg_start()
4160 capture = self.pg0.get_capture(len(pkts))
4161 self.verify_capture_in(capture, self.pg0)
4162
4163 pkts = self.create_stream_in(self.pg0, self.pg1)
4164 self.pg0.add_stream(pkts)
4165 self.pg_enable_capture(self.pg_interfaces)
4166 self.pg_start()
4167 capture = self.pg1.get_capture(len(pkts))
4168 self.verify_capture_out(capture, same_port=True)
4169
4170 # in2out - no static mapping match
4171
4172 host0 = self.pg0.remote_hosts[0]
4173 self.pg0.remote_hosts[0] = self.pg0.remote_hosts[1]
4174 try:
4175 pkts = self.create_stream_out(self.pg1,
4176 dst_ip=self.pg0.remote_ip4,
4177 use_inside_ports=True)
4178 self.pg1.add_stream(pkts)
4179 self.pg_enable_capture(self.pg_interfaces)
4180 self.pg_start()
4181 capture = self.pg0.get_capture(len(pkts))
4182 self.verify_capture_in(capture, self.pg0)
4183
4184 pkts = self.create_stream_in(self.pg0, self.pg1)
4185 self.pg0.add_stream(pkts)
4186 self.pg_enable_capture(self.pg_interfaces)
4187 self.pg_start()
4188 capture = self.pg1.get_capture(len(pkts))
4189 self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4,
4190 same_port=True)
4191 finally:
4192 self.pg0.remote_hosts[0] = host0
4193
4194 user = self.pg0.remote_hosts[1]
4195 sessions = self.vapi.nat44_user_session_dump(user.ip4n, 0)
4196 self.assertEqual(len(sessions), 3)
4197 self.assertTrue(sessions[0].ext_host_valid)
4198 self.vapi.nat44_del_session(
4199 sessions[0].inside_ip_address,
4200 sessions[0].inside_port,
4201 sessions[0].protocol,
4202 ext_host_address=sessions[0].ext_host_address,
4203 ext_host_port=sessions[0].ext_host_port)
4204 sessions = self.vapi.nat44_user_session_dump(user.ip4n, 0)
4205 self.assertEqual(len(sessions), 2)
4206
4207 finally:
4208 self.vapi.nat44_forwarding_enable_disable(0)
4209 self.vapi.nat44_add_del_static_mapping(local_ip=real_ip,
4210 external_ip=alias_ip,
4211 is_add=0)
4212
4213 def test_static_lb(self):
4214 """ NAT44 local service load balancing """
4215 external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr)
4216 external_port = 80
4217 local_port = 8080
4218 server1 = self.pg0.remote_hosts[0]
4219 server2 = self.pg0.remote_hosts[1]
4220
4221 locals = [{'addr': server1.ip4n,
4222 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004223 'probability': 70,
4224 'vrf_id': 0},
Matus Fabiana6110b62018-06-13 05:39:07 -07004225 {'addr': server2.ip4n,
4226 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004227 'probability': 30,
4228 'vrf_id': 0}]
Matus Fabiana6110b62018-06-13 05:39:07 -07004229
4230 self.nat44_add_address(self.nat_addr)
4231 self.vapi.nat44_add_del_lb_static_mapping(external_addr_n,
4232 external_port,
4233 IP_PROTOS.tcp,
4234 local_num=len(locals),
4235 locals=locals)
4236 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4237 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4238 is_inside=0)
4239
4240 # from client to service
4241 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4242 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4243 TCP(sport=12345, dport=external_port))
4244 self.pg1.add_stream(p)
4245 self.pg_enable_capture(self.pg_interfaces)
4246 self.pg_start()
4247 capture = self.pg0.get_capture(1)
4248 p = capture[0]
4249 server = None
4250 try:
4251 ip = p[IP]
4252 tcp = p[TCP]
4253 self.assertIn(ip.dst, [server1.ip4, server2.ip4])
4254 if ip.dst == server1.ip4:
4255 server = server1
4256 else:
4257 server = server2
4258 self.assertEqual(tcp.dport, local_port)
4259 self.assert_packet_checksums_valid(p)
4260 except:
4261 self.logger.error(ppp("Unexpected or invalid packet:", p))
4262 raise
4263
4264 # from service back to client
4265 p = (Ether(src=server.mac, dst=self.pg0.local_mac) /
4266 IP(src=server.ip4, dst=self.pg1.remote_ip4) /
4267 TCP(sport=local_port, dport=12345))
4268 self.pg0.add_stream(p)
4269 self.pg_enable_capture(self.pg_interfaces)
4270 self.pg_start()
4271 capture = self.pg1.get_capture(1)
4272 p = capture[0]
4273 try:
4274 ip = p[IP]
4275 tcp = p[TCP]
4276 self.assertEqual(ip.src, self.nat_addr)
4277 self.assertEqual(tcp.sport, external_port)
4278 self.assert_packet_checksums_valid(p)
4279 except:
4280 self.logger.error(ppp("Unexpected or invalid packet:", p))
4281 raise
4282
4283 sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0)
4284 self.assertEqual(len(sessions), 1)
4285 self.assertTrue(sessions[0].ext_host_valid)
4286 self.vapi.nat44_del_session(
4287 sessions[0].inside_ip_address,
4288 sessions[0].inside_port,
4289 sessions[0].protocol,
4290 ext_host_address=sessions[0].ext_host_address,
4291 ext_host_port=sessions[0].ext_host_port)
4292 sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0)
4293 self.assertEqual(len(sessions), 0)
4294
Paul Vinciguerradefde0f2018-12-06 07:46:13 -08004295 @unittest.skipUnless(running_extended_tests, "part of extended tests")
Matus Fabiana6110b62018-06-13 05:39:07 -07004296 def test_static_lb_multi_clients(self):
4297 """ NAT44 local service load balancing - multiple clients"""
4298
4299 external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr)
4300 external_port = 80
4301 local_port = 8080
4302 server1 = self.pg0.remote_hosts[0]
4303 server2 = self.pg0.remote_hosts[1]
Matus Fabianb6865082018-12-06 03:11:09 -08004304 server3 = self.pg0.remote_hosts[2]
Matus Fabiana6110b62018-06-13 05:39:07 -07004305
4306 locals = [{'addr': server1.ip4n,
4307 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004308 'probability': 90,
4309 'vrf_id': 0},
Matus Fabiana6110b62018-06-13 05:39:07 -07004310 {'addr': server2.ip4n,
4311 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004312 'probability': 10,
4313 'vrf_id': 0}]
Matus Fabiana6110b62018-06-13 05:39:07 -07004314
4315 self.nat44_add_address(self.nat_addr)
4316 self.vapi.nat44_add_del_lb_static_mapping(external_addr_n,
4317 external_port,
4318 IP_PROTOS.tcp,
4319 local_num=len(locals),
4320 locals=locals)
4321 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4322 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4323 is_inside=0)
4324
4325 server1_n = 0
4326 server2_n = 0
4327 clients = ip4_range(self.pg1.remote_ip4, 10, 50)
4328 pkts = []
4329 for client in clients:
4330 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4331 IP(src=client, dst=self.nat_addr) /
4332 TCP(sport=12345, dport=external_port))
4333 pkts.append(p)
4334 self.pg1.add_stream(pkts)
4335 self.pg_enable_capture(self.pg_interfaces)
4336 self.pg_start()
4337 capture = self.pg0.get_capture(len(pkts))
4338 for p in capture:
4339 if p[IP].dst == server1.ip4:
4340 server1_n += 1
4341 else:
4342 server2_n += 1
Paul Vinciguerra3d2df212018-11-24 23:19:53 -08004343 self.assertGreater(server1_n, server2_n)
Matus Fabiana6110b62018-06-13 05:39:07 -07004344
Matus Fabianb6865082018-12-06 03:11:09 -08004345 # add new back-end
4346 self.vapi.nat44_lb_static_mapping_add_del_local(external_addr_n,
4347 external_port,
4348 server3.ip4n,
4349 local_port,
4350 IP_PROTOS.tcp,
4351 20)
4352 server1_n = 0
4353 server2_n = 0
4354 server3_n = 0
4355 clients = ip4_range(self.pg1.remote_ip4, 60, 110)
4356 pkts = []
4357 for client in clients:
4358 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4359 IP(src=client, dst=self.nat_addr) /
4360 TCP(sport=12346, dport=external_port))
4361 pkts.append(p)
4362 self.assertGreater(len(pkts), 0)
4363 self.pg1.add_stream(pkts)
4364 self.pg_enable_capture(self.pg_interfaces)
4365 self.pg_start()
4366 capture = self.pg0.get_capture(len(pkts))
4367 for p in capture:
4368 if p[IP].dst == server1.ip4:
4369 server1_n += 1
4370 elif p[IP].dst == server2.ip4:
4371 server2_n += 1
4372 else:
4373 server3_n += 1
4374 self.assertGreater(server1_n, 0)
4375 self.assertGreater(server2_n, 0)
4376 self.assertGreater(server3_n, 0)
4377
4378 # remove one back-end
4379 self.vapi.nat44_lb_static_mapping_add_del_local(external_addr_n,
4380 external_port,
4381 server2.ip4n,
4382 local_port,
4383 IP_PROTOS.tcp,
4384 10,
4385 is_add=0)
4386 server1_n = 0
4387 server2_n = 0
4388 server3_n = 0
4389 self.pg1.add_stream(pkts)
4390 self.pg_enable_capture(self.pg_interfaces)
4391 self.pg_start()
4392 capture = self.pg0.get_capture(len(pkts))
4393 for p in capture:
4394 if p[IP].dst == server1.ip4:
4395 server1_n += 1
4396 elif p[IP].dst == server2.ip4:
4397 server2_n += 1
4398 else:
4399 server3_n += 1
4400 self.assertGreater(server1_n, 0)
4401 self.assertEqual(server2_n, 0)
4402 self.assertGreater(server3_n, 0)
4403
Matus Fabiana6110b62018-06-13 05:39:07 -07004404 def test_static_lb_2(self):
4405 """ NAT44 local service load balancing (asymmetrical rule) """
4406 external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr)
4407 external_port = 80
4408 local_port = 8080
4409 server1 = self.pg0.remote_hosts[0]
4410 server2 = self.pg0.remote_hosts[1]
4411
4412 locals = [{'addr': server1.ip4n,
4413 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004414 'probability': 70,
4415 'vrf_id': 0},
Matus Fabiana6110b62018-06-13 05:39:07 -07004416 {'addr': server2.ip4n,
4417 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004418 'probability': 30,
4419 'vrf_id': 0}]
Matus Fabiana6110b62018-06-13 05:39:07 -07004420
4421 self.vapi.nat44_forwarding_enable_disable(1)
4422 self.vapi.nat44_add_del_lb_static_mapping(external_addr_n,
4423 external_port,
4424 IP_PROTOS.tcp,
4425 out2in_only=1,
4426 local_num=len(locals),
4427 locals=locals)
4428 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4429 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4430 is_inside=0)
4431
4432 # from client to service
4433 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4434 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4435 TCP(sport=12345, dport=external_port))
4436 self.pg1.add_stream(p)
4437 self.pg_enable_capture(self.pg_interfaces)
4438 self.pg_start()
4439 capture = self.pg0.get_capture(1)
4440 p = capture[0]
4441 server = None
4442 try:
4443 ip = p[IP]
4444 tcp = p[TCP]
4445 self.assertIn(ip.dst, [server1.ip4, server2.ip4])
4446 if ip.dst == server1.ip4:
4447 server = server1
4448 else:
4449 server = server2
4450 self.assertEqual(tcp.dport, local_port)
4451 self.assert_packet_checksums_valid(p)
4452 except:
4453 self.logger.error(ppp("Unexpected or invalid packet:", p))
4454 raise
4455
4456 # from service back to client
4457 p = (Ether(src=server.mac, dst=self.pg0.local_mac) /
4458 IP(src=server.ip4, dst=self.pg1.remote_ip4) /
4459 TCP(sport=local_port, dport=12345))
4460 self.pg0.add_stream(p)
4461 self.pg_enable_capture(self.pg_interfaces)
4462 self.pg_start()
4463 capture = self.pg1.get_capture(1)
4464 p = capture[0]
4465 try:
4466 ip = p[IP]
4467 tcp = p[TCP]
4468 self.assertEqual(ip.src, self.nat_addr)
4469 self.assertEqual(tcp.sport, external_port)
4470 self.assert_packet_checksums_valid(p)
4471 except:
4472 self.logger.error(ppp("Unexpected or invalid packet:", p))
4473 raise
4474
4475 # from client to server (no translation)
4476 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4477 IP(src=self.pg1.remote_ip4, dst=server1.ip4) /
4478 TCP(sport=12346, dport=local_port))
4479 self.pg1.add_stream(p)
4480 self.pg_enable_capture(self.pg_interfaces)
4481 self.pg_start()
4482 capture = self.pg0.get_capture(1)
4483 p = capture[0]
4484 server = None
4485 try:
4486 ip = p[IP]
4487 tcp = p[TCP]
4488 self.assertEqual(ip.dst, server1.ip4)
4489 self.assertEqual(tcp.dport, local_port)
4490 self.assert_packet_checksums_valid(p)
4491 except:
4492 self.logger.error(ppp("Unexpected or invalid packet:", p))
4493 raise
4494
4495 # from service back to client (no translation)
4496 p = (Ether(src=server1.mac, dst=self.pg0.local_mac) /
4497 IP(src=server1.ip4, dst=self.pg1.remote_ip4) /
4498 TCP(sport=local_port, dport=12346))
4499 self.pg0.add_stream(p)
4500 self.pg_enable_capture(self.pg_interfaces)
4501 self.pg_start()
4502 capture = self.pg1.get_capture(1)
4503 p = capture[0]
4504 try:
4505 ip = p[IP]
4506 tcp = p[TCP]
4507 self.assertEqual(ip.src, server1.ip4)
4508 self.assertEqual(tcp.sport, local_port)
4509 self.assert_packet_checksums_valid(p)
4510 except:
4511 self.logger.error(ppp("Unexpected or invalid packet:", p))
4512 raise
4513
Matus Fabianea5b5be2018-09-03 05:02:23 -07004514 def test_lb_affinity(self):
4515 """ NAT44 local service load balancing affinity """
4516 external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr)
4517 external_port = 80
4518 local_port = 8080
4519 server1 = self.pg0.remote_hosts[0]
4520 server2 = self.pg0.remote_hosts[1]
4521
4522 locals = [{'addr': server1.ip4n,
4523 'port': local_port,
4524 'probability': 50,
4525 'vrf_id': 0},
4526 {'addr': server2.ip4n,
4527 'port': local_port,
4528 'probability': 50,
4529 'vrf_id': 0}]
4530
4531 self.nat44_add_address(self.nat_addr)
4532 self.vapi.nat44_add_del_lb_static_mapping(external_addr_n,
4533 external_port,
4534 IP_PROTOS.tcp,
4535 affinity=10800,
4536 local_num=len(locals),
4537 locals=locals)
4538 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4539 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4540 is_inside=0)
4541
4542 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
4543 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4544 TCP(sport=1025, dport=external_port))
4545 self.pg1.add_stream(p)
4546 self.pg_enable_capture(self.pg_interfaces)
4547 self.pg_start()
4548 capture = self.pg0.get_capture(1)
4549 backend = capture[0][IP].dst
4550
4551 sessions = self.vapi.nat44_user_session_dump(
4552 socket.inet_pton(socket.AF_INET, backend), 0)
4553 self.assertEqual(len(sessions), 1)
4554 self.assertTrue(sessions[0].ext_host_valid)
4555 self.vapi.nat44_del_session(
4556 sessions[0].inside_ip_address,
4557 sessions[0].inside_port,
4558 sessions[0].protocol,
4559 ext_host_address=sessions[0].ext_host_address,
4560 ext_host_port=sessions[0].ext_host_port)
4561
4562 pkts = []
4563 for port in range(1030, 1100):
4564 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
4565 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4566 TCP(sport=port, dport=external_port))
4567 pkts.append(p)
4568 self.pg1.add_stream(pkts)
4569 self.pg_enable_capture(self.pg_interfaces)
4570 self.pg_start()
4571 capture = self.pg0.get_capture(len(pkts))
4572 for p in capture:
4573 self.assertEqual(p[IP].dst, backend)
4574
Matus Fabiana6110b62018-06-13 05:39:07 -07004575 def test_unknown_proto(self):
4576 """ NAT44 translate packet with unknown protocol """
4577 self.nat44_add_address(self.nat_addr)
4578 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4579 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4580 is_inside=0)
4581
4582 # in2out
4583 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
4584 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
4585 TCP(sport=self.tcp_port_in, dport=20))
4586 self.pg0.add_stream(p)
4587 self.pg_enable_capture(self.pg_interfaces)
4588 self.pg_start()
4589 p = self.pg1.get_capture(1)
4590
4591 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
4592 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
4593 GRE() /
4594 IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) /
4595 TCP(sport=1234, dport=1234))
4596 self.pg0.add_stream(p)
4597 self.pg_enable_capture(self.pg_interfaces)
4598 self.pg_start()
4599 p = self.pg1.get_capture(1)
4600 packet = p[0]
4601 try:
4602 self.assertEqual(packet[IP].src, self.nat_addr)
4603 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08004604 self.assertEqual(packet.haslayer(GRE), 1)
Matus Fabiana6110b62018-06-13 05:39:07 -07004605 self.assert_packet_checksums_valid(packet)
4606 except:
4607 self.logger.error(ppp("Unexpected or invalid packet:", packet))
4608 raise
4609
4610 # out2in
4611 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
4612 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4613 GRE() /
4614 IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) /
4615 TCP(sport=1234, dport=1234))
4616 self.pg1.add_stream(p)
4617 self.pg_enable_capture(self.pg_interfaces)
4618 self.pg_start()
4619 p = self.pg0.get_capture(1)
4620 packet = p[0]
4621 try:
4622 self.assertEqual(packet[IP].src, self.pg1.remote_ip4)
4623 self.assertEqual(packet[IP].dst, self.pg0.remote_ip4)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08004624 self.assertEqual(packet.haslayer(GRE), 1)
Matus Fabiana6110b62018-06-13 05:39:07 -07004625 self.assert_packet_checksums_valid(packet)
4626 except:
4627 self.logger.error(ppp("Unexpected or invalid packet:", packet))
4628 raise
4629
4630 def test_hairpinning_unknown_proto(self):
4631 """ NAT44 translate packet with unknown protocol - hairpinning """
4632 host = self.pg0.remote_hosts[0]
4633 server = self.pg0.remote_hosts[1]
4634 host_in_port = 1234
4635 server_out_port = 8765
4636 server_nat_ip = "10.0.0.11"
4637
4638 self.nat44_add_address(self.nat_addr)
4639 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4640 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4641 is_inside=0)
4642
4643 # add static mapping for server
4644 self.nat44_add_static_mapping(server.ip4, server_nat_ip)
4645
4646 # host to server
4647 p = (Ether(src=host.mac, dst=self.pg0.local_mac) /
4648 IP(src=host.ip4, dst=server_nat_ip) /
4649 TCP(sport=host_in_port, dport=server_out_port))
4650 self.pg0.add_stream(p)
4651 self.pg_enable_capture(self.pg_interfaces)
4652 self.pg_start()
4653 self.pg0.get_capture(1)
4654
4655 p = (Ether(dst=self.pg0.local_mac, src=host.mac) /
4656 IP(src=host.ip4, dst=server_nat_ip) /
4657 GRE() /
4658 IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) /
4659 TCP(sport=1234, dport=1234))
4660 self.pg0.add_stream(p)
4661 self.pg_enable_capture(self.pg_interfaces)
4662 self.pg_start()
4663 p = self.pg0.get_capture(1)
4664 packet = p[0]
4665 try:
4666 self.assertEqual(packet[IP].src, self.nat_addr)
4667 self.assertEqual(packet[IP].dst, server.ip4)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08004668 self.assertEqual(packet.haslayer(GRE), 1)
Matus Fabiana6110b62018-06-13 05:39:07 -07004669 self.assert_packet_checksums_valid(packet)
4670 except:
4671 self.logger.error(ppp("Unexpected or invalid packet:", packet))
4672 raise
4673
4674 # server to host
4675 p = (Ether(dst=self.pg0.local_mac, src=server.mac) /
4676 IP(src=server.ip4, dst=self.nat_addr) /
4677 GRE() /
4678 IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) /
4679 TCP(sport=1234, dport=1234))
4680 self.pg0.add_stream(p)
4681 self.pg_enable_capture(self.pg_interfaces)
4682 self.pg_start()
4683 p = self.pg0.get_capture(1)
4684 packet = p[0]
4685 try:
4686 self.assertEqual(packet[IP].src, server_nat_ip)
4687 self.assertEqual(packet[IP].dst, host.ip4)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08004688 self.assertEqual(packet.haslayer(GRE), 1)
Matus Fabiana6110b62018-06-13 05:39:07 -07004689 self.assert_packet_checksums_valid(packet)
4690 except:
4691 self.logger.error(ppp("Unexpected or invalid packet:", packet))
4692 raise
4693
4694 def test_output_feature_and_service(self):
4695 """ NAT44 interface output feature and services """
4696 external_addr = '1.2.3.4'
4697 external_port = 80
4698 local_port = 8080
4699
4700 self.vapi.nat44_forwarding_enable_disable(1)
4701 self.nat44_add_address(self.nat_addr)
4702 self.vapi.nat44_add_del_identity_mapping(ip=self.pg1.remote_ip4n)
4703 self.nat44_add_static_mapping(self.pg0.remote_ip4, external_addr,
4704 local_port, external_port,
4705 proto=IP_PROTOS.tcp, out2in_only=1)
4706 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4707 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
4708 is_inside=0)
4709 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
4710 is_inside=0)
4711
4712 # from client to service
4713 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4714 IP(src=self.pg1.remote_ip4, dst=external_addr) /
4715 TCP(sport=12345, dport=external_port))
4716 self.pg1.add_stream(p)
4717 self.pg_enable_capture(self.pg_interfaces)
4718 self.pg_start()
4719 capture = self.pg0.get_capture(1)
4720 p = capture[0]
4721 try:
4722 ip = p[IP]
4723 tcp = p[TCP]
4724 self.assertEqual(ip.dst, self.pg0.remote_ip4)
4725 self.assertEqual(tcp.dport, local_port)
4726 self.assert_packet_checksums_valid(p)
4727 except:
4728 self.logger.error(ppp("Unexpected or invalid packet:", p))
4729 raise
4730
4731 # from service back to client
4732 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
4733 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
4734 TCP(sport=local_port, dport=12345))
4735 self.pg0.add_stream(p)
4736 self.pg_enable_capture(self.pg_interfaces)
4737 self.pg_start()
4738 capture = self.pg1.get_capture(1)
4739 p = capture[0]
4740 try:
4741 ip = p[IP]
4742 tcp = p[TCP]
4743 self.assertEqual(ip.src, external_addr)
4744 self.assertEqual(tcp.sport, external_port)
4745 self.assert_packet_checksums_valid(p)
4746 except:
4747 self.logger.error(ppp("Unexpected or invalid packet:", p))
4748 raise
4749
4750 # from local network host to external network
4751 pkts = self.create_stream_in(self.pg0, self.pg1)
4752 self.pg0.add_stream(pkts)
4753 self.pg_enable_capture(self.pg_interfaces)
4754 self.pg_start()
4755 capture = self.pg1.get_capture(len(pkts))
4756 self.verify_capture_out(capture)
4757 pkts = self.create_stream_in(self.pg0, self.pg1)
4758 self.pg0.add_stream(pkts)
4759 self.pg_enable_capture(self.pg_interfaces)
4760 self.pg_start()
4761 capture = self.pg1.get_capture(len(pkts))
4762 self.verify_capture_out(capture)
4763
4764 # from external network back to local network host
4765 pkts = self.create_stream_out(self.pg1)
4766 self.pg1.add_stream(pkts)
4767 self.pg_enable_capture(self.pg_interfaces)
4768 self.pg_start()
4769 capture = self.pg0.get_capture(len(pkts))
4770 self.verify_capture_in(capture, self.pg0)
4771
4772 def test_output_feature_and_service2(self):
4773 """ NAT44 interface output feature and service host direct access """
4774 self.vapi.nat44_forwarding_enable_disable(1)
4775 self.nat44_add_address(self.nat_addr)
4776 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
4777 is_inside=0)
4778
4779 # session initiaded from service host - translate
4780 pkts = self.create_stream_in(self.pg0, self.pg1)
4781 self.pg0.add_stream(pkts)
4782 self.pg_enable_capture(self.pg_interfaces)
4783 self.pg_start()
4784 capture = self.pg1.get_capture(len(pkts))
4785 self.verify_capture_out(capture)
4786
4787 pkts = self.create_stream_out(self.pg1)
4788 self.pg1.add_stream(pkts)
4789 self.pg_enable_capture(self.pg_interfaces)
4790 self.pg_start()
4791 capture = self.pg0.get_capture(len(pkts))
4792 self.verify_capture_in(capture, self.pg0)
4793
4794 # session initiaded from remote host - do not translate
4795 self.tcp_port_in = 60303
4796 self.udp_port_in = 60304
4797 self.icmp_id_in = 60305
4798 pkts = self.create_stream_out(self.pg1,
4799 self.pg0.remote_ip4,
4800 use_inside_ports=True)
4801 self.pg1.add_stream(pkts)
4802 self.pg_enable_capture(self.pg_interfaces)
4803 self.pg_start()
4804 capture = self.pg0.get_capture(len(pkts))
4805 self.verify_capture_in(capture, self.pg0)
4806
4807 pkts = self.create_stream_in(self.pg0, self.pg1)
4808 self.pg0.add_stream(pkts)
4809 self.pg_enable_capture(self.pg_interfaces)
4810 self.pg_start()
4811 capture = self.pg1.get_capture(len(pkts))
4812 self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4,
4813 same_port=True)
4814
4815 def test_output_feature_and_service3(self):
4816 """ NAT44 interface output feature and DST NAT """
4817 external_addr = '1.2.3.4'
4818 external_port = 80
4819 local_port = 8080
4820
4821 self.vapi.nat44_forwarding_enable_disable(1)
4822 self.nat44_add_address(self.nat_addr)
4823 self.nat44_add_static_mapping(self.pg1.remote_ip4, external_addr,
4824 local_port, external_port,
4825 proto=IP_PROTOS.tcp, out2in_only=1)
4826 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4827 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
4828 is_inside=0)
4829 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
4830 is_inside=0)
4831
4832 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
4833 IP(src=self.pg0.remote_ip4, dst=external_addr) /
4834 TCP(sport=12345, dport=external_port))
4835 self.pg0.add_stream(p)
4836 self.pg_enable_capture(self.pg_interfaces)
4837 self.pg_start()
4838 capture = self.pg1.get_capture(1)
4839 p = capture[0]
4840 try:
4841 ip = p[IP]
4842 tcp = p[TCP]
4843 self.assertEqual(ip.src, self.pg0.remote_ip4)
4844 self.assertEqual(tcp.sport, 12345)
4845 self.assertEqual(ip.dst, self.pg1.remote_ip4)
4846 self.assertEqual(tcp.dport, local_port)
4847 self.assert_packet_checksums_valid(p)
4848 except:
4849 self.logger.error(ppp("Unexpected or invalid packet:", p))
4850 raise
4851
4852 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4853 IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) /
4854 TCP(sport=local_port, dport=12345))
4855 self.pg1.add_stream(p)
4856 self.pg_enable_capture(self.pg_interfaces)
4857 self.pg_start()
4858 capture = self.pg0.get_capture(1)
4859 p = capture[0]
4860 try:
4861 ip = p[IP]
4862 tcp = p[TCP]
4863 self.assertEqual(ip.src, external_addr)
4864 self.assertEqual(tcp.sport, external_port)
4865 self.assertEqual(ip.dst, self.pg0.remote_ip4)
4866 self.assertEqual(tcp.dport, 12345)
4867 self.assert_packet_checksums_valid(p)
4868 except:
4869 self.logger.error(ppp("Unexpected or invalid packet:", p))
4870 raise
4871
Matus Fabian182e37e2018-08-14 04:21:26 -07004872 def test_next_src_nat(self):
4873 """ On way back forward packet to nat44-in2out node. """
4874 twice_nat_addr = '10.0.1.3'
4875 external_port = 80
4876 local_port = 8080
4877 post_twice_nat_port = 0
4878
4879 self.vapi.nat44_forwarding_enable_disable(1)
4880 self.nat44_add_address(twice_nat_addr, twice_nat=1)
4881 self.nat44_add_static_mapping(self.pg6.remote_ip4, self.pg1.remote_ip4,
4882 local_port, external_port,
4883 proto=IP_PROTOS.tcp, out2in_only=1,
4884 self_twice_nat=1, vrf_id=1)
4885 self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index,
4886 is_inside=0)
4887
4888 p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) /
4889 IP(src=self.pg6.remote_ip4, dst=self.pg1.remote_ip4) /
4890 TCP(sport=12345, dport=external_port))
4891 self.pg6.add_stream(p)
4892 self.pg_enable_capture(self.pg_interfaces)
4893 self.pg_start()
4894 capture = self.pg6.get_capture(1)
4895 p = capture[0]
4896 try:
4897 ip = p[IP]
4898 tcp = p[TCP]
4899 self.assertEqual(ip.src, twice_nat_addr)
4900 self.assertNotEqual(tcp.sport, 12345)
4901 post_twice_nat_port = tcp.sport
4902 self.assertEqual(ip.dst, self.pg6.remote_ip4)
4903 self.assertEqual(tcp.dport, local_port)
4904 self.assert_packet_checksums_valid(p)
4905 except:
4906 self.logger.error(ppp("Unexpected or invalid packet:", p))
4907 raise
4908
4909 p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) /
4910 IP(src=self.pg6.remote_ip4, dst=twice_nat_addr) /
4911 TCP(sport=local_port, dport=post_twice_nat_port))
4912 self.pg6.add_stream(p)
4913 self.pg_enable_capture(self.pg_interfaces)
4914 self.pg_start()
4915 capture = self.pg6.get_capture(1)
4916 p = capture[0]
4917 try:
4918 ip = p[IP]
4919 tcp = p[TCP]
4920 self.assertEqual(ip.src, self.pg1.remote_ip4)
4921 self.assertEqual(tcp.sport, external_port)
4922 self.assertEqual(ip.dst, self.pg6.remote_ip4)
4923 self.assertEqual(tcp.dport, 12345)
4924 self.assert_packet_checksums_valid(p)
4925 except:
4926 self.logger.error(ppp("Unexpected or invalid packet:", p))
4927 raise
4928
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004929 def twice_nat_common(self, self_twice_nat=False, same_pg=False, lb=False,
4930 client_id=None):
Matus Fabianb932d262017-12-18 05:38:24 -08004931 twice_nat_addr = '10.0.1.3'
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004932
Matus Fabianb932d262017-12-18 05:38:24 -08004933 port_in = 8080
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004934 if lb:
4935 if not same_pg:
4936 port_in1 = port_in
4937 port_in2 = port_in
4938 else:
4939 port_in1 = port_in+1
4940 port_in2 = port_in+2
4941
Matus Fabianb932d262017-12-18 05:38:24 -08004942 port_out = 80
4943 eh_port_out = 4567
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004944
4945 server1 = self.pg0.remote_hosts[0]
4946 server2 = self.pg0.remote_hosts[1]
4947 if lb and same_pg:
4948 server2 = server1
4949 if not lb:
4950 server = server1
4951
4952 pg0 = self.pg0
4953 if same_pg:
4954 pg1 = self.pg0
4955 else:
4956 pg1 = self.pg1
4957
4958 eh_translate = ((not self_twice_nat) or (not lb and same_pg) or
4959 client_id == 1)
4960
Matus Fabianb932d262017-12-18 05:38:24 -08004961 self.nat44_add_address(self.nat_addr)
4962 self.nat44_add_address(twice_nat_addr, twice_nat=1)
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004963 if not lb:
4964 self.nat44_add_static_mapping(pg0.remote_ip4, self.nat_addr,
4965 port_in, port_out,
4966 proto=IP_PROTOS.tcp,
4967 twice_nat=int(not self_twice_nat),
4968 self_twice_nat=int(self_twice_nat))
4969 else:
4970 locals = [{'addr': server1.ip4n,
4971 'port': port_in1,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004972 'probability': 50,
4973 'vrf_id': 0},
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004974 {'addr': server2.ip4n,
4975 'port': port_in2,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004976 'probability': 50,
4977 'vrf_id': 0}]
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004978 out_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr)
4979 self.vapi.nat44_add_del_lb_static_mapping(out_addr_n,
4980 port_out,
4981 IP_PROTOS.tcp,
4982 twice_nat=int(
4983 not self_twice_nat),
4984 self_twice_nat=int(
4985 self_twice_nat),
4986 local_num=len(locals),
4987 locals=locals)
4988 self.vapi.nat44_interface_add_del_feature(pg0.sw_if_index)
4989 self.vapi.nat44_interface_add_del_feature(pg1.sw_if_index,
Matus Fabianb932d262017-12-18 05:38:24 -08004990 is_inside=0)
4991
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004992 if same_pg:
4993 if not lb:
4994 client = server
4995 else:
4996 assert client_id is not None
4997 if client_id == 1:
4998 client = self.pg0.remote_hosts[0]
4999 elif client_id == 2:
5000 client = self.pg0.remote_hosts[1]
5001 else:
5002 client = pg1.remote_hosts[0]
5003 p = (Ether(src=pg1.remote_mac, dst=pg1.local_mac) /
5004 IP(src=client.ip4, dst=self.nat_addr) /
Matus Fabianb932d262017-12-18 05:38:24 -08005005 TCP(sport=eh_port_out, dport=port_out))
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005006 pg1.add_stream(p)
Matus Fabianb932d262017-12-18 05:38:24 -08005007 self.pg_enable_capture(self.pg_interfaces)
5008 self.pg_start()
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005009 capture = pg0.get_capture(1)
Matus Fabianb932d262017-12-18 05:38:24 -08005010 p = capture[0]
5011 try:
5012 ip = p[IP]
5013 tcp = p[TCP]
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005014 if lb:
5015 if ip.dst == server1.ip4:
5016 server = server1
5017 port_in = port_in1
5018 else:
5019 server = server2
5020 port_in = port_in2
5021 self.assertEqual(ip.dst, server.ip4)
5022 if lb and same_pg:
5023 self.assertIn(tcp.dport, [port_in1, port_in2])
5024 else:
5025 self.assertEqual(tcp.dport, port_in)
5026 if eh_translate:
5027 self.assertEqual(ip.src, twice_nat_addr)
5028 self.assertNotEqual(tcp.sport, eh_port_out)
5029 else:
5030 self.assertEqual(ip.src, client.ip4)
5031 self.assertEqual(tcp.sport, eh_port_out)
5032 eh_addr_in = ip.src
Matus Fabianb932d262017-12-18 05:38:24 -08005033 eh_port_in = tcp.sport
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005034 saved_port_in = tcp.dport
Klement Sekerad81ae412018-05-16 10:52:54 +02005035 self.assert_packet_checksums_valid(p)
Matus Fabianb932d262017-12-18 05:38:24 -08005036 except:
5037 self.logger.error(ppp("Unexpected or invalid packet:", p))
5038 raise
5039
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005040 p = (Ether(src=server.mac, dst=pg0.local_mac) /
5041 IP(src=server.ip4, dst=eh_addr_in) /
5042 TCP(sport=saved_port_in, dport=eh_port_in))
5043 pg0.add_stream(p)
Matus Fabianb932d262017-12-18 05:38:24 -08005044 self.pg_enable_capture(self.pg_interfaces)
5045 self.pg_start()
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005046 capture = pg1.get_capture(1)
Matus Fabianb932d262017-12-18 05:38:24 -08005047 p = capture[0]
5048 try:
5049 ip = p[IP]
5050 tcp = p[TCP]
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005051 self.assertEqual(ip.dst, client.ip4)
Matus Fabianb932d262017-12-18 05:38:24 -08005052 self.assertEqual(ip.src, self.nat_addr)
5053 self.assertEqual(tcp.dport, eh_port_out)
5054 self.assertEqual(tcp.sport, port_out)
Klement Sekerad81ae412018-05-16 10:52:54 +02005055 self.assert_packet_checksums_valid(p)
Matus Fabianb932d262017-12-18 05:38:24 -08005056 except:
5057 self.logger.error(ppp("Unexpected or invalid packet:", p))
5058 raise
5059
Matus Fabian70a26ac2018-05-14 06:20:28 -07005060 if eh_translate:
5061 sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0)
5062 self.assertEqual(len(sessions), 1)
5063 self.assertTrue(sessions[0].ext_host_valid)
5064 self.assertTrue(sessions[0].is_twicenat)
5065 self.vapi.nat44_del_session(
5066 sessions[0].inside_ip_address,
5067 sessions[0].inside_port,
5068 sessions[0].protocol,
5069 ext_host_address=sessions[0].ext_host_nat_address,
5070 ext_host_port=sessions[0].ext_host_nat_port)
5071 sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0)
5072 self.assertEqual(len(sessions), 0)
5073
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005074 def test_twice_nat(self):
5075 """ Twice NAT44 """
5076 self.twice_nat_common()
5077
5078 def test_self_twice_nat_positive(self):
5079 """ Self Twice NAT44 (positive test) """
5080 self.twice_nat_common(self_twice_nat=True, same_pg=True)
5081
5082 def test_self_twice_nat_negative(self):
5083 """ Self Twice NAT44 (negative test) """
5084 self.twice_nat_common(self_twice_nat=True)
5085
Matus Fabianb932d262017-12-18 05:38:24 -08005086 def test_twice_nat_lb(self):
5087 """ Twice NAT44 local service load balancing """
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005088 self.twice_nat_common(lb=True)
Matus Fabianb932d262017-12-18 05:38:24 -08005089
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005090 def test_self_twice_nat_lb_positive(self):
5091 """ Self Twice NAT44 local service load balancing (positive test) """
5092 self.twice_nat_common(lb=True, self_twice_nat=True, same_pg=True,
5093 client_id=1)
Matus Fabianb932d262017-12-18 05:38:24 -08005094
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02005095 def test_self_twice_nat_lb_negative(self):
5096 """ Self Twice NAT44 local service load balancing (negative test) """
5097 self.twice_nat_common(lb=True, self_twice_nat=True, same_pg=True,
5098 client_id=2)
Matus Fabianb932d262017-12-18 05:38:24 -08005099
5100 def test_twice_nat_interface_addr(self):
5101 """ Acquire twice NAT44 addresses from interface """
Matus Fabiana6110b62018-06-13 05:39:07 -07005102 self.vapi.nat44_add_interface_addr(self.pg3.sw_if_index, twice_nat=1)
Matus Fabianb932d262017-12-18 05:38:24 -08005103
5104 # no address in NAT pool
5105 adresses = self.vapi.nat44_address_dump()
5106 self.assertEqual(0, len(adresses))
5107
5108 # configure interface address and check NAT address pool
Matus Fabiana6110b62018-06-13 05:39:07 -07005109 self.pg3.config_ip4()
Matus Fabianb932d262017-12-18 05:38:24 -08005110 adresses = self.vapi.nat44_address_dump()
5111 self.assertEqual(1, len(adresses))
Matus Fabiana6110b62018-06-13 05:39:07 -07005112 self.assertEqual(adresses[0].ip_address[0:4], self.pg3.local_ip4n)
Matus Fabianb932d262017-12-18 05:38:24 -08005113 self.assertEqual(adresses[0].twice_nat, 1)
5114
5115 # remove interface address and check NAT address pool
Matus Fabiana6110b62018-06-13 05:39:07 -07005116 self.pg3.unconfig_ip4()
Matus Fabianb932d262017-12-18 05:38:24 -08005117 adresses = self.vapi.nat44_address_dump()
5118 self.assertEqual(0, len(adresses))
5119
Matus Fabian6c01dce2018-11-16 04:41:31 -08005120 def test_tcp_close(self):
5121 """ Close TCP session from inside network - output feature """
5122 self.vapi.nat44_forwarding_enable_disable(1)
5123 self.nat44_add_address(self.pg1.local_ip4)
5124 twice_nat_addr = '10.0.1.3'
5125 service_ip = '192.168.16.150'
5126 self.nat44_add_address(twice_nat_addr, twice_nat=1)
5127 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5128 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
5129 is_inside=0)
5130 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
5131 is_inside=0)
5132 self.nat44_add_static_mapping(self.pg0.remote_ip4,
5133 service_ip,
5134 80,
5135 80,
5136 proto=IP_PROTOS.tcp,
5137 out2in_only=1,
5138 twice_nat=1)
5139 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
5140 start_sessnum = len(sessions)
5141
5142 # SYN packet out->in
5143 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5144 IP(src=self.pg1.remote_ip4, dst=service_ip) /
5145 TCP(sport=33898, dport=80, flags="S"))
5146 self.pg1.add_stream(p)
5147 self.pg_enable_capture(self.pg_interfaces)
5148 self.pg_start()
5149 capture = self.pg0.get_capture(1)
5150 p = capture[0]
5151 tcp_port = p[TCP].sport
5152
5153 # SYN + ACK packet in->out
5154 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5155 IP(src=self.pg0.remote_ip4, dst=twice_nat_addr) /
5156 TCP(sport=80, dport=tcp_port, flags="SA"))
5157 self.pg0.add_stream(p)
5158 self.pg_enable_capture(self.pg_interfaces)
5159 self.pg_start()
5160 self.pg1.get_capture(1)
5161
5162 # ACK packet out->in
5163 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5164 IP(src=self.pg1.remote_ip4, dst=service_ip) /
5165 TCP(sport=33898, dport=80, flags="A"))
5166 self.pg1.add_stream(p)
5167 self.pg_enable_capture(self.pg_interfaces)
5168 self.pg_start()
5169 self.pg0.get_capture(1)
5170
5171 # FIN packet in -> out
5172 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5173 IP(src=self.pg0.remote_ip4, dst=twice_nat_addr) /
5174 TCP(sport=80, dport=tcp_port, flags="FA", seq=100, ack=300))
5175 self.pg0.add_stream(p)
5176 self.pg_enable_capture(self.pg_interfaces)
5177 self.pg_start()
5178 self.pg1.get_capture(1)
5179
5180 # FIN+ACK packet out -> in
5181 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5182 IP(src=self.pg1.remote_ip4, dst=service_ip) /
5183 TCP(sport=33898, dport=80, flags="FA", seq=300, ack=101))
5184 self.pg1.add_stream(p)
5185 self.pg_enable_capture(self.pg_interfaces)
5186 self.pg_start()
5187 self.pg0.get_capture(1)
5188
5189 # ACK packet in -> out
5190 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5191 IP(src=self.pg0.remote_ip4, dst=twice_nat_addr) /
5192 TCP(sport=80, dport=tcp_port, flags="A", seq=101, ack=301))
5193 self.pg0.add_stream(p)
5194 self.pg_enable_capture(self.pg_interfaces)
5195 self.pg_start()
5196 self.pg1.get_capture(1)
5197
5198 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n,
5199 0)
5200 self.assertEqual(len(sessions) - start_sessnum, 0)
5201
Matus Fabianebdf1902018-05-04 03:57:42 -07005202 def test_tcp_session_close_in(self):
5203 """ Close TCP session from inside network """
Matus Fabian229c1aa2018-05-28 04:09:52 -07005204 self.tcp_port_out = 10505
Matus Fabianebdf1902018-05-04 03:57:42 -07005205 self.nat44_add_address(self.nat_addr)
Matus Fabian229c1aa2018-05-28 04:09:52 -07005206 self.nat44_add_static_mapping(self.pg0.remote_ip4,
5207 self.nat_addr,
5208 self.tcp_port_in,
5209 self.tcp_port_out,
5210 proto=IP_PROTOS.tcp,
5211 twice_nat=1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005212 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5213 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5214 is_inside=0)
5215
5216 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
5217 start_sessnum = len(sessions)
5218
5219 self.initiate_tcp_session(self.pg0, self.pg1)
5220
Matus Fabian229c1aa2018-05-28 04:09:52 -07005221 # FIN packet in -> out
5222 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5223 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5224 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5225 flags="FA", seq=100, ack=300))
5226 self.pg0.add_stream(p)
5227 self.pg_enable_capture(self.pg_interfaces)
5228 self.pg_start()
5229 self.pg1.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005230
Matus Fabian229c1aa2018-05-28 04:09:52 -07005231 pkts = []
Matus Fabianebdf1902018-05-04 03:57:42 -07005232
Matus Fabian229c1aa2018-05-28 04:09:52 -07005233 # ACK packet out -> in
5234 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5235 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5236 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
5237 flags="A", seq=300, ack=101))
5238 pkts.append(p)
Matus Fabianebdf1902018-05-04 03:57:42 -07005239
Matus Fabian229c1aa2018-05-28 04:09:52 -07005240 # FIN packet out -> in
5241 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5242 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5243 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
5244 flags="FA", seq=300, ack=101))
5245 pkts.append(p)
Matus Fabianebdf1902018-05-04 03:57:42 -07005246
Matus Fabian229c1aa2018-05-28 04:09:52 -07005247 self.pg1.add_stream(pkts)
5248 self.pg_enable_capture(self.pg_interfaces)
5249 self.pg_start()
5250 self.pg0.get_capture(2)
Matus Fabianebdf1902018-05-04 03:57:42 -07005251
Matus Fabian229c1aa2018-05-28 04:09:52 -07005252 # ACK packet in -> out
5253 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5254 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5255 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5256 flags="A", seq=101, ack=301))
5257 self.pg0.add_stream(p)
5258 self.pg_enable_capture(self.pg_interfaces)
5259 self.pg_start()
5260 self.pg1.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005261
Matus Fabian229c1aa2018-05-28 04:09:52 -07005262 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n,
5263 0)
5264 self.assertEqual(len(sessions) - start_sessnum, 0)
Matus Fabianebdf1902018-05-04 03:57:42 -07005265
5266 def test_tcp_session_close_out(self):
5267 """ Close TCP session from outside network """
Matus Fabian229c1aa2018-05-28 04:09:52 -07005268 self.tcp_port_out = 10505
Matus Fabianebdf1902018-05-04 03:57:42 -07005269 self.nat44_add_address(self.nat_addr)
Matus Fabian229c1aa2018-05-28 04:09:52 -07005270 self.nat44_add_static_mapping(self.pg0.remote_ip4,
5271 self.nat_addr,
5272 self.tcp_port_in,
5273 self.tcp_port_out,
5274 proto=IP_PROTOS.tcp,
5275 twice_nat=1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005276 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5277 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5278 is_inside=0)
5279
5280 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
5281 start_sessnum = len(sessions)
5282
5283 self.initiate_tcp_session(self.pg0, self.pg1)
5284
Matus Fabian229c1aa2018-05-28 04:09:52 -07005285 # FIN packet out -> in
5286 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5287 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5288 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
5289 flags="FA", seq=100, ack=300))
5290 self.pg1.add_stream(p)
5291 self.pg_enable_capture(self.pg_interfaces)
5292 self.pg_start()
5293 self.pg0.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005294
Matus Fabian229c1aa2018-05-28 04:09:52 -07005295 # FIN+ACK packet in -> out
5296 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5297 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5298 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5299 flags="FA", seq=300, ack=101))
Matus Fabianebdf1902018-05-04 03:57:42 -07005300
Matus Fabian229c1aa2018-05-28 04:09:52 -07005301 self.pg0.add_stream(p)
5302 self.pg_enable_capture(self.pg_interfaces)
5303 self.pg_start()
5304 self.pg1.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005305
Matus Fabian229c1aa2018-05-28 04:09:52 -07005306 # ACK packet out -> in
5307 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5308 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5309 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
5310 flags="A", seq=101, ack=301))
5311 self.pg1.add_stream(p)
5312 self.pg_enable_capture(self.pg_interfaces)
5313 self.pg_start()
5314 self.pg0.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005315
Matus Fabian229c1aa2018-05-28 04:09:52 -07005316 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n,
5317 0)
5318 self.assertEqual(len(sessions) - start_sessnum, 0)
Matus Fabianebdf1902018-05-04 03:57:42 -07005319
5320 def test_tcp_session_close_simultaneous(self):
5321 """ Close TCP session from inside network """
Matus Fabian229c1aa2018-05-28 04:09:52 -07005322 self.tcp_port_out = 10505
Matus Fabianebdf1902018-05-04 03:57:42 -07005323 self.nat44_add_address(self.nat_addr)
Matus Fabian229c1aa2018-05-28 04:09:52 -07005324 self.nat44_add_static_mapping(self.pg0.remote_ip4,
5325 self.nat_addr,
5326 self.tcp_port_in,
5327 self.tcp_port_out,
5328 proto=IP_PROTOS.tcp,
5329 twice_nat=1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005330 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5331 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5332 is_inside=0)
5333
5334 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
5335 start_sessnum = len(sessions)
5336
5337 self.initiate_tcp_session(self.pg0, self.pg1)
5338
Matus Fabian229c1aa2018-05-28 04:09:52 -07005339 # FIN packet in -> out
5340 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5341 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5342 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5343 flags="FA", seq=100, ack=300))
5344 self.pg0.add_stream(p)
5345 self.pg_enable_capture(self.pg_interfaces)
5346 self.pg_start()
5347 self.pg1.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005348
Matus Fabian229c1aa2018-05-28 04:09:52 -07005349 # FIN packet out -> in
5350 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5351 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5352 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
5353 flags="FA", seq=300, ack=100))
5354 self.pg1.add_stream(p)
5355 self.pg_enable_capture(self.pg_interfaces)
5356 self.pg_start()
5357 self.pg0.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005358
Matus Fabian229c1aa2018-05-28 04:09:52 -07005359 # ACK packet in -> out
5360 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5361 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5362 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5363 flags="A", seq=101, ack=301))
5364 self.pg0.add_stream(p)
5365 self.pg_enable_capture(self.pg_interfaces)
5366 self.pg_start()
5367 self.pg1.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005368
Matus Fabian229c1aa2018-05-28 04:09:52 -07005369 # ACK packet out -> in
5370 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5371 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5372 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
5373 flags="A", seq=301, ack=101))
5374 self.pg1.add_stream(p)
5375 self.pg_enable_capture(self.pg_interfaces)
5376 self.pg_start()
5377 self.pg0.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005378
Matus Fabian229c1aa2018-05-28 04:09:52 -07005379 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n,
5380 0)
5381 self.assertEqual(len(sessions) - start_sessnum, 0)
Matus Fabianebdf1902018-05-04 03:57:42 -07005382
Matus Fabiana6110b62018-06-13 05:39:07 -07005383 def test_one_armed_nat44_static(self):
5384 """ One armed NAT44 and 1:1 NAPT asymmetrical rule """
5385 remote_host = self.pg4.remote_hosts[0]
5386 local_host = self.pg4.remote_hosts[1]
5387 external_port = 80
5388 local_port = 8080
5389 eh_port_in = 0
5390
5391 self.vapi.nat44_forwarding_enable_disable(1)
5392 self.nat44_add_address(self.nat_addr, twice_nat=1)
5393 self.nat44_add_static_mapping(local_host.ip4, self.nat_addr,
5394 local_port, external_port,
5395 proto=IP_PROTOS.tcp, out2in_only=1,
5396 twice_nat=1)
5397 self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index)
5398 self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index,
5399 is_inside=0)
5400
5401 # from client to service
5402 p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) /
5403 IP(src=remote_host.ip4, dst=self.nat_addr) /
5404 TCP(sport=12345, dport=external_port))
5405 self.pg4.add_stream(p)
5406 self.pg_enable_capture(self.pg_interfaces)
5407 self.pg_start()
5408 capture = self.pg4.get_capture(1)
5409 p = capture[0]
5410 try:
5411 ip = p[IP]
5412 tcp = p[TCP]
5413 self.assertEqual(ip.dst, local_host.ip4)
5414 self.assertEqual(ip.src, self.nat_addr)
5415 self.assertEqual(tcp.dport, local_port)
5416 self.assertNotEqual(tcp.sport, 12345)
5417 eh_port_in = tcp.sport
5418 self.assert_packet_checksums_valid(p)
5419 except:
5420 self.logger.error(ppp("Unexpected or invalid packet:", p))
5421 raise
5422
5423 # from service back to client
5424 p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) /
5425 IP(src=local_host.ip4, dst=self.nat_addr) /
5426 TCP(sport=local_port, dport=eh_port_in))
5427 self.pg4.add_stream(p)
5428 self.pg_enable_capture(self.pg_interfaces)
5429 self.pg_start()
5430 capture = self.pg4.get_capture(1)
5431 p = capture[0]
5432 try:
5433 ip = p[IP]
5434 tcp = p[TCP]
5435 self.assertEqual(ip.src, self.nat_addr)
5436 self.assertEqual(ip.dst, remote_host.ip4)
5437 self.assertEqual(tcp.sport, external_port)
5438 self.assertEqual(tcp.dport, 12345)
5439 self.assert_packet_checksums_valid(p)
5440 except:
5441 self.logger.error(ppp("Unexpected or invalid packet:", p))
5442 raise
5443
5444 def test_static_with_port_out2(self):
5445 """ 1:1 NAPT asymmetrical rule """
5446
5447 external_port = 80
5448 local_port = 8080
5449
5450 self.vapi.nat44_forwarding_enable_disable(1)
5451 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
5452 local_port, external_port,
5453 proto=IP_PROTOS.tcp, out2in_only=1)
5454 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5455 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5456 is_inside=0)
5457
5458 # from client to service
5459 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5460 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5461 TCP(sport=12345, dport=external_port))
5462 self.pg1.add_stream(p)
5463 self.pg_enable_capture(self.pg_interfaces)
5464 self.pg_start()
5465 capture = self.pg0.get_capture(1)
5466 p = capture[0]
5467 try:
5468 ip = p[IP]
5469 tcp = p[TCP]
5470 self.assertEqual(ip.dst, self.pg0.remote_ip4)
5471 self.assertEqual(tcp.dport, local_port)
5472 self.assert_packet_checksums_valid(p)
5473 except:
5474 self.logger.error(ppp("Unexpected or invalid packet:", p))
5475 raise
5476
5477 # ICMP error
5478 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
5479 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5480 ICMP(type=11) / capture[0][IP])
5481 self.pg0.add_stream(p)
5482 self.pg_enable_capture(self.pg_interfaces)
5483 self.pg_start()
5484 capture = self.pg1.get_capture(1)
5485 p = capture[0]
5486 try:
5487 self.assertEqual(p[IP].src, self.nat_addr)
5488 inner = p[IPerror]
5489 self.assertEqual(inner.dst, self.nat_addr)
5490 self.assertEqual(inner[TCPerror].dport, external_port)
5491 except:
5492 self.logger.error(ppp("Unexpected or invalid packet:", p))
5493 raise
5494
5495 # from service back to client
5496 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5497 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5498 TCP(sport=local_port, dport=12345))
5499 self.pg0.add_stream(p)
5500 self.pg_enable_capture(self.pg_interfaces)
5501 self.pg_start()
5502 capture = self.pg1.get_capture(1)
5503 p = capture[0]
5504 try:
5505 ip = p[IP]
5506 tcp = p[TCP]
5507 self.assertEqual(ip.src, self.nat_addr)
5508 self.assertEqual(tcp.sport, external_port)
5509 self.assert_packet_checksums_valid(p)
5510 except:
5511 self.logger.error(ppp("Unexpected or invalid packet:", p))
5512 raise
5513
5514 # ICMP error
5515 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
5516 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5517 ICMP(type=11) / capture[0][IP])
5518 self.pg1.add_stream(p)
5519 self.pg_enable_capture(self.pg_interfaces)
5520 self.pg_start()
5521 capture = self.pg0.get_capture(1)
5522 p = capture[0]
5523 try:
5524 self.assertEqual(p[IP].dst, self.pg0.remote_ip4)
5525 inner = p[IPerror]
5526 self.assertEqual(inner.src, self.pg0.remote_ip4)
5527 self.assertEqual(inner[TCPerror].sport, local_port)
5528 except:
5529 self.logger.error(ppp("Unexpected or invalid packet:", p))
5530 raise
5531
5532 # from client to server (no translation)
5533 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5534 IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) /
5535 TCP(sport=12346, dport=local_port))
5536 self.pg1.add_stream(p)
5537 self.pg_enable_capture(self.pg_interfaces)
5538 self.pg_start()
5539 capture = self.pg0.get_capture(1)
5540 p = capture[0]
5541 try:
5542 ip = p[IP]
5543 tcp = p[TCP]
5544 self.assertEqual(ip.dst, self.pg0.remote_ip4)
5545 self.assertEqual(tcp.dport, local_port)
5546 self.assert_packet_checksums_valid(p)
5547 except:
5548 self.logger.error(ppp("Unexpected or invalid packet:", p))
5549 raise
5550
5551 # from service back to client (no translation)
5552 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5553 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5554 TCP(sport=local_port, dport=12346))
5555 self.pg0.add_stream(p)
5556 self.pg_enable_capture(self.pg_interfaces)
5557 self.pg_start()
5558 capture = self.pg1.get_capture(1)
5559 p = capture[0]
5560 try:
5561 ip = p[IP]
5562 tcp = p[TCP]
5563 self.assertEqual(ip.src, self.pg0.remote_ip4)
5564 self.assertEqual(tcp.sport, local_port)
5565 self.assert_packet_checksums_valid(p)
5566 except:
5567 self.logger.error(ppp("Unexpected or invalid packet:", p))
5568 raise
5569
Matus Fabian235a47e2018-06-25 16:42:36 -07005570 def test_output_feature(self):
5571 """ NAT44 interface output feature (in2out postrouting) """
5572 self.vapi.nat44_forwarding_enable_disable(1)
5573 self.nat44_add_address(self.nat_addr)
5574 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
5575 is_inside=0)
5576 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
5577 is_inside=0)
5578
5579 # in2out
5580 pkts = self.create_stream_in(self.pg0, self.pg1)
5581 self.pg0.add_stream(pkts)
5582 self.pg_enable_capture(self.pg_interfaces)
5583 self.pg_start()
5584 capture = self.pg1.get_capture(len(pkts))
5585 self.verify_capture_out(capture)
5586
5587 # out2in
5588 pkts = self.create_stream_out(self.pg1)
5589 self.pg1.add_stream(pkts)
5590 self.pg_enable_capture(self.pg_interfaces)
5591 self.pg_start()
5592 capture = self.pg0.get_capture(len(pkts))
5593 self.verify_capture_in(capture, self.pg0)
5594
Matus Fabian8008d7c2018-07-09 01:34:20 -07005595 def test_multiple_vrf(self):
5596 """ Multiple VRF setup """
5597 external_addr = '1.2.3.4'
5598 external_port = 80
5599 local_port = 8080
5600 port = 0
5601
5602 self.vapi.nat44_forwarding_enable_disable(1)
5603 self.nat44_add_address(self.nat_addr)
5604 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5605 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
5606 is_inside=0)
5607 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
5608 is_inside=0)
5609 self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index)
5610 self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index,
5611 is_inside=0)
5612 self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index,
5613 is_inside=0)
5614 self.nat44_add_static_mapping(self.pg5.remote_ip4, external_addr,
5615 local_port, external_port, vrf_id=1,
5616 proto=IP_PROTOS.tcp, out2in_only=1)
5617 self.nat44_add_static_mapping(
5618 self.pg0.remote_ip4, external_sw_if_index=self.pg0.sw_if_index,
5619 local_port=local_port, vrf_id=0, external_port=external_port,
5620 proto=IP_PROTOS.tcp, out2in_only=1)
5621
5622 # from client to service (both VRF1)
5623 p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) /
5624 IP(src=self.pg6.remote_ip4, dst=external_addr) /
5625 TCP(sport=12345, dport=external_port))
5626 self.pg6.add_stream(p)
5627 self.pg_enable_capture(self.pg_interfaces)
5628 self.pg_start()
5629 capture = self.pg5.get_capture(1)
5630 p = capture[0]
5631 try:
5632 ip = p[IP]
5633 tcp = p[TCP]
5634 self.assertEqual(ip.dst, self.pg5.remote_ip4)
5635 self.assertEqual(tcp.dport, local_port)
5636 self.assert_packet_checksums_valid(p)
5637 except:
5638 self.logger.error(ppp("Unexpected or invalid packet:", p))
5639 raise
5640
5641 # from service back to client (both VRF1)
5642 p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
5643 IP(src=self.pg5.remote_ip4, dst=self.pg6.remote_ip4) /
5644 TCP(sport=local_port, dport=12345))
5645 self.pg5.add_stream(p)
5646 self.pg_enable_capture(self.pg_interfaces)
5647 self.pg_start()
5648 capture = self.pg6.get_capture(1)
5649 p = capture[0]
5650 try:
5651 ip = p[IP]
5652 tcp = p[TCP]
5653 self.assertEqual(ip.src, external_addr)
5654 self.assertEqual(tcp.sport, external_port)
5655 self.assert_packet_checksums_valid(p)
5656 except:
5657 self.logger.error(ppp("Unexpected or invalid packet:", p))
5658 raise
5659
5660 # dynamic NAT from VRF1 to VRF0 (output-feature)
5661 p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
5662 IP(src=self.pg5.remote_ip4, dst=self.pg1.remote_ip4) /
5663 TCP(sport=2345, dport=22))
5664 self.pg5.add_stream(p)
5665 self.pg_enable_capture(self.pg_interfaces)
5666 self.pg_start()
5667 capture = self.pg1.get_capture(1)
5668 p = capture[0]
5669 try:
5670 ip = p[IP]
5671 tcp = p[TCP]
5672 self.assertEqual(ip.src, self.nat_addr)
5673 self.assertNotEqual(tcp.sport, 2345)
5674 self.assert_packet_checksums_valid(p)
5675 port = tcp.sport
5676 except:
5677 self.logger.error(ppp("Unexpected or invalid packet:", p))
5678 raise
5679
5680 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5681 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5682 TCP(sport=22, dport=port))
5683 self.pg1.add_stream(p)
5684 self.pg_enable_capture(self.pg_interfaces)
5685 self.pg_start()
5686 capture = self.pg5.get_capture(1)
5687 p = capture[0]
5688 try:
5689 ip = p[IP]
5690 tcp = p[TCP]
5691 self.assertEqual(ip.dst, self.pg5.remote_ip4)
5692 self.assertEqual(tcp.dport, 2345)
5693 self.assert_packet_checksums_valid(p)
5694 except:
5695 self.logger.error(ppp("Unexpected or invalid packet:", p))
5696 raise
5697
5698 # from client VRF1 to service VRF0
5699 p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) /
5700 IP(src=self.pg6.remote_ip4, dst=self.pg0.local_ip4) /
5701 TCP(sport=12346, dport=external_port))
5702 self.pg6.add_stream(p)
5703 self.pg_enable_capture(self.pg_interfaces)
5704 self.pg_start()
5705 capture = self.pg0.get_capture(1)
5706 p = capture[0]
5707 try:
5708 ip = p[IP]
5709 tcp = p[TCP]
5710 self.assertEqual(ip.dst, self.pg0.remote_ip4)
5711 self.assertEqual(tcp.dport, local_port)
5712 self.assert_packet_checksums_valid(p)
5713 except:
5714 self.logger.error(ppp("Unexpected or invalid packet:", p))
5715 raise
5716
5717 # from service VRF0 back to client VRF1
5718 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5719 IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) /
5720 TCP(sport=local_port, dport=12346))
5721 self.pg0.add_stream(p)
5722 self.pg_enable_capture(self.pg_interfaces)
5723 self.pg_start()
5724 capture = self.pg6.get_capture(1)
5725 p = capture[0]
5726 try:
5727 ip = p[IP]
5728 tcp = p[TCP]
5729 self.assertEqual(ip.src, self.pg0.local_ip4)
5730 self.assertEqual(tcp.sport, external_port)
5731 self.assert_packet_checksums_valid(p)
5732 except:
5733 self.logger.error(ppp("Unexpected or invalid packet:", p))
5734 raise
5735
5736 # from client VRF0 to service VRF1
5737 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5738 IP(src=self.pg0.remote_ip4, dst=external_addr) /
5739 TCP(sport=12347, dport=external_port))
5740 self.pg0.add_stream(p)
5741 self.pg_enable_capture(self.pg_interfaces)
5742 self.pg_start()
5743 capture = self.pg5.get_capture(1)
5744 p = capture[0]
5745 try:
5746 ip = p[IP]
5747 tcp = p[TCP]
5748 self.assertEqual(ip.dst, self.pg5.remote_ip4)
5749 self.assertEqual(tcp.dport, local_port)
5750 self.assert_packet_checksums_valid(p)
5751 except:
5752 self.logger.error(ppp("Unexpected or invalid packet:", p))
5753 raise
5754
5755 # from service VRF1 back to client VRF0
5756 p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
5757 IP(src=self.pg5.remote_ip4, dst=self.pg0.remote_ip4) /
5758 TCP(sport=local_port, dport=12347))
5759 self.pg5.add_stream(p)
5760 self.pg_enable_capture(self.pg_interfaces)
5761 self.pg_start()
5762 capture = self.pg0.get_capture(1)
5763 p = capture[0]
5764 try:
5765 ip = p[IP]
5766 tcp = p[TCP]
5767 self.assertEqual(ip.src, external_addr)
5768 self.assertEqual(tcp.sport, external_port)
5769 self.assert_packet_checksums_valid(p)
5770 except:
5771 self.logger.error(ppp("Unexpected or invalid packet:", p))
5772 raise
5773
5774 # from client to server (both VRF1, no translation)
5775 p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) /
5776 IP(src=self.pg6.remote_ip4, dst=self.pg5.remote_ip4) /
5777 TCP(sport=12348, dport=local_port))
5778 self.pg6.add_stream(p)
5779 self.pg_enable_capture(self.pg_interfaces)
5780 self.pg_start()
5781 capture = self.pg5.get_capture(1)
5782 p = capture[0]
5783 try:
5784 ip = p[IP]
5785 tcp = p[TCP]
5786 self.assertEqual(ip.dst, self.pg5.remote_ip4)
5787 self.assertEqual(tcp.dport, local_port)
5788 self.assert_packet_checksums_valid(p)
5789 except:
5790 self.logger.error(ppp("Unexpected or invalid packet:", p))
5791 raise
5792
5793 # from server back to client (both VRF1, no translation)
5794 p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
5795 IP(src=self.pg5.remote_ip4, dst=self.pg6.remote_ip4) /
5796 TCP(sport=local_port, dport=12348))
5797 self.pg5.add_stream(p)
5798 self.pg_enable_capture(self.pg_interfaces)
5799 self.pg_start()
5800 capture = self.pg6.get_capture(1)
5801 p = capture[0]
5802 try:
5803 ip = p[IP]
5804 tcp = p[TCP]
5805 self.assertEqual(ip.src, self.pg5.remote_ip4)
5806 self.assertEqual(tcp.sport, local_port)
5807 self.assert_packet_checksums_valid(p)
5808 except:
5809 self.logger.error(ppp("Unexpected or invalid packet:", p))
5810 raise
5811
5812 # from client VRF1 to server VRF0 (no translation)
5813 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5814 IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) /
5815 TCP(sport=local_port, dport=12349))
5816 self.pg0.add_stream(p)
5817 self.pg_enable_capture(self.pg_interfaces)
5818 self.pg_start()
5819 capture = self.pg6.get_capture(1)
5820 p = capture[0]
5821 try:
5822 ip = p[IP]
5823 tcp = p[TCP]
5824 self.assertEqual(ip.src, self.pg0.remote_ip4)
5825 self.assertEqual(tcp.sport, local_port)
5826 self.assert_packet_checksums_valid(p)
5827 except:
5828 self.logger.error(ppp("Unexpected or invalid packet:", p))
5829 raise
5830
5831 # from server VRF0 back to client VRF1 (no translation)
5832 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5833 IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) /
5834 TCP(sport=local_port, dport=12349))
5835 self.pg0.add_stream(p)
5836 self.pg_enable_capture(self.pg_interfaces)
5837 self.pg_start()
5838 capture = self.pg6.get_capture(1)
5839 p = capture[0]
5840 try:
5841 ip = p[IP]
5842 tcp = p[TCP]
5843 self.assertEqual(ip.src, self.pg0.remote_ip4)
5844 self.assertEqual(tcp.sport, local_port)
5845 self.assert_packet_checksums_valid(p)
5846 except:
5847 self.logger.error(ppp("Unexpected or invalid packet:", p))
5848 raise
5849
5850 # from client VRF0 to server VRF1 (no translation)
5851 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5852 IP(src=self.pg0.remote_ip4, dst=self.pg5.remote_ip4) /
5853 TCP(sport=12344, dport=local_port))
5854 self.pg0.add_stream(p)
5855 self.pg_enable_capture(self.pg_interfaces)
5856 self.pg_start()
5857 capture = self.pg5.get_capture(1)
5858 p = capture[0]
5859 try:
5860 ip = p[IP]
5861 tcp = p[TCP]
5862 self.assertEqual(ip.dst, self.pg5.remote_ip4)
5863 self.assertEqual(tcp.dport, local_port)
5864 self.assert_packet_checksums_valid(p)
5865 except:
5866 self.logger.error(ppp("Unexpected or invalid packet:", p))
5867 raise
5868
5869 # from server VRF1 back to client VRF0 (no translation)
5870 p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
5871 IP(src=self.pg5.remote_ip4, dst=self.pg0.remote_ip4) /
5872 TCP(sport=local_port, dport=12344))
5873 self.pg5.add_stream(p)
5874 self.pg_enable_capture(self.pg_interfaces)
5875 self.pg_start()
5876 capture = self.pg0.get_capture(1)
5877 p = capture[0]
5878 try:
5879 ip = p[IP]
5880 tcp = p[TCP]
5881 self.assertEqual(ip.src, self.pg5.remote_ip4)
5882 self.assertEqual(tcp.sport, local_port)
5883 self.assert_packet_checksums_valid(p)
5884 except:
5885 self.logger.error(ppp("Unexpected or invalid packet:", p))
5886 raise
5887
Paul Vinciguerradefde0f2018-12-06 07:46:13 -08005888 @unittest.skipUnless(running_extended_tests, "part of extended tests")
Matus Fabian878c6462018-08-23 00:33:35 -07005889 def test_session_timeout(self):
5890 """ NAT44 session timeouts """
5891 self.nat44_add_address(self.nat_addr)
5892 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5893 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5894 is_inside=0)
5895 self.vapi.nat_set_timeouts(icmp=5)
5896
5897 max_sessions = 1000
5898 pkts = []
5899 for i in range(0, max_sessions):
5900 src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF)
5901 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
5902 IP(src=src, dst=self.pg1.remote_ip4) /
5903 ICMP(id=1025, type='echo-request'))
5904 pkts.append(p)
5905 self.pg0.add_stream(pkts)
5906 self.pg_enable_capture(self.pg_interfaces)
5907 self.pg_start()
5908 self.pg1.get_capture(max_sessions)
5909
5910 sleep(10)
5911
5912 pkts = []
5913 for i in range(0, max_sessions):
Matus Fabian7f8a8db2018-11-22 00:12:15 -08005914 src = "10.11.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF)
Matus Fabian878c6462018-08-23 00:33:35 -07005915 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
5916 IP(src=src, dst=self.pg1.remote_ip4) /
5917 ICMP(id=1026, type='echo-request'))
5918 pkts.append(p)
5919 self.pg0.add_stream(pkts)
5920 self.pg_enable_capture(self.pg_interfaces)
5921 self.pg_start()
5922 self.pg1.get_capture(max_sessions)
5923
5924 nsessions = 0
5925 users = self.vapi.nat44_user_dump()
5926 for user in users:
5927 nsessions = nsessions + user.nsessions
5928 self.assertLess(nsessions, 2 * max_sessions)
5929
Paul Vinciguerradefde0f2018-12-06 07:46:13 -08005930 @unittest.skipUnless(running_extended_tests, "part of extended tests")
Matus Fabian15e8e682018-11-21 04:53:10 -08005931 def test_session_rst_timeout(self):
5932 """ NAT44 session RST timeouts """
5933 self.nat44_add_address(self.nat_addr)
5934 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5935 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5936 is_inside=0)
5937 self.vapi.nat_set_timeouts(tcp_transitory=5)
5938
Matus Fabian15e8e682018-11-21 04:53:10 -08005939 self.initiate_tcp_session(self.pg0, self.pg1)
5940 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5941 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5942 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5943 flags="R"))
5944 self.pg0.add_stream(p)
5945 self.pg_enable_capture(self.pg_interfaces)
5946 self.pg_start()
5947 self.pg1.get_capture(1)
5948
Matus Fabian15e8e682018-11-21 04:53:10 -08005949 sleep(6)
5950
5951 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5952 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5953 TCP(sport=self.tcp_port_in + 1, dport=self.tcp_external_port + 1,
5954 flags="S"))
5955 self.pg0.add_stream(p)
5956 self.pg_enable_capture(self.pg_interfaces)
5957 self.pg_start()
5958 self.pg1.get_capture(1)
5959
5960 nsessions = 0
5961 users = self.vapi.nat44_user_dump()
5962 self.assertEqual(len(users), 1)
5963 self.assertEqual(users[0].ip_address, self.pg0.remote_ip4n)
Matus Fabian7f8a8db2018-11-22 00:12:15 -08005964 self.assertEqual(users[0].nsessions, 1)
Matus Fabian15e8e682018-11-21 04:53:10 -08005965
Paul Vinciguerradefde0f2018-12-06 07:46:13 -08005966 @unittest.skipUnless(running_extended_tests, "part of extended tests")
Matus Fabian878c6462018-08-23 00:33:35 -07005967 def test_session_limit_per_user(self):
5968 """ Maximum sessions per user limit """
5969 self.nat44_add_address(self.nat_addr)
5970 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5971 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5972 is_inside=0)
5973 self.vapi.set_ipfix_exporter(collector_address=self.pg2.remote_ip4n,
5974 src_address=self.pg2.local_ip4n,
5975 path_mtu=512,
5976 template_interval=10)
Matus Fabian8fdc0152018-09-24 04:41:28 -07005977 self.vapi.nat_set_timeouts(udp=5)
Matus Fabian878c6462018-08-23 00:33:35 -07005978
5979 # get maximum number of translations per user
5980 nat44_config = self.vapi.nat_show_config()
5981
5982 pkts = []
5983 for port in range(0, nat44_config.max_translations_per_user):
5984 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5985 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5986 UDP(sport=1025 + port, dport=1025 + port))
5987 pkts.append(p)
5988
5989 self.pg0.add_stream(pkts)
5990 self.pg_enable_capture(self.pg_interfaces)
5991 self.pg_start()
5992 capture = self.pg1.get_capture(len(pkts))
5993
5994 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
5995 src_port=self.ipfix_src_port)
5996
5997 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5998 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5999 UDP(sport=3001, dport=3002))
6000 self.pg0.add_stream(p)
6001 self.pg_enable_capture(self.pg_interfaces)
6002 self.pg_start()
6003 capture = self.pg1.assert_nothing_captured()
6004
6005 # verify IPFIX logging
6006 self.vapi.cli("ipfix flush") # FIXME this should be an API call
6007 sleep(1)
6008 capture = self.pg2.get_capture(10)
6009 ipfix = IPFIXDecoder()
6010 # first load template
6011 for p in capture:
6012 self.assertTrue(p.haslayer(IPFIX))
6013 if p.haslayer(Template):
6014 ipfix.add_template(p.getlayer(Template))
6015 # verify events in data set
6016 for p in capture:
6017 if p.haslayer(Data):
6018 data = ipfix.decode_data_set(p.getlayer(Set))
6019 self.verify_ipfix_max_entries_per_user(
6020 data,
6021 nat44_config.max_translations_per_user,
6022 self.pg0.remote_ip4n)
6023
Matus Fabian8fdc0152018-09-24 04:41:28 -07006024 sleep(6)
6025 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6026 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
6027 UDP(sport=3001, dport=3002))
6028 self.pg0.add_stream(p)
6029 self.pg_enable_capture(self.pg_interfaces)
6030 self.pg_start()
6031 self.pg1.get_capture(1)
6032
Matus Fabianad1f3e12018-11-28 21:26:34 -08006033 def test_syslog_sess(self):
6034 """ Test syslog session creation and deletion """
6035 self.vapi.syslog_set_filter(SYSLOG_SEVERITY.INFO)
6036 self.vapi.syslog_set_sender(self.pg2.remote_ip4n, self.pg2.local_ip4n)
6037 self.nat44_add_address(self.nat_addr)
6038 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6039 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6040 is_inside=0)
6041
6042 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
6043 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
6044 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port))
6045 self.pg0.add_stream(p)
6046 self.pg_enable_capture(self.pg_interfaces)
6047 self.pg_start()
6048 capture = self.pg1.get_capture(1)
6049 self.tcp_port_out = capture[0][TCP].sport
6050 capture = self.pg2.get_capture(1)
6051 self.verify_syslog_sess(capture[0][Raw].load)
6052
6053 self.pg_enable_capture(self.pg_interfaces)
6054 self.pg_start()
6055 self.nat44_add_address(self.nat_addr, is_add=0)
6056 capture = self.pg2.get_capture(1)
6057 self.verify_syslog_sess(capture[0][Raw].load, False)
6058
Matus Fabiande886752016-12-07 03:38:19 -08006059 def tearDown(self):
Matus Fabiana6110b62018-06-13 05:39:07 -07006060 super(TestNAT44EndpointDependent, self).tearDown()
Matus Fabiande886752016-12-07 03:38:19 -08006061 if not self.vpp_dead:
Matus Fabian82119542018-01-25 01:13:22 -08006062 self.logger.info(self.vapi.cli("show nat44 addresses"))
6063 self.logger.info(self.vapi.cli("show nat44 interfaces"))
6064 self.logger.info(self.vapi.cli("show nat44 static mappings"))
6065 self.logger.info(self.vapi.cli("show nat44 interface address"))
6066 self.logger.info(self.vapi.cli("show nat44 sessions detail"))
Matus Fabian229c1aa2018-05-28 04:09:52 -07006067 self.logger.info(self.vapi.cli("show nat44 hash tables detail"))
Matus Fabian878c6462018-08-23 00:33:35 -07006068 self.logger.info(self.vapi.cli("show nat timeouts"))
Matus Fabian2ba92e32017-08-21 07:05:03 -07006069 self.clear_nat44()
Matus Fabian229c1aa2018-05-28 04:09:52 -07006070 self.vapi.cli("clear logging")
Matus Fabiande886752016-12-07 03:38:19 -08006071
Matus Fabianeea28d72017-01-13 04:15:54 -08006072
Juraj Slobodacba69362017-12-19 02:09:32 +01006073class TestNAT44Out2InDPO(MethodHolder):
6074 """ NAT44 Test Cases using out2in DPO """
6075
6076 @classmethod
6077 def setUpConstants(cls):
6078 super(TestNAT44Out2InDPO, cls).setUpConstants()
6079 cls.vpp_cmdline.extend(["nat", "{", "out2in dpo", "}"])
6080
6081 @classmethod
6082 def setUpClass(cls):
6083 super(TestNAT44Out2InDPO, cls).setUpClass()
Matus Fabian229c1aa2018-05-28 04:09:52 -07006084 cls.vapi.cli("set log class nat level debug")
Juraj Slobodacba69362017-12-19 02:09:32 +01006085
6086 try:
6087 cls.tcp_port_in = 6303
6088 cls.tcp_port_out = 6303
6089 cls.udp_port_in = 6304
6090 cls.udp_port_out = 6304
6091 cls.icmp_id_in = 6305
6092 cls.icmp_id_out = 6305
6093 cls.nat_addr = '10.0.0.3'
6094 cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
6095 cls.dst_ip4 = '192.168.70.1'
6096
6097 cls.create_pg_interfaces(range(2))
6098
6099 cls.pg0.admin_up()
6100 cls.pg0.config_ip4()
6101 cls.pg0.resolve_arp()
6102
6103 cls.pg1.admin_up()
6104 cls.pg1.config_ip6()
6105 cls.pg1.resolve_ndp()
6106
6107 cls.vapi.ip_add_del_route(is_ipv6=True, dst_address='\x00'*16,
6108 dst_address_length=0,
6109 next_hop_address=cls.pg1.remote_ip6n,
6110 next_hop_sw_if_index=cls.pg1.sw_if_index)
6111
6112 except Exception:
6113 super(TestNAT44Out2InDPO, cls).tearDownClass()
6114 raise
6115
6116 def configure_xlat(self):
6117 self.dst_ip6_pfx = '1:2:3::'
6118 self.dst_ip6_pfx_n = socket.inet_pton(socket.AF_INET6,
6119 self.dst_ip6_pfx)
6120 self.dst_ip6_pfx_len = 96
6121 self.src_ip6_pfx = '4:5:6::'
6122 self.src_ip6_pfx_n = socket.inet_pton(socket.AF_INET6,
6123 self.src_ip6_pfx)
6124 self.src_ip6_pfx_len = 96
6125 self.vapi.map_add_domain(self.dst_ip6_pfx_n, self.dst_ip6_pfx_len,
6126 self.src_ip6_pfx_n, self.src_ip6_pfx_len,
Jon Loeligerfc7344f2018-12-20 11:47:30 -06006127 '\x00\x00\x00\x00', 0)
Juraj Slobodacba69362017-12-19 02:09:32 +01006128
Ole Troanffba3c32018-11-22 12:53:00 +01006129 @unittest.skip('Temporary disabled')
Juraj Slobodacba69362017-12-19 02:09:32 +01006130 def test_464xlat_ce(self):
6131 """ Test 464XLAT CE with NAT44 """
6132
Matus Fabian69ce30d2018-08-22 01:27:10 -07006133 nat_config = self.vapi.nat_show_config()
6134 self.assertEqual(1, nat_config.out2in_dpo)
6135
Juraj Slobodacba69362017-12-19 02:09:32 +01006136 self.configure_xlat()
6137
6138 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6139 self.vapi.nat44_add_del_address_range(self.nat_addr_n, self.nat_addr_n)
6140
6141 out_src_ip6 = self.compose_ip6(self.dst_ip4, self.dst_ip6_pfx,
6142 self.dst_ip6_pfx_len)
6143 out_dst_ip6 = self.compose_ip6(self.nat_addr, self.src_ip6_pfx,
6144 self.src_ip6_pfx_len)
6145
6146 try:
6147 pkts = self.create_stream_in(self.pg0, self.pg1, self.dst_ip4)
6148 self.pg0.add_stream(pkts)
6149 self.pg_enable_capture(self.pg_interfaces)
6150 self.pg_start()
6151 capture = self.pg1.get_capture(len(pkts))
6152 self.verify_capture_out_ip6(capture, nat_ip=out_dst_ip6,
6153 dst_ip=out_src_ip6)
6154
6155 pkts = self.create_stream_out_ip6(self.pg1, out_src_ip6,
6156 out_dst_ip6)
6157 self.pg1.add_stream(pkts)
6158 self.pg_enable_capture(self.pg_interfaces)
6159 self.pg_start()
6160 capture = self.pg0.get_capture(len(pkts))
6161 self.verify_capture_in(capture, self.pg0)
6162 finally:
6163 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
6164 is_add=0)
6165 self.vapi.nat44_add_del_address_range(self.nat_addr_n,
6166 self.nat_addr_n, is_add=0)
6167
Ole Troanffba3c32018-11-22 12:53:00 +01006168 @unittest.skip('Temporary disabled')
Juraj Slobodacba69362017-12-19 02:09:32 +01006169 def test_464xlat_ce_no_nat(self):
6170 """ Test 464XLAT CE without NAT44 """
6171
6172 self.configure_xlat()
6173
6174 out_src_ip6 = self.compose_ip6(self.dst_ip4, self.dst_ip6_pfx,
6175 self.dst_ip6_pfx_len)
6176 out_dst_ip6 = self.compose_ip6(self.pg0.remote_ip4, self.src_ip6_pfx,
6177 self.src_ip6_pfx_len)
6178
6179 pkts = self.create_stream_in(self.pg0, self.pg1, self.dst_ip4)
6180 self.pg0.add_stream(pkts)
6181 self.pg_enable_capture(self.pg_interfaces)
6182 self.pg_start()
6183 capture = self.pg1.get_capture(len(pkts))
6184 self.verify_capture_out_ip6(capture, dst_ip=out_src_ip6,
6185 nat_ip=out_dst_ip6, same_port=True)
6186
6187 pkts = self.create_stream_out_ip6(self.pg1, out_src_ip6, out_dst_ip6)
6188 self.pg1.add_stream(pkts)
6189 self.pg_enable_capture(self.pg_interfaces)
6190 self.pg_start()
6191 capture = self.pg0.get_capture(len(pkts))
6192 self.verify_capture_in(capture, self.pg0)
6193
6194
Martin Gálikd7f75cd2017-03-27 06:02:47 -07006195class TestDeterministicNAT(MethodHolder):
Matus Fabian066f0342017-02-10 03:48:01 -08006196 """ Deterministic NAT Test Cases """
6197
6198 @classmethod
6199 def setUpConstants(cls):
6200 super(TestDeterministicNAT, cls).setUpConstants()
Matus Fabian2ba92e32017-08-21 07:05:03 -07006201 cls.vpp_cmdline.extend(["nat", "{", "deterministic", "}"])
Matus Fabian066f0342017-02-10 03:48:01 -08006202
6203 @classmethod
6204 def setUpClass(cls):
6205 super(TestDeterministicNAT, cls).setUpClass()
Matus Fabian229c1aa2018-05-28 04:09:52 -07006206 cls.vapi.cli("set log class nat level debug")
Matus Fabian066f0342017-02-10 03:48:01 -08006207
6208 try:
Martin Gálik977c1cb2017-03-30 23:21:51 -07006209 cls.tcp_port_in = 6303
Martin Gálik9806eae2017-04-25 01:25:08 -07006210 cls.tcp_external_port = 6303
Martin Gálik977c1cb2017-03-30 23:21:51 -07006211 cls.udp_port_in = 6304
Martin Gálik9806eae2017-04-25 01:25:08 -07006212 cls.udp_external_port = 6304
Martin Gálik977c1cb2017-03-30 23:21:51 -07006213 cls.icmp_id_in = 6305
Matus Fabian2ba92e32017-08-21 07:05:03 -07006214 cls.nat_addr = '10.0.0.3'
Martin Gálik977c1cb2017-03-30 23:21:51 -07006215
Matus Fabian2f2db1c2017-04-18 05:29:59 -07006216 cls.create_pg_interfaces(range(3))
Matus Fabian066f0342017-02-10 03:48:01 -08006217 cls.interfaces = list(cls.pg_interfaces)
6218
6219 for i in cls.interfaces:
6220 i.admin_up()
6221 i.config_ip4()
6222 i.resolve_arp()
6223
Martin Gálik977c1cb2017-03-30 23:21:51 -07006224 cls.pg0.generate_remote_hosts(2)
6225 cls.pg0.configure_ipv4_neighbors()
6226
Matus Fabian066f0342017-02-10 03:48:01 -08006227 except Exception:
6228 super(TestDeterministicNAT, cls).tearDownClass()
6229 raise
6230
Martin Gálik977c1cb2017-03-30 23:21:51 -07006231 def create_stream_in(self, in_if, out_if, ttl=64):
6232 """
6233 Create packet stream for inside network
6234
6235 :param in_if: Inside interface
6236 :param out_if: Outside interface
6237 :param ttl: TTL of generated packets
6238 """
6239 pkts = []
6240 # TCP
6241 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
6242 IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=ttl) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006243 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006244 pkts.append(p)
6245
6246 # UDP
6247 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
6248 IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=ttl) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006249 UDP(sport=self.udp_port_in, dport=self.udp_external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006250 pkts.append(p)
6251
6252 # ICMP
6253 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
6254 IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=ttl) /
6255 ICMP(id=self.icmp_id_in, type='echo-request'))
6256 pkts.append(p)
6257
6258 return pkts
6259
6260 def create_stream_out(self, out_if, dst_ip=None, ttl=64):
6261 """
6262 Create packet stream for outside network
6263
6264 :param out_if: Outside interface
Matus Fabian2ba92e32017-08-21 07:05:03 -07006265 :param dst_ip: Destination IP address (Default use global NAT address)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006266 :param ttl: TTL of generated packets
6267 """
6268 if dst_ip is None:
Matus Fabian2ba92e32017-08-21 07:05:03 -07006269 dst_ip = self.nat_addr
Martin Gálik977c1cb2017-03-30 23:21:51 -07006270 pkts = []
6271 # TCP
6272 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
6273 IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006274 TCP(dport=self.tcp_port_out, sport=self.tcp_external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006275 pkts.append(p)
6276
6277 # UDP
6278 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
6279 IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006280 UDP(dport=self.udp_port_out, sport=self.udp_external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006281 pkts.append(p)
6282
6283 # ICMP
6284 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
6285 IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) /
6286 ICMP(id=self.icmp_external_id, type='echo-reply'))
6287 pkts.append(p)
6288
6289 return pkts
6290
Matus Fabian05ca4a32018-09-04 23:45:13 -07006291 def verify_capture_out(self, capture, nat_ip=None):
Martin Gálik977c1cb2017-03-30 23:21:51 -07006292 """
6293 Verify captured packets on outside network
6294
6295 :param capture: Captured packets
Matus Fabian2ba92e32017-08-21 07:05:03 -07006296 :param nat_ip: Translated IP address (Default use global NAT address)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006297 :param same_port: Sorce port number is not translated (Default False)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006298 """
6299 if nat_ip is None:
Matus Fabian2ba92e32017-08-21 07:05:03 -07006300 nat_ip = self.nat_addr
Martin Gálik977c1cb2017-03-30 23:21:51 -07006301 for packet in capture:
6302 try:
6303 self.assertEqual(packet[IP].src, nat_ip)
6304 if packet.haslayer(TCP):
Martin Gálik9806eae2017-04-25 01:25:08 -07006305 self.tcp_port_out = packet[TCP].sport
Martin Gálik977c1cb2017-03-30 23:21:51 -07006306 elif packet.haslayer(UDP):
Martin Gálik9806eae2017-04-25 01:25:08 -07006307 self.udp_port_out = packet[UDP].sport
Martin Gálik977c1cb2017-03-30 23:21:51 -07006308 else:
6309 self.icmp_external_id = packet[ICMP].id
6310 except:
6311 self.logger.error(ppp("Unexpected or invalid packet "
6312 "(outside network):", packet))
6313 raise
6314
Matus Fabian066f0342017-02-10 03:48:01 -08006315 def test_deterministic_mode(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006316 """ NAT plugin run deterministic mode """
Matus Fabian066f0342017-02-10 03:48:01 -08006317 in_addr = '172.16.255.0'
6318 out_addr = '172.17.255.50'
6319 in_addr_t = '172.16.255.20'
6320 in_addr_n = socket.inet_aton(in_addr)
6321 out_addr_n = socket.inet_aton(out_addr)
6322 in_addr_t_n = socket.inet_aton(in_addr_t)
6323 in_plen = 24
6324 out_plen = 32
6325
Matus Fabian2ba92e32017-08-21 07:05:03 -07006326 nat_config = self.vapi.nat_show_config()
6327 self.assertEqual(1, nat_config.deterministic)
Matus Fabian066f0342017-02-10 03:48:01 -08006328
Matus Fabian2ba92e32017-08-21 07:05:03 -07006329 self.vapi.nat_det_add_del_map(in_addr_n, in_plen, out_addr_n, out_plen)
Matus Fabian066f0342017-02-10 03:48:01 -08006330
Matus Fabian2ba92e32017-08-21 07:05:03 -07006331 rep1 = self.vapi.nat_det_forward(in_addr_t_n)
Matus Fabian066f0342017-02-10 03:48:01 -08006332 self.assertEqual(rep1.out_addr[:4], out_addr_n)
Matus Fabian2ba92e32017-08-21 07:05:03 -07006333 rep2 = self.vapi.nat_det_reverse(out_addr_n, rep1.out_port_hi)
Matus Fabian066f0342017-02-10 03:48:01 -08006334 self.assertEqual(rep2.in_addr[:4], in_addr_t_n)
6335
Matus Fabian2ba92e32017-08-21 07:05:03 -07006336 deterministic_mappings = self.vapi.nat_det_map_dump()
Martin17a75cb2017-03-08 05:53:20 -08006337 self.assertEqual(len(deterministic_mappings), 1)
6338 dsm = deterministic_mappings[0]
6339 self.assertEqual(in_addr_n, dsm.in_addr[:4])
6340 self.assertEqual(in_plen, dsm.in_plen)
6341 self.assertEqual(out_addr_n, dsm.out_addr[:4])
6342 self.assertEqual(out_plen, dsm.out_plen)
6343
Matus Fabian2ba92e32017-08-21 07:05:03 -07006344 self.clear_nat_det()
6345 deterministic_mappings = self.vapi.nat_det_map_dump()
Martinb616e9f2017-03-14 02:25:45 -07006346 self.assertEqual(len(deterministic_mappings), 0)
6347
Matus Fabian6a0946f2017-04-12 03:36:13 -07006348 def test_set_timeouts(self):
6349 """ Set deterministic NAT timeouts """
Matus Fabian878c6462018-08-23 00:33:35 -07006350 timeouts_before = self.vapi.nat_get_timeouts()
Matus Fabian6a0946f2017-04-12 03:36:13 -07006351
Matus Fabian878c6462018-08-23 00:33:35 -07006352 self.vapi.nat_set_timeouts(timeouts_before.udp + 10,
6353 timeouts_before.tcp_established + 10,
6354 timeouts_before.tcp_transitory + 10,
6355 timeouts_before.icmp + 10)
Matus Fabian6a0946f2017-04-12 03:36:13 -07006356
Matus Fabian878c6462018-08-23 00:33:35 -07006357 timeouts_after = self.vapi.nat_get_timeouts()
Matus Fabian6a0946f2017-04-12 03:36:13 -07006358
6359 self.assertNotEqual(timeouts_before.udp, timeouts_after.udp)
6360 self.assertNotEqual(timeouts_before.icmp, timeouts_after.icmp)
6361 self.assertNotEqual(timeouts_before.tcp_established,
6362 timeouts_after.tcp_established)
6363 self.assertNotEqual(timeouts_before.tcp_transitory,
6364 timeouts_after.tcp_transitory)
6365
Martin Gálik977c1cb2017-03-30 23:21:51 -07006366 def test_det_in(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006367 """ Deterministic NAT translation test (TCP, UDP, ICMP) """
Martin Gálik977c1cb2017-03-30 23:21:51 -07006368
6369 nat_ip = "10.0.0.10"
Martin Gálik977c1cb2017-03-30 23:21:51 -07006370
Matus Fabian2ba92e32017-08-21 07:05:03 -07006371 self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n,
6372 32,
6373 socket.inet_aton(nat_ip),
6374 32)
6375 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6376 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6377 is_inside=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006378
6379 # in2out
6380 pkts = self.create_stream_in(self.pg0, self.pg1)
6381 self.pg0.add_stream(pkts)
6382 self.pg_enable_capture(self.pg_interfaces)
6383 self.pg_start()
6384 capture = self.pg1.get_capture(len(pkts))
6385 self.verify_capture_out(capture, nat_ip)
6386
6387 # out2in
6388 pkts = self.create_stream_out(self.pg1, nat_ip)
6389 self.pg1.add_stream(pkts)
6390 self.pg_enable_capture(self.pg_interfaces)
6391 self.pg_start()
6392 capture = self.pg0.get_capture(len(pkts))
6393 self.verify_capture_in(capture, self.pg0)
6394
Martin Gálik9806eae2017-04-25 01:25:08 -07006395 # session dump test
Matus Fabian2ba92e32017-08-21 07:05:03 -07006396 sessions = self.vapi.nat_det_session_dump(self.pg0.remote_ip4n)
Martin Gálik9806eae2017-04-25 01:25:08 -07006397 self.assertEqual(len(sessions), 3)
6398
6399 # TCP session
6400 s = sessions[0]
6401 self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n)
6402 self.assertEqual(s.in_port, self.tcp_port_in)
6403 self.assertEqual(s.out_port, self.tcp_port_out)
6404 self.assertEqual(s.ext_port, self.tcp_external_port)
6405
6406 # UDP session
6407 s = sessions[1]
6408 self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n)
6409 self.assertEqual(s.in_port, self.udp_port_in)
6410 self.assertEqual(s.out_port, self.udp_port_out)
6411 self.assertEqual(s.ext_port, self.udp_external_port)
6412
6413 # ICMP session
6414 s = sessions[2]
6415 self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n)
6416 self.assertEqual(s.in_port, self.icmp_id_in)
6417 self.assertEqual(s.out_port, self.icmp_external_id)
6418
Martin Gálik977c1cb2017-03-30 23:21:51 -07006419 def test_multiple_users(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006420 """ Deterministic NAT multiple users """
Martin Gálik977c1cb2017-03-30 23:21:51 -07006421
6422 nat_ip = "10.0.0.10"
6423 port_in = 80
Martin Gálik9806eae2017-04-25 01:25:08 -07006424 external_port = 6303
Martin Gálik977c1cb2017-03-30 23:21:51 -07006425
6426 host0 = self.pg0.remote_hosts[0]
6427 host1 = self.pg0.remote_hosts[1]
6428
Matus Fabian2ba92e32017-08-21 07:05:03 -07006429 self.vapi.nat_det_add_del_map(host0.ip4n,
6430 24,
6431 socket.inet_aton(nat_ip),
6432 32)
6433 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6434 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6435 is_inside=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006436
6437 # host0 to out
6438 p = (Ether(src=host0.mac, dst=self.pg0.local_mac) /
6439 IP(src=host0.ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006440 TCP(sport=port_in, dport=external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006441 self.pg0.add_stream(p)
6442 self.pg_enable_capture(self.pg_interfaces)
6443 self.pg_start()
6444 capture = self.pg1.get_capture(1)
6445 p = capture[0]
6446 try:
6447 ip = p[IP]
6448 tcp = p[TCP]
6449 self.assertEqual(ip.src, nat_ip)
6450 self.assertEqual(ip.dst, self.pg1.remote_ip4)
Martin Gálik9806eae2017-04-25 01:25:08 -07006451 self.assertEqual(tcp.dport, external_port)
6452 port_out0 = tcp.sport
Martin Gálik977c1cb2017-03-30 23:21:51 -07006453 except:
6454 self.logger.error(ppp("Unexpected or invalid packet:", p))
6455 raise
6456
6457 # host1 to out
6458 p = (Ether(src=host1.mac, dst=self.pg0.local_mac) /
6459 IP(src=host1.ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006460 TCP(sport=port_in, dport=external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006461 self.pg0.add_stream(p)
6462 self.pg_enable_capture(self.pg_interfaces)
6463 self.pg_start()
6464 capture = self.pg1.get_capture(1)
6465 p = capture[0]
6466 try:
6467 ip = p[IP]
6468 tcp = p[TCP]
6469 self.assertEqual(ip.src, nat_ip)
6470 self.assertEqual(ip.dst, self.pg1.remote_ip4)
Martin Gálik9806eae2017-04-25 01:25:08 -07006471 self.assertEqual(tcp.dport, external_port)
6472 port_out1 = tcp.sport
Martin Gálik977c1cb2017-03-30 23:21:51 -07006473 except:
6474 self.logger.error(ppp("Unexpected or invalid packet:", p))
6475 raise
6476
Matus Fabian2ba92e32017-08-21 07:05:03 -07006477 dms = self.vapi.nat_det_map_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006478 self.assertEqual(1, len(dms))
6479 self.assertEqual(2, dms[0].ses_num)
6480
6481 # out to host0
6482 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
6483 IP(src=self.pg1.remote_ip4, dst=nat_ip) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006484 TCP(sport=external_port, dport=port_out0))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006485 self.pg1.add_stream(p)
6486 self.pg_enable_capture(self.pg_interfaces)
6487 self.pg_start()
6488 capture = self.pg0.get_capture(1)
6489 p = capture[0]
6490 try:
6491 ip = p[IP]
6492 tcp = p[TCP]
6493 self.assertEqual(ip.src, self.pg1.remote_ip4)
6494 self.assertEqual(ip.dst, host0.ip4)
6495 self.assertEqual(tcp.dport, port_in)
Martin Gálik9806eae2017-04-25 01:25:08 -07006496 self.assertEqual(tcp.sport, external_port)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006497 except:
6498 self.logger.error(ppp("Unexpected or invalid packet:", p))
6499 raise
6500
6501 # out to host1
6502 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
6503 IP(src=self.pg1.remote_ip4, dst=nat_ip) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006504 TCP(sport=external_port, dport=port_out1))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006505 self.pg1.add_stream(p)
6506 self.pg_enable_capture(self.pg_interfaces)
6507 self.pg_start()
6508 capture = self.pg0.get_capture(1)
6509 p = capture[0]
6510 try:
6511 ip = p[IP]
6512 tcp = p[TCP]
6513 self.assertEqual(ip.src, self.pg1.remote_ip4)
6514 self.assertEqual(ip.dst, host1.ip4)
6515 self.assertEqual(tcp.dport, port_in)
Martin Gálik9806eae2017-04-25 01:25:08 -07006516 self.assertEqual(tcp.sport, external_port)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006517 except:
6518 self.logger.error(ppp("Unexpected or invalid packet", p))
6519 raise
6520
Martin Gálik6bc8c642017-04-19 01:12:27 -07006521 # session close api test
Matus Fabian2ba92e32017-08-21 07:05:03 -07006522 self.vapi.nat_det_close_session_out(socket.inet_aton(nat_ip),
6523 port_out1,
Martin Gálik6bc8c642017-04-19 01:12:27 -07006524 self.pg1.remote_ip4n,
Martin Gálik9806eae2017-04-25 01:25:08 -07006525 external_port)
Matus Fabian2ba92e32017-08-21 07:05:03 -07006526 dms = self.vapi.nat_det_map_dump()
6527 self.assertEqual(dms[0].ses_num, 1)
6528
6529 self.vapi.nat_det_close_session_in(host0.ip4n,
6530 port_in,
6531 self.pg1.remote_ip4n,
6532 external_port)
6533 dms = self.vapi.nat_det_map_dump()
Martin Gálik6bc8c642017-04-19 01:12:27 -07006534 self.assertEqual(dms[0].ses_num, 0)
6535
Martin Gálik977c1cb2017-03-30 23:21:51 -07006536 def test_tcp_session_close_detection_in(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006537 """ Deterministic NAT TCP session close from inside network """
6538 self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n,
6539 32,
6540 socket.inet_aton(self.nat_addr),
6541 32)
6542 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6543 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6544 is_inside=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006545
6546 self.initiate_tcp_session(self.pg0, self.pg1)
6547
6548 # close the session from inside
6549 try:
6550 # FIN packet in -> out
6551 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6552 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006553 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006554 flags="F"))
6555 self.pg0.add_stream(p)
6556 self.pg_enable_capture(self.pg_interfaces)
6557 self.pg_start()
6558 self.pg1.get_capture(1)
6559
6560 pkts = []
6561
6562 # ACK packet out -> in
6563 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07006564 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006565 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006566 flags="A"))
6567 pkts.append(p)
6568
6569 # FIN packet out -> in
6570 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07006571 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006572 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006573 flags="F"))
6574 pkts.append(p)
6575
6576 self.pg1.add_stream(pkts)
6577 self.pg_enable_capture(self.pg_interfaces)
6578 self.pg_start()
6579 self.pg0.get_capture(2)
6580
6581 # ACK packet in -> out
6582 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6583 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006584 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006585 flags="A"))
6586 self.pg0.add_stream(p)
6587 self.pg_enable_capture(self.pg_interfaces)
6588 self.pg_start()
6589 self.pg1.get_capture(1)
6590
Matus Fabian2ba92e32017-08-21 07:05:03 -07006591 # Check if deterministic NAT44 closed the session
6592 dms = self.vapi.nat_det_map_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006593 self.assertEqual(0, dms[0].ses_num)
6594 except:
6595 self.logger.error("TCP session termination failed")
6596 raise
6597
6598 def test_tcp_session_close_detection_out(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006599 """ Deterministic NAT TCP session close from outside network """
6600 self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n,
6601 32,
6602 socket.inet_aton(self.nat_addr),
6603 32)
6604 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6605 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6606 is_inside=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006607
6608 self.initiate_tcp_session(self.pg0, self.pg1)
6609
6610 # close the session from outside
6611 try:
6612 # FIN packet out -> in
6613 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07006614 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006615 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006616 flags="F"))
6617 self.pg1.add_stream(p)
6618 self.pg_enable_capture(self.pg_interfaces)
6619 self.pg_start()
6620 self.pg0.get_capture(1)
6621
6622 pkts = []
6623
6624 # ACK packet in -> out
6625 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6626 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006627 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006628 flags="A"))
6629 pkts.append(p)
6630
6631 # ACK packet in -> out
6632 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6633 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006634 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006635 flags="F"))
6636 pkts.append(p)
6637
6638 self.pg0.add_stream(pkts)
6639 self.pg_enable_capture(self.pg_interfaces)
6640 self.pg_start()
6641 self.pg1.get_capture(2)
6642
6643 # ACK packet out -> in
6644 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07006645 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006646 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006647 flags="A"))
6648 self.pg1.add_stream(p)
6649 self.pg_enable_capture(self.pg_interfaces)
6650 self.pg_start()
6651 self.pg0.get_capture(1)
6652
Matus Fabian2ba92e32017-08-21 07:05:03 -07006653 # Check if deterministic NAT44 closed the session
6654 dms = self.vapi.nat_det_map_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006655 self.assertEqual(0, dms[0].ses_num)
6656 except:
6657 self.logger.error("TCP session termination failed")
6658 raise
6659
Paul Vinciguerradefde0f2018-12-06 07:46:13 -08006660 @unittest.skipUnless(running_extended_tests, "part of extended tests")
Martin Gálik977c1cb2017-03-30 23:21:51 -07006661 def test_session_timeout(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006662 """ Deterministic NAT session timeouts """
6663 self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n,
6664 32,
6665 socket.inet_aton(self.nat_addr),
6666 32)
6667 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6668 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6669 is_inside=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006670
6671 self.initiate_tcp_session(self.pg0, self.pg1)
Matus Fabian878c6462018-08-23 00:33:35 -07006672 self.vapi.nat_set_timeouts(5, 5, 5, 5)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006673 pkts = self.create_stream_in(self.pg0, self.pg1)
6674 self.pg0.add_stream(pkts)
6675 self.pg_enable_capture(self.pg_interfaces)
6676 self.pg_start()
6677 capture = self.pg1.get_capture(len(pkts))
6678 sleep(15)
6679
Matus Fabian2ba92e32017-08-21 07:05:03 -07006680 dms = self.vapi.nat_det_map_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006681 self.assertEqual(0, dms[0].ses_num)
6682
Paul Vinciguerradefde0f2018-12-06 07:46:13 -08006683 @unittest.skipUnless(running_extended_tests, "part of extended tests")
Martin Gálik977c1cb2017-03-30 23:21:51 -07006684 def test_session_limit_per_user(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006685 """ Deterministic NAT maximum sessions per user limit """
6686 self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n,
6687 32,
6688 socket.inet_aton(self.nat_addr),
6689 32)
6690 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6691 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6692 is_inside=0)
Matus Fabian2f2db1c2017-04-18 05:29:59 -07006693 self.vapi.set_ipfix_exporter(collector_address=self.pg2.remote_ip4n,
6694 src_address=self.pg2.local_ip4n,
6695 path_mtu=512,
6696 template_interval=10)
Matus Fabian2ba92e32017-08-21 07:05:03 -07006697 self.vapi.nat_ipfix()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006698
6699 pkts = []
6700 for port in range(1025, 2025):
6701 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6702 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
6703 UDP(sport=port, dport=port))
6704 pkts.append(p)
6705
6706 self.pg0.add_stream(pkts)
6707 self.pg_enable_capture(self.pg_interfaces)
6708 self.pg_start()
6709 capture = self.pg1.get_capture(len(pkts))
6710
6711 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6712 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
Martin Gálikf7e655d2017-04-27 02:13:26 -07006713 UDP(sport=3001, dport=3002))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006714 self.pg0.add_stream(p)
6715 self.pg_enable_capture(self.pg_interfaces)
6716 self.pg_start()
6717 capture = self.pg1.assert_nothing_captured()
6718
Martin Gálikf7e655d2017-04-27 02:13:26 -07006719 # verify ICMP error packet
6720 capture = self.pg0.get_capture(1)
6721 p = capture[0]
6722 self.assertTrue(p.haslayer(ICMP))
6723 icmp = p[ICMP]
6724 self.assertEqual(icmp.type, 3)
6725 self.assertEqual(icmp.code, 1)
6726 self.assertTrue(icmp.haslayer(IPerror))
6727 inner_ip = icmp[IPerror]
6728 self.assertEqual(inner_ip[UDPerror].sport, 3001)
6729 self.assertEqual(inner_ip[UDPerror].dport, 3002)
6730
Matus Fabian2ba92e32017-08-21 07:05:03 -07006731 dms = self.vapi.nat_det_map_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006732
6733 self.assertEqual(1000, dms[0].ses_num)
6734
Matus Fabian2f2db1c2017-04-18 05:29:59 -07006735 # verify IPFIX logging
6736 self.vapi.cli("ipfix flush") # FIXME this should be an API call
Matus Fabian7c0aecc2017-07-03 01:21:38 -07006737 sleep(1)
Matus Fabian2f2db1c2017-04-18 05:29:59 -07006738 capture = self.pg2.get_capture(2)
6739 ipfix = IPFIXDecoder()
6740 # first load template
6741 for p in capture:
6742 self.assertTrue(p.haslayer(IPFIX))
6743 if p.haslayer(Template):
6744 ipfix.add_template(p.getlayer(Template))
6745 # verify events in data set
6746 for p in capture:
6747 if p.haslayer(Data):
6748 data = ipfix.decode_data_set(p.getlayer(Set))
Matus Fabian878c6462018-08-23 00:33:35 -07006749 self.verify_ipfix_max_entries_per_user(data,
6750 1000,
6751 self.pg0.remote_ip4n)
Matus Fabian2f2db1c2017-04-18 05:29:59 -07006752
Matus Fabian2ba92e32017-08-21 07:05:03 -07006753 def clear_nat_det(self):
Martin17a75cb2017-03-08 05:53:20 -08006754 """
Matus Fabian2ba92e32017-08-21 07:05:03 -07006755 Clear deterministic NAT configuration.
Martin17a75cb2017-03-08 05:53:20 -08006756 """
Matus Fabian2ba92e32017-08-21 07:05:03 -07006757 self.vapi.nat_ipfix(enable=0)
Matus Fabian878c6462018-08-23 00:33:35 -07006758 self.vapi.nat_set_timeouts()
Matus Fabian2ba92e32017-08-21 07:05:03 -07006759 deterministic_mappings = self.vapi.nat_det_map_dump()
Martin17a75cb2017-03-08 05:53:20 -08006760 for dsm in deterministic_mappings:
Matus Fabian2ba92e32017-08-21 07:05:03 -07006761 self.vapi.nat_det_add_del_map(dsm.in_addr,
6762 dsm.in_plen,
6763 dsm.out_addr,
6764 dsm.out_plen,
6765 is_add=0)
Martin17a75cb2017-03-08 05:53:20 -08006766
Matus Fabian2ba92e32017-08-21 07:05:03 -07006767 interfaces = self.vapi.nat44_interface_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006768 for intf in interfaces:
Matus Fabian2ba92e32017-08-21 07:05:03 -07006769 self.vapi.nat44_interface_add_del_feature(intf.sw_if_index,
6770 intf.is_inside,
6771 is_add=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006772
Matus Fabian066f0342017-02-10 03:48:01 -08006773 def tearDown(self):
6774 super(TestDeterministicNAT, self).tearDown()
6775 if not self.vpp_dead:
Matus Fabian82119542018-01-25 01:13:22 -08006776 self.logger.info(self.vapi.cli("show nat44 interfaces"))
Matus Fabian878c6462018-08-23 00:33:35 -07006777 self.logger.info(self.vapi.cli("show nat timeouts"))
Matus Fabian82119542018-01-25 01:13:22 -08006778 self.logger.info(
6779 self.vapi.cli("show nat44 deterministic mappings"))
6780 self.logger.info(
Matus Fabian82119542018-01-25 01:13:22 -08006781 self.vapi.cli("show nat44 deterministic sessions"))
Matus Fabian2ba92e32017-08-21 07:05:03 -07006782 self.clear_nat_det()
Matus Fabian066f0342017-02-10 03:48:01 -08006783
Matus Fabian06596c52017-06-06 04:53:28 -07006784
6785class TestNAT64(MethodHolder):
6786 """ NAT64 Test Cases """
6787
6788 @classmethod
Matus Fabiana431ad12018-01-04 04:03:14 -08006789 def setUpConstants(cls):
6790 super(TestNAT64, cls).setUpConstants()
6791 cls.vpp_cmdline.extend(["nat", "{", "nat64 bib hash buckets 128",
6792 "nat64 st hash buckets 256", "}"])
6793
6794 @classmethod
Matus Fabian06596c52017-06-06 04:53:28 -07006795 def setUpClass(cls):
6796 super(TestNAT64, cls).setUpClass()
6797
6798 try:
6799 cls.tcp_port_in = 6303
6800 cls.tcp_port_out = 6303
6801 cls.udp_port_in = 6304
6802 cls.udp_port_out = 6304
6803 cls.icmp_id_in = 6305
6804 cls.icmp_id_out = 6305
Matus Fabianad1f3e12018-11-28 21:26:34 -08006805 cls.tcp_external_port = 80
Matus Fabian06596c52017-06-06 04:53:28 -07006806 cls.nat_addr = '10.0.0.3'
6807 cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
Matus Fabian029f3d22017-06-15 02:28:50 -07006808 cls.vrf1_id = 10
6809 cls.vrf1_nat_addr = '10.0.10.3'
6810 cls.vrf1_nat_addr_n = socket.inet_pton(socket.AF_INET,
6811 cls.vrf1_nat_addr)
Matus Fabiana431ad12018-01-04 04:03:14 -08006812 cls.ipfix_src_port = 4739
6813 cls.ipfix_domain_id = 1
Matus Fabian06596c52017-06-06 04:53:28 -07006814
Juraj Slobodac746a152018-07-09 02:36:37 +02006815 cls.create_pg_interfaces(range(6))
Matus Fabian06596c52017-06-06 04:53:28 -07006816 cls.ip6_interfaces = list(cls.pg_interfaces[0:1])
Matus Fabian029f3d22017-06-15 02:28:50 -07006817 cls.ip6_interfaces.append(cls.pg_interfaces[2])
Matus Fabian06596c52017-06-06 04:53:28 -07006818 cls.ip4_interfaces = list(cls.pg_interfaces[1:2])
6819
Neale Ranns15002542017-09-10 04:39:11 -07006820 cls.vapi.ip_table_add_del(cls.vrf1_id, is_add=1, is_ipv6=1)
6821
Matus Fabian029f3d22017-06-15 02:28:50 -07006822 cls.pg_interfaces[2].set_table_ip6(cls.vrf1_id)
6823
6824 cls.pg0.generate_remote_hosts(2)
6825
Matus Fabian06596c52017-06-06 04:53:28 -07006826 for i in cls.ip6_interfaces:
6827 i.admin_up()
6828 i.config_ip6()
Matus Fabian029f3d22017-06-15 02:28:50 -07006829 i.configure_ipv6_neighbors()
Matus Fabian06596c52017-06-06 04:53:28 -07006830
6831 for i in cls.ip4_interfaces:
6832 i.admin_up()
6833 i.config_ip4()
6834 i.resolve_arp()
6835
Matus Fabian36ea2d62017-10-24 04:13:49 -07006836 cls.pg3.admin_up()
6837 cls.pg3.config_ip4()
6838 cls.pg3.resolve_arp()
6839 cls.pg3.config_ip6()
6840 cls.pg3.configure_ipv6_neighbors()
6841
Juraj Slobodac746a152018-07-09 02:36:37 +02006842 cls.pg5.admin_up()
6843 cls.pg5.config_ip6()
6844
Matus Fabian06596c52017-06-06 04:53:28 -07006845 except Exception:
6846 super(TestNAT64, cls).tearDownClass()
6847 raise
6848
Juraj Slobodac746a152018-07-09 02:36:37 +02006849 def test_nat64_inside_interface_handles_neighbor_advertisement(self):
6850 """ NAT64 inside interface handles Neighbor Advertisement """
6851
6852 self.vapi.nat64_add_del_interface(self.pg5.sw_if_index)
6853
6854 # Try to send ping
6855 ping = (Ether(dst=self.pg5.local_mac, src=self.pg5.remote_mac) /
6856 IPv6(src=self.pg5.remote_ip6, dst=self.pg5.local_ip6) /
6857 ICMPv6EchoRequest())
6858 pkts = [ping]
6859 self.pg5.add_stream(pkts)
6860 self.pg_enable_capture(self.pg_interfaces)
6861 self.pg_start()
6862
6863 # Wait for Neighbor Solicitation
6864 capture = self.pg5.get_capture(len(pkts))
Juraj Slobodac746a152018-07-09 02:36:37 +02006865 packet = capture[0]
6866 try:
6867 self.assertEqual(packet[IPv6].src, self.pg5.local_ip6)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08006868 self.assertEqual(packet.haslayer(ICMPv6ND_NS), 1)
Juraj Slobodac746a152018-07-09 02:36:37 +02006869 tgt = packet[ICMPv6ND_NS].tgt
6870 except:
6871 self.logger.error(ppp("Unexpected or invalid packet:", packet))
6872 raise
6873
6874 # Send Neighbor Advertisement
6875 p = (Ether(dst=self.pg5.local_mac, src=self.pg5.remote_mac) /
6876 IPv6(src=self.pg5.remote_ip6, dst=self.pg5.local_ip6) /
6877 ICMPv6ND_NA(tgt=tgt) /
6878 ICMPv6NDOptDstLLAddr(lladdr=self.pg5.remote_mac))
6879 pkts = [p]
6880 self.pg5.add_stream(pkts)
6881 self.pg_enable_capture(self.pg_interfaces)
6882 self.pg_start()
6883
6884 # Try to send ping again
6885 pkts = [ping]
6886 self.pg5.add_stream(pkts)
6887 self.pg_enable_capture(self.pg_interfaces)
6888 self.pg_start()
6889
6890 # Wait for ping reply
6891 capture = self.pg5.get_capture(len(pkts))
Juraj Slobodac746a152018-07-09 02:36:37 +02006892 packet = capture[0]
6893 try:
6894 self.assertEqual(packet[IPv6].src, self.pg5.local_ip6)
6895 self.assertEqual(packet[IPv6].dst, self.pg5.remote_ip6)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08006896 self.assertEqual(packet.haslayer(ICMPv6EchoReply), 1)
Juraj Slobodac746a152018-07-09 02:36:37 +02006897 except:
6898 self.logger.error(ppp("Unexpected or invalid packet:", packet))
6899 raise
6900
Matus Fabian06596c52017-06-06 04:53:28 -07006901 def test_pool(self):
6902 """ Add/delete address to NAT64 pool """
6903 nat_addr = socket.inet_pton(socket.AF_INET, '1.2.3.4')
6904
6905 self.vapi.nat64_add_del_pool_addr_range(nat_addr, nat_addr)
6906
6907 addresses = self.vapi.nat64_pool_addr_dump()
6908 self.assertEqual(len(addresses), 1)
6909 self.assertEqual(addresses[0].address, nat_addr)
6910
6911 self.vapi.nat64_add_del_pool_addr_range(nat_addr, nat_addr, is_add=0)
6912
6913 addresses = self.vapi.nat64_pool_addr_dump()
6914 self.assertEqual(len(addresses), 0)
6915
6916 def test_interface(self):
6917 """ Enable/disable NAT64 feature on the interface """
6918 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
6919 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
6920
6921 interfaces = self.vapi.nat64_interface_dump()
6922 self.assertEqual(len(interfaces), 2)
6923 pg0_found = False
6924 pg1_found = False
6925 for intf in interfaces:
6926 if intf.sw_if_index == self.pg0.sw_if_index:
6927 self.assertEqual(intf.is_inside, 1)
6928 pg0_found = True
6929 elif intf.sw_if_index == self.pg1.sw_if_index:
6930 self.assertEqual(intf.is_inside, 0)
6931 pg1_found = True
6932 self.assertTrue(pg0_found)
6933 self.assertTrue(pg1_found)
6934
6935 features = self.vapi.cli("show interface features pg0")
6936 self.assertNotEqual(features.find('nat64-in2out'), -1)
6937 features = self.vapi.cli("show interface features pg1")
6938 self.assertNotEqual(features.find('nat64-out2in'), -1)
6939
6940 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index, is_add=0)
6941 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_add=0)
6942
6943 interfaces = self.vapi.nat64_interface_dump()
6944 self.assertEqual(len(interfaces), 0)
6945
6946 def test_static_bib(self):
6947 """ Add/delete static BIB entry """
6948 in_addr = socket.inet_pton(socket.AF_INET6,
6949 '2001:db8:85a3::8a2e:370:7334')
6950 out_addr = socket.inet_pton(socket.AF_INET, '10.1.1.3')
6951 in_port = 1234
6952 out_port = 5678
6953 proto = IP_PROTOS.tcp
6954
6955 self.vapi.nat64_add_del_static_bib(in_addr,
6956 out_addr,
6957 in_port,
6958 out_port,
6959 proto)
6960 bib = self.vapi.nat64_bib_dump(IP_PROTOS.tcp)
6961 static_bib_num = 0
6962 for bibe in bib:
6963 if bibe.is_static:
6964 static_bib_num += 1
6965 self.assertEqual(bibe.i_addr, in_addr)
6966 self.assertEqual(bibe.o_addr, out_addr)
6967 self.assertEqual(bibe.i_port, in_port)
6968 self.assertEqual(bibe.o_port, out_port)
6969 self.assertEqual(static_bib_num, 1)
Matus Fabianfd0d5082018-12-18 01:08:51 -08006970 bibs = self.statistics.get_counter('/nat64/total-bibs')
6971 self.assertEqual(bibs[0][0], 1)
Matus Fabian06596c52017-06-06 04:53:28 -07006972
6973 self.vapi.nat64_add_del_static_bib(in_addr,
6974 out_addr,
6975 in_port,
6976 out_port,
6977 proto,
6978 is_add=0)
6979 bib = self.vapi.nat64_bib_dump(IP_PROTOS.tcp)
6980 static_bib_num = 0
6981 for bibe in bib:
6982 if bibe.is_static:
6983 static_bib_num += 1
6984 self.assertEqual(static_bib_num, 0)
Matus Fabianfd0d5082018-12-18 01:08:51 -08006985 bibs = self.statistics.get_counter('/nat64/total-bibs')
6986 self.assertEqual(bibs[0][0], 0)
Matus Fabian06596c52017-06-06 04:53:28 -07006987
6988 def test_set_timeouts(self):
6989 """ Set NAT64 timeouts """
6990 # verify default values
Matus Fabian878c6462018-08-23 00:33:35 -07006991 timeouts = self.vapi.nat_get_timeouts()
Matus Fabian06596c52017-06-06 04:53:28 -07006992 self.assertEqual(timeouts.udp, 300)
6993 self.assertEqual(timeouts.icmp, 60)
Matus Fabian878c6462018-08-23 00:33:35 -07006994 self.assertEqual(timeouts.tcp_transitory, 240)
6995 self.assertEqual(timeouts.tcp_established, 7440)
Matus Fabian06596c52017-06-06 04:53:28 -07006996
6997 # set and verify custom values
Matus Fabian878c6462018-08-23 00:33:35 -07006998 self.vapi.nat_set_timeouts(udp=200, icmp=30, tcp_transitory=250,
6999 tcp_established=7450)
7000 timeouts = self.vapi.nat_get_timeouts()
Matus Fabian06596c52017-06-06 04:53:28 -07007001 self.assertEqual(timeouts.udp, 200)
7002 self.assertEqual(timeouts.icmp, 30)
Matus Fabian878c6462018-08-23 00:33:35 -07007003 self.assertEqual(timeouts.tcp_transitory, 250)
7004 self.assertEqual(timeouts.tcp_established, 7450)
Matus Fabian06596c52017-06-06 04:53:28 -07007005
7006 def test_dynamic(self):
7007 """ NAT64 dynamic translation test """
7008 self.tcp_port_in = 6303
7009 self.udp_port_in = 6304
7010 self.icmp_id_in = 6305
7011
7012 ses_num_start = self.nat64_get_ses_num()
7013
7014 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7015 self.nat_addr_n)
7016 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7017 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7018
7019 # in2out
Matus Fabiana5e73762018-12-14 01:55:16 -08007020 tcpn = self.statistics.get_counter('/err/nat64-in2out/TCP packets')
7021 udpn = self.statistics.get_counter('/err/nat64-in2out/UDP packets')
7022 icmpn = self.statistics.get_counter('/err/nat64-in2out/ICMP packets')
7023 totaln = self.statistics.get_counter(
7024 '/err/nat64-in2out/good in2out packets processed')
7025
Matus Fabian06596c52017-06-06 04:53:28 -07007026 pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
7027 self.pg0.add_stream(pkts)
7028 self.pg_enable_capture(self.pg_interfaces)
7029 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07007030 capture = self.pg1.get_capture(len(pkts))
7031 self.verify_capture_out(capture, nat_ip=self.nat_addr,
Matus Fabian06596c52017-06-06 04:53:28 -07007032 dst_ip=self.pg1.remote_ip4)
7033
Matus Fabiana5e73762018-12-14 01:55:16 -08007034 err = self.statistics.get_counter('/err/nat64-in2out/TCP packets')
7035 self.assertEqual(err - tcpn, 1)
7036 err = self.statistics.get_counter('/err/nat64-in2out/UDP packets')
7037 self.assertEqual(err - udpn, 1)
7038 err = self.statistics.get_counter('/err/nat64-in2out/ICMP packets')
7039 self.assertEqual(err - icmpn, 1)
7040 err = self.statistics.get_counter(
7041 '/err/nat64-in2out/good in2out packets processed')
7042 self.assertEqual(err - totaln, 3)
7043
Matus Fabian06596c52017-06-06 04:53:28 -07007044 # out2in
Matus Fabiana5e73762018-12-14 01:55:16 -08007045 tcpn = self.statistics.get_counter('/err/nat64-out2in/TCP packets')
7046 udpn = self.statistics.get_counter('/err/nat64-out2in/UDP packets')
7047 icmpn = self.statistics.get_counter('/err/nat64-out2in/ICMP packets')
7048 totaln = self.statistics.get_counter(
7049 '/err/nat64-out2in/good out2in packets processed')
7050
Matus Fabian06596c52017-06-06 04:53:28 -07007051 pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
7052 self.pg1.add_stream(pkts)
7053 self.pg_enable_capture(self.pg_interfaces)
7054 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07007055 capture = self.pg0.get_capture(len(pkts))
Matus Fabian06596c52017-06-06 04:53:28 -07007056 ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4]))
7057 self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6)
7058
Matus Fabiana5e73762018-12-14 01:55:16 -08007059 err = self.statistics.get_counter('/err/nat64-out2in/TCP packets')
7060 self.assertEqual(err - tcpn, 1)
7061 err = self.statistics.get_counter('/err/nat64-out2in/UDP packets')
7062 self.assertEqual(err - udpn, 1)
7063 err = self.statistics.get_counter('/err/nat64-out2in/ICMP packets')
7064 self.assertEqual(err - icmpn, 1)
7065 err = self.statistics.get_counter(
7066 '/err/nat64-out2in/good out2in packets processed')
7067 self.assertEqual(err - totaln, 3)
7068
Matus Fabianfd0d5082018-12-18 01:08:51 -08007069 bibs = self.statistics.get_counter('/nat64/total-bibs')
7070 self.assertEqual(bibs[0][0], 3)
7071 sessions = self.statistics.get_counter('/nat64/total-sessions')
7072 self.assertEqual(sessions[0][0], 3)
7073
Matus Fabian06596c52017-06-06 04:53:28 -07007074 # in2out
7075 pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
7076 self.pg0.add_stream(pkts)
7077 self.pg_enable_capture(self.pg_interfaces)
7078 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07007079 capture = self.pg1.get_capture(len(pkts))
7080 self.verify_capture_out(capture, nat_ip=self.nat_addr,
Matus Fabian06596c52017-06-06 04:53:28 -07007081 dst_ip=self.pg1.remote_ip4)
7082
7083 # out2in
7084 pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
7085 self.pg1.add_stream(pkts)
7086 self.pg_enable_capture(self.pg_interfaces)
7087 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07007088 capture = self.pg0.get_capture(len(pkts))
Matus Fabian06596c52017-06-06 04:53:28 -07007089 self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6)
7090
7091 ses_num_end = self.nat64_get_ses_num()
7092
7093 self.assertEqual(ses_num_end - ses_num_start, 3)
7094
Matus Fabian029f3d22017-06-15 02:28:50 -07007095 # tenant with specific VRF
7096 self.vapi.nat64_add_del_pool_addr_range(self.vrf1_nat_addr_n,
7097 self.vrf1_nat_addr_n,
7098 vrf_id=self.vrf1_id)
7099 self.vapi.nat64_add_del_interface(self.pg2.sw_if_index)
7100
7101 pkts = self.create_stream_in_ip6(self.pg2, self.pg1)
7102 self.pg2.add_stream(pkts)
7103 self.pg_enable_capture(self.pg_interfaces)
7104 self.pg_start()
7105 capture = self.pg1.get_capture(len(pkts))
7106 self.verify_capture_out(capture, nat_ip=self.vrf1_nat_addr,
7107 dst_ip=self.pg1.remote_ip4)
7108
7109 pkts = self.create_stream_out(self.pg1, dst_ip=self.vrf1_nat_addr)
7110 self.pg1.add_stream(pkts)
7111 self.pg_enable_capture(self.pg_interfaces)
7112 self.pg_start()
7113 capture = self.pg2.get_capture(len(pkts))
7114 self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg2.remote_ip6)
7115
Matus Fabian06596c52017-06-06 04:53:28 -07007116 def test_static(self):
7117 """ NAT64 static translation test """
7118 self.tcp_port_in = 60303
7119 self.udp_port_in = 60304
7120 self.icmp_id_in = 60305
7121 self.tcp_port_out = 60303
7122 self.udp_port_out = 60304
7123 self.icmp_id_out = 60305
7124
7125 ses_num_start = self.nat64_get_ses_num()
7126
7127 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7128 self.nat_addr_n)
7129 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7130 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7131
7132 self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n,
7133 self.nat_addr_n,
7134 self.tcp_port_in,
7135 self.tcp_port_out,
7136 IP_PROTOS.tcp)
7137 self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n,
7138 self.nat_addr_n,
7139 self.udp_port_in,
7140 self.udp_port_out,
7141 IP_PROTOS.udp)
7142 self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n,
7143 self.nat_addr_n,
7144 self.icmp_id_in,
7145 self.icmp_id_out,
7146 IP_PROTOS.icmp)
7147
7148 # in2out
7149 pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
7150 self.pg0.add_stream(pkts)
7151 self.pg_enable_capture(self.pg_interfaces)
7152 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07007153 capture = self.pg1.get_capture(len(pkts))
7154 self.verify_capture_out(capture, nat_ip=self.nat_addr,
Matus Fabian06596c52017-06-06 04:53:28 -07007155 dst_ip=self.pg1.remote_ip4, same_port=True)
7156
7157 # out2in
7158 pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
7159 self.pg1.add_stream(pkts)
7160 self.pg_enable_capture(self.pg_interfaces)
7161 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07007162 capture = self.pg0.get_capture(len(pkts))
Matus Fabian06596c52017-06-06 04:53:28 -07007163 ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4]))
7164 self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6)
7165
7166 ses_num_end = self.nat64_get_ses_num()
7167
7168 self.assertEqual(ses_num_end - ses_num_start, 3)
7169
Paul Vinciguerradefde0f2018-12-06 07:46:13 -08007170 @unittest.skipUnless(running_extended_tests, "part of extended tests")
Matus Fabian06596c52017-06-06 04:53:28 -07007171 def test_session_timeout(self):
7172 """ NAT64 session timeout """
7173 self.icmp_id_in = 1234
7174 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7175 self.nat_addr_n)
7176 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7177 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
Matus Fabian878c6462018-08-23 00:33:35 -07007178 self.vapi.nat_set_timeouts(icmp=5, tcp_transitory=5, tcp_established=5)
Matus Fabian06596c52017-06-06 04:53:28 -07007179
7180 pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
7181 self.pg0.add_stream(pkts)
7182 self.pg_enable_capture(self.pg_interfaces)
7183 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07007184 capture = self.pg1.get_capture(len(pkts))
Matus Fabian06596c52017-06-06 04:53:28 -07007185
7186 ses_num_before_timeout = self.nat64_get_ses_num()
7187
7188 sleep(15)
7189
Matus Fabian8fed4242018-08-14 05:14:55 -07007190 # ICMP and TCP session after timeout
Matus Fabian06596c52017-06-06 04:53:28 -07007191 ses_num_after_timeout = self.nat64_get_ses_num()
Matus Fabian8fed4242018-08-14 05:14:55 -07007192 self.assertEqual(ses_num_before_timeout - ses_num_after_timeout, 2)
Matus Fabian06596c52017-06-06 04:53:28 -07007193
Matus Fabian732036d2017-06-08 05:24:28 -07007194 def test_icmp_error(self):
7195 """ NAT64 ICMP Error message translation """
7196 self.tcp_port_in = 6303
7197 self.udp_port_in = 6304
7198 self.icmp_id_in = 6305
7199
Matus Fabian732036d2017-06-08 05:24:28 -07007200 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7201 self.nat_addr_n)
7202 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7203 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7204
7205 # send some packets to create sessions
7206 pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
7207 self.pg0.add_stream(pkts)
7208 self.pg_enable_capture(self.pg_interfaces)
7209 self.pg_start()
7210 capture_ip4 = self.pg1.get_capture(len(pkts))
Matus Fabian029f3d22017-06-15 02:28:50 -07007211 self.verify_capture_out(capture_ip4,
Matus Fabian732036d2017-06-08 05:24:28 -07007212 nat_ip=self.nat_addr,
7213 dst_ip=self.pg1.remote_ip4)
7214
7215 pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
7216 self.pg1.add_stream(pkts)
7217 self.pg_enable_capture(self.pg_interfaces)
7218 self.pg_start()
7219 capture_ip6 = self.pg0.get_capture(len(pkts))
7220 ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4]))
7221 self.verify_capture_in_ip6(capture_ip6, ip[IPv6].src,
7222 self.pg0.remote_ip6)
7223
7224 # in2out
7225 pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7226 IPv6(src=self.pg0.remote_ip6, dst=ip[IPv6].src) /
7227 ICMPv6DestUnreach(code=1) /
7228 packet[IPv6] for packet in capture_ip6]
7229 self.pg0.add_stream(pkts)
7230 self.pg_enable_capture(self.pg_interfaces)
7231 self.pg_start()
7232 capture = self.pg1.get_capture(len(pkts))
7233 for packet in capture:
7234 try:
7235 self.assertEqual(packet[IP].src, self.nat_addr)
7236 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
7237 self.assertEqual(packet[ICMP].type, 3)
7238 self.assertEqual(packet[ICMP].code, 13)
7239 inner = packet[IPerror]
7240 self.assertEqual(inner.src, self.pg1.remote_ip4)
7241 self.assertEqual(inner.dst, self.nat_addr)
Klement Sekerad81ae412018-05-16 10:52:54 +02007242 self.assert_packet_checksums_valid(packet)
Matus Fabian732036d2017-06-08 05:24:28 -07007243 if inner.haslayer(TCPerror):
7244 self.assertEqual(inner[TCPerror].dport, self.tcp_port_out)
7245 elif inner.haslayer(UDPerror):
7246 self.assertEqual(inner[UDPerror].dport, self.udp_port_out)
7247 else:
7248 self.assertEqual(inner[ICMPerror].id, self.icmp_id_out)
7249 except:
7250 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7251 raise
7252
7253 # out2in
7254 pkts = [Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
7255 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
7256 ICMP(type=3, code=13) /
7257 packet[IP] for packet in capture_ip4]
7258 self.pg1.add_stream(pkts)
7259 self.pg_enable_capture(self.pg_interfaces)
7260 self.pg_start()
7261 capture = self.pg0.get_capture(len(pkts))
7262 for packet in capture:
7263 try:
7264 self.assertEqual(packet[IPv6].src, ip.src)
7265 self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6)
7266 icmp = packet[ICMPv6DestUnreach]
7267 self.assertEqual(icmp.code, 1)
7268 inner = icmp[IPerror6]
7269 self.assertEqual(inner.src, self.pg0.remote_ip6)
7270 self.assertEqual(inner.dst, ip.src)
Klement Sekerad81ae412018-05-16 10:52:54 +02007271 self.assert_icmpv6_checksum_valid(packet)
Matus Fabian732036d2017-06-08 05:24:28 -07007272 if inner.haslayer(TCPerror):
7273 self.assertEqual(inner[TCPerror].sport, self.tcp_port_in)
7274 elif inner.haslayer(UDPerror):
7275 self.assertEqual(inner[UDPerror].sport, self.udp_port_in)
7276 else:
7277 self.assertEqual(inner[ICMPv6EchoRequest].id,
7278 self.icmp_id_in)
7279 except:
7280 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7281 raise
7282
Matus Fabian029f3d22017-06-15 02:28:50 -07007283 def test_hairpinning(self):
7284 """ NAT64 hairpinning """
7285
7286 client = self.pg0.remote_hosts[0]
7287 server = self.pg0.remote_hosts[1]
7288 server_tcp_in_port = 22
7289 server_tcp_out_port = 4022
7290 server_udp_in_port = 23
7291 server_udp_out_port = 4023
7292 client_tcp_in_port = 1234
7293 client_udp_in_port = 1235
7294 client_tcp_out_port = 0
7295 client_udp_out_port = 0
7296 ip = IPv6(src=''.join(['64:ff9b::', self.nat_addr]))
7297 nat_addr_ip6 = ip.src
7298
7299 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7300 self.nat_addr_n)
7301 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7302 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7303
7304 self.vapi.nat64_add_del_static_bib(server.ip6n,
7305 self.nat_addr_n,
7306 server_tcp_in_port,
7307 server_tcp_out_port,
7308 IP_PROTOS.tcp)
7309 self.vapi.nat64_add_del_static_bib(server.ip6n,
7310 self.nat_addr_n,
7311 server_udp_in_port,
7312 server_udp_out_port,
7313 IP_PROTOS.udp)
7314
7315 # client to server
7316 pkts = []
7317 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7318 IPv6(src=client.ip6, dst=nat_addr_ip6) /
7319 TCP(sport=client_tcp_in_port, dport=server_tcp_out_port))
7320 pkts.append(p)
7321 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7322 IPv6(src=client.ip6, dst=nat_addr_ip6) /
7323 UDP(sport=client_udp_in_port, dport=server_udp_out_port))
7324 pkts.append(p)
7325 self.pg0.add_stream(pkts)
7326 self.pg_enable_capture(self.pg_interfaces)
7327 self.pg_start()
7328 capture = self.pg0.get_capture(len(pkts))
7329 for packet in capture:
7330 try:
7331 self.assertEqual(packet[IPv6].src, nat_addr_ip6)
7332 self.assertEqual(packet[IPv6].dst, server.ip6)
Klement Sekerad81ae412018-05-16 10:52:54 +02007333 self.assert_packet_checksums_valid(packet)
Matus Fabian029f3d22017-06-15 02:28:50 -07007334 if packet.haslayer(TCP):
7335 self.assertNotEqual(packet[TCP].sport, client_tcp_in_port)
7336 self.assertEqual(packet[TCP].dport, server_tcp_in_port)
Matus Fabian029f3d22017-06-15 02:28:50 -07007337 client_tcp_out_port = packet[TCP].sport
7338 else:
7339 self.assertNotEqual(packet[UDP].sport, client_udp_in_port)
7340 self.assertEqual(packet[UDP].dport, server_udp_in_port)
Matus Fabian029f3d22017-06-15 02:28:50 -07007341 client_udp_out_port = packet[UDP].sport
7342 except:
7343 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7344 raise
7345
7346 # server to client
7347 pkts = []
7348 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7349 IPv6(src=server.ip6, dst=nat_addr_ip6) /
7350 TCP(sport=server_tcp_in_port, dport=client_tcp_out_port))
7351 pkts.append(p)
7352 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7353 IPv6(src=server.ip6, dst=nat_addr_ip6) /
7354 UDP(sport=server_udp_in_port, dport=client_udp_out_port))
7355 pkts.append(p)
7356 self.pg0.add_stream(pkts)
7357 self.pg_enable_capture(self.pg_interfaces)
7358 self.pg_start()
7359 capture = self.pg0.get_capture(len(pkts))
7360 for packet in capture:
7361 try:
7362 self.assertEqual(packet[IPv6].src, nat_addr_ip6)
7363 self.assertEqual(packet[IPv6].dst, client.ip6)
Klement Sekerad81ae412018-05-16 10:52:54 +02007364 self.assert_packet_checksums_valid(packet)
Matus Fabian029f3d22017-06-15 02:28:50 -07007365 if packet.haslayer(TCP):
7366 self.assertEqual(packet[TCP].sport, server_tcp_out_port)
7367 self.assertEqual(packet[TCP].dport, client_tcp_in_port)
Matus Fabian029f3d22017-06-15 02:28:50 -07007368 else:
7369 self.assertEqual(packet[UDP].sport, server_udp_out_port)
7370 self.assertEqual(packet[UDP].dport, client_udp_in_port)
Matus Fabian029f3d22017-06-15 02:28:50 -07007371 except:
7372 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7373 raise
7374
7375 # ICMP error
7376 pkts = []
7377 pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7378 IPv6(src=client.ip6, dst=nat_addr_ip6) /
7379 ICMPv6DestUnreach(code=1) /
7380 packet[IPv6] for packet in capture]
7381 self.pg0.add_stream(pkts)
7382 self.pg_enable_capture(self.pg_interfaces)
7383 self.pg_start()
7384 capture = self.pg0.get_capture(len(pkts))
7385 for packet in capture:
7386 try:
7387 self.assertEqual(packet[IPv6].src, nat_addr_ip6)
7388 self.assertEqual(packet[IPv6].dst, server.ip6)
7389 icmp = packet[ICMPv6DestUnreach]
7390 self.assertEqual(icmp.code, 1)
7391 inner = icmp[IPerror6]
7392 self.assertEqual(inner.src, server.ip6)
7393 self.assertEqual(inner.dst, nat_addr_ip6)
Klement Sekerad81ae412018-05-16 10:52:54 +02007394 self.assert_packet_checksums_valid(packet)
Matus Fabian029f3d22017-06-15 02:28:50 -07007395 if inner.haslayer(TCPerror):
7396 self.assertEqual(inner[TCPerror].sport, server_tcp_in_port)
7397 self.assertEqual(inner[TCPerror].dport,
7398 client_tcp_out_port)
7399 else:
7400 self.assertEqual(inner[UDPerror].sport, server_udp_in_port)
7401 self.assertEqual(inner[UDPerror].dport,
7402 client_udp_out_port)
7403 except:
7404 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7405 raise
7406
Matus Fabian428dc912017-06-21 06:15:18 -07007407 def test_prefix(self):
7408 """ NAT64 Network-Specific Prefix """
7409
7410 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7411 self.nat_addr_n)
7412 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7413 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7414 self.vapi.nat64_add_del_pool_addr_range(self.vrf1_nat_addr_n,
7415 self.vrf1_nat_addr_n,
7416 vrf_id=self.vrf1_id)
7417 self.vapi.nat64_add_del_interface(self.pg2.sw_if_index)
7418
7419 # Add global prefix
7420 global_pref64 = "2001:db8::"
7421 global_pref64_n = socket.inet_pton(socket.AF_INET6, global_pref64)
7422 global_pref64_len = 32
7423 self.vapi.nat64_add_del_prefix(global_pref64_n, global_pref64_len)
7424
7425 prefix = self.vapi.nat64_prefix_dump()
7426 self.assertEqual(len(prefix), 1)
7427 self.assertEqual(prefix[0].prefix, global_pref64_n)
7428 self.assertEqual(prefix[0].prefix_len, global_pref64_len)
7429 self.assertEqual(prefix[0].vrf_id, 0)
7430
7431 # Add tenant specific prefix
7432 vrf1_pref64 = "2001:db8:122:300::"
7433 vrf1_pref64_n = socket.inet_pton(socket.AF_INET6, vrf1_pref64)
7434 vrf1_pref64_len = 56
7435 self.vapi.nat64_add_del_prefix(vrf1_pref64_n,
7436 vrf1_pref64_len,
7437 vrf_id=self.vrf1_id)
7438 prefix = self.vapi.nat64_prefix_dump()
7439 self.assertEqual(len(prefix), 2)
7440
7441 # Global prefix
7442 pkts = self.create_stream_in_ip6(self.pg0,
7443 self.pg1,
7444 pref=global_pref64,
7445 plen=global_pref64_len)
7446 self.pg0.add_stream(pkts)
7447 self.pg_enable_capture(self.pg_interfaces)
7448 self.pg_start()
7449 capture = self.pg1.get_capture(len(pkts))
7450 self.verify_capture_out(capture, nat_ip=self.nat_addr,
7451 dst_ip=self.pg1.remote_ip4)
7452
7453 pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
7454 self.pg1.add_stream(pkts)
7455 self.pg_enable_capture(self.pg_interfaces)
7456 self.pg_start()
7457 capture = self.pg0.get_capture(len(pkts))
7458 dst_ip = self.compose_ip6(self.pg1.remote_ip4,
7459 global_pref64,
7460 global_pref64_len)
7461 self.verify_capture_in_ip6(capture, dst_ip, self.pg0.remote_ip6)
7462
7463 # Tenant specific prefix
7464 pkts = self.create_stream_in_ip6(self.pg2,
7465 self.pg1,
7466 pref=vrf1_pref64,
7467 plen=vrf1_pref64_len)
7468 self.pg2.add_stream(pkts)
7469 self.pg_enable_capture(self.pg_interfaces)
7470 self.pg_start()
7471 capture = self.pg1.get_capture(len(pkts))
7472 self.verify_capture_out(capture, nat_ip=self.vrf1_nat_addr,
7473 dst_ip=self.pg1.remote_ip4)
7474
7475 pkts = self.create_stream_out(self.pg1, dst_ip=self.vrf1_nat_addr)
7476 self.pg1.add_stream(pkts)
7477 self.pg_enable_capture(self.pg_interfaces)
7478 self.pg_start()
7479 capture = self.pg2.get_capture(len(pkts))
7480 dst_ip = self.compose_ip6(self.pg1.remote_ip4,
7481 vrf1_pref64,
7482 vrf1_pref64_len)
7483 self.verify_capture_in_ip6(capture, dst_ip, self.pg2.remote_ip6)
7484
Matus Fabianf8cd5812017-07-11 03:55:02 -07007485 def test_unknown_proto(self):
Matus Fabian7968e6c2017-07-06 05:37:49 -07007486 """ NAT64 translate packet with unknown protocol """
7487
7488 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7489 self.nat_addr_n)
7490 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7491 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7492 remote_ip6 = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96)
7493
7494 # in2out
7495 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7496 IPv6(src=self.pg0.remote_ip6, dst=remote_ip6) /
7497 TCP(sport=self.tcp_port_in, dport=20))
7498 self.pg0.add_stream(p)
7499 self.pg_enable_capture(self.pg_interfaces)
7500 self.pg_start()
7501 p = self.pg1.get_capture(1)
7502
7503 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabianf8cd5812017-07-11 03:55:02 -07007504 IPv6(src=self.pg0.remote_ip6, dst=remote_ip6, nh=47) /
Matus Fabian7968e6c2017-07-06 05:37:49 -07007505 GRE() /
7506 IP(src=self.pg2.local_ip4, dst=self.pg2.remote_ip4) /
7507 TCP(sport=1234, dport=1234))
7508 self.pg0.add_stream(p)
7509 self.pg_enable_capture(self.pg_interfaces)
7510 self.pg_start()
7511 p = self.pg1.get_capture(1)
7512 packet = p[0]
7513 try:
7514 self.assertEqual(packet[IP].src, self.nat_addr)
7515 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08007516 self.assertEqual(packet.haslayer(GRE), 1)
Klement Sekerad81ae412018-05-16 10:52:54 +02007517 self.assert_packet_checksums_valid(packet)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007518 except:
7519 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7520 raise
7521
7522 # out2in
7523 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
7524 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
7525 GRE() /
7526 IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) /
7527 TCP(sport=1234, dport=1234))
7528 self.pg1.add_stream(p)
7529 self.pg_enable_capture(self.pg_interfaces)
7530 self.pg_start()
7531 p = self.pg0.get_capture(1)
7532 packet = p[0]
7533 try:
7534 self.assertEqual(packet[IPv6].src, remote_ip6)
Matus Fabianf8cd5812017-07-11 03:55:02 -07007535 self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6)
7536 self.assertEqual(packet[IPv6].nh, 47)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007537 except:
7538 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7539 raise
7540
Matus Fabianf8cd5812017-07-11 03:55:02 -07007541 def test_hairpinning_unknown_proto(self):
Matus Fabian7968e6c2017-07-06 05:37:49 -07007542 """ NAT64 translate packet with unknown protocol - hairpinning """
7543
7544 client = self.pg0.remote_hosts[0]
7545 server = self.pg0.remote_hosts[1]
7546 server_tcp_in_port = 22
7547 server_tcp_out_port = 4022
7548 client_tcp_in_port = 1234
Matus Fabianf8cd5812017-07-11 03:55:02 -07007549 client_tcp_out_port = 1235
7550 server_nat_ip = "10.0.0.100"
7551 client_nat_ip = "10.0.0.110"
7552 server_nat_ip_n = socket.inet_pton(socket.AF_INET, server_nat_ip)
7553 client_nat_ip_n = socket.inet_pton(socket.AF_INET, client_nat_ip)
7554 server_nat_ip6 = self.compose_ip6(server_nat_ip, '64:ff9b::', 96)
7555 client_nat_ip6 = self.compose_ip6(client_nat_ip, '64:ff9b::', 96)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007556
Matus Fabianf8cd5812017-07-11 03:55:02 -07007557 self.vapi.nat64_add_del_pool_addr_range(server_nat_ip_n,
7558 client_nat_ip_n)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007559 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7560 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7561
7562 self.vapi.nat64_add_del_static_bib(server.ip6n,
Matus Fabianf8cd5812017-07-11 03:55:02 -07007563 server_nat_ip_n,
Matus Fabian7968e6c2017-07-06 05:37:49 -07007564 server_tcp_in_port,
7565 server_tcp_out_port,
7566 IP_PROTOS.tcp)
7567
Matus Fabianf8cd5812017-07-11 03:55:02 -07007568 self.vapi.nat64_add_del_static_bib(server.ip6n,
7569 server_nat_ip_n,
7570 0,
7571 0,
7572 IP_PROTOS.gre)
7573
7574 self.vapi.nat64_add_del_static_bib(client.ip6n,
7575 client_nat_ip_n,
7576 client_tcp_in_port,
7577 client_tcp_out_port,
7578 IP_PROTOS.tcp)
7579
Matus Fabian7968e6c2017-07-06 05:37:49 -07007580 # client to server
7581 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabianf8cd5812017-07-11 03:55:02 -07007582 IPv6(src=client.ip6, dst=server_nat_ip6) /
Matus Fabian7968e6c2017-07-06 05:37:49 -07007583 TCP(sport=client_tcp_in_port, dport=server_tcp_out_port))
7584 self.pg0.add_stream(p)
7585 self.pg_enable_capture(self.pg_interfaces)
7586 self.pg_start()
7587 p = self.pg0.get_capture(1)
7588
7589 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabianf8cd5812017-07-11 03:55:02 -07007590 IPv6(src=client.ip6, dst=server_nat_ip6, nh=IP_PROTOS.gre) /
Matus Fabian7968e6c2017-07-06 05:37:49 -07007591 GRE() /
7592 IP(src=self.pg2.local_ip4, dst=self.pg2.remote_ip4) /
7593 TCP(sport=1234, dport=1234))
7594 self.pg0.add_stream(p)
7595 self.pg_enable_capture(self.pg_interfaces)
7596 self.pg_start()
7597 p = self.pg0.get_capture(1)
7598 packet = p[0]
7599 try:
Matus Fabianf8cd5812017-07-11 03:55:02 -07007600 self.assertEqual(packet[IPv6].src, client_nat_ip6)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007601 self.assertEqual(packet[IPv6].dst, server.ip6)
Matus Fabianf8cd5812017-07-11 03:55:02 -07007602 self.assertEqual(packet[IPv6].nh, IP_PROTOS.gre)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007603 except:
7604 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7605 raise
7606
7607 # server to client
7608 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabianf8cd5812017-07-11 03:55:02 -07007609 IPv6(src=server.ip6, dst=client_nat_ip6, nh=IP_PROTOS.gre) /
Matus Fabian7968e6c2017-07-06 05:37:49 -07007610 GRE() /
7611 IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) /
7612 TCP(sport=1234, dport=1234))
7613 self.pg0.add_stream(p)
7614 self.pg_enable_capture(self.pg_interfaces)
7615 self.pg_start()
7616 p = self.pg0.get_capture(1)
7617 packet = p[0]
7618 try:
Matus Fabianf8cd5812017-07-11 03:55:02 -07007619 self.assertEqual(packet[IPv6].src, server_nat_ip6)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007620 self.assertEqual(packet[IPv6].dst, client.ip6)
Matus Fabianf8cd5812017-07-11 03:55:02 -07007621 self.assertEqual(packet[IPv6].nh, IP_PROTOS.gre)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007622 except:
7623 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7624 raise
7625
Matus Fabian36ea2d62017-10-24 04:13:49 -07007626 def test_one_armed_nat64(self):
7627 """ One armed NAT64 """
7628 external_port = 0
7629 remote_host_ip6 = self.compose_ip6(self.pg3.remote_ip4,
7630 '64:ff9b::',
7631 96)
7632
7633 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7634 self.nat_addr_n)
7635 self.vapi.nat64_add_del_interface(self.pg3.sw_if_index)
7636 self.vapi.nat64_add_del_interface(self.pg3.sw_if_index, is_inside=0)
7637
7638 # in2out
7639 p = (Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) /
7640 IPv6(src=self.pg3.remote_ip6, dst=remote_host_ip6) /
7641 TCP(sport=12345, dport=80))
7642 self.pg3.add_stream(p)
7643 self.pg_enable_capture(self.pg_interfaces)
7644 self.pg_start()
7645 capture = self.pg3.get_capture(1)
7646 p = capture[0]
7647 try:
7648 ip = p[IP]
7649 tcp = p[TCP]
7650 self.assertEqual(ip.src, self.nat_addr)
7651 self.assertEqual(ip.dst, self.pg3.remote_ip4)
7652 self.assertNotEqual(tcp.sport, 12345)
7653 external_port = tcp.sport
7654 self.assertEqual(tcp.dport, 80)
Klement Sekerad81ae412018-05-16 10:52:54 +02007655 self.assert_packet_checksums_valid(p)
Matus Fabian36ea2d62017-10-24 04:13:49 -07007656 except:
7657 self.logger.error(ppp("Unexpected or invalid packet:", p))
7658 raise
7659
7660 # out2in
7661 p = (Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) /
7662 IP(src=self.pg3.remote_ip4, dst=self.nat_addr) /
7663 TCP(sport=80, dport=external_port))
7664 self.pg3.add_stream(p)
7665 self.pg_enable_capture(self.pg_interfaces)
7666 self.pg_start()
7667 capture = self.pg3.get_capture(1)
7668 p = capture[0]
7669 try:
7670 ip = p[IPv6]
7671 tcp = p[TCP]
7672 self.assertEqual(ip.src, remote_host_ip6)
7673 self.assertEqual(ip.dst, self.pg3.remote_ip6)
7674 self.assertEqual(tcp.sport, 80)
7675 self.assertEqual(tcp.dport, 12345)
Klement Sekerad81ae412018-05-16 10:52:54 +02007676 self.assert_packet_checksums_valid(p)
Matus Fabian36ea2d62017-10-24 04:13:49 -07007677 except:
7678 self.logger.error(ppp("Unexpected or invalid packet:", p))
7679 raise
7680
Matus Fabianefcd1e92017-08-15 06:59:19 -07007681 def test_frag_in_order(self):
7682 """ NAT64 translate fragments arriving in order """
7683 self.tcp_port_in = random.randint(1025, 65535)
7684
7685 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7686 self.nat_addr_n)
7687 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7688 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7689
7690 reass = self.vapi.nat_reass_dump()
7691 reass_n_start = len(reass)
7692
7693 # in2out
7694 data = 'a' * 200
7695 pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4,
7696 self.tcp_port_in, 20, data)
7697 self.pg0.add_stream(pkts)
7698 self.pg_enable_capture(self.pg_interfaces)
7699 self.pg_start()
7700 frags = self.pg1.get_capture(len(pkts))
7701 p = self.reass_frags_and_verify(frags,
7702 self.nat_addr,
7703 self.pg1.remote_ip4)
7704 self.assertEqual(p[TCP].dport, 20)
7705 self.assertNotEqual(p[TCP].sport, self.tcp_port_in)
7706 self.tcp_port_out = p[TCP].sport
7707 self.assertEqual(data, p[Raw].load)
7708
7709 # out2in
7710 data = "A" * 4 + "b" * 16 + "C" * 3
7711 pkts = self.create_stream_frag(self.pg1,
7712 self.nat_addr,
7713 20,
7714 self.tcp_port_out,
7715 data)
7716 self.pg1.add_stream(pkts)
7717 self.pg_enable_capture(self.pg_interfaces)
7718 self.pg_start()
7719 frags = self.pg0.get_capture(len(pkts))
7720 src = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96)
7721 p = self.reass_frags_and_verify_ip6(frags, src, self.pg0.remote_ip6)
7722 self.assertEqual(p[TCP].sport, 20)
7723 self.assertEqual(p[TCP].dport, self.tcp_port_in)
7724 self.assertEqual(data, p[Raw].load)
7725
7726 reass = self.vapi.nat_reass_dump()
7727 reass_n_end = len(reass)
7728
7729 self.assertEqual(reass_n_end - reass_n_start, 2)
7730
7731 def test_reass_hairpinning(self):
7732 """ NAT64 fragments hairpinning """
7733 data = 'a' * 200
Matus Fabianefcd1e92017-08-15 06:59:19 -07007734 server = self.pg0.remote_hosts[1]
7735 server_in_port = random.randint(1025, 65535)
7736 server_out_port = random.randint(1025, 65535)
7737 client_in_port = random.randint(1025, 65535)
7738 ip = IPv6(src=''.join(['64:ff9b::', self.nat_addr]))
7739 nat_addr_ip6 = ip.src
7740
7741 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7742 self.nat_addr_n)
7743 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7744 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7745
7746 # add static BIB entry for server
7747 self.vapi.nat64_add_del_static_bib(server.ip6n,
7748 self.nat_addr_n,
7749 server_in_port,
7750 server_out_port,
7751 IP_PROTOS.tcp)
7752
7753 # send packet from host to server
7754 pkts = self.create_stream_frag_ip6(self.pg0,
7755 self.nat_addr,
7756 client_in_port,
7757 server_out_port,
7758 data)
7759 self.pg0.add_stream(pkts)
7760 self.pg_enable_capture(self.pg_interfaces)
7761 self.pg_start()
7762 frags = self.pg0.get_capture(len(pkts))
7763 p = self.reass_frags_and_verify_ip6(frags, nat_addr_ip6, server.ip6)
7764 self.assertNotEqual(p[TCP].sport, client_in_port)
7765 self.assertEqual(p[TCP].dport, server_in_port)
7766 self.assertEqual(data, p[Raw].load)
7767
7768 def test_frag_out_of_order(self):
7769 """ NAT64 translate fragments arriving out of order """
7770 self.tcp_port_in = random.randint(1025, 65535)
7771
7772 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7773 self.nat_addr_n)
7774 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7775 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7776
7777 # in2out
7778 data = 'a' * 200
7779 pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4,
7780 self.tcp_port_in, 20, data)
7781 pkts.reverse()
7782 self.pg0.add_stream(pkts)
7783 self.pg_enable_capture(self.pg_interfaces)
7784 self.pg_start()
7785 frags = self.pg1.get_capture(len(pkts))
7786 p = self.reass_frags_and_verify(frags,
7787 self.nat_addr,
7788 self.pg1.remote_ip4)
7789 self.assertEqual(p[TCP].dport, 20)
7790 self.assertNotEqual(p[TCP].sport, self.tcp_port_in)
7791 self.tcp_port_out = p[TCP].sport
7792 self.assertEqual(data, p[Raw].load)
7793
7794 # out2in
7795 data = "A" * 4 + "B" * 16 + "C" * 3
7796 pkts = self.create_stream_frag(self.pg1,
7797 self.nat_addr,
7798 20,
7799 self.tcp_port_out,
7800 data)
7801 pkts.reverse()
7802 self.pg1.add_stream(pkts)
7803 self.pg_enable_capture(self.pg_interfaces)
7804 self.pg_start()
7805 frags = self.pg0.get_capture(len(pkts))
7806 src = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96)
7807 p = self.reass_frags_and_verify_ip6(frags, src, self.pg0.remote_ip6)
7808 self.assertEqual(p[TCP].sport, 20)
7809 self.assertEqual(p[TCP].dport, self.tcp_port_in)
7810 self.assertEqual(data, p[Raw].load)
7811
Matus Fabian0938dcf2017-11-08 01:59:38 -08007812 def test_interface_addr(self):
7813 """ Acquire NAT64 pool addresses from interface """
7814 self.vapi.nat64_add_interface_addr(self.pg4.sw_if_index)
7815
7816 # no address in NAT64 pool
7817 adresses = self.vapi.nat44_address_dump()
7818 self.assertEqual(0, len(adresses))
7819
7820 # configure interface address and check NAT64 address pool
7821 self.pg4.config_ip4()
7822 addresses = self.vapi.nat64_pool_addr_dump()
7823 self.assertEqual(len(addresses), 1)
7824 self.assertEqual(addresses[0].address, self.pg4.local_ip4n)
7825
7826 # remove interface address and check NAT64 address pool
7827 self.pg4.unconfig_ip4()
7828 addresses = self.vapi.nat64_pool_addr_dump()
7829 self.assertEqual(0, len(adresses))
7830
Paul Vinciguerradefde0f2018-12-06 07:46:13 -08007831 @unittest.skipUnless(running_extended_tests, "part of extended tests")
Matus Fabiana431ad12018-01-04 04:03:14 -08007832 def test_ipfix_max_bibs_sessions(self):
7833 """ IPFIX logging maximum session and BIB entries exceeded """
7834 max_bibs = 1280
7835 max_sessions = 2560
7836 remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4,
7837 '64:ff9b::',
7838 96)
7839
7840 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7841 self.nat_addr_n)
7842 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7843 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7844
7845 pkts = []
7846 src = ""
7847 for i in range(0, max_bibs):
7848 src = "fd01:aa::%x" % (i)
7849 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
7850 IPv6(src=src, dst=remote_host_ip6) /
7851 TCP(sport=12345, dport=80))
7852 pkts.append(p)
7853 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
7854 IPv6(src=src, dst=remote_host_ip6) /
7855 TCP(sport=12345, dport=22))
7856 pkts.append(p)
7857 self.pg0.add_stream(pkts)
7858 self.pg_enable_capture(self.pg_interfaces)
7859 self.pg_start()
7860 self.pg1.get_capture(max_sessions)
7861
7862 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
7863 src_address=self.pg3.local_ip4n,
7864 path_mtu=512,
7865 template_interval=10)
7866 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
7867 src_port=self.ipfix_src_port)
7868
7869 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
7870 IPv6(src=src, dst=remote_host_ip6) /
7871 TCP(sport=12345, dport=25))
7872 self.pg0.add_stream(p)
7873 self.pg_enable_capture(self.pg_interfaces)
7874 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07007875 self.pg1.assert_nothing_captured()
7876 sleep(1)
Matus Fabiana431ad12018-01-04 04:03:14 -08007877 self.vapi.cli("ipfix flush") # FIXME this should be an API call
7878 capture = self.pg3.get_capture(9)
7879 ipfix = IPFIXDecoder()
7880 # first load template
7881 for p in capture:
7882 self.assertTrue(p.haslayer(IPFIX))
7883 self.assertEqual(p[IP].src, self.pg3.local_ip4)
7884 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
7885 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
7886 self.assertEqual(p[UDP].dport, 4739)
7887 self.assertEqual(p[IPFIX].observationDomainID,
7888 self.ipfix_domain_id)
7889 if p.haslayer(Template):
7890 ipfix.add_template(p.getlayer(Template))
7891 # verify events in data set
7892 for p in capture:
7893 if p.haslayer(Data):
7894 data = ipfix.decode_data_set(p.getlayer(Set))
7895 self.verify_ipfix_max_sessions(data, max_sessions)
7896
7897 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
7898 IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6) /
7899 TCP(sport=12345, dport=80))
7900 self.pg0.add_stream(p)
7901 self.pg_enable_capture(self.pg_interfaces)
7902 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07007903 self.pg1.assert_nothing_captured()
7904 sleep(1)
Matus Fabiana431ad12018-01-04 04:03:14 -08007905 self.vapi.cli("ipfix flush") # FIXME this should be an API call
7906 capture = self.pg3.get_capture(1)
7907 # verify events in data set
7908 for p in capture:
7909 self.assertTrue(p.haslayer(IPFIX))
7910 self.assertEqual(p[IP].src, self.pg3.local_ip4)
7911 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
7912 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
7913 self.assertEqual(p[UDP].dport, 4739)
7914 self.assertEqual(p[IPFIX].observationDomainID,
7915 self.ipfix_domain_id)
7916 if p.haslayer(Data):
7917 data = ipfix.decode_data_set(p.getlayer(Set))
7918 self.verify_ipfix_max_bibs(data, max_bibs)
7919
7920 def test_ipfix_max_frags(self):
7921 """ IPFIX logging maximum fragments pending reassembly exceeded """
7922 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7923 self.nat_addr_n)
7924 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7925 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
Matus Fabiana7f8b222018-09-05 06:01:55 -07007926 self.vapi.nat_set_reass(max_frag=1, is_ip6=1)
Matus Fabiana431ad12018-01-04 04:03:14 -08007927 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
7928 src_address=self.pg3.local_ip4n,
7929 path_mtu=512,
7930 template_interval=10)
7931 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
7932 src_port=self.ipfix_src_port)
7933
7934 data = 'a' * 200
7935 pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4,
7936 self.tcp_port_in, 20, data)
Matus Fabiana7f8b222018-09-05 06:01:55 -07007937 pkts.reverse()
7938 self.pg0.add_stream(pkts)
Matus Fabiana431ad12018-01-04 04:03:14 -08007939 self.pg_enable_capture(self.pg_interfaces)
7940 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07007941 self.pg1.assert_nothing_captured()
7942 sleep(1)
Matus Fabiana431ad12018-01-04 04:03:14 -08007943 self.vapi.cli("ipfix flush") # FIXME this should be an API call
7944 capture = self.pg3.get_capture(9)
7945 ipfix = IPFIXDecoder()
7946 # first load template
7947 for p in capture:
7948 self.assertTrue(p.haslayer(IPFIX))
7949 self.assertEqual(p[IP].src, self.pg3.local_ip4)
7950 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
7951 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
7952 self.assertEqual(p[UDP].dport, 4739)
7953 self.assertEqual(p[IPFIX].observationDomainID,
7954 self.ipfix_domain_id)
7955 if p.haslayer(Template):
7956 ipfix.add_template(p.getlayer(Template))
7957 # verify events in data set
7958 for p in capture:
7959 if p.haslayer(Data):
7960 data = ipfix.decode_data_set(p.getlayer(Set))
Matus Fabiana7f8b222018-09-05 06:01:55 -07007961 self.verify_ipfix_max_fragments_ip6(data, 1,
Matus Fabiana431ad12018-01-04 04:03:14 -08007962 self.pg0.remote_ip6n)
7963
7964 def test_ipfix_bib_ses(self):
7965 """ IPFIX logging NAT64 BIB/session create and delete events """
7966 self.tcp_port_in = random.randint(1025, 65535)
7967 remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4,
7968 '64:ff9b::',
7969 96)
7970
7971 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7972 self.nat_addr_n)
7973 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7974 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7975 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
7976 src_address=self.pg3.local_ip4n,
7977 path_mtu=512,
7978 template_interval=10)
7979 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
7980 src_port=self.ipfix_src_port)
7981
7982 # Create
7983 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
7984 IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6) /
7985 TCP(sport=self.tcp_port_in, dport=25))
7986 self.pg0.add_stream(p)
7987 self.pg_enable_capture(self.pg_interfaces)
7988 self.pg_start()
7989 p = self.pg1.get_capture(1)
7990 self.tcp_port_out = p[0][TCP].sport
7991 self.vapi.cli("ipfix flush") # FIXME this should be an API call
7992 capture = self.pg3.get_capture(10)
7993 ipfix = IPFIXDecoder()
7994 # first load template
7995 for p in capture:
7996 self.assertTrue(p.haslayer(IPFIX))
7997 self.assertEqual(p[IP].src, self.pg3.local_ip4)
7998 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
7999 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
8000 self.assertEqual(p[UDP].dport, 4739)
8001 self.assertEqual(p[IPFIX].observationDomainID,
8002 self.ipfix_domain_id)
8003 if p.haslayer(Template):
8004 ipfix.add_template(p.getlayer(Template))
8005 # verify events in data set
8006 for p in capture:
8007 if p.haslayer(Data):
8008 data = ipfix.decode_data_set(p.getlayer(Set))
8009 if ord(data[0][230]) == 10:
8010 self.verify_ipfix_bib(data, 1, self.pg0.remote_ip6n)
8011 elif ord(data[0][230]) == 6:
8012 self.verify_ipfix_nat64_ses(data,
8013 1,
8014 self.pg0.remote_ip6n,
8015 self.pg1.remote_ip4,
8016 25)
8017 else:
8018 self.logger.error(ppp("Unexpected or invalid packet: ", p))
8019
8020 # Delete
8021 self.pg_enable_capture(self.pg_interfaces)
8022 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
8023 self.nat_addr_n,
8024 is_add=0)
8025 self.vapi.cli("ipfix flush") # FIXME this should be an API call
8026 capture = self.pg3.get_capture(2)
8027 # verify events in data set
8028 for p in capture:
8029 self.assertTrue(p.haslayer(IPFIX))
8030 self.assertEqual(p[IP].src, self.pg3.local_ip4)
8031 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
8032 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
8033 self.assertEqual(p[UDP].dport, 4739)
8034 self.assertEqual(p[IPFIX].observationDomainID,
8035 self.ipfix_domain_id)
8036 if p.haslayer(Data):
8037 data = ipfix.decode_data_set(p.getlayer(Set))
8038 if ord(data[0][230]) == 11:
8039 self.verify_ipfix_bib(data, 0, self.pg0.remote_ip6n)
8040 elif ord(data[0][230]) == 7:
8041 self.verify_ipfix_nat64_ses(data,
8042 0,
8043 self.pg0.remote_ip6n,
8044 self.pg1.remote_ip4,
8045 25)
8046 else:
8047 self.logger.error(ppp("Unexpected or invalid packet: ", p))
8048
Matus Fabianad1f3e12018-11-28 21:26:34 -08008049 def test_syslog_sess(self):
8050 """ Test syslog session creation and deletion """
8051 self.tcp_port_in = random.randint(1025, 65535)
8052 remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4,
8053 '64:ff9b::',
8054 96)
8055
8056 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
8057 self.nat_addr_n)
8058 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
8059 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
8060 self.vapi.syslog_set_filter(SYSLOG_SEVERITY.INFO)
8061 self.vapi.syslog_set_sender(self.pg3.remote_ip4n, self.pg3.local_ip4n)
8062
8063 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
8064 IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6) /
8065 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port))
8066 self.pg0.add_stream(p)
8067 self.pg_enable_capture(self.pg_interfaces)
8068 self.pg_start()
8069 p = self.pg1.get_capture(1)
8070 self.tcp_port_out = p[0][TCP].sport
8071 capture = self.pg3.get_capture(1)
8072 self.verify_syslog_sess(capture[0][Raw].load, is_ip6=True)
8073
8074 self.pg_enable_capture(self.pg_interfaces)
8075 self.pg_start()
8076 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
8077 self.nat_addr_n,
8078 is_add=0)
8079 capture = self.pg3.get_capture(1)
8080 self.verify_syslog_sess(capture[0][Raw].load, False, True)
8081
Matus Fabian06596c52017-06-06 04:53:28 -07008082 def nat64_get_ses_num(self):
8083 """
8084 Return number of active NAT64 sessions.
8085 """
Matus Fabianab9a59c2017-08-16 05:37:36 -07008086 st = self.vapi.nat64_st_dump()
8087 return len(st)
Matus Fabian06596c52017-06-06 04:53:28 -07008088
8089 def clear_nat64(self):
8090 """
8091 Clear NAT64 configuration.
8092 """
Matus Fabiana431ad12018-01-04 04:03:14 -08008093 self.vapi.nat_ipfix(enable=0, src_port=self.ipfix_src_port,
8094 domain_id=self.ipfix_domain_id)
8095 self.ipfix_src_port = 4739
8096 self.ipfix_domain_id = 1
8097
Matus Fabianad1f3e12018-11-28 21:26:34 -08008098 self.vapi.syslog_set_filter(SYSLOG_SEVERITY.EMERG)
8099
Matus Fabian878c6462018-08-23 00:33:35 -07008100 self.vapi.nat_set_timeouts()
Matus Fabian06596c52017-06-06 04:53:28 -07008101
8102 interfaces = self.vapi.nat64_interface_dump()
8103 for intf in interfaces:
Matus Fabian36ea2d62017-10-24 04:13:49 -07008104 if intf.is_inside > 1:
8105 self.vapi.nat64_add_del_interface(intf.sw_if_index,
8106 0,
8107 is_add=0)
Matus Fabian06596c52017-06-06 04:53:28 -07008108 self.vapi.nat64_add_del_interface(intf.sw_if_index,
8109 intf.is_inside,
8110 is_add=0)
8111
Matus Fabiana431ad12018-01-04 04:03:14 -08008112 bib = self.vapi.nat64_bib_dump(255)
Matus Fabian06596c52017-06-06 04:53:28 -07008113 for bibe in bib:
8114 if bibe.is_static:
8115 self.vapi.nat64_add_del_static_bib(bibe.i_addr,
8116 bibe.o_addr,
8117 bibe.i_port,
8118 bibe.o_port,
8119 bibe.proto,
8120 bibe.vrf_id,
8121 is_add=0)
8122
8123 adresses = self.vapi.nat64_pool_addr_dump()
8124 for addr in adresses:
8125 self.vapi.nat64_add_del_pool_addr_range(addr.address,
8126 addr.address,
Matus Fabian029f3d22017-06-15 02:28:50 -07008127 vrf_id=addr.vrf_id,
Matus Fabian06596c52017-06-06 04:53:28 -07008128 is_add=0)
8129
Matus Fabian428dc912017-06-21 06:15:18 -07008130 prefixes = self.vapi.nat64_prefix_dump()
8131 for prefix in prefixes:
8132 self.vapi.nat64_add_del_prefix(prefix.prefix,
8133 prefix.prefix_len,
8134 vrf_id=prefix.vrf_id,
8135 is_add=0)
8136
Matus Fabianfd0d5082018-12-18 01:08:51 -08008137 bibs = self.statistics.get_counter('/nat64/total-bibs')
8138 self.assertEqual(bibs[0][0], 0)
8139 sessions = self.statistics.get_counter('/nat64/total-sessions')
8140 self.assertEqual(sessions[0][0], 0)
8141
Matus Fabian06596c52017-06-06 04:53:28 -07008142 def tearDown(self):
8143 super(TestNAT64, self).tearDown()
8144 if not self.vpp_dead:
8145 self.logger.info(self.vapi.cli("show nat64 pool"))
8146 self.logger.info(self.vapi.cli("show nat64 interfaces"))
Matus Fabian428dc912017-06-21 06:15:18 -07008147 self.logger.info(self.vapi.cli("show nat64 prefix"))
Matus Fabianab9a59c2017-08-16 05:37:36 -07008148 self.logger.info(self.vapi.cli("show nat64 bib all"))
8149 self.logger.info(self.vapi.cli("show nat64 session table all"))
Matus Fabianefcd1e92017-08-15 06:59:19 -07008150 self.logger.info(self.vapi.cli("show nat virtual-reassembly"))
Matus Fabian06596c52017-06-06 04:53:28 -07008151 self.clear_nat64()
8152
Matus Fabian8ebe6252017-11-06 05:04:53 -08008153
8154class TestDSlite(MethodHolder):
8155 """ DS-Lite Test Cases """
8156
8157 @classmethod
8158 def setUpClass(cls):
8159 super(TestDSlite, cls).setUpClass()
8160
8161 try:
8162 cls.nat_addr = '10.0.0.3'
8163 cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
8164
Matus Fabianad1f3e12018-11-28 21:26:34 -08008165 cls.create_pg_interfaces(range(3))
Matus Fabian8ebe6252017-11-06 05:04:53 -08008166 cls.pg0.admin_up()
8167 cls.pg0.config_ip4()
8168 cls.pg0.resolve_arp()
8169 cls.pg1.admin_up()
8170 cls.pg1.config_ip6()
8171 cls.pg1.generate_remote_hosts(2)
8172 cls.pg1.configure_ipv6_neighbors()
Matus Fabianad1f3e12018-11-28 21:26:34 -08008173 cls.pg2.admin_up()
8174 cls.pg2.config_ip4()
8175 cls.pg2.resolve_arp()
Matus Fabian8ebe6252017-11-06 05:04:53 -08008176
8177 except Exception:
8178 super(TestDSlite, cls).tearDownClass()
8179 raise
8180
Matus Fabianad1f3e12018-11-28 21:26:34 -08008181 def verify_syslog_apmadd(self, data, isaddr, isport, xsaddr, xsport,
8182 sv6enc, proto):
8183 message = data.decode('utf-8')
8184 try:
8185 message = SyslogMessage.parse(message)
Paul Vinciguerrad7532092019-01-14 08:51:17 -08008186 except ParseError as e:
8187 self.logger.error(e)
8188 else:
Matus Fabianad1f3e12018-11-28 21:26:34 -08008189 self.assertEqual(message.severity, SyslogSeverity.info)
8190 self.assertEqual(message.appname, 'NAT')
8191 self.assertEqual(message.msgid, 'APMADD')
8192 sd_params = message.sd.get('napmap')
8193 self.assertTrue(sd_params is not None)
8194 self.assertEqual(sd_params.get('IATYP'), 'IPv4')
8195 self.assertEqual(sd_params.get('ISADDR'), isaddr)
8196 self.assertEqual(sd_params.get('ISPORT'), "%d" % isport)
8197 self.assertEqual(sd_params.get('XATYP'), 'IPv4')
8198 self.assertEqual(sd_params.get('XSADDR'), xsaddr)
8199 self.assertEqual(sd_params.get('XSPORT'), "%d" % xsport)
8200 self.assertEqual(sd_params.get('PROTO'), "%d" % proto)
8201 self.assertTrue(sd_params.get('SSUBIX') is not None)
8202 self.assertEqual(sd_params.get('SV6ENC'), sv6enc)
Matus Fabianad1f3e12018-11-28 21:26:34 -08008203
Matus Fabian8ebe6252017-11-06 05:04:53 -08008204 def test_dslite(self):
8205 """ Test DS-Lite """
Matus Fabian69ce30d2018-08-22 01:27:10 -07008206 nat_config = self.vapi.nat_show_config()
8207 self.assertEqual(0, nat_config.dslite_ce)
8208
Matus Fabian8ebe6252017-11-06 05:04:53 -08008209 self.vapi.dslite_add_del_pool_addr_range(self.nat_addr_n,
8210 self.nat_addr_n)
8211 aftr_ip4 = '192.0.0.1'
8212 aftr_ip4_n = socket.inet_pton(socket.AF_INET, aftr_ip4)
8213 aftr_ip6 = '2001:db8:85a3::8a2e:370:1'
8214 aftr_ip6_n = socket.inet_pton(socket.AF_INET6, aftr_ip6)
8215 self.vapi.dslite_set_aftr_addr(aftr_ip6_n, aftr_ip4_n)
Matus Fabianad1f3e12018-11-28 21:26:34 -08008216 self.vapi.syslog_set_sender(self.pg2.remote_ip4n, self.pg2.local_ip4n)
Matus Fabian8ebe6252017-11-06 05:04:53 -08008217
8218 # UDP
8219 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8220 IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[0].ip6) /
8221 IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
8222 UDP(sport=20000, dport=10000))
8223 self.pg1.add_stream(p)
8224 self.pg_enable_capture(self.pg_interfaces)
8225 self.pg_start()
8226 capture = self.pg0.get_capture(1)
8227 capture = capture[0]
8228 self.assertFalse(capture.haslayer(IPv6))
8229 self.assertEqual(capture[IP].src, self.nat_addr)
8230 self.assertEqual(capture[IP].dst, self.pg0.remote_ip4)
8231 self.assertNotEqual(capture[UDP].sport, 20000)
8232 self.assertEqual(capture[UDP].dport, 10000)
Klement Sekerad81ae412018-05-16 10:52:54 +02008233 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08008234 out_port = capture[UDP].sport
Matus Fabianad1f3e12018-11-28 21:26:34 -08008235 capture = self.pg2.get_capture(1)
8236 self.verify_syslog_apmadd(capture[0][Raw].load, '192.168.1.1',
8237 20000, self.nat_addr, out_port,
8238 self.pg1.remote_hosts[0].ip6, IP_PROTOS.udp)
Matus Fabian8ebe6252017-11-06 05:04:53 -08008239
8240 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8241 IP(dst=self.nat_addr, src=self.pg0.remote_ip4) /
8242 UDP(sport=10000, dport=out_port))
8243 self.pg0.add_stream(p)
8244 self.pg_enable_capture(self.pg_interfaces)
8245 self.pg_start()
8246 capture = self.pg1.get_capture(1)
8247 capture = capture[0]
8248 self.assertEqual(capture[IPv6].src, aftr_ip6)
8249 self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[0].ip6)
8250 self.assertEqual(capture[IP].src, self.pg0.remote_ip4)
8251 self.assertEqual(capture[IP].dst, '192.168.1.1')
8252 self.assertEqual(capture[UDP].sport, 10000)
8253 self.assertEqual(capture[UDP].dport, 20000)
Klement Sekerad81ae412018-05-16 10:52:54 +02008254 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08008255
8256 # TCP
8257 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8258 IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[1].ip6) /
8259 IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
8260 TCP(sport=20001, dport=10001))
8261 self.pg1.add_stream(p)
8262 self.pg_enable_capture(self.pg_interfaces)
8263 self.pg_start()
8264 capture = self.pg0.get_capture(1)
8265 capture = capture[0]
8266 self.assertFalse(capture.haslayer(IPv6))
8267 self.assertEqual(capture[IP].src, self.nat_addr)
8268 self.assertEqual(capture[IP].dst, self.pg0.remote_ip4)
8269 self.assertNotEqual(capture[TCP].sport, 20001)
8270 self.assertEqual(capture[TCP].dport, 10001)
Klement Sekerad81ae412018-05-16 10:52:54 +02008271 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08008272 out_port = capture[TCP].sport
8273
8274 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8275 IP(dst=self.nat_addr, src=self.pg0.remote_ip4) /
8276 TCP(sport=10001, dport=out_port))
8277 self.pg0.add_stream(p)
8278 self.pg_enable_capture(self.pg_interfaces)
8279 self.pg_start()
8280 capture = self.pg1.get_capture(1)
8281 capture = capture[0]
8282 self.assertEqual(capture[IPv6].src, aftr_ip6)
8283 self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6)
8284 self.assertEqual(capture[IP].src, self.pg0.remote_ip4)
8285 self.assertEqual(capture[IP].dst, '192.168.1.1')
8286 self.assertEqual(capture[TCP].sport, 10001)
8287 self.assertEqual(capture[TCP].dport, 20001)
Klement Sekerad81ae412018-05-16 10:52:54 +02008288 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08008289
8290 # ICMP
8291 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8292 IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[1].ip6) /
8293 IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
8294 ICMP(id=4000, type='echo-request'))
8295 self.pg1.add_stream(p)
8296 self.pg_enable_capture(self.pg_interfaces)
8297 self.pg_start()
8298 capture = self.pg0.get_capture(1)
8299 capture = capture[0]
8300 self.assertFalse(capture.haslayer(IPv6))
8301 self.assertEqual(capture[IP].src, self.nat_addr)
8302 self.assertEqual(capture[IP].dst, self.pg0.remote_ip4)
8303 self.assertNotEqual(capture[ICMP].id, 4000)
Klement Sekerad81ae412018-05-16 10:52:54 +02008304 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08008305 out_id = capture[ICMP].id
8306
8307 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8308 IP(dst=self.nat_addr, src=self.pg0.remote_ip4) /
8309 ICMP(id=out_id, type='echo-reply'))
8310 self.pg0.add_stream(p)
8311 self.pg_enable_capture(self.pg_interfaces)
8312 self.pg_start()
8313 capture = self.pg1.get_capture(1)
8314 capture = capture[0]
8315 self.assertEqual(capture[IPv6].src, aftr_ip6)
8316 self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6)
8317 self.assertEqual(capture[IP].src, self.pg0.remote_ip4)
8318 self.assertEqual(capture[IP].dst, '192.168.1.1')
8319 self.assertEqual(capture[ICMP].id, 4000)
Klement Sekerad81ae412018-05-16 10:52:54 +02008320 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08008321
Matus Fabian331acc62017-12-08 03:38:51 -08008322 # ping DS-Lite AFTR tunnel endpoint address
8323 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8324 IPv6(src=self.pg1.remote_hosts[1].ip6, dst=aftr_ip6) /
8325 ICMPv6EchoRequest())
8326 self.pg1.add_stream(p)
8327 self.pg_enable_capture(self.pg_interfaces)
8328 self.pg_start()
8329 capture = self.pg1.get_capture(1)
Matus Fabian331acc62017-12-08 03:38:51 -08008330 capture = capture[0]
8331 self.assertEqual(capture[IPv6].src, aftr_ip6)
8332 self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6)
8333 self.assertTrue(capture.haslayer(ICMPv6EchoReply))
8334
Matus Fabianfd0d5082018-12-18 01:08:51 -08008335 b4s = self.statistics.get_counter('/dslite/total-b4s')
8336 self.assertEqual(b4s[0][0], 2)
8337 sessions = self.statistics.get_counter('/dslite/total-sessions')
8338 self.assertEqual(sessions[0][0], 3)
8339
Matus Fabian8ebe6252017-11-06 05:04:53 -08008340 def tearDown(self):
8341 super(TestDSlite, self).tearDown()
8342 if not self.vpp_dead:
8343 self.logger.info(self.vapi.cli("show dslite pool"))
8344 self.logger.info(
8345 self.vapi.cli("show dslite aftr-tunnel-endpoint-address"))
8346 self.logger.info(self.vapi.cli("show dslite sessions"))
8347
Juraj Slobodac5c6a332018-01-09 16:08:32 +01008348
8349class TestDSliteCE(MethodHolder):
8350 """ DS-Lite CE Test Cases """
8351
8352 @classmethod
8353 def setUpConstants(cls):
8354 super(TestDSliteCE, cls).setUpConstants()
8355 cls.vpp_cmdline.extend(["nat", "{", "dslite ce", "}"])
8356
8357 @classmethod
8358 def setUpClass(cls):
8359 super(TestDSliteCE, cls).setUpClass()
8360
8361 try:
8362 cls.create_pg_interfaces(range(2))
8363 cls.pg0.admin_up()
8364 cls.pg0.config_ip4()
8365 cls.pg0.resolve_arp()
8366 cls.pg1.admin_up()
8367 cls.pg1.config_ip6()
8368 cls.pg1.generate_remote_hosts(1)
8369 cls.pg1.configure_ipv6_neighbors()
8370
8371 except Exception:
8372 super(TestDSliteCE, cls).tearDownClass()
8373 raise
8374
8375 def test_dslite_ce(self):
8376 """ Test DS-Lite CE """
8377
Matus Fabian69ce30d2018-08-22 01:27:10 -07008378 nat_config = self.vapi.nat_show_config()
8379 self.assertEqual(1, nat_config.dslite_ce)
8380
Juraj Slobodac5c6a332018-01-09 16:08:32 +01008381 b4_ip4 = '192.0.0.2'
8382 b4_ip4_n = socket.inet_pton(socket.AF_INET, b4_ip4)
8383 b4_ip6 = '2001:db8:62aa::375e:f4c1:1'
8384 b4_ip6_n = socket.inet_pton(socket.AF_INET6, b4_ip6)
8385 self.vapi.dslite_set_b4_addr(b4_ip6_n, b4_ip4_n)
8386
8387 aftr_ip4 = '192.0.0.1'
8388 aftr_ip4_n = socket.inet_pton(socket.AF_INET, aftr_ip4)
8389 aftr_ip6 = '2001:db8:85a3::8a2e:370:1'
8390 aftr_ip6_n = socket.inet_pton(socket.AF_INET6, aftr_ip6)
8391 self.vapi.dslite_set_aftr_addr(aftr_ip6_n, aftr_ip4_n)
8392
8393 self.vapi.ip_add_del_route(dst_address=aftr_ip6_n,
8394 dst_address_length=128,
8395 next_hop_address=self.pg1.remote_ip6n,
8396 next_hop_sw_if_index=self.pg1.sw_if_index,
8397 is_ipv6=1)
8398
8399 # UDP encapsulation
8400 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8401 IP(dst=self.pg1.remote_ip4, src=self.pg0.remote_ip4) /
8402 UDP(sport=10000, dport=20000))
8403 self.pg0.add_stream(p)
8404 self.pg_enable_capture(self.pg_interfaces)
8405 self.pg_start()
8406 capture = self.pg1.get_capture(1)
8407 capture = capture[0]
8408 self.assertEqual(capture[IPv6].src, b4_ip6)
8409 self.assertEqual(capture[IPv6].dst, aftr_ip6)
8410 self.assertEqual(capture[IP].src, self.pg0.remote_ip4)
8411 self.assertEqual(capture[IP].dst, self.pg1.remote_ip4)
8412 self.assertEqual(capture[UDP].sport, 10000)
8413 self.assertEqual(capture[UDP].dport, 20000)
Klement Sekerad81ae412018-05-16 10:52:54 +02008414 self.assert_packet_checksums_valid(capture)
Juraj Slobodac5c6a332018-01-09 16:08:32 +01008415
8416 # UDP decapsulation
8417 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8418 IPv6(dst=b4_ip6, src=aftr_ip6) /
8419 IP(dst=self.pg0.remote_ip4, src=self.pg1.remote_ip4) /
8420 UDP(sport=20000, dport=10000))
8421 self.pg1.add_stream(p)
8422 self.pg_enable_capture(self.pg_interfaces)
8423 self.pg_start()
8424 capture = self.pg0.get_capture(1)
8425 capture = capture[0]
8426 self.assertFalse(capture.haslayer(IPv6))
8427 self.assertEqual(capture[IP].src, self.pg1.remote_ip4)
8428 self.assertEqual(capture[IP].dst, self.pg0.remote_ip4)
8429 self.assertEqual(capture[UDP].sport, 20000)
8430 self.assertEqual(capture[UDP].dport, 10000)
Klement Sekerad81ae412018-05-16 10:52:54 +02008431 self.assert_packet_checksums_valid(capture)
Juraj Slobodac5c6a332018-01-09 16:08:32 +01008432
8433 # ping DS-Lite B4 tunnel endpoint address
8434 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8435 IPv6(src=self.pg1.remote_hosts[0].ip6, dst=b4_ip6) /
8436 ICMPv6EchoRequest())
8437 self.pg1.add_stream(p)
8438 self.pg_enable_capture(self.pg_interfaces)
8439 self.pg_start()
8440 capture = self.pg1.get_capture(1)
Juraj Slobodac5c6a332018-01-09 16:08:32 +01008441 capture = capture[0]
8442 self.assertEqual(capture[IPv6].src, b4_ip6)
8443 self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[0].ip6)
8444 self.assertTrue(capture.haslayer(ICMPv6EchoReply))
8445
8446 def tearDown(self):
8447 super(TestDSliteCE, self).tearDown()
8448 if not self.vpp_dead:
8449 self.logger.info(
8450 self.vapi.cli("show dslite aftr-tunnel-endpoint-address"))
8451 self.logger.info(
8452 self.vapi.cli("show dslite b4-tunnel-endpoint-address"))
8453
Matus Fabianf2a23cc2018-01-22 03:41:53 -08008454
8455class TestNAT66(MethodHolder):
8456 """ NAT66 Test Cases """
8457
8458 @classmethod
8459 def setUpClass(cls):
8460 super(TestNAT66, cls).setUpClass()
8461
8462 try:
8463 cls.nat_addr = 'fd01:ff::2'
8464 cls.nat_addr_n = socket.inet_pton(socket.AF_INET6, cls.nat_addr)
8465
8466 cls.create_pg_interfaces(range(2))
8467 cls.interfaces = list(cls.pg_interfaces)
8468
8469 for i in cls.interfaces:
8470 i.admin_up()
8471 i.config_ip6()
8472 i.configure_ipv6_neighbors()
8473
8474 except Exception:
8475 super(TestNAT66, cls).tearDownClass()
8476 raise
8477
8478 def test_static(self):
8479 """ 1:1 NAT66 test """
8480 self.vapi.nat66_add_del_interface(self.pg0.sw_if_index)
8481 self.vapi.nat66_add_del_interface(self.pg1.sw_if_index, is_inside=0)
8482 self.vapi.nat66_add_del_static_mapping(self.pg0.remote_ip6n,
8483 self.nat_addr_n)
8484
8485 # in2out
8486 pkts = []
8487 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8488 IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
8489 TCP())
8490 pkts.append(p)
8491 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8492 IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
8493 UDP())
8494 pkts.append(p)
8495 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8496 IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
8497 ICMPv6EchoRequest())
8498 pkts.append(p)
8499 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8500 IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
8501 GRE() / IP() / TCP())
8502 pkts.append(p)
8503 self.pg0.add_stream(pkts)
8504 self.pg_enable_capture(self.pg_interfaces)
8505 self.pg_start()
8506 capture = self.pg1.get_capture(len(pkts))
8507 for packet in capture:
8508 try:
8509 self.assertEqual(packet[IPv6].src, self.nat_addr)
8510 self.assertEqual(packet[IPv6].dst, self.pg1.remote_ip6)
Klement Sekerad81ae412018-05-16 10:52:54 +02008511 self.assert_packet_checksums_valid(packet)
Matus Fabianf2a23cc2018-01-22 03:41:53 -08008512 except:
8513 self.logger.error(ppp("Unexpected or invalid packet:", packet))
8514 raise
8515
8516 # out2in
8517 pkts = []
8518 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8519 IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
8520 TCP())
8521 pkts.append(p)
8522 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8523 IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
8524 UDP())
8525 pkts.append(p)
8526 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8527 IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
8528 ICMPv6EchoReply())
8529 pkts.append(p)
8530 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8531 IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
8532 GRE() / IP() / TCP())
8533 pkts.append(p)
8534 self.pg1.add_stream(pkts)
8535 self.pg_enable_capture(self.pg_interfaces)
8536 self.pg_start()
8537 capture = self.pg0.get_capture(len(pkts))
8538 for packet in capture:
8539 try:
8540 self.assertEqual(packet[IPv6].src, self.pg1.remote_ip6)
8541 self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6)
Klement Sekerad81ae412018-05-16 10:52:54 +02008542 self.assert_packet_checksums_valid(packet)
Matus Fabianf2a23cc2018-01-22 03:41:53 -08008543 except:
8544 self.logger.error(ppp("Unexpected or invalid packet:", packet))
8545 raise
8546
8547 sm = self.vapi.nat66_static_mapping_dump()
8548 self.assertEqual(len(sm), 1)
8549 self.assertEqual(sm[0].total_pkts, 8)
8550
Juraj Sloboda9341e342018-04-13 12:00:46 +02008551 def test_check_no_translate(self):
8552 """ NAT66 translate only when egress interface is outside interface """
8553 self.vapi.nat66_add_del_interface(self.pg0.sw_if_index)
8554 self.vapi.nat66_add_del_interface(self.pg1.sw_if_index)
8555 self.vapi.nat66_add_del_static_mapping(self.pg0.remote_ip6n,
8556 self.nat_addr_n)
8557
8558 # in2out
8559 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8560 IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
8561 UDP())
8562 self.pg0.add_stream([p])
8563 self.pg_enable_capture(self.pg_interfaces)
8564 self.pg_start()
8565 capture = self.pg1.get_capture(1)
8566 packet = capture[0]
8567 try:
8568 self.assertEqual(packet[IPv6].src, self.pg0.remote_ip6)
8569 self.assertEqual(packet[IPv6].dst, self.pg1.remote_ip6)
8570 except:
8571 self.logger.error(ppp("Unexpected or invalid packet:", packet))
8572 raise
8573
Matus Fabianf2a23cc2018-01-22 03:41:53 -08008574 def clear_nat66(self):
8575 """
8576 Clear NAT66 configuration.
8577 """
8578 interfaces = self.vapi.nat66_interface_dump()
8579 for intf in interfaces:
8580 self.vapi.nat66_add_del_interface(intf.sw_if_index,
8581 intf.is_inside,
8582 is_add=0)
8583
8584 static_mappings = self.vapi.nat66_static_mapping_dump()
8585 for sm in static_mappings:
8586 self.vapi.nat66_add_del_static_mapping(sm.local_ip_address,
8587 sm.external_ip_address,
8588 sm.vrf_id,
8589 is_add=0)
8590
8591 def tearDown(self):
8592 super(TestNAT66, self).tearDown()
8593 if not self.vpp_dead:
8594 self.logger.info(self.vapi.cli("show nat66 interfaces"))
8595 self.logger.info(self.vapi.cli("show nat66 static mappings"))
8596 self.clear_nat66()
8597
Klement Sekerad81ae412018-05-16 10:52:54 +02008598
Matus Fabiande886752016-12-07 03:38:19 -08008599if __name__ == '__main__':
8600 unittest.main(testRunner=VppTestRunner)