blob: d3849da4321d8defb2f5afcd3f31188bef7d252c [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 StringIO
7import random
Matus Fabiande886752016-12-07 03:38:19 -08008
Martin Gálik977c1cb2017-03-30 23:21:51 -07009from framework import VppTestCase, VppTestRunner, running_extended_tests
Matus Fabiande886752016-12-07 03:38:19 -080010from scapy.layers.inet import IP, TCP, UDP, ICMP
Juraj Slobodab33f4132017-02-08 23:54:21 -080011from scapy.layers.inet import IPerror, TCPerror, UDPerror, ICMPerror
Juraj Slobodac746a152018-07-09 02:36:37 +020012from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest, ICMPv6EchoReply, \
13 ICMPv6ND_NS, ICMPv6ND_NA, ICMPv6NDOptDstLLAddr
Matus Fabianefcd1e92017-08-15 06:59:19 -070014from scapy.layers.inet6 import ICMPv6DestUnreach, IPerror6, IPv6ExtHdrFragment
Matus Fabian328dbc82017-06-19 04:28:04 -070015from scapy.layers.l2 import Ether, ARP, GRE
Matus Fabianeea28d72017-01-13 04:15:54 -080016from scapy.data import IP_PROTOS
Matus Fabianefcd1e92017-08-15 06:59:19 -070017from scapy.packet import bind_layers, Raw
18from scapy.all import fragment6
Klement Sekera9225dee2016-12-12 08:36:58 +010019from util import ppp
Matus Fabianeea28d72017-01-13 04:15:54 -080020from ipfix import IPFIX, Set, Template, Data, IPFIXDecoder
Martin Gálik977c1cb2017-03-30 23:21:51 -070021from time import sleep
Matus Fabian704018c2017-09-04 02:17:18 -070022from util import ip4_range
Ole Troan895b6e82017-10-20 13:28:20 +020023from util import mactobinary
Matus Fabiande886752016-12-07 03:38:19 -080024
25
Martin Gálikd7f75cd2017-03-27 06:02:47 -070026class MethodHolder(VppTestCase):
Matus Fabian2ba92e32017-08-21 07:05:03 -070027 """ NAT create capture and verify method holder """
Matus Fabiande886752016-12-07 03:38:19 -080028
Matus Fabiana6110b62018-06-13 05:39:07 -070029 def clear_nat44(self):
30 """
31 Clear NAT44 configuration.
32 """
33 if hasattr(self, 'pg7') and hasattr(self, 'pg8'):
34 # I found no elegant way to do this
35 self.vapi.ip_add_del_route(
36 dst_address=self.pg7.remote_ip4n,
37 dst_address_length=32,
38 next_hop_address=self.pg7.remote_ip4n,
39 next_hop_sw_if_index=self.pg7.sw_if_index,
40 is_add=0)
41 self.vapi.ip_add_del_route(
42 dst_address=self.pg8.remote_ip4n,
43 dst_address_length=32,
44 next_hop_address=self.pg8.remote_ip4n,
45 next_hop_sw_if_index=self.pg8.sw_if_index,
46 is_add=0)
47
48 for intf in [self.pg7, self.pg8]:
49 neighbors = self.vapi.ip_neighbor_dump(intf.sw_if_index)
50 for n in neighbors:
51 self.vapi.ip_neighbor_add_del(intf.sw_if_index,
52 n.mac_address,
53 n.ip_address,
54 is_add=0)
55
56 if self.pg7.has_ip4_config:
57 self.pg7.unconfig_ip4()
58
59 self.vapi.nat44_forwarding_enable_disable(0)
60
61 interfaces = self.vapi.nat44_interface_addr_dump()
62 for intf in interfaces:
63 self.vapi.nat44_add_interface_addr(intf.sw_if_index,
64 twice_nat=intf.twice_nat,
65 is_add=0)
66
67 self.vapi.nat_ipfix(enable=0, src_port=self.ipfix_src_port,
68 domain_id=self.ipfix_domain_id)
69 self.ipfix_src_port = 4739
70 self.ipfix_domain_id = 1
71
72 interfaces = self.vapi.nat44_interface_dump()
73 for intf in interfaces:
74 if intf.is_inside > 1:
75 self.vapi.nat44_interface_add_del_feature(intf.sw_if_index,
76 0,
77 is_add=0)
78 self.vapi.nat44_interface_add_del_feature(intf.sw_if_index,
79 intf.is_inside,
80 is_add=0)
81
82 interfaces = self.vapi.nat44_interface_output_feature_dump()
83 for intf in interfaces:
84 self.vapi.nat44_interface_add_del_output_feature(intf.sw_if_index,
85 intf.is_inside,
86 is_add=0)
87
88 static_mappings = self.vapi.nat44_static_mapping_dump()
89 for sm in static_mappings:
90 self.vapi.nat44_add_del_static_mapping(
91 sm.local_ip_address,
92 sm.external_ip_address,
93 local_port=sm.local_port,
94 external_port=sm.external_port,
95 addr_only=sm.addr_only,
96 vrf_id=sm.vrf_id,
97 protocol=sm.protocol,
98 twice_nat=sm.twice_nat,
99 self_twice_nat=sm.self_twice_nat,
100 out2in_only=sm.out2in_only,
101 tag=sm.tag,
102 external_sw_if_index=sm.external_sw_if_index,
103 is_add=0)
104
105 lb_static_mappings = self.vapi.nat44_lb_static_mapping_dump()
106 for lb_sm in lb_static_mappings:
107 self.vapi.nat44_add_del_lb_static_mapping(
108 lb_sm.external_addr,
109 lb_sm.external_port,
110 lb_sm.protocol,
Matus Fabiana6110b62018-06-13 05:39:07 -0700111 twice_nat=lb_sm.twice_nat,
112 self_twice_nat=lb_sm.self_twice_nat,
113 out2in_only=lb_sm.out2in_only,
114 tag=lb_sm.tag,
115 is_add=0,
116 local_num=0,
117 locals=[])
118
119 identity_mappings = self.vapi.nat44_identity_mapping_dump()
120 for id_m in identity_mappings:
121 self.vapi.nat44_add_del_identity_mapping(
122 addr_only=id_m.addr_only,
123 ip=id_m.ip_address,
124 port=id_m.port,
125 sw_if_index=id_m.sw_if_index,
126 vrf_id=id_m.vrf_id,
127 protocol=id_m.protocol,
128 is_add=0)
129
130 adresses = self.vapi.nat44_address_dump()
131 for addr in adresses:
132 self.vapi.nat44_add_del_address_range(addr.ip_address,
133 addr.ip_address,
134 twice_nat=addr.twice_nat,
135 is_add=0)
136
137 self.vapi.nat_set_reass()
138 self.vapi.nat_set_reass(is_ip6=1)
Matus Fabian68ba8802018-08-08 05:52:47 -0700139 self.verify_no_nat44_user()
Matus Fabian878c6462018-08-23 00:33:35 -0700140 self.vapi.nat_set_timeouts()
Matus Fabian5d28c7a2018-09-04 03:55:45 -0700141 self.vapi.nat_set_addr_and_port_alloc_alg()
Matus Fabianbb4e0222018-09-13 02:36:25 -0700142 self.vapi.nat_set_mss_clamping()
Matus Fabiana6110b62018-06-13 05:39:07 -0700143
144 def nat44_add_static_mapping(self, local_ip, external_ip='0.0.0.0',
145 local_port=0, external_port=0, vrf_id=0,
146 is_add=1, external_sw_if_index=0xFFFFFFFF,
147 proto=0, twice_nat=0, self_twice_nat=0,
148 out2in_only=0, tag=""):
149 """
150 Add/delete NAT44 static mapping
151
152 :param local_ip: Local IP address
153 :param external_ip: External IP address
154 :param local_port: Local port number (Optional)
155 :param external_port: External port number (Optional)
156 :param vrf_id: VRF ID (Default 0)
157 :param is_add: 1 if add, 0 if delete (Default add)
158 :param external_sw_if_index: External interface instead of IP address
159 :param proto: IP protocol (Mandatory if port specified)
160 :param twice_nat: 1 if translate external host address and port
161 :param self_twice_nat: 1 if translate external host address and port
162 whenever external host address equals
163 local address of internal host
164 :param out2in_only: if 1 rule is matching only out2in direction
165 :param tag: Opaque string tag
166 """
167 addr_only = 1
168 if local_port and external_port:
169 addr_only = 0
170 l_ip = socket.inet_pton(socket.AF_INET, local_ip)
171 e_ip = socket.inet_pton(socket.AF_INET, external_ip)
172 self.vapi.nat44_add_del_static_mapping(
173 l_ip,
174 e_ip,
175 external_sw_if_index,
176 local_port,
177 external_port,
178 addr_only,
179 vrf_id,
180 proto,
181 twice_nat,
182 self_twice_nat,
183 out2in_only,
184 tag,
185 is_add)
186
187 def nat44_add_address(self, ip, is_add=1, vrf_id=0xFFFFFFFF, twice_nat=0):
188 """
189 Add/delete NAT44 address
190
191 :param ip: IP address
192 :param is_add: 1 if add, 0 if delete (Default add)
193 :param twice_nat: twice NAT address for extenal hosts
194 """
195 nat_addr = socket.inet_pton(socket.AF_INET, ip)
196 self.vapi.nat44_add_del_address_range(nat_addr, nat_addr, is_add,
197 vrf_id=vrf_id,
198 twice_nat=twice_nat)
199
Juraj Slobodacba69362017-12-19 02:09:32 +0100200 def create_stream_in(self, in_if, out_if, dst_ip=None, ttl=64):
Matus Fabiande886752016-12-07 03:38:19 -0800201 """
202 Create packet stream for inside network
203
204 :param in_if: Inside interface
205 :param out_if: Outside interface
Juraj Slobodacba69362017-12-19 02:09:32 +0100206 :param dst_ip: Destination address
Juraj Slobodab33f4132017-02-08 23:54:21 -0800207 :param ttl: TTL of generated packets
Matus Fabiande886752016-12-07 03:38:19 -0800208 """
Juraj Slobodacba69362017-12-19 02:09:32 +0100209 if dst_ip is None:
210 dst_ip = out_if.remote_ip4
211
Matus Fabiande886752016-12-07 03:38:19 -0800212 pkts = []
213 # TCP
214 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
Juraj Slobodacba69362017-12-19 02:09:32 +0100215 IP(src=in_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Matus Fabian06596c52017-06-06 04:53:28 -0700216 TCP(sport=self.tcp_port_in, dport=20))
Matus Fabiande886752016-12-07 03:38:19 -0800217 pkts.append(p)
218
219 # UDP
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 UDP(sport=self.udp_port_in, dport=20))
Matus Fabiande886752016-12-07 03:38:19 -0800223 pkts.append(p)
224
225 # ICMP
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 Fabiande886752016-12-07 03:38:19 -0800228 ICMP(id=self.icmp_id_in, type='echo-request'))
229 pkts.append(p)
230
231 return pkts
232
Matus Fabian428dc912017-06-21 06:15:18 -0700233 def compose_ip6(self, ip4, pref, plen):
234 """
235 Compose IPv4-embedded IPv6 addresses
236
237 :param ip4: IPv4 address
238 :param pref: IPv6 prefix
239 :param plen: IPv6 prefix length
240 :returns: IPv4-embedded IPv6 addresses
241 """
242 pref_n = list(socket.inet_pton(socket.AF_INET6, pref))
243 ip4_n = list(socket.inet_pton(socket.AF_INET, ip4))
244 if plen == 32:
245 pref_n[4] = ip4_n[0]
246 pref_n[5] = ip4_n[1]
247 pref_n[6] = ip4_n[2]
248 pref_n[7] = ip4_n[3]
249 elif plen == 40:
250 pref_n[5] = ip4_n[0]
251 pref_n[6] = ip4_n[1]
252 pref_n[7] = ip4_n[2]
253 pref_n[9] = ip4_n[3]
254 elif plen == 48:
255 pref_n[6] = ip4_n[0]
256 pref_n[7] = ip4_n[1]
257 pref_n[9] = ip4_n[2]
258 pref_n[10] = ip4_n[3]
259 elif plen == 56:
260 pref_n[7] = ip4_n[0]
261 pref_n[9] = ip4_n[1]
262 pref_n[10] = ip4_n[2]
263 pref_n[11] = ip4_n[3]
264 elif plen == 64:
265 pref_n[9] = ip4_n[0]
266 pref_n[10] = ip4_n[1]
267 pref_n[11] = ip4_n[2]
268 pref_n[12] = ip4_n[3]
269 elif plen == 96:
270 pref_n[12] = ip4_n[0]
271 pref_n[13] = ip4_n[1]
272 pref_n[14] = ip4_n[2]
273 pref_n[15] = ip4_n[3]
274 return socket.inet_ntop(socket.AF_INET6, ''.join(pref_n))
275
Juraj Slobodacba69362017-12-19 02:09:32 +0100276 def extract_ip4(self, ip6, plen):
277 """
278 Extract IPv4 address embedded in IPv6 addresses
279
280 :param ip6: IPv6 address
281 :param plen: IPv6 prefix length
282 :returns: extracted IPv4 address
283 """
284 ip6_n = list(socket.inet_pton(socket.AF_INET6, ip6))
285 ip4_n = [None] * 4
286 if plen == 32:
287 ip4_n[0] = ip6_n[4]
288 ip4_n[1] = ip6_n[5]
289 ip4_n[2] = ip6_n[6]
290 ip4_n[3] = ip6_n[7]
291 elif plen == 40:
292 ip4_n[0] = ip6_n[5]
293 ip4_n[1] = ip6_n[6]
294 ip4_n[2] = ip6_n[7]
295 ip4_n[3] = ip6_n[9]
296 elif plen == 48:
297 ip4_n[0] = ip6_n[6]
298 ip4_n[1] = ip6_n[7]
299 ip4_n[2] = ip6_n[9]
300 ip4_n[3] = ip6_n[10]
301 elif plen == 56:
302 ip4_n[0] = ip6_n[7]
303 ip4_n[1] = ip6_n[9]
304 ip4_n[2] = ip6_n[10]
305 ip4_n[3] = ip6_n[11]
306 elif plen == 64:
307 ip4_n[0] = ip6_n[9]
308 ip4_n[1] = ip6_n[10]
309 ip4_n[2] = ip6_n[11]
310 ip4_n[3] = ip6_n[12]
311 elif plen == 96:
312 ip4_n[0] = ip6_n[12]
313 ip4_n[1] = ip6_n[13]
314 ip4_n[2] = ip6_n[14]
315 ip4_n[3] = ip6_n[15]
316 return socket.inet_ntop(socket.AF_INET, ''.join(ip4_n))
317
Matus Fabian428dc912017-06-21 06:15:18 -0700318 def create_stream_in_ip6(self, in_if, out_if, hlim=64, pref=None, plen=0):
Matus Fabian06596c52017-06-06 04:53:28 -0700319 """
320 Create IPv6 packet stream for inside network
321
322 :param in_if: Inside interface
323 :param out_if: Outside interface
324 :param ttl: Hop Limit of generated packets
Matus Fabian428dc912017-06-21 06:15:18 -0700325 :param pref: NAT64 prefix
326 :param plen: NAT64 prefix length
Matus Fabian06596c52017-06-06 04:53:28 -0700327 """
328 pkts = []
Matus Fabian428dc912017-06-21 06:15:18 -0700329 if pref is None:
330 dst = ''.join(['64:ff9b::', out_if.remote_ip4])
331 else:
332 dst = self.compose_ip6(out_if.remote_ip4, pref, plen)
333
Matus Fabian06596c52017-06-06 04:53:28 -0700334 # TCP
335 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
336 IPv6(src=in_if.remote_ip6, dst=dst, hlim=hlim) /
337 TCP(sport=self.tcp_port_in, dport=20))
338 pkts.append(p)
339
340 # UDP
341 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
342 IPv6(src=in_if.remote_ip6, dst=dst, hlim=hlim) /
343 UDP(sport=self.udp_port_in, dport=20))
344 pkts.append(p)
345
346 # ICMP
347 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
348 IPv6(src=in_if.remote_ip6, dst=dst, hlim=hlim) /
349 ICMPv6EchoRequest(id=self.icmp_id_in))
350 pkts.append(p)
351
352 return pkts
353
Juraj Sloboda7b929792017-11-23 13:20:48 +0100354 def create_stream_out(self, out_if, dst_ip=None, ttl=64,
355 use_inside_ports=False):
Matus Fabiande886752016-12-07 03:38:19 -0800356 """
357 Create packet stream for outside network
358
359 :param out_if: Outside interface
Matus Fabian2ba92e32017-08-21 07:05:03 -0700360 :param dst_ip: Destination IP address (Default use global NAT address)
Juraj Slobodab33f4132017-02-08 23:54:21 -0800361 :param ttl: TTL of generated packets
Juraj Sloboda7b929792017-11-23 13:20:48 +0100362 :param use_inside_ports: Use inside NAT ports as destination ports
363 instead of outside ports
Matus Fabiande886752016-12-07 03:38:19 -0800364 """
365 if dst_ip is None:
Matus Fabian2ba92e32017-08-21 07:05:03 -0700366 dst_ip = self.nat_addr
Juraj Sloboda7b929792017-11-23 13:20:48 +0100367 if not use_inside_ports:
368 tcp_port = self.tcp_port_out
369 udp_port = self.udp_port_out
370 icmp_id = self.icmp_id_out
371 else:
372 tcp_port = self.tcp_port_in
373 udp_port = self.udp_port_in
374 icmp_id = self.icmp_id_in
Matus Fabiande886752016-12-07 03:38:19 -0800375 pkts = []
376 # TCP
377 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
Juraj Slobodab33f4132017-02-08 23:54:21 -0800378 IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Juraj Sloboda7b929792017-11-23 13:20:48 +0100379 TCP(dport=tcp_port, sport=20))
Matus Fabiande886752016-12-07 03:38:19 -0800380 pkts.append(p)
381
382 # UDP
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 UDP(dport=udp_port, sport=20))
Matus Fabiande886752016-12-07 03:38:19 -0800386 pkts.append(p)
387
388 # ICMP
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 ICMP(id=icmp_id, type='echo-reply'))
Matus Fabiande886752016-12-07 03:38:19 -0800392 pkts.append(p)
393
394 return pkts
395
Juraj Slobodacba69362017-12-19 02:09:32 +0100396 def create_stream_out_ip6(self, out_if, src_ip, dst_ip, hl=64):
397 """
398 Create packet stream for outside network
399
400 :param out_if: Outside interface
401 :param dst_ip: Destination IP address (Default use global NAT address)
402 :param hl: HL of generated packets
403 """
404 pkts = []
405 # TCP
406 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
407 IPv6(src=src_ip, dst=dst_ip, hlim=hl) /
408 TCP(dport=self.tcp_port_out, sport=20))
409 pkts.append(p)
410
411 # UDP
412 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
413 IPv6(src=src_ip, dst=dst_ip, hlim=hl) /
414 UDP(dport=self.udp_port_out, sport=20))
415 pkts.append(p)
416
417 # ICMP
418 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
419 IPv6(src=src_ip, dst=dst_ip, hlim=hl) /
420 ICMPv6EchoReply(id=self.icmp_id_out))
421 pkts.append(p)
422
423 return pkts
424
Matus Fabiande886752016-12-07 03:38:19 -0800425 def verify_capture_out(self, capture, nat_ip=None, same_port=False,
Matus Fabian05ca4a32018-09-04 23:45:13 -0700426 dst_ip=None, is_ip6=False):
Matus Fabiande886752016-12-07 03:38:19 -0800427 """
428 Verify captured packets on outside network
429
430 :param capture: Captured packets
Matus Fabian2ba92e32017-08-21 07:05:03 -0700431 :param nat_ip: Translated IP address (Default use global NAT address)
Matus Fabiande886752016-12-07 03:38:19 -0800432 :param same_port: Sorce port number is not translated (Default False)
Matus Fabian06596c52017-06-06 04:53:28 -0700433 :param dst_ip: Destination IP address (Default do not verify)
Juraj Slobodacba69362017-12-19 02:09:32 +0100434 :param is_ip6: If L3 protocol is IPv6 (Default False)
Matus Fabiande886752016-12-07 03:38:19 -0800435 """
Juraj Slobodacba69362017-12-19 02:09:32 +0100436 if is_ip6:
437 IP46 = IPv6
438 ICMP46 = ICMPv6EchoRequest
439 else:
440 IP46 = IP
441 ICMP46 = ICMP
Matus Fabiande886752016-12-07 03:38:19 -0800442 if nat_ip is None:
Matus Fabian2ba92e32017-08-21 07:05:03 -0700443 nat_ip = self.nat_addr
Matus Fabiande886752016-12-07 03:38:19 -0800444 for packet in capture:
445 try:
Juraj Slobodacba69362017-12-19 02:09:32 +0100446 if not is_ip6:
Klement Sekerad81ae412018-05-16 10:52:54 +0200447 self.assert_packet_checksums_valid(packet)
Juraj Slobodacba69362017-12-19 02:09:32 +0100448 self.assertEqual(packet[IP46].src, nat_ip)
Matus Fabian06596c52017-06-06 04:53:28 -0700449 if dst_ip is not None:
Juraj Slobodacba69362017-12-19 02:09:32 +0100450 self.assertEqual(packet[IP46].dst, dst_ip)
Matus Fabiande886752016-12-07 03:38:19 -0800451 if packet.haslayer(TCP):
452 if same_port:
453 self.assertEqual(packet[TCP].sport, self.tcp_port_in)
454 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100455 self.assertNotEqual(
456 packet[TCP].sport, self.tcp_port_in)
Matus Fabiande886752016-12-07 03:38:19 -0800457 self.tcp_port_out = packet[TCP].sport
Klement Sekerad81ae412018-05-16 10:52:54 +0200458 self.assert_packet_checksums_valid(packet)
Matus Fabiande886752016-12-07 03:38:19 -0800459 elif packet.haslayer(UDP):
460 if same_port:
461 self.assertEqual(packet[UDP].sport, self.udp_port_in)
462 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100463 self.assertNotEqual(
464 packet[UDP].sport, self.udp_port_in)
Matus Fabiande886752016-12-07 03:38:19 -0800465 self.udp_port_out = packet[UDP].sport
466 else:
467 if same_port:
Juraj Slobodacba69362017-12-19 02:09:32 +0100468 self.assertEqual(packet[ICMP46].id, self.icmp_id_in)
Matus Fabiande886752016-12-07 03:38:19 -0800469 else:
Juraj Slobodacba69362017-12-19 02:09:32 +0100470 self.assertNotEqual(packet[ICMP46].id, self.icmp_id_in)
471 self.icmp_id_out = packet[ICMP46].id
Klement Sekerad81ae412018-05-16 10:52:54 +0200472 self.assert_packet_checksums_valid(packet)
Matus Fabiande886752016-12-07 03:38:19 -0800473 except:
Klement Sekera9225dee2016-12-12 08:36:58 +0100474 self.logger.error(ppp("Unexpected or invalid packet "
475 "(outside network):", packet))
Matus Fabiande886752016-12-07 03:38:19 -0800476 raise
477
Juraj Slobodacba69362017-12-19 02:09:32 +0100478 def verify_capture_out_ip6(self, capture, nat_ip, same_port=False,
Matus Fabian05ca4a32018-09-04 23:45:13 -0700479 dst_ip=None):
Juraj Slobodacba69362017-12-19 02:09:32 +0100480 """
481 Verify captured packets on outside network
482
483 :param capture: Captured packets
484 :param nat_ip: Translated IP address
485 :param same_port: Sorce port number is not translated (Default False)
Juraj Slobodacba69362017-12-19 02:09:32 +0100486 :param dst_ip: Destination IP address (Default do not verify)
487 """
Matus Fabian05ca4a32018-09-04 23:45:13 -0700488 return self.verify_capture_out(capture, nat_ip, same_port, dst_ip,
489 True)
Juraj Slobodacba69362017-12-19 02:09:32 +0100490
Matus Fabian05ca4a32018-09-04 23:45:13 -0700491 def verify_capture_in(self, capture, in_if):
Matus Fabiande886752016-12-07 03:38:19 -0800492 """
493 Verify captured packets on inside network
494
495 :param capture: Captured packets
496 :param in_if: Inside interface
Matus Fabiande886752016-12-07 03:38:19 -0800497 """
Matus Fabiande886752016-12-07 03:38:19 -0800498 for packet in capture:
499 try:
Klement Sekerad81ae412018-05-16 10:52:54 +0200500 self.assert_packet_checksums_valid(packet)
Matus Fabiande886752016-12-07 03:38:19 -0800501 self.assertEqual(packet[IP].dst, in_if.remote_ip4)
502 if packet.haslayer(TCP):
503 self.assertEqual(packet[TCP].dport, self.tcp_port_in)
504 elif packet.haslayer(UDP):
505 self.assertEqual(packet[UDP].dport, self.udp_port_in)
506 else:
507 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
508 except:
Klement Sekera9225dee2016-12-12 08:36:58 +0100509 self.logger.error(ppp("Unexpected or invalid packet "
510 "(inside network):", packet))
Matus Fabiande886752016-12-07 03:38:19 -0800511 raise
512
Matus Fabian05ca4a32018-09-04 23:45:13 -0700513 def verify_capture_in_ip6(self, capture, src_ip, dst_ip):
Matus Fabian06596c52017-06-06 04:53:28 -0700514 """
515 Verify captured IPv6 packets on inside network
516
517 :param capture: Captured packets
518 :param src_ip: Source IP
519 :param dst_ip: Destination IP address
Matus Fabian06596c52017-06-06 04:53:28 -0700520 """
Matus Fabian06596c52017-06-06 04:53:28 -0700521 for packet in capture:
522 try:
523 self.assertEqual(packet[IPv6].src, src_ip)
524 self.assertEqual(packet[IPv6].dst, dst_ip)
Klement Sekerad81ae412018-05-16 10:52:54 +0200525 self.assert_packet_checksums_valid(packet)
Matus Fabian06596c52017-06-06 04:53:28 -0700526 if packet.haslayer(TCP):
527 self.assertEqual(packet[TCP].dport, self.tcp_port_in)
528 elif packet.haslayer(UDP):
529 self.assertEqual(packet[UDP].dport, self.udp_port_in)
530 else:
531 self.assertEqual(packet[ICMPv6EchoReply].id,
532 self.icmp_id_in)
533 except:
534 self.logger.error(ppp("Unexpected or invalid packet "
535 "(inside network):", packet))
536 raise
537
Matus Fabian675a69c2017-01-18 01:46:01 -0800538 def verify_capture_no_translation(self, capture, ingress_if, egress_if):
539 """
540 Verify captured packet that don't have to be translated
541
542 :param capture: Captured packets
543 :param ingress_if: Ingress interface
544 :param egress_if: Egress interface
545 """
546 for packet in capture:
547 try:
548 self.assertEqual(packet[IP].src, ingress_if.remote_ip4)
549 self.assertEqual(packet[IP].dst, egress_if.remote_ip4)
550 if packet.haslayer(TCP):
551 self.assertEqual(packet[TCP].sport, self.tcp_port_in)
552 elif packet.haslayer(UDP):
553 self.assertEqual(packet[UDP].sport, self.udp_port_in)
554 else:
555 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
556 except:
557 self.logger.error(ppp("Unexpected or invalid packet "
558 "(inside network):", packet))
559 raise
560
Juraj Slobodab33f4132017-02-08 23:54:21 -0800561 def verify_capture_out_with_icmp_errors(self, capture, src_ip=None,
Matus Fabian05ca4a32018-09-04 23:45:13 -0700562 icmp_type=11):
Juraj Slobodab33f4132017-02-08 23:54:21 -0800563 """
564 Verify captured packets with ICMP errors on outside network
565
566 :param capture: Captured packets
567 :param src_ip: Translated IP address or IP address of VPP
Matus Fabian2ba92e32017-08-21 07:05:03 -0700568 (Default use global NAT address)
Juraj Slobodab33f4132017-02-08 23:54:21 -0800569 :param icmp_type: Type of error ICMP packet
570 we are expecting (Default 11)
571 """
572 if src_ip is None:
Matus Fabian2ba92e32017-08-21 07:05:03 -0700573 src_ip = self.nat_addr
Juraj Slobodab33f4132017-02-08 23:54:21 -0800574 for packet in capture:
575 try:
576 self.assertEqual(packet[IP].src, src_ip)
577 self.assertTrue(packet.haslayer(ICMP))
578 icmp = packet[ICMP]
579 self.assertEqual(icmp.type, icmp_type)
580 self.assertTrue(icmp.haslayer(IPerror))
581 inner_ip = icmp[IPerror]
582 if inner_ip.haslayer(TCPerror):
583 self.assertEqual(inner_ip[TCPerror].dport,
584 self.tcp_port_out)
585 elif inner_ip.haslayer(UDPerror):
586 self.assertEqual(inner_ip[UDPerror].dport,
587 self.udp_port_out)
588 else:
589 self.assertEqual(inner_ip[ICMPerror].id, self.icmp_id_out)
590 except:
591 self.logger.error(ppp("Unexpected or invalid packet "
592 "(outside network):", packet))
593 raise
594
Matus Fabian05ca4a32018-09-04 23:45:13 -0700595 def verify_capture_in_with_icmp_errors(self, capture, in_if, icmp_type=11):
Juraj Slobodab33f4132017-02-08 23:54:21 -0800596 """
597 Verify captured packets with ICMP errors on inside network
598
599 :param capture: Captured packets
600 :param in_if: Inside interface
Juraj Slobodab33f4132017-02-08 23:54:21 -0800601 :param icmp_type: Type of error ICMP packet
602 we are expecting (Default 11)
603 """
Juraj Slobodab33f4132017-02-08 23:54:21 -0800604 for packet in capture:
605 try:
606 self.assertEqual(packet[IP].dst, in_if.remote_ip4)
607 self.assertTrue(packet.haslayer(ICMP))
608 icmp = packet[ICMP]
609 self.assertEqual(icmp.type, icmp_type)
610 self.assertTrue(icmp.haslayer(IPerror))
611 inner_ip = icmp[IPerror]
612 if inner_ip.haslayer(TCPerror):
613 self.assertEqual(inner_ip[TCPerror].sport,
614 self.tcp_port_in)
615 elif inner_ip.haslayer(UDPerror):
616 self.assertEqual(inner_ip[UDPerror].sport,
617 self.udp_port_in)
618 else:
619 self.assertEqual(inner_ip[ICMPerror].id, self.icmp_id_in)
620 except:
621 self.logger.error(ppp("Unexpected or invalid packet "
622 "(inside network):", packet))
623 raise
624
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200625 def create_stream_frag(self, src_if, dst, sport, dport, data,
626 proto=IP_PROTOS.tcp, echo_reply=False):
Matus Fabianefcd1e92017-08-15 06:59:19 -0700627 """
628 Create fragmented packet stream
629
630 :param src_if: Source interface
631 :param dst: Destination IPv4 address
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200632 :param sport: Source port
633 :param dport: Destination port
Matus Fabianefcd1e92017-08-15 06:59:19 -0700634 :param data: Payload data
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200635 :param proto: protocol (TCP, UDP, ICMP)
636 :param echo_reply: use echo_reply if protocol is ICMP
Matus Fabianefcd1e92017-08-15 06:59:19 -0700637 :returns: Fragmets
638 """
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200639 if proto == IP_PROTOS.tcp:
640 p = (IP(src=src_if.remote_ip4, dst=dst) /
641 TCP(sport=sport, dport=dport) /
642 Raw(data))
643 p = p.__class__(str(p))
644 chksum = p['TCP'].chksum
645 proto_header = TCP(sport=sport, dport=dport, chksum=chksum)
646 elif proto == IP_PROTOS.udp:
647 proto_header = UDP(sport=sport, dport=dport)
648 elif proto == IP_PROTOS.icmp:
649 if not echo_reply:
650 proto_header = ICMP(id=sport, type='echo-request')
651 else:
652 proto_header = ICMP(id=sport, type='echo-reply')
653 else:
654 raise Exception("Unsupported protocol")
Matus Fabianefcd1e92017-08-15 06:59:19 -0700655 id = random.randint(0, 65535)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700656 pkts = []
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200657 if proto == IP_PROTOS.tcp:
658 raw = Raw(data[0:4])
659 else:
660 raw = Raw(data[0:16])
Matus Fabianefcd1e92017-08-15 06:59:19 -0700661 p = (Ether(src=src_if.remote_mac, dst=src_if.local_mac) /
662 IP(src=src_if.remote_ip4, dst=dst, flags="MF", frag=0, id=id) /
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200663 proto_header /
664 raw)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700665 pkts.append(p)
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200666 if proto == IP_PROTOS.tcp:
667 raw = Raw(data[4:20])
668 else:
669 raw = Raw(data[16:32])
Matus Fabianefcd1e92017-08-15 06:59:19 -0700670 p = (Ether(src=src_if.remote_mac, dst=src_if.local_mac) /
671 IP(src=src_if.remote_ip4, dst=dst, flags="MF", frag=3, id=id,
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200672 proto=proto) /
673 raw)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700674 pkts.append(p)
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200675 if proto == IP_PROTOS.tcp:
676 raw = Raw(data[20:])
677 else:
678 raw = Raw(data[32:])
Matus Fabianefcd1e92017-08-15 06:59:19 -0700679 p = (Ether(src=src_if.remote_mac, dst=src_if.local_mac) /
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200680 IP(src=src_if.remote_ip4, dst=dst, frag=5, proto=proto,
Matus Fabianefcd1e92017-08-15 06:59:19 -0700681 id=id) /
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200682 raw)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700683 pkts.append(p)
684 return pkts
685
686 def create_stream_frag_ip6(self, src_if, dst, sport, dport, data,
687 pref=None, plen=0, frag_size=128):
688 """
689 Create fragmented packet stream
690
691 :param src_if: Source interface
692 :param dst: Destination IPv4 address
693 :param sport: Source TCP port
694 :param dport: Destination TCP port
695 :param data: Payload data
696 :param pref: NAT64 prefix
697 :param plen: NAT64 prefix length
698 :param fragsize: size of fragments
699 :returns: Fragmets
700 """
701 if pref is None:
702 dst_ip6 = ''.join(['64:ff9b::', dst])
703 else:
704 dst_ip6 = self.compose_ip6(dst, pref, plen)
705
706 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
707 IPv6(src=src_if.remote_ip6, dst=dst_ip6) /
708 IPv6ExtHdrFragment(id=random.randint(0, 65535)) /
709 TCP(sport=sport, dport=dport) /
710 Raw(data))
711
712 return fragment6(p, frag_size)
713
714 def reass_frags_and_verify(self, frags, src, dst):
715 """
716 Reassemble and verify fragmented packet
717
718 :param frags: Captured fragments
719 :param src: Source IPv4 address to verify
720 :param dst: Destination IPv4 address to verify
721
722 :returns: Reassembled IPv4 packet
723 """
724 buffer = StringIO.StringIO()
725 for p in frags:
726 self.assertEqual(p[IP].src, src)
727 self.assertEqual(p[IP].dst, dst)
Klement Sekerad81ae412018-05-16 10:52:54 +0200728 self.assert_ip_checksum_valid(p)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700729 buffer.seek(p[IP].frag * 8)
730 buffer.write(p[IP].payload)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700731 ip = IP(src=frags[0][IP].src, dst=frags[0][IP].dst,
732 proto=frags[0][IP].proto)
733 if ip.proto == IP_PROTOS.tcp:
734 p = (ip / TCP(buffer.getvalue()))
Klement Sekerad81ae412018-05-16 10:52:54 +0200735 self.assert_tcp_checksum_valid(p)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700736 elif ip.proto == IP_PROTOS.udp:
Juraj Slobodafe0aa762018-07-23 12:22:54 +0200737 p = (ip / UDP(buffer.getvalue()[:8]) /
738 Raw(buffer.getvalue()[8:]))
739 elif ip.proto == IP_PROTOS.icmp:
740 p = (ip / ICMP(buffer.getvalue()))
Matus Fabianefcd1e92017-08-15 06:59:19 -0700741 return p
742
743 def reass_frags_and_verify_ip6(self, frags, src, dst):
744 """
745 Reassemble and verify fragmented packet
746
747 :param frags: Captured fragments
748 :param src: Source IPv6 address to verify
749 :param dst: Destination IPv6 address to verify
750
751 :returns: Reassembled IPv6 packet
752 """
753 buffer = StringIO.StringIO()
754 for p in frags:
755 self.assertEqual(p[IPv6].src, src)
756 self.assertEqual(p[IPv6].dst, dst)
757 buffer.seek(p[IPv6ExtHdrFragment].offset * 8)
758 buffer.write(p[IPv6ExtHdrFragment].payload)
759 ip = IPv6(src=frags[0][IPv6].src, dst=frags[0][IPv6].dst,
760 nh=frags[0][IPv6ExtHdrFragment].nh)
761 if ip.nh == IP_PROTOS.tcp:
762 p = (ip / TCP(buffer.getvalue()))
Matus Fabianefcd1e92017-08-15 06:59:19 -0700763 elif ip.nh == IP_PROTOS.udp:
764 p = (ip / UDP(buffer.getvalue()))
Klement Sekerad81ae412018-05-16 10:52:54 +0200765 self.assert_packet_checksums_valid(p)
Matus Fabianefcd1e92017-08-15 06:59:19 -0700766 return p
767
Matus Fabianebdf1902018-05-04 03:57:42 -0700768 def initiate_tcp_session(self, in_if, out_if):
769 """
770 Initiates TCP session
771
772 :param in_if: Inside interface
773 :param out_if: Outside interface
774 """
775 try:
776 # SYN packet in->out
777 p = (Ether(src=in_if.remote_mac, dst=in_if.local_mac) /
778 IP(src=in_if.remote_ip4, dst=out_if.remote_ip4) /
779 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
780 flags="S"))
781 in_if.add_stream(p)
782 self.pg_enable_capture(self.pg_interfaces)
783 self.pg_start()
784 capture = out_if.get_capture(1)
785 p = capture[0]
786 self.tcp_port_out = p[TCP].sport
787
788 # SYN + ACK packet out->in
789 p = (Ether(src=out_if.remote_mac, dst=out_if.local_mac) /
790 IP(src=out_if.remote_ip4, dst=self.nat_addr) /
791 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
792 flags="SA"))
793 out_if.add_stream(p)
794 self.pg_enable_capture(self.pg_interfaces)
795 self.pg_start()
796 in_if.get_capture(1)
797
798 # ACK packet in->out
799 p = (Ether(src=in_if.remote_mac, dst=in_if.local_mac) /
800 IP(src=in_if.remote_ip4, dst=out_if.remote_ip4) /
801 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
802 flags="A"))
803 in_if.add_stream(p)
804 self.pg_enable_capture(self.pg_interfaces)
805 self.pg_start()
806 out_if.get_capture(1)
807
808 except:
809 self.logger.error("TCP 3 way handshake failed")
810 raise
811
Matus Fabianeea28d72017-01-13 04:15:54 -0800812 def verify_ipfix_nat44_ses(self, data):
813 """
814 Verify IPFIX NAT44 session create/delete event
815
816 :param data: Decoded IPFIX data records
817 """
818 nat44_ses_create_num = 0
819 nat44_ses_delete_num = 0
820 self.assertEqual(6, len(data))
821 for record in data:
822 # natEvent
823 self.assertIn(ord(record[230]), [4, 5])
824 if ord(record[230]) == 4:
825 nat44_ses_create_num += 1
826 else:
827 nat44_ses_delete_num += 1
828 # sourceIPv4Address
829 self.assertEqual(self.pg0.remote_ip4n, record[8])
830 # postNATSourceIPv4Address
Matus Fabian2ba92e32017-08-21 07:05:03 -0700831 self.assertEqual(socket.inet_pton(socket.AF_INET, self.nat_addr),
Matus Fabianeea28d72017-01-13 04:15:54 -0800832 record[225])
833 # ingressVRFID
834 self.assertEqual(struct.pack("!I", 0), record[234])
835 # protocolIdentifier/sourceTransportPort/postNAPTSourceTransportPort
836 if IP_PROTOS.icmp == ord(record[4]):
837 self.assertEqual(struct.pack("!H", self.icmp_id_in), record[7])
838 self.assertEqual(struct.pack("!H", self.icmp_id_out),
839 record[227])
840 elif IP_PROTOS.tcp == ord(record[4]):
841 self.assertEqual(struct.pack("!H", self.tcp_port_in),
842 record[7])
843 self.assertEqual(struct.pack("!H", self.tcp_port_out),
844 record[227])
845 elif IP_PROTOS.udp == ord(record[4]):
846 self.assertEqual(struct.pack("!H", self.udp_port_in),
847 record[7])
848 self.assertEqual(struct.pack("!H", self.udp_port_out),
849 record[227])
850 else:
851 self.fail("Invalid protocol")
852 self.assertEqual(3, nat44_ses_create_num)
853 self.assertEqual(3, nat44_ses_delete_num)
854
855 def verify_ipfix_addr_exhausted(self, data):
856 """
857 Verify IPFIX NAT addresses event
858
859 :param data: Decoded IPFIX data records
860 """
861 self.assertEqual(1, len(data))
862 record = data[0]
863 # natEvent
864 self.assertEqual(ord(record[230]), 3)
865 # natPoolID
866 self.assertEqual(struct.pack("!I", 0), record[283])
867
Matus Fabiana431ad12018-01-04 04:03:14 -0800868 def verify_ipfix_max_sessions(self, data, limit):
869 """
870 Verify IPFIX maximum session entries exceeded event
871
872 :param data: Decoded IPFIX data records
873 :param limit: Number of maximum session entries that can be created.
874 """
875 self.assertEqual(1, len(data))
876 record = data[0]
877 # natEvent
878 self.assertEqual(ord(record[230]), 13)
879 # natQuotaExceededEvent
880 self.assertEqual(struct.pack("I", 1), record[466])
881 # maxSessionEntries
882 self.assertEqual(struct.pack("I", limit), record[471])
883
884 def verify_ipfix_max_bibs(self, data, limit):
885 """
886 Verify IPFIX maximum BIB entries exceeded event
887
888 :param data: Decoded IPFIX data records
889 :param limit: Number of maximum BIB entries that can be created.
890 """
891 self.assertEqual(1, len(data))
892 record = data[0]
893 # natEvent
894 self.assertEqual(ord(record[230]), 13)
895 # natQuotaExceededEvent
896 self.assertEqual(struct.pack("I", 2), record[466])
897 # maxBIBEntries
898 self.assertEqual(struct.pack("I", limit), record[472])
899
900 def verify_ipfix_max_fragments_ip6(self, data, limit, src_addr):
901 """
902 Verify IPFIX maximum IPv6 fragments pending reassembly exceeded event
903
904 :param data: Decoded IPFIX data records
905 :param limit: Number of maximum fragments pending reassembly
906 :param src_addr: IPv6 source address
907 """
908 self.assertEqual(1, len(data))
909 record = data[0]
910 # natEvent
911 self.assertEqual(ord(record[230]), 13)
912 # natQuotaExceededEvent
913 self.assertEqual(struct.pack("I", 5), record[466])
914 # maxFragmentsPendingReassembly
915 self.assertEqual(struct.pack("I", limit), record[475])
916 # sourceIPv6Address
917 self.assertEqual(src_addr, record[27])
918
919 def verify_ipfix_max_fragments_ip4(self, data, limit, src_addr):
920 """
921 Verify IPFIX maximum IPv4 fragments pending reassembly exceeded event
922
923 :param data: Decoded IPFIX data records
924 :param limit: Number of maximum fragments pending reassembly
925 :param src_addr: IPv4 source address
926 """
927 self.assertEqual(1, len(data))
928 record = data[0]
929 # natEvent
930 self.assertEqual(ord(record[230]), 13)
931 # natQuotaExceededEvent
932 self.assertEqual(struct.pack("I", 5), record[466])
933 # maxFragmentsPendingReassembly
934 self.assertEqual(struct.pack("I", limit), record[475])
935 # sourceIPv4Address
936 self.assertEqual(src_addr, record[8])
937
938 def verify_ipfix_bib(self, data, is_create, src_addr):
939 """
940 Verify IPFIX NAT64 BIB create and delete events
941
942 :param data: Decoded IPFIX data records
943 :param is_create: Create event if nonzero value otherwise delete event
944 :param src_addr: IPv6 source address
945 """
946 self.assertEqual(1, len(data))
947 record = data[0]
948 # natEvent
949 if is_create:
950 self.assertEqual(ord(record[230]), 10)
951 else:
952 self.assertEqual(ord(record[230]), 11)
953 # sourceIPv6Address
954 self.assertEqual(src_addr, record[27])
955 # postNATSourceIPv4Address
956 self.assertEqual(self.nat_addr_n, record[225])
957 # protocolIdentifier
958 self.assertEqual(IP_PROTOS.tcp, ord(record[4]))
959 # ingressVRFID
960 self.assertEqual(struct.pack("!I", 0), record[234])
961 # sourceTransportPort
962 self.assertEqual(struct.pack("!H", self.tcp_port_in), record[7])
963 # postNAPTSourceTransportPort
964 self.assertEqual(struct.pack("!H", self.tcp_port_out), record[227])
965
966 def verify_ipfix_nat64_ses(self, data, is_create, src_addr, dst_addr,
967 dst_port):
968 """
969 Verify IPFIX NAT64 session create and delete events
970
971 :param data: Decoded IPFIX data records
972 :param is_create: Create event if nonzero value otherwise delete event
973 :param src_addr: IPv6 source address
974 :param dst_addr: IPv4 destination address
975 :param dst_port: destination TCP port
976 """
977 self.assertEqual(1, len(data))
978 record = data[0]
979 # natEvent
980 if is_create:
981 self.assertEqual(ord(record[230]), 6)
982 else:
983 self.assertEqual(ord(record[230]), 7)
984 # sourceIPv6Address
985 self.assertEqual(src_addr, record[27])
986 # destinationIPv6Address
987 self.assertEqual(socket.inet_pton(socket.AF_INET6,
988 self.compose_ip6(dst_addr,
989 '64:ff9b::',
990 96)),
991 record[28])
992 # postNATSourceIPv4Address
993 self.assertEqual(self.nat_addr_n, record[225])
994 # postNATDestinationIPv4Address
995 self.assertEqual(socket.inet_pton(socket.AF_INET, dst_addr),
996 record[226])
997 # protocolIdentifier
998 self.assertEqual(IP_PROTOS.tcp, ord(record[4]))
999 # ingressVRFID
1000 self.assertEqual(struct.pack("!I", 0), record[234])
1001 # sourceTransportPort
1002 self.assertEqual(struct.pack("!H", self.tcp_port_in), record[7])
1003 # postNAPTSourceTransportPort
1004 self.assertEqual(struct.pack("!H", self.tcp_port_out), record[227])
1005 # destinationTransportPort
1006 self.assertEqual(struct.pack("!H", dst_port), record[11])
1007 # postNAPTDestinationTransportPort
1008 self.assertEqual(struct.pack("!H", dst_port), record[228])
1009
Matus Fabian68ba8802018-08-08 05:52:47 -07001010 def verify_no_nat44_user(self):
1011 """ Verify that there is no NAT44 user """
1012 users = self.vapi.nat44_user_dump()
1013 self.assertEqual(len(users), 0)
1014
Matus Fabian878c6462018-08-23 00:33:35 -07001015 def verify_ipfix_max_entries_per_user(self, data, limit, src_addr):
1016 """
1017 Verify IPFIX maximum entries per user exceeded event
1018
1019 :param data: Decoded IPFIX data records
1020 :param limit: Number of maximum entries per user
1021 :param src_addr: IPv4 source address
1022 """
1023 self.assertEqual(1, len(data))
1024 record = data[0]
1025 # natEvent
1026 self.assertEqual(ord(record[230]), 13)
1027 # natQuotaExceededEvent
1028 self.assertEqual(struct.pack("I", 3), record[466])
1029 # maxEntriesPerUser
1030 self.assertEqual(struct.pack("I", limit), record[473])
1031 # sourceIPv4Address
1032 self.assertEqual(src_addr, record[8])
1033
Matus Fabianbb4e0222018-09-13 02:36:25 -07001034 def verify_mss_value(self, pkt, mss):
1035 """
1036 Verify TCP MSS value
1037
1038 :param pkt:
1039 :param mss:
1040 """
1041 if not pkt.haslayer(IP) or not pkt.haslayer(TCP):
1042 raise TypeError("Not a TCP/IP packet")
1043
1044 for option in pkt[TCP].options:
1045 if option[0] == 'MSS':
1046 self.assertEqual(option[1], mss)
1047 self.assert_tcp_checksum_valid(pkt)
1048
Juraj Slobodafe0aa762018-07-23 12:22:54 +02001049 @staticmethod
1050 def proto2layer(proto):
1051 if proto == IP_PROTOS.tcp:
1052 return TCP
1053 elif proto == IP_PROTOS.udp:
1054 return UDP
1055 elif proto == IP_PROTOS.icmp:
1056 return ICMP
1057 else:
1058 raise Exception("Unsupported protocol")
1059
1060 def frag_in_order(self, proto=IP_PROTOS.tcp, dont_translate=False):
1061 layer = self.proto2layer(proto)
1062
1063 if proto == IP_PROTOS.tcp:
1064 data = "A" * 4 + "B" * 16 + "C" * 3
1065 else:
1066 data = "A" * 16 + "B" * 16 + "C" * 3
1067 self.port_in = random.randint(1025, 65535)
1068
1069 reass = self.vapi.nat_reass_dump()
1070 reass_n_start = len(reass)
1071
1072 # in2out
1073 pkts = self.create_stream_frag(self.pg0,
1074 self.pg1.remote_ip4,
1075 self.port_in,
1076 20,
1077 data,
1078 proto)
1079 self.pg0.add_stream(pkts)
1080 self.pg_enable_capture(self.pg_interfaces)
1081 self.pg_start()
1082 frags = self.pg1.get_capture(len(pkts))
1083 if not dont_translate:
1084 p = self.reass_frags_and_verify(frags,
1085 self.nat_addr,
1086 self.pg1.remote_ip4)
1087 else:
1088 p = self.reass_frags_and_verify(frags,
1089 self.pg0.remote_ip4,
1090 self.pg1.remote_ip4)
1091 if proto != IP_PROTOS.icmp:
1092 if not dont_translate:
1093 self.assertEqual(p[layer].dport, 20)
1094 self.assertNotEqual(p[layer].sport, self.port_in)
1095 else:
1096 self.assertEqual(p[layer].sport, self.port_in)
1097 else:
1098 if not dont_translate:
1099 self.assertNotEqual(p[layer].id, self.port_in)
1100 else:
1101 self.assertEqual(p[layer].id, self.port_in)
1102 self.assertEqual(data, p[Raw].load)
1103
1104 # out2in
1105 if not dont_translate:
1106 dst_addr = self.nat_addr
1107 else:
1108 dst_addr = self.pg0.remote_ip4
1109 if proto != IP_PROTOS.icmp:
1110 sport = 20
1111 dport = p[layer].sport
1112 else:
1113 sport = p[layer].id
1114 dport = 0
1115 pkts = self.create_stream_frag(self.pg1,
1116 dst_addr,
1117 sport,
1118 dport,
1119 data,
1120 proto,
1121 echo_reply=True)
1122 self.pg1.add_stream(pkts)
1123 self.pg_enable_capture(self.pg_interfaces)
1124 self.pg_start()
1125 frags = self.pg0.get_capture(len(pkts))
1126 p = self.reass_frags_and_verify(frags,
1127 self.pg1.remote_ip4,
1128 self.pg0.remote_ip4)
1129 if proto != IP_PROTOS.icmp:
1130 self.assertEqual(p[layer].sport, 20)
1131 self.assertEqual(p[layer].dport, self.port_in)
1132 else:
1133 self.assertEqual(p[layer].id, self.port_in)
1134 self.assertEqual(data, p[Raw].load)
1135
1136 reass = self.vapi.nat_reass_dump()
1137 reass_n_end = len(reass)
1138
1139 self.assertEqual(reass_n_end - reass_n_start, 2)
1140
1141 def frag_in_order_in_plus_out(self, proto=IP_PROTOS.tcp):
1142 layer = self.proto2layer(proto)
1143
1144 if proto == IP_PROTOS.tcp:
1145 data = "A" * 4 + "B" * 16 + "C" * 3
1146 else:
1147 data = "A" * 16 + "B" * 16 + "C" * 3
1148 self.port_in = random.randint(1025, 65535)
1149
1150 for i in range(2):
1151 reass = self.vapi.nat_reass_dump()
1152 reass_n_start = len(reass)
1153
1154 # out2in
1155 pkts = self.create_stream_frag(self.pg0,
1156 self.server_out_addr,
1157 self.port_in,
1158 self.server_out_port,
1159 data,
1160 proto)
1161 self.pg0.add_stream(pkts)
1162 self.pg_enable_capture(self.pg_interfaces)
1163 self.pg_start()
1164 frags = self.pg1.get_capture(len(pkts))
1165 p = self.reass_frags_and_verify(frags,
1166 self.pg0.remote_ip4,
1167 self.server_in_addr)
1168 if proto != IP_PROTOS.icmp:
1169 self.assertEqual(p[layer].sport, self.port_in)
1170 self.assertEqual(p[layer].dport, self.server_in_port)
1171 else:
1172 self.assertEqual(p[layer].id, self.port_in)
1173 self.assertEqual(data, p[Raw].load)
1174
1175 # in2out
1176 if proto != IP_PROTOS.icmp:
1177 pkts = self.create_stream_frag(self.pg1,
1178 self.pg0.remote_ip4,
1179 self.server_in_port,
1180 p[layer].sport,
1181 data,
1182 proto)
1183 else:
1184 pkts = self.create_stream_frag(self.pg1,
1185 self.pg0.remote_ip4,
1186 p[layer].id,
1187 0,
1188 data,
1189 proto,
1190 echo_reply=True)
1191 self.pg1.add_stream(pkts)
1192 self.pg_enable_capture(self.pg_interfaces)
1193 self.pg_start()
1194 frags = self.pg0.get_capture(len(pkts))
1195 p = self.reass_frags_and_verify(frags,
1196 self.server_out_addr,
1197 self.pg0.remote_ip4)
1198 if proto != IP_PROTOS.icmp:
1199 self.assertEqual(p[layer].sport, self.server_out_port)
1200 self.assertEqual(p[layer].dport, self.port_in)
1201 else:
1202 self.assertEqual(p[layer].id, self.port_in)
1203 self.assertEqual(data, p[Raw].load)
1204
1205 reass = self.vapi.nat_reass_dump()
1206 reass_n_end = len(reass)
1207
1208 self.assertEqual(reass_n_end - reass_n_start, 2)
1209
1210 def reass_hairpinning(self, proto=IP_PROTOS.tcp):
1211 layer = self.proto2layer(proto)
1212
1213 if proto == IP_PROTOS.tcp:
1214 data = "A" * 4 + "B" * 16 + "C" * 3
1215 else:
1216 data = "A" * 16 + "B" * 16 + "C" * 3
1217
1218 # send packet from host to server
1219 pkts = self.create_stream_frag(self.pg0,
1220 self.nat_addr,
1221 self.host_in_port,
1222 self.server_out_port,
1223 data,
1224 proto)
1225 self.pg0.add_stream(pkts)
1226 self.pg_enable_capture(self.pg_interfaces)
1227 self.pg_start()
1228 frags = self.pg0.get_capture(len(pkts))
1229 p = self.reass_frags_and_verify(frags,
1230 self.nat_addr,
1231 self.server.ip4)
1232 if proto != IP_PROTOS.icmp:
1233 self.assertNotEqual(p[layer].sport, self.host_in_port)
1234 self.assertEqual(p[layer].dport, self.server_in_port)
1235 else:
1236 self.assertNotEqual(p[layer].id, self.host_in_port)
1237 self.assertEqual(data, p[Raw].load)
1238
1239 def frag_out_of_order(self, proto=IP_PROTOS.tcp, dont_translate=False):
1240 layer = self.proto2layer(proto)
1241
1242 if proto == IP_PROTOS.tcp:
1243 data = "A" * 4 + "B" * 16 + "C" * 3
1244 else:
1245 data = "A" * 16 + "B" * 16 + "C" * 3
1246 self.port_in = random.randint(1025, 65535)
1247
1248 for i in range(2):
1249 # in2out
1250 pkts = self.create_stream_frag(self.pg0,
1251 self.pg1.remote_ip4,
1252 self.port_in,
1253 20,
1254 data,
1255 proto)
1256 pkts.reverse()
1257 self.pg0.add_stream(pkts)
1258 self.pg_enable_capture(self.pg_interfaces)
1259 self.pg_start()
1260 frags = self.pg1.get_capture(len(pkts))
1261 if not dont_translate:
1262 p = self.reass_frags_and_verify(frags,
1263 self.nat_addr,
1264 self.pg1.remote_ip4)
1265 else:
1266 p = self.reass_frags_and_verify(frags,
1267 self.pg0.remote_ip4,
1268 self.pg1.remote_ip4)
1269 if proto != IP_PROTOS.icmp:
1270 if not dont_translate:
1271 self.assertEqual(p[layer].dport, 20)
1272 self.assertNotEqual(p[layer].sport, self.port_in)
1273 else:
1274 self.assertEqual(p[layer].sport, self.port_in)
1275 else:
1276 if not dont_translate:
1277 self.assertNotEqual(p[layer].id, self.port_in)
1278 else:
1279 self.assertEqual(p[layer].id, self.port_in)
1280 self.assertEqual(data, p[Raw].load)
1281
1282 # out2in
1283 if not dont_translate:
1284 dst_addr = self.nat_addr
1285 else:
1286 dst_addr = self.pg0.remote_ip4
1287 if proto != IP_PROTOS.icmp:
1288 sport = 20
1289 dport = p[layer].sport
1290 else:
1291 sport = p[layer].id
1292 dport = 0
1293 pkts = self.create_stream_frag(self.pg1,
1294 dst_addr,
1295 sport,
1296 dport,
1297 data,
1298 proto,
1299 echo_reply=True)
1300 pkts.reverse()
1301 self.pg1.add_stream(pkts)
1302 self.pg_enable_capture(self.pg_interfaces)
1303 self.pg_start()
1304 frags = self.pg0.get_capture(len(pkts))
1305 p = self.reass_frags_and_verify(frags,
1306 self.pg1.remote_ip4,
1307 self.pg0.remote_ip4)
1308 if proto != IP_PROTOS.icmp:
1309 self.assertEqual(p[layer].sport, 20)
1310 self.assertEqual(p[layer].dport, self.port_in)
1311 else:
1312 self.assertEqual(p[layer].id, self.port_in)
1313 self.assertEqual(data, p[Raw].load)
1314
1315 def frag_out_of_order_in_plus_out(self, proto=IP_PROTOS.tcp):
1316 layer = self.proto2layer(proto)
1317
1318 if proto == IP_PROTOS.tcp:
1319 data = "A" * 4 + "B" * 16 + "C" * 3
1320 else:
1321 data = "A" * 16 + "B" * 16 + "C" * 3
1322 self.port_in = random.randint(1025, 65535)
1323
1324 for i in range(2):
1325 # out2in
1326 pkts = self.create_stream_frag(self.pg0,
1327 self.server_out_addr,
1328 self.port_in,
1329 self.server_out_port,
1330 data,
1331 proto)
1332 pkts.reverse()
1333 self.pg0.add_stream(pkts)
1334 self.pg_enable_capture(self.pg_interfaces)
1335 self.pg_start()
1336 frags = self.pg1.get_capture(len(pkts))
1337 p = self.reass_frags_and_verify(frags,
1338 self.pg0.remote_ip4,
1339 self.server_in_addr)
1340 if proto != IP_PROTOS.icmp:
1341 self.assertEqual(p[layer].dport, self.server_in_port)
1342 self.assertEqual(p[layer].sport, self.port_in)
1343 self.assertEqual(p[layer].dport, self.server_in_port)
1344 else:
1345 self.assertEqual(p[layer].id, self.port_in)
1346 self.assertEqual(data, p[Raw].load)
1347
1348 # in2out
1349 if proto != IP_PROTOS.icmp:
1350 pkts = self.create_stream_frag(self.pg1,
1351 self.pg0.remote_ip4,
1352 self.server_in_port,
1353 p[layer].sport,
1354 data,
1355 proto)
1356 else:
1357 pkts = self.create_stream_frag(self.pg1,
1358 self.pg0.remote_ip4,
1359 p[layer].id,
1360 0,
1361 data,
1362 proto,
1363 echo_reply=True)
1364 pkts.reverse()
1365 self.pg1.add_stream(pkts)
1366 self.pg_enable_capture(self.pg_interfaces)
1367 self.pg_start()
1368 frags = self.pg0.get_capture(len(pkts))
1369 p = self.reass_frags_and_verify(frags,
1370 self.server_out_addr,
1371 self.pg0.remote_ip4)
1372 if proto != IP_PROTOS.icmp:
1373 self.assertEqual(p[layer].sport, self.server_out_port)
1374 self.assertEqual(p[layer].dport, self.port_in)
1375 else:
1376 self.assertEqual(p[layer].id, self.port_in)
1377 self.assertEqual(data, p[Raw].load)
1378
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001379
Matus Fabian2ba92e32017-08-21 07:05:03 -07001380class TestNAT44(MethodHolder):
1381 """ NAT44 Test Cases """
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001382
1383 @classmethod
1384 def setUpClass(cls):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001385 super(TestNAT44, cls).setUpClass()
Matus Fabian229c1aa2018-05-28 04:09:52 -07001386 cls.vapi.cli("set log class nat level debug")
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001387
1388 try:
1389 cls.tcp_port_in = 6303
1390 cls.tcp_port_out = 6303
1391 cls.udp_port_in = 6304
1392 cls.udp_port_out = 6304
1393 cls.icmp_id_in = 6305
1394 cls.icmp_id_out = 6305
Matus Fabian2ba92e32017-08-21 07:05:03 -07001395 cls.nat_addr = '10.0.0.3'
Juraj Sloboda7b929792017-11-23 13:20:48 +01001396 cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
Matus Fabian6631e9c2017-05-24 01:52:20 -07001397 cls.ipfix_src_port = 4739
1398 cls.ipfix_domain_id = 1
Matus Fabianebdf1902018-05-04 03:57:42 -07001399 cls.tcp_external_port = 80
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001400
Matus Fabian36ea2d62017-10-24 04:13:49 -07001401 cls.create_pg_interfaces(range(10))
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001402 cls.interfaces = list(cls.pg_interfaces[0:4])
1403
1404 for i in cls.interfaces:
1405 i.admin_up()
1406 i.config_ip4()
1407 i.resolve_arp()
1408
Matus Fabian6fa74c62017-06-05 05:55:48 -07001409 cls.pg0.generate_remote_hosts(3)
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001410 cls.pg0.configure_ipv4_neighbors()
1411
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02001412 cls.pg1.generate_remote_hosts(1)
1413 cls.pg1.configure_ipv4_neighbors()
1414
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001415 cls.overlapping_interfaces = list(list(cls.pg_interfaces[4:7]))
Neale Ranns15002542017-09-10 04:39:11 -07001416 cls.vapi.ip_table_add_del(10, is_add=1)
1417 cls.vapi.ip_table_add_del(20, is_add=1)
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001418
1419 cls.pg4._local_ip4 = "172.16.255.1"
1420 cls.pg4._local_ip4n = socket.inet_pton(socket.AF_INET, i.local_ip4)
1421 cls.pg4._remote_hosts[0]._ip4 = "172.16.255.2"
1422 cls.pg4.set_table_ip4(10)
Matus Fabian69126282017-08-14 23:39:58 -07001423 cls.pg5._local_ip4 = "172.17.255.3"
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001424 cls.pg5._local_ip4n = socket.inet_pton(socket.AF_INET, i.local_ip4)
Matus Fabian69126282017-08-14 23:39:58 -07001425 cls.pg5._remote_hosts[0]._ip4 = "172.17.255.4"
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001426 cls.pg5.set_table_ip4(10)
1427 cls.pg6._local_ip4 = "172.16.255.1"
1428 cls.pg6._local_ip4n = socket.inet_pton(socket.AF_INET, i.local_ip4)
1429 cls.pg6._remote_hosts[0]._ip4 = "172.16.255.2"
1430 cls.pg6.set_table_ip4(20)
1431 for i in cls.overlapping_interfaces:
1432 i.config_ip4()
1433 i.admin_up()
1434 i.resolve_arp()
1435
1436 cls.pg7.admin_up()
Martin Gálik406eb1d2017-05-04 04:35:04 -07001437 cls.pg8.admin_up()
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001438
Matus Fabian36ea2d62017-10-24 04:13:49 -07001439 cls.pg9.generate_remote_hosts(2)
1440 cls.pg9.config_ip4()
1441 ip_addr_n = socket.inet_pton(socket.AF_INET, "10.0.0.1")
1442 cls.vapi.sw_interface_add_del_address(cls.pg9.sw_if_index,
1443 ip_addr_n,
1444 24)
1445 cls.pg9.admin_up()
1446 cls.pg9.resolve_arp()
1447 cls.pg9._remote_hosts[1]._ip4 = cls.pg9._remote_hosts[0]._ip4
1448 cls.pg4._remote_ip4 = cls.pg9._remote_hosts[0]._ip4 = "10.0.0.2"
1449 cls.pg9.resolve_arp()
1450
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001451 except Exception:
Matus Fabian2ba92e32017-08-21 07:05:03 -07001452 super(TestNAT44, cls).tearDownClass()
Martin Gálikd7f75cd2017-03-27 06:02:47 -07001453 raise
1454
Matus Fabiande886752016-12-07 03:38:19 -08001455 def test_dynamic(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001456 """ NAT44 dynamic translation test """
Matus Fabiande886752016-12-07 03:38:19 -08001457
Matus Fabian2ba92e32017-08-21 07:05:03 -07001458 self.nat44_add_address(self.nat_addr)
1459 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1460 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1461 is_inside=0)
Matus Fabiande886752016-12-07 03:38:19 -08001462
1463 # in2out
1464 pkts = self.create_stream_in(self.pg0, self.pg1)
1465 self.pg0.add_stream(pkts)
1466 self.pg_enable_capture(self.pg_interfaces)
1467 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001468 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001469 self.verify_capture_out(capture)
1470
1471 # out2in
1472 pkts = self.create_stream_out(self.pg1)
1473 self.pg1.add_stream(pkts)
1474 self.pg_enable_capture(self.pg_interfaces)
1475 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001476 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001477 self.verify_capture_in(capture, self.pg0)
1478
Juraj Slobodab33f4132017-02-08 23:54:21 -08001479 def test_dynamic_icmp_errors_in2out_ttl_1(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001480 """ NAT44 handling of client packets with TTL=1 """
Juraj Slobodab33f4132017-02-08 23:54:21 -08001481
Matus Fabian2ba92e32017-08-21 07:05:03 -07001482 self.nat44_add_address(self.nat_addr)
1483 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1484 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1485 is_inside=0)
Juraj Slobodab33f4132017-02-08 23:54:21 -08001486
1487 # Client side - generate traffic
1488 pkts = self.create_stream_in(self.pg0, self.pg1, ttl=1)
1489 self.pg0.add_stream(pkts)
1490 self.pg_enable_capture(self.pg_interfaces)
1491 self.pg_start()
1492
1493 # Client side - verify ICMP type 11 packets
1494 capture = self.pg0.get_capture(len(pkts))
1495 self.verify_capture_in_with_icmp_errors(capture, self.pg0)
1496
1497 def test_dynamic_icmp_errors_out2in_ttl_1(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001498 """ NAT44 handling of server packets with TTL=1 """
Juraj Slobodab33f4132017-02-08 23:54:21 -08001499
Matus Fabian2ba92e32017-08-21 07:05:03 -07001500 self.nat44_add_address(self.nat_addr)
1501 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1502 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1503 is_inside=0)
Juraj Slobodab33f4132017-02-08 23:54:21 -08001504
1505 # Client side - create sessions
1506 pkts = self.create_stream_in(self.pg0, self.pg1)
1507 self.pg0.add_stream(pkts)
1508 self.pg_enable_capture(self.pg_interfaces)
1509 self.pg_start()
1510
1511 # Server side - generate traffic
1512 capture = self.pg1.get_capture(len(pkts))
1513 self.verify_capture_out(capture)
1514 pkts = self.create_stream_out(self.pg1, ttl=1)
1515 self.pg1.add_stream(pkts)
1516 self.pg_enable_capture(self.pg_interfaces)
1517 self.pg_start()
1518
1519 # Server side - verify ICMP type 11 packets
1520 capture = self.pg1.get_capture(len(pkts))
1521 self.verify_capture_out_with_icmp_errors(capture,
1522 src_ip=self.pg1.local_ip4)
1523
1524 def test_dynamic_icmp_errors_in2out_ttl_2(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001525 """ NAT44 handling of error responses to client packets with TTL=2 """
Juraj Slobodab33f4132017-02-08 23:54:21 -08001526
Matus Fabian2ba92e32017-08-21 07:05:03 -07001527 self.nat44_add_address(self.nat_addr)
1528 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1529 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1530 is_inside=0)
Juraj Slobodab33f4132017-02-08 23:54:21 -08001531
1532 # Client side - generate traffic
1533 pkts = self.create_stream_in(self.pg0, self.pg1, ttl=2)
1534 self.pg0.add_stream(pkts)
1535 self.pg_enable_capture(self.pg_interfaces)
1536 self.pg_start()
1537
1538 # Server side - simulate ICMP type 11 response
1539 capture = self.pg1.get_capture(len(pkts))
1540 pkts = [Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07001541 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
Juraj Slobodab33f4132017-02-08 23:54:21 -08001542 ICMP(type=11) / packet[IP] for packet in capture]
1543 self.pg1.add_stream(pkts)
1544 self.pg_enable_capture(self.pg_interfaces)
1545 self.pg_start()
1546
1547 # Client side - verify ICMP type 11 packets
1548 capture = self.pg0.get_capture(len(pkts))
1549 self.verify_capture_in_with_icmp_errors(capture, self.pg0)
1550
1551 def test_dynamic_icmp_errors_out2in_ttl_2(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001552 """ NAT44 handling of error responses to server packets with TTL=2 """
Juraj Slobodab33f4132017-02-08 23:54:21 -08001553
Matus Fabian2ba92e32017-08-21 07:05:03 -07001554 self.nat44_add_address(self.nat_addr)
1555 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1556 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1557 is_inside=0)
Juraj Slobodab33f4132017-02-08 23:54:21 -08001558
1559 # Client side - create sessions
1560 pkts = self.create_stream_in(self.pg0, self.pg1)
1561 self.pg0.add_stream(pkts)
1562 self.pg_enable_capture(self.pg_interfaces)
1563 self.pg_start()
1564
1565 # Server side - generate traffic
1566 capture = self.pg1.get_capture(len(pkts))
1567 self.verify_capture_out(capture)
1568 pkts = self.create_stream_out(self.pg1, ttl=2)
1569 self.pg1.add_stream(pkts)
1570 self.pg_enable_capture(self.pg_interfaces)
1571 self.pg_start()
1572
1573 # Client side - simulate ICMP type 11 response
1574 capture = self.pg0.get_capture(len(pkts))
1575 pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1576 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
1577 ICMP(type=11) / packet[IP] for packet in capture]
1578 self.pg0.add_stream(pkts)
1579 self.pg_enable_capture(self.pg_interfaces)
1580 self.pg_start()
1581
1582 # Server side - verify ICMP type 11 packets
1583 capture = self.pg1.get_capture(len(pkts))
1584 self.verify_capture_out_with_icmp_errors(capture)
1585
Juraj Sloboda665e4822017-02-16 17:17:19 -08001586 def test_ping_out_interface_from_outside(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001587 """ Ping NAT44 out interface from outside network """
Juraj Sloboda665e4822017-02-16 17:17:19 -08001588
Matus Fabian2ba92e32017-08-21 07:05:03 -07001589 self.nat44_add_address(self.nat_addr)
1590 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1591 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1592 is_inside=0)
Juraj Sloboda665e4822017-02-16 17:17:19 -08001593
1594 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1595 IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
1596 ICMP(id=self.icmp_id_out, type='echo-request'))
1597 pkts = [p]
1598 self.pg1.add_stream(pkts)
1599 self.pg_enable_capture(self.pg_interfaces)
1600 self.pg_start()
1601 capture = self.pg1.get_capture(len(pkts))
Juraj Sloboda665e4822017-02-16 17:17:19 -08001602 packet = capture[0]
1603 try:
1604 self.assertEqual(packet[IP].src, self.pg1.local_ip4)
1605 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
1606 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
1607 self.assertEqual(packet[ICMP].type, 0) # echo reply
1608 except:
1609 self.logger.error(ppp("Unexpected or invalid packet "
1610 "(outside network):", packet))
1611 raise
1612
Juraj Slobodad3677682017-04-14 03:24:45 +02001613 def test_ping_internal_host_from_outside(self):
1614 """ Ping internal host from outside network """
1615
Matus Fabian2ba92e32017-08-21 07:05:03 -07001616 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr)
1617 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1618 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1619 is_inside=0)
Juraj Slobodad3677682017-04-14 03:24:45 +02001620
1621 # out2in
1622 pkt = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07001623 IP(src=self.pg1.remote_ip4, dst=self.nat_addr, ttl=64) /
Juraj Slobodad3677682017-04-14 03:24:45 +02001624 ICMP(id=self.icmp_id_out, type='echo-request'))
1625 self.pg1.add_stream(pkt)
1626 self.pg_enable_capture(self.pg_interfaces)
1627 self.pg_start()
1628 capture = self.pg0.get_capture(1)
Matus Fabian05ca4a32018-09-04 23:45:13 -07001629 self.verify_capture_in(capture, self.pg0)
Juraj Slobodad3677682017-04-14 03:24:45 +02001630 self.assert_equal(capture[0][IP].proto, IP_PROTOS.icmp)
1631
1632 # in2out
1633 pkt = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1634 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, ttl=64) /
1635 ICMP(id=self.icmp_id_in, type='echo-reply'))
1636 self.pg0.add_stream(pkt)
1637 self.pg_enable_capture(self.pg_interfaces)
1638 self.pg_start()
1639 capture = self.pg1.get_capture(1)
Matus Fabian05ca4a32018-09-04 23:45:13 -07001640 self.verify_capture_out(capture, same_port=True)
Juraj Slobodad3677682017-04-14 03:24:45 +02001641 self.assert_equal(capture[0][IP].proto, IP_PROTOS.icmp)
1642
Matus Fabianc79396e2018-07-23 00:05:22 -07001643 def test_forwarding(self):
Juraj Sloboda7b929792017-11-23 13:20:48 +01001644 """ NAT44 forwarding test """
1645
1646 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1647 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1648 is_inside=0)
1649 self.vapi.nat44_forwarding_enable_disable(1)
1650
1651 real_ip = self.pg0.remote_ip4n
1652 alias_ip = self.nat_addr_n
1653 self.vapi.nat44_add_del_static_mapping(local_ip=real_ip,
1654 external_ip=alias_ip)
1655
1656 try:
Matus Fabianc79396e2018-07-23 00:05:22 -07001657 # static mapping match
Juraj Sloboda7b929792017-11-23 13:20:48 +01001658
1659 pkts = self.create_stream_out(self.pg1)
1660 self.pg1.add_stream(pkts)
1661 self.pg_enable_capture(self.pg_interfaces)
1662 self.pg_start()
1663 capture = self.pg0.get_capture(len(pkts))
1664 self.verify_capture_in(capture, self.pg0)
1665
1666 pkts = self.create_stream_in(self.pg0, self.pg1)
1667 self.pg0.add_stream(pkts)
1668 self.pg_enable_capture(self.pg_interfaces)
1669 self.pg_start()
1670 capture = self.pg1.get_capture(len(pkts))
1671 self.verify_capture_out(capture, same_port=True)
1672
Matus Fabianc79396e2018-07-23 00:05:22 -07001673 # no static mapping match
Juraj Sloboda7b929792017-11-23 13:20:48 +01001674
1675 host0 = self.pg0.remote_hosts[0]
1676 self.pg0.remote_hosts[0] = self.pg0.remote_hosts[1]
1677 try:
1678 pkts = self.create_stream_out(self.pg1,
1679 dst_ip=self.pg0.remote_ip4,
1680 use_inside_ports=True)
1681 self.pg1.add_stream(pkts)
1682 self.pg_enable_capture(self.pg_interfaces)
1683 self.pg_start()
1684 capture = self.pg0.get_capture(len(pkts))
1685 self.verify_capture_in(capture, self.pg0)
1686
1687 pkts = self.create_stream_in(self.pg0, self.pg1)
1688 self.pg0.add_stream(pkts)
1689 self.pg_enable_capture(self.pg_interfaces)
1690 self.pg_start()
1691 capture = self.pg1.get_capture(len(pkts))
1692 self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4,
1693 same_port=True)
1694 finally:
1695 self.pg0.remote_hosts[0] = host0
1696
1697 finally:
1698 self.vapi.nat44_forwarding_enable_disable(0)
1699 self.vapi.nat44_add_del_static_mapping(local_ip=real_ip,
1700 external_ip=alias_ip,
1701 is_add=0)
1702
Matus Fabiande886752016-12-07 03:38:19 -08001703 def test_static_in(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001704 """ 1:1 NAT initialized from inside network """
Matus Fabiande886752016-12-07 03:38:19 -08001705
1706 nat_ip = "10.0.0.10"
1707 self.tcp_port_out = 6303
1708 self.udp_port_out = 6304
1709 self.icmp_id_out = 6305
1710
Matus Fabian2ba92e32017-08-21 07:05:03 -07001711 self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip)
1712 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1713 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1714 is_inside=0)
Matus Fabian5f224992018-01-25 21:59:16 -08001715 sm = self.vapi.nat44_static_mapping_dump()
1716 self.assertEqual(len(sm), 1)
1717 self.assertEqual((sm[0].tag).split('\0', 1)[0], '')
Matus Fabian9dba7812018-01-31 01:13:23 -08001718 self.assertEqual(sm[0].protocol, 0)
1719 self.assertEqual(sm[0].local_port, 0)
1720 self.assertEqual(sm[0].external_port, 0)
Matus Fabiande886752016-12-07 03:38:19 -08001721
1722 # in2out
1723 pkts = self.create_stream_in(self.pg0, self.pg1)
1724 self.pg0.add_stream(pkts)
1725 self.pg_enable_capture(self.pg_interfaces)
1726 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001727 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001728 self.verify_capture_out(capture, nat_ip, True)
1729
1730 # out2in
1731 pkts = self.create_stream_out(self.pg1, nat_ip)
1732 self.pg1.add_stream(pkts)
1733 self.pg_enable_capture(self.pg_interfaces)
1734 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001735 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001736 self.verify_capture_in(capture, self.pg0)
1737
1738 def test_static_out(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001739 """ 1:1 NAT initialized from outside network """
Matus Fabiande886752016-12-07 03:38:19 -08001740
1741 nat_ip = "10.0.0.20"
1742 self.tcp_port_out = 6303
1743 self.udp_port_out = 6304
1744 self.icmp_id_out = 6305
Matus Fabian5f224992018-01-25 21:59:16 -08001745 tag = "testTAG"
Matus Fabiande886752016-12-07 03:38:19 -08001746
Matus Fabian5f224992018-01-25 21:59:16 -08001747 self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip, tag=tag)
Matus Fabian2ba92e32017-08-21 07:05:03 -07001748 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1749 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1750 is_inside=0)
Matus Fabian5f224992018-01-25 21:59:16 -08001751 sm = self.vapi.nat44_static_mapping_dump()
1752 self.assertEqual(len(sm), 1)
1753 self.assertEqual((sm[0].tag).split('\0', 1)[0], tag)
Matus Fabiande886752016-12-07 03:38:19 -08001754
1755 # out2in
1756 pkts = self.create_stream_out(self.pg1, nat_ip)
1757 self.pg1.add_stream(pkts)
1758 self.pg_enable_capture(self.pg_interfaces)
1759 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001760 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001761 self.verify_capture_in(capture, self.pg0)
1762
1763 # in2out
1764 pkts = self.create_stream_in(self.pg0, self.pg1)
1765 self.pg0.add_stream(pkts)
1766 self.pg_enable_capture(self.pg_interfaces)
1767 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001768 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001769 self.verify_capture_out(capture, nat_ip, True)
1770
1771 def test_static_with_port_in(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001772 """ 1:1 NAPT initialized from inside network """
Matus Fabiande886752016-12-07 03:38:19 -08001773
1774 self.tcp_port_out = 3606
1775 self.udp_port_out = 3607
1776 self.icmp_id_out = 3608
1777
Matus Fabian2ba92e32017-08-21 07:05:03 -07001778 self.nat44_add_address(self.nat_addr)
1779 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1780 self.tcp_port_in, self.tcp_port_out,
1781 proto=IP_PROTOS.tcp)
1782 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1783 self.udp_port_in, self.udp_port_out,
1784 proto=IP_PROTOS.udp)
1785 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1786 self.icmp_id_in, self.icmp_id_out,
1787 proto=IP_PROTOS.icmp)
1788 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1789 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1790 is_inside=0)
Matus Fabiande886752016-12-07 03:38:19 -08001791
1792 # in2out
1793 pkts = self.create_stream_in(self.pg0, self.pg1)
1794 self.pg0.add_stream(pkts)
1795 self.pg_enable_capture(self.pg_interfaces)
1796 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001797 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001798 self.verify_capture_out(capture)
1799
1800 # out2in
1801 pkts = self.create_stream_out(self.pg1)
1802 self.pg1.add_stream(pkts)
1803 self.pg_enable_capture(self.pg_interfaces)
1804 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001805 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001806 self.verify_capture_in(capture, self.pg0)
1807
1808 def test_static_with_port_out(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001809 """ 1:1 NAPT initialized from outside network """
Matus Fabiande886752016-12-07 03:38:19 -08001810
1811 self.tcp_port_out = 30606
1812 self.udp_port_out = 30607
1813 self.icmp_id_out = 30608
1814
Matus Fabian2ba92e32017-08-21 07:05:03 -07001815 self.nat44_add_address(self.nat_addr)
1816 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1817 self.tcp_port_in, self.tcp_port_out,
1818 proto=IP_PROTOS.tcp)
1819 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1820 self.udp_port_in, self.udp_port_out,
1821 proto=IP_PROTOS.udp)
1822 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
1823 self.icmp_id_in, self.icmp_id_out,
1824 proto=IP_PROTOS.icmp)
1825 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1826 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1827 is_inside=0)
Matus Fabiande886752016-12-07 03:38:19 -08001828
1829 # out2in
1830 pkts = self.create_stream_out(self.pg1)
1831 self.pg1.add_stream(pkts)
1832 self.pg_enable_capture(self.pg_interfaces)
1833 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001834 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001835 self.verify_capture_in(capture, self.pg0)
1836
1837 # in2out
1838 pkts = self.create_stream_in(self.pg0, self.pg1)
1839 self.pg0.add_stream(pkts)
1840 self.pg_enable_capture(self.pg_interfaces)
1841 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001842 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001843 self.verify_capture_out(capture)
1844
1845 def test_static_vrf_aware(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001846 """ 1:1 NAT VRF awareness """
Matus Fabiande886752016-12-07 03:38:19 -08001847
1848 nat_ip1 = "10.0.0.30"
1849 nat_ip2 = "10.0.0.40"
1850 self.tcp_port_out = 6303
1851 self.udp_port_out = 6304
1852 self.icmp_id_out = 6305
1853
Matus Fabian2ba92e32017-08-21 07:05:03 -07001854 self.nat44_add_static_mapping(self.pg4.remote_ip4, nat_ip1,
1855 vrf_id=10)
1856 self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip2,
1857 vrf_id=10)
1858 self.vapi.nat44_interface_add_del_feature(self.pg3.sw_if_index,
1859 is_inside=0)
1860 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1861 self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index)
Matus Fabiande886752016-12-07 03:38:19 -08001862
Matus Fabian2ba92e32017-08-21 07:05:03 -07001863 # inside interface VRF match NAT44 static mapping VRF
Matus Fabiande886752016-12-07 03:38:19 -08001864 pkts = self.create_stream_in(self.pg4, self.pg3)
1865 self.pg4.add_stream(pkts)
1866 self.pg_enable_capture(self.pg_interfaces)
1867 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001868 capture = self.pg3.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001869 self.verify_capture_out(capture, nat_ip1, True)
1870
Matus Fabian2ba92e32017-08-21 07:05:03 -07001871 # inside interface VRF don't match NAT44 static mapping VRF (packets
Matus Fabiande886752016-12-07 03:38:19 -08001872 # are dropped)
1873 pkts = self.create_stream_in(self.pg0, self.pg3)
1874 self.pg0.add_stream(pkts)
1875 self.pg_enable_capture(self.pg_interfaces)
1876 self.pg_start()
Klement Sekera9225dee2016-12-12 08:36:58 +01001877 self.pg3.assert_nothing_captured()
Matus Fabiande886752016-12-07 03:38:19 -08001878
Matus Fabianb793d092018-01-31 05:50:21 -08001879 def test_dynamic_to_static(self):
1880 """ Switch from dynamic translation to 1:1NAT """
1881 nat_ip = "10.0.0.10"
1882 self.tcp_port_out = 6303
1883 self.udp_port_out = 6304
1884 self.icmp_id_out = 6305
1885
1886 self.nat44_add_address(self.nat_addr)
1887 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1888 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1889 is_inside=0)
1890
1891 # dynamic
1892 pkts = self.create_stream_in(self.pg0, self.pg1)
1893 self.pg0.add_stream(pkts)
1894 self.pg_enable_capture(self.pg_interfaces)
1895 self.pg_start()
1896 capture = self.pg1.get_capture(len(pkts))
1897 self.verify_capture_out(capture)
1898
1899 # 1:1NAT
1900 self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip)
1901 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
1902 self.assertEqual(len(sessions), 0)
1903 pkts = self.create_stream_in(self.pg0, self.pg1)
1904 self.pg0.add_stream(pkts)
1905 self.pg_enable_capture(self.pg_interfaces)
1906 self.pg_start()
1907 capture = self.pg1.get_capture(len(pkts))
1908 self.verify_capture_out(capture, nat_ip, True)
1909
Matus Fabianab7a8052017-11-28 04:29:41 -08001910 def test_identity_nat(self):
1911 """ Identity NAT """
1912
1913 self.vapi.nat44_add_del_identity_mapping(ip=self.pg0.remote_ip4n)
1914 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1915 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
1916 is_inside=0)
1917
1918 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
1919 IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) /
1920 TCP(sport=12345, dport=56789))
1921 self.pg1.add_stream(p)
1922 self.pg_enable_capture(self.pg_interfaces)
1923 self.pg_start()
1924 capture = self.pg0.get_capture(1)
1925 p = capture[0]
1926 try:
1927 ip = p[IP]
1928 tcp = p[TCP]
1929 self.assertEqual(ip.dst, self.pg0.remote_ip4)
1930 self.assertEqual(ip.src, self.pg1.remote_ip4)
1931 self.assertEqual(tcp.dport, 56789)
1932 self.assertEqual(tcp.sport, 12345)
Klement Sekerad81ae412018-05-16 10:52:54 +02001933 self.assert_packet_checksums_valid(p)
Matus Fabianab7a8052017-11-28 04:29:41 -08001934 except:
1935 self.logger.error(ppp("Unexpected or invalid packet:", p))
1936 raise
1937
Matus Fabiane2f4e2f2018-10-07 21:28:23 -07001938 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
1939 self.assertEqual(len(sessions), 0)
Matus Fabian82b4ceb2018-10-11 04:28:48 -07001940 self.vapi.nat44_add_del_identity_mapping(ip=self.pg0.remote_ip4n,
1941 vrf_id=1)
1942 identity_mappings = self.vapi.nat44_identity_mapping_dump()
1943 self.assertEqual(len(identity_mappings), 2)
Matus Fabiane2f4e2f2018-10-07 21:28:23 -07001944
Matus Fabiande886752016-12-07 03:38:19 -08001945 def test_multiple_inside_interfaces(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07001946 """ NAT44 multiple non-overlapping address space inside interfaces """
Matus Fabiande886752016-12-07 03:38:19 -08001947
Matus Fabian2ba92e32017-08-21 07:05:03 -07001948 self.nat44_add_address(self.nat_addr)
1949 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
1950 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index)
1951 self.vapi.nat44_interface_add_del_feature(self.pg3.sw_if_index,
1952 is_inside=0)
Matus Fabiande886752016-12-07 03:38:19 -08001953
Matus Fabian2ba92e32017-08-21 07:05:03 -07001954 # between two NAT44 inside interfaces (no translation)
Matus Fabian675a69c2017-01-18 01:46:01 -08001955 pkts = self.create_stream_in(self.pg0, self.pg1)
1956 self.pg0.add_stream(pkts)
1957 self.pg_enable_capture(self.pg_interfaces)
1958 self.pg_start()
1959 capture = self.pg1.get_capture(len(pkts))
1960 self.verify_capture_no_translation(capture, self.pg0, self.pg1)
1961
Matus Fabian2ba92e32017-08-21 07:05:03 -07001962 # from NAT44 inside to interface without NAT44 feature (no translation)
Matus Fabian675a69c2017-01-18 01:46:01 -08001963 pkts = self.create_stream_in(self.pg0, self.pg2)
1964 self.pg0.add_stream(pkts)
1965 self.pg_enable_capture(self.pg_interfaces)
1966 self.pg_start()
1967 capture = self.pg2.get_capture(len(pkts))
1968 self.verify_capture_no_translation(capture, self.pg0, self.pg2)
1969
Matus Fabiande886752016-12-07 03:38:19 -08001970 # in2out 1st interface
1971 pkts = self.create_stream_in(self.pg0, self.pg3)
1972 self.pg0.add_stream(pkts)
1973 self.pg_enable_capture(self.pg_interfaces)
1974 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001975 capture = self.pg3.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001976 self.verify_capture_out(capture)
1977
1978 # out2in 1st interface
1979 pkts = self.create_stream_out(self.pg3)
1980 self.pg3.add_stream(pkts)
1981 self.pg_enable_capture(self.pg_interfaces)
1982 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001983 capture = self.pg0.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001984 self.verify_capture_in(capture, self.pg0)
1985
1986 # in2out 2nd interface
1987 pkts = self.create_stream_in(self.pg1, self.pg3)
1988 self.pg1.add_stream(pkts)
1989 self.pg_enable_capture(self.pg_interfaces)
1990 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001991 capture = self.pg3.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08001992 self.verify_capture_out(capture)
1993
1994 # out2in 2nd interface
1995 pkts = self.create_stream_out(self.pg3)
1996 self.pg3.add_stream(pkts)
1997 self.pg_enable_capture(self.pg_interfaces)
1998 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01001999 capture = self.pg1.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002000 self.verify_capture_in(capture, self.pg1)
2001
Matus Fabiande886752016-12-07 03:38:19 -08002002 def test_inside_overlapping_interfaces(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002003 """ NAT44 multiple inside interfaces with overlapping address space """
Matus Fabiande886752016-12-07 03:38:19 -08002004
Matus Fabian675a69c2017-01-18 01:46:01 -08002005 static_nat_ip = "10.0.0.10"
Matus Fabian2ba92e32017-08-21 07:05:03 -07002006 self.nat44_add_address(self.nat_addr)
2007 self.vapi.nat44_interface_add_del_feature(self.pg3.sw_if_index,
2008 is_inside=0)
2009 self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index)
2010 self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index)
2011 self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index)
2012 self.nat44_add_static_mapping(self.pg6.remote_ip4, static_nat_ip,
2013 vrf_id=20)
Matus Fabian675a69c2017-01-18 01:46:01 -08002014
Matus Fabian2ba92e32017-08-21 07:05:03 -07002015 # between NAT44 inside interfaces with same VRF (no translation)
Matus Fabian675a69c2017-01-18 01:46:01 -08002016 pkts = self.create_stream_in(self.pg4, self.pg5)
2017 self.pg4.add_stream(pkts)
2018 self.pg_enable_capture(self.pg_interfaces)
2019 self.pg_start()
2020 capture = self.pg5.get_capture(len(pkts))
2021 self.verify_capture_no_translation(capture, self.pg4, self.pg5)
2022
Matus Fabian2ba92e32017-08-21 07:05:03 -07002023 # between NAT44 inside interfaces with different VRF (hairpinning)
Matus Fabian675a69c2017-01-18 01:46:01 -08002024 p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) /
2025 IP(src=self.pg4.remote_ip4, dst=static_nat_ip) /
2026 TCP(sport=1234, dport=5678))
2027 self.pg4.add_stream(p)
2028 self.pg_enable_capture(self.pg_interfaces)
2029 self.pg_start()
2030 capture = self.pg6.get_capture(1)
2031 p = capture[0]
2032 try:
2033 ip = p[IP]
2034 tcp = p[TCP]
Matus Fabian2ba92e32017-08-21 07:05:03 -07002035 self.assertEqual(ip.src, self.nat_addr)
Matus Fabian675a69c2017-01-18 01:46:01 -08002036 self.assertEqual(ip.dst, self.pg6.remote_ip4)
2037 self.assertNotEqual(tcp.sport, 1234)
2038 self.assertEqual(tcp.dport, 5678)
2039 except:
2040 self.logger.error(ppp("Unexpected or invalid packet:", p))
2041 raise
Matus Fabiande886752016-12-07 03:38:19 -08002042
2043 # in2out 1st interface
2044 pkts = self.create_stream_in(self.pg4, self.pg3)
2045 self.pg4.add_stream(pkts)
2046 self.pg_enable_capture(self.pg_interfaces)
2047 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002048 capture = self.pg3.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002049 self.verify_capture_out(capture)
2050
2051 # out2in 1st interface
2052 pkts = self.create_stream_out(self.pg3)
2053 self.pg3.add_stream(pkts)
2054 self.pg_enable_capture(self.pg_interfaces)
2055 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002056 capture = self.pg4.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002057 self.verify_capture_in(capture, self.pg4)
2058
2059 # in2out 2nd interface
2060 pkts = self.create_stream_in(self.pg5, self.pg3)
2061 self.pg5.add_stream(pkts)
2062 self.pg_enable_capture(self.pg_interfaces)
2063 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002064 capture = self.pg3.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002065 self.verify_capture_out(capture)
2066
2067 # out2in 2nd interface
2068 pkts = self.create_stream_out(self.pg3)
2069 self.pg3.add_stream(pkts)
2070 self.pg_enable_capture(self.pg_interfaces)
2071 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002072 capture = self.pg5.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002073 self.verify_capture_in(capture, self.pg5)
2074
magalik23caa882017-02-08 23:25:45 -08002075 # pg5 session dump
Matus Fabian2ba92e32017-08-21 07:05:03 -07002076 addresses = self.vapi.nat44_address_dump()
magalik23caa882017-02-08 23:25:45 -08002077 self.assertEqual(len(addresses), 1)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002078 sessions = self.vapi.nat44_user_session_dump(self.pg5.remote_ip4n, 10)
magalik23caa882017-02-08 23:25:45 -08002079 self.assertEqual(len(sessions), 3)
2080 for session in sessions:
2081 self.assertFalse(session.is_static)
2082 self.assertEqual(session.inside_ip_address[0:4],
2083 self.pg5.remote_ip4n)
2084 self.assertEqual(session.outside_ip_address,
2085 addresses[0].ip_address)
2086 self.assertEqual(sessions[0].protocol, IP_PROTOS.tcp)
2087 self.assertEqual(sessions[1].protocol, IP_PROTOS.udp)
2088 self.assertEqual(sessions[2].protocol, IP_PROTOS.icmp)
2089 self.assertEqual(sessions[0].inside_port, self.tcp_port_in)
2090 self.assertEqual(sessions[1].inside_port, self.udp_port_in)
2091 self.assertEqual(sessions[2].inside_port, self.icmp_id_in)
2092 self.assertEqual(sessions[0].outside_port, self.tcp_port_out)
2093 self.assertEqual(sessions[1].outside_port, self.udp_port_out)
2094 self.assertEqual(sessions[2].outside_port, self.icmp_id_out)
2095
Matus Fabiande886752016-12-07 03:38:19 -08002096 # in2out 3rd interface
2097 pkts = self.create_stream_in(self.pg6, self.pg3)
2098 self.pg6.add_stream(pkts)
2099 self.pg_enable_capture(self.pg_interfaces)
2100 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002101 capture = self.pg3.get_capture(len(pkts))
Matus Fabian675a69c2017-01-18 01:46:01 -08002102 self.verify_capture_out(capture, static_nat_ip, True)
Matus Fabiande886752016-12-07 03:38:19 -08002103
2104 # out2in 3rd interface
Matus Fabian675a69c2017-01-18 01:46:01 -08002105 pkts = self.create_stream_out(self.pg3, static_nat_ip)
Matus Fabiande886752016-12-07 03:38:19 -08002106 self.pg3.add_stream(pkts)
2107 self.pg_enable_capture(self.pg_interfaces)
2108 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002109 capture = self.pg6.get_capture(len(pkts))
Matus Fabiande886752016-12-07 03:38:19 -08002110 self.verify_capture_in(capture, self.pg6)
2111
magalik23caa882017-02-08 23:25:45 -08002112 # general user and session dump verifications
Matus Fabian2ba92e32017-08-21 07:05:03 -07002113 users = self.vapi.nat44_user_dump()
magalik23caa882017-02-08 23:25:45 -08002114 self.assertTrue(len(users) >= 3)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002115 addresses = self.vapi.nat44_address_dump()
magalik23caa882017-02-08 23:25:45 -08002116 self.assertEqual(len(addresses), 1)
2117 for user in users:
Matus Fabian2ba92e32017-08-21 07:05:03 -07002118 sessions = self.vapi.nat44_user_session_dump(user.ip_address,
2119 user.vrf_id)
magalik23caa882017-02-08 23:25:45 -08002120 for session in sessions:
2121 self.assertEqual(user.ip_address, session.inside_ip_address)
2122 self.assertTrue(session.total_bytes > session.total_pkts > 0)
2123 self.assertTrue(session.protocol in
2124 [IP_PROTOS.tcp, IP_PROTOS.udp,
2125 IP_PROTOS.icmp])
Matus Fabian70a26ac2018-05-14 06:20:28 -07002126 self.assertFalse(session.ext_host_valid)
magalik23caa882017-02-08 23:25:45 -08002127
2128 # pg4 session dump
Matus Fabian2ba92e32017-08-21 07:05:03 -07002129 sessions = self.vapi.nat44_user_session_dump(self.pg4.remote_ip4n, 10)
magalik23caa882017-02-08 23:25:45 -08002130 self.assertTrue(len(sessions) >= 4)
2131 for session in sessions:
2132 self.assertFalse(session.is_static)
2133 self.assertEqual(session.inside_ip_address[0:4],
2134 self.pg4.remote_ip4n)
2135 self.assertEqual(session.outside_ip_address,
2136 addresses[0].ip_address)
2137
2138 # pg6 session dump
Matus Fabian2ba92e32017-08-21 07:05:03 -07002139 sessions = self.vapi.nat44_user_session_dump(self.pg6.remote_ip4n, 20)
magalik23caa882017-02-08 23:25:45 -08002140 self.assertTrue(len(sessions) >= 3)
2141 for session in sessions:
2142 self.assertTrue(session.is_static)
2143 self.assertEqual(session.inside_ip_address[0:4],
2144 self.pg6.remote_ip4n)
2145 self.assertEqual(map(ord, session.outside_ip_address[0:4]),
2146 map(int, static_nat_ip.split('.')))
2147 self.assertTrue(session.inside_port in
2148 [self.tcp_port_in, self.udp_port_in,
2149 self.icmp_id_in])
2150
Matus Fabianf78a70d2016-12-12 04:30:39 -08002151 def test_hairpinning(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002152 """ NAT44 hairpinning - 1:1 NAPT """
Matus Fabianf78a70d2016-12-12 04:30:39 -08002153
2154 host = self.pg0.remote_hosts[0]
2155 server = self.pg0.remote_hosts[1]
2156 host_in_port = 1234
2157 host_out_port = 0
2158 server_in_port = 5678
2159 server_out_port = 8765
2160
Matus Fabian2ba92e32017-08-21 07:05:03 -07002161 self.nat44_add_address(self.nat_addr)
2162 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2163 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2164 is_inside=0)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002165 # add static mapping for server
Matus Fabian2ba92e32017-08-21 07:05:03 -07002166 self.nat44_add_static_mapping(server.ip4, self.nat_addr,
2167 server_in_port, server_out_port,
2168 proto=IP_PROTOS.tcp)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002169
2170 # send packet from host to server
2171 p = (Ether(src=host.mac, dst=self.pg0.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002172 IP(src=host.ip4, dst=self.nat_addr) /
Matus Fabianf78a70d2016-12-12 04:30:39 -08002173 TCP(sport=host_in_port, dport=server_out_port))
2174 self.pg0.add_stream(p)
2175 self.pg_enable_capture(self.pg_interfaces)
2176 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002177 capture = self.pg0.get_capture(1)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002178 p = capture[0]
2179 try:
2180 ip = p[IP]
2181 tcp = p[TCP]
Matus Fabian2ba92e32017-08-21 07:05:03 -07002182 self.assertEqual(ip.src, self.nat_addr)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002183 self.assertEqual(ip.dst, server.ip4)
2184 self.assertNotEqual(tcp.sport, host_in_port)
2185 self.assertEqual(tcp.dport, server_in_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02002186 self.assert_packet_checksums_valid(p)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002187 host_out_port = tcp.sport
2188 except:
Klement Sekera9225dee2016-12-12 08:36:58 +01002189 self.logger.error(ppp("Unexpected or invalid packet:", p))
Matus Fabianf78a70d2016-12-12 04:30:39 -08002190 raise
2191
2192 # send reply from server to host
2193 p = (Ether(src=server.mac, dst=self.pg0.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002194 IP(src=server.ip4, dst=self.nat_addr) /
Matus Fabianf78a70d2016-12-12 04:30:39 -08002195 TCP(sport=server_in_port, dport=host_out_port))
2196 self.pg0.add_stream(p)
2197 self.pg_enable_capture(self.pg_interfaces)
2198 self.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +01002199 capture = self.pg0.get_capture(1)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002200 p = capture[0]
2201 try:
2202 ip = p[IP]
2203 tcp = p[TCP]
Matus Fabian2ba92e32017-08-21 07:05:03 -07002204 self.assertEqual(ip.src, self.nat_addr)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002205 self.assertEqual(ip.dst, host.ip4)
2206 self.assertEqual(tcp.sport, server_out_port)
2207 self.assertEqual(tcp.dport, host_in_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02002208 self.assert_packet_checksums_valid(p)
Matus Fabianf78a70d2016-12-12 04:30:39 -08002209 except:
Matus Fabiane82488f2018-01-18 03:38:45 -08002210 self.logger.error(ppp("Unexpected or invalid packet:", p))
Matus Fabianf78a70d2016-12-12 04:30:39 -08002211 raise
2212
Matus Fabian6fa74c62017-06-05 05:55:48 -07002213 def test_hairpinning2(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002214 """ NAT44 hairpinning - 1:1 NAT"""
Matus Fabian6fa74c62017-06-05 05:55:48 -07002215
2216 server1_nat_ip = "10.0.0.10"
2217 server2_nat_ip = "10.0.0.11"
2218 host = self.pg0.remote_hosts[0]
2219 server1 = self.pg0.remote_hosts[1]
2220 server2 = self.pg0.remote_hosts[2]
2221 server_tcp_port = 22
2222 server_udp_port = 20
2223
Matus Fabian2ba92e32017-08-21 07:05:03 -07002224 self.nat44_add_address(self.nat_addr)
2225 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2226 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2227 is_inside=0)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002228
2229 # add static mapping for servers
Matus Fabian2ba92e32017-08-21 07:05:03 -07002230 self.nat44_add_static_mapping(server1.ip4, server1_nat_ip)
2231 self.nat44_add_static_mapping(server2.ip4, server2_nat_ip)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002232
2233 # host to server1
2234 pkts = []
2235 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2236 IP(src=host.ip4, dst=server1_nat_ip) /
2237 TCP(sport=self.tcp_port_in, dport=server_tcp_port))
2238 pkts.append(p)
2239 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2240 IP(src=host.ip4, dst=server1_nat_ip) /
2241 UDP(sport=self.udp_port_in, dport=server_udp_port))
2242 pkts.append(p)
2243 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2244 IP(src=host.ip4, dst=server1_nat_ip) /
2245 ICMP(id=self.icmp_id_in, type='echo-request'))
2246 pkts.append(p)
2247 self.pg0.add_stream(pkts)
2248 self.pg_enable_capture(self.pg_interfaces)
2249 self.pg_start()
2250 capture = self.pg0.get_capture(len(pkts))
2251 for packet in capture:
2252 try:
Matus Fabian2ba92e32017-08-21 07:05:03 -07002253 self.assertEqual(packet[IP].src, self.nat_addr)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002254 self.assertEqual(packet[IP].dst, server1.ip4)
2255 if packet.haslayer(TCP):
2256 self.assertNotEqual(packet[TCP].sport, self.tcp_port_in)
2257 self.assertEqual(packet[TCP].dport, server_tcp_port)
2258 self.tcp_port_out = packet[TCP].sport
Klement Sekerad81ae412018-05-16 10:52:54 +02002259 self.assert_packet_checksums_valid(packet)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002260 elif packet.haslayer(UDP):
2261 self.assertNotEqual(packet[UDP].sport, self.udp_port_in)
2262 self.assertEqual(packet[UDP].dport, server_udp_port)
2263 self.udp_port_out = packet[UDP].sport
2264 else:
2265 self.assertNotEqual(packet[ICMP].id, self.icmp_id_in)
2266 self.icmp_id_out = packet[ICMP].id
2267 except:
2268 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2269 raise
2270
2271 # server1 to host
2272 pkts = []
2273 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002274 IP(src=server1.ip4, dst=self.nat_addr) /
Matus Fabian6fa74c62017-06-05 05:55:48 -07002275 TCP(sport=server_tcp_port, dport=self.tcp_port_out))
2276 pkts.append(p)
2277 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002278 IP(src=server1.ip4, dst=self.nat_addr) /
Matus Fabian6fa74c62017-06-05 05:55:48 -07002279 UDP(sport=server_udp_port, dport=self.udp_port_out))
2280 pkts.append(p)
2281 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002282 IP(src=server1.ip4, dst=self.nat_addr) /
Matus Fabian6fa74c62017-06-05 05:55:48 -07002283 ICMP(id=self.icmp_id_out, type='echo-reply'))
2284 pkts.append(p)
2285 self.pg0.add_stream(pkts)
2286 self.pg_enable_capture(self.pg_interfaces)
2287 self.pg_start()
2288 capture = self.pg0.get_capture(len(pkts))
2289 for packet in capture:
2290 try:
2291 self.assertEqual(packet[IP].src, server1_nat_ip)
2292 self.assertEqual(packet[IP].dst, host.ip4)
2293 if packet.haslayer(TCP):
2294 self.assertEqual(packet[TCP].dport, self.tcp_port_in)
2295 self.assertEqual(packet[TCP].sport, server_tcp_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02002296 self.assert_packet_checksums_valid(packet)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002297 elif packet.haslayer(UDP):
2298 self.assertEqual(packet[UDP].dport, self.udp_port_in)
2299 self.assertEqual(packet[UDP].sport, server_udp_port)
2300 else:
2301 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
2302 except:
2303 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2304 raise
2305
2306 # server2 to server1
2307 pkts = []
2308 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2309 IP(src=server2.ip4, dst=server1_nat_ip) /
2310 TCP(sport=self.tcp_port_in, dport=server_tcp_port))
2311 pkts.append(p)
2312 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2313 IP(src=server2.ip4, dst=server1_nat_ip) /
2314 UDP(sport=self.udp_port_in, dport=server_udp_port))
2315 pkts.append(p)
2316 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2317 IP(src=server2.ip4, dst=server1_nat_ip) /
2318 ICMP(id=self.icmp_id_in, type='echo-request'))
2319 pkts.append(p)
2320 self.pg0.add_stream(pkts)
2321 self.pg_enable_capture(self.pg_interfaces)
2322 self.pg_start()
2323 capture = self.pg0.get_capture(len(pkts))
2324 for packet in capture:
2325 try:
2326 self.assertEqual(packet[IP].src, server2_nat_ip)
2327 self.assertEqual(packet[IP].dst, server1.ip4)
2328 if packet.haslayer(TCP):
2329 self.assertEqual(packet[TCP].sport, self.tcp_port_in)
2330 self.assertEqual(packet[TCP].dport, server_tcp_port)
2331 self.tcp_port_out = packet[TCP].sport
Klement Sekerad81ae412018-05-16 10:52:54 +02002332 self.assert_packet_checksums_valid(packet)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002333 elif packet.haslayer(UDP):
2334 self.assertEqual(packet[UDP].sport, self.udp_port_in)
2335 self.assertEqual(packet[UDP].dport, server_udp_port)
2336 self.udp_port_out = packet[UDP].sport
2337 else:
2338 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
2339 self.icmp_id_out = packet[ICMP].id
2340 except:
2341 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2342 raise
2343
2344 # server1 to server2
2345 pkts = []
2346 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2347 IP(src=server1.ip4, dst=server2_nat_ip) /
2348 TCP(sport=server_tcp_port, dport=self.tcp_port_out))
2349 pkts.append(p)
2350 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2351 IP(src=server1.ip4, dst=server2_nat_ip) /
2352 UDP(sport=server_udp_port, dport=self.udp_port_out))
2353 pkts.append(p)
2354 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2355 IP(src=server1.ip4, dst=server2_nat_ip) /
2356 ICMP(id=self.icmp_id_out, type='echo-reply'))
2357 pkts.append(p)
2358 self.pg0.add_stream(pkts)
2359 self.pg_enable_capture(self.pg_interfaces)
2360 self.pg_start()
2361 capture = self.pg0.get_capture(len(pkts))
2362 for packet in capture:
2363 try:
2364 self.assertEqual(packet[IP].src, server1_nat_ip)
2365 self.assertEqual(packet[IP].dst, server2.ip4)
2366 if packet.haslayer(TCP):
2367 self.assertEqual(packet[TCP].dport, self.tcp_port_in)
2368 self.assertEqual(packet[TCP].sport, server_tcp_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02002369 self.assert_packet_checksums_valid(packet)
Matus Fabian6fa74c62017-06-05 05:55:48 -07002370 elif packet.haslayer(UDP):
2371 self.assertEqual(packet[UDP].dport, self.udp_port_in)
2372 self.assertEqual(packet[UDP].sport, server_udp_port)
2373 else:
2374 self.assertEqual(packet[ICMP].id, self.icmp_id_in)
2375 except:
2376 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2377 raise
2378
Matus Fabian9902fcd2016-12-21 23:58:46 -08002379 def test_max_translations_per_user(self):
2380 """ MAX translations per user - recycle the least recently used """
2381
Matus Fabian2ba92e32017-08-21 07:05:03 -07002382 self.nat44_add_address(self.nat_addr)
2383 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2384 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2385 is_inside=0)
Matus Fabian9902fcd2016-12-21 23:58:46 -08002386
2387 # get maximum number of translations per user
Matus Fabian2ba92e32017-08-21 07:05:03 -07002388 nat44_config = self.vapi.nat_show_config()
Matus Fabian9902fcd2016-12-21 23:58:46 -08002389
2390 # send more than maximum number of translations per user packets
Matus Fabian2ba92e32017-08-21 07:05:03 -07002391 pkts_num = nat44_config.max_translations_per_user + 5
Matus Fabian9902fcd2016-12-21 23:58:46 -08002392 pkts = []
2393 for port in range(0, pkts_num):
2394 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2395 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
2396 TCP(sport=1025 + port))
2397 pkts.append(p)
2398 self.pg0.add_stream(pkts)
2399 self.pg_enable_capture(self.pg_interfaces)
2400 self.pg_start()
2401
2402 # verify number of translated packet
Klement Sekeradab231a2016-12-21 08:50:14 +01002403 self.pg1.get_capture(pkts_num)
Matus Fabian9902fcd2016-12-21 23:58:46 -08002404
Matus Fabian132dc492018-05-09 04:51:03 -07002405 users = self.vapi.nat44_user_dump()
2406 for user in users:
2407 if user.ip_address == self.pg0.remote_ip4n:
2408 self.assertEqual(user.nsessions,
2409 nat44_config.max_translations_per_user)
2410 self.assertEqual(user.nstaticsessions, 0)
2411
2412 tcp_port = 22
2413 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
2414 tcp_port, tcp_port,
2415 proto=IP_PROTOS.tcp)
2416 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2417 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
2418 TCP(sport=tcp_port))
2419 self.pg0.add_stream(p)
2420 self.pg_enable_capture(self.pg_interfaces)
2421 self.pg_start()
2422 self.pg1.get_capture(1)
2423 users = self.vapi.nat44_user_dump()
2424 for user in users:
2425 if user.ip_address == self.pg0.remote_ip4n:
2426 self.assertEqual(user.nsessions,
2427 nat44_config.max_translations_per_user - 1)
2428 self.assertEqual(user.nstaticsessions, 1)
2429
Matus Fabian8bf68e82017-01-12 04:24:35 -08002430 def test_interface_addr(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002431 """ Acquire NAT44 addresses from interface """
2432 self.vapi.nat44_add_interface_addr(self.pg7.sw_if_index)
Matus Fabian8bf68e82017-01-12 04:24:35 -08002433
2434 # no address in NAT pool
Matus Fabian2ba92e32017-08-21 07:05:03 -07002435 adresses = self.vapi.nat44_address_dump()
Matus Fabian8bf68e82017-01-12 04:24:35 -08002436 self.assertEqual(0, len(adresses))
2437
2438 # configure interface address and check NAT address pool
2439 self.pg7.config_ip4()
Matus Fabian2ba92e32017-08-21 07:05:03 -07002440 adresses = self.vapi.nat44_address_dump()
Matus Fabian8bf68e82017-01-12 04:24:35 -08002441 self.assertEqual(1, len(adresses))
Matus Fabian36532bd2017-01-23 23:42:28 -08002442 self.assertEqual(adresses[0].ip_address[0:4], self.pg7.local_ip4n)
Matus Fabian8bf68e82017-01-12 04:24:35 -08002443
2444 # remove interface address and check NAT address pool
2445 self.pg7.unconfig_ip4()
Matus Fabian2ba92e32017-08-21 07:05:03 -07002446 adresses = self.vapi.nat44_address_dump()
Matus Fabian8bf68e82017-01-12 04:24:35 -08002447 self.assertEqual(0, len(adresses))
2448
Matus Fabian36532bd2017-01-23 23:42:28 -08002449 def test_interface_addr_static_mapping(self):
2450 """ Static mapping with addresses from interface """
Matus Fabian5f224992018-01-25 21:59:16 -08002451 tag = "testTAG"
2452
Matus Fabian2ba92e32017-08-21 07:05:03 -07002453 self.vapi.nat44_add_interface_addr(self.pg7.sw_if_index)
2454 self.nat44_add_static_mapping(
2455 '1.2.3.4',
Matus Fabian5f224992018-01-25 21:59:16 -08002456 external_sw_if_index=self.pg7.sw_if_index,
2457 tag=tag)
Matus Fabian36532bd2017-01-23 23:42:28 -08002458
Matus Fabiane22e5462017-02-14 23:33:43 -08002459 # static mappings with external interface
Matus Fabian2ba92e32017-08-21 07:05:03 -07002460 static_mappings = self.vapi.nat44_static_mapping_dump()
Matus Fabiane22e5462017-02-14 23:33:43 -08002461 self.assertEqual(1, len(static_mappings))
2462 self.assertEqual(self.pg7.sw_if_index,
2463 static_mappings[0].external_sw_if_index)
Matus Fabian5f224992018-01-25 21:59:16 -08002464 self.assertEqual((static_mappings[0].tag).split('\0', 1)[0], tag)
Matus Fabian36532bd2017-01-23 23:42:28 -08002465
2466 # configure interface address and check static mappings
2467 self.pg7.config_ip4()
Matus Fabian2ba92e32017-08-21 07:05:03 -07002468 static_mappings = self.vapi.nat44_static_mapping_dump()
Matus Fabianea2600a2018-03-28 04:06:26 -07002469 self.assertEqual(2, len(static_mappings))
2470 resolved = False
2471 for sm in static_mappings:
2472 if sm.external_sw_if_index == 0xFFFFFFFF:
2473 self.assertEqual(sm.external_ip_address[0:4],
2474 self.pg7.local_ip4n)
2475 self.assertEqual((sm.tag).split('\0', 1)[0], tag)
2476 resolved = True
2477 self.assertTrue(resolved)
Matus Fabian36532bd2017-01-23 23:42:28 -08002478
2479 # remove interface address and check static mappings
2480 self.pg7.unconfig_ip4()
Matus Fabian2ba92e32017-08-21 07:05:03 -07002481 static_mappings = self.vapi.nat44_static_mapping_dump()
Matus Fabianea2600a2018-03-28 04:06:26 -07002482 self.assertEqual(1, len(static_mappings))
2483 self.assertEqual(self.pg7.sw_if_index,
2484 static_mappings[0].external_sw_if_index)
2485 self.assertEqual((static_mappings[0].tag).split('\0', 1)[0], tag)
2486
2487 # configure interface address again and check static mappings
2488 self.pg7.config_ip4()
2489 static_mappings = self.vapi.nat44_static_mapping_dump()
2490 self.assertEqual(2, len(static_mappings))
2491 resolved = False
2492 for sm in static_mappings:
2493 if sm.external_sw_if_index == 0xFFFFFFFF:
2494 self.assertEqual(sm.external_ip_address[0:4],
2495 self.pg7.local_ip4n)
2496 self.assertEqual((sm.tag).split('\0', 1)[0], tag)
2497 resolved = True
2498 self.assertTrue(resolved)
2499
2500 # remove static mapping
2501 self.nat44_add_static_mapping(
2502 '1.2.3.4',
2503 external_sw_if_index=self.pg7.sw_if_index,
2504 tag=tag,
2505 is_add=0)
2506 static_mappings = self.vapi.nat44_static_mapping_dump()
Matus Fabian36532bd2017-01-23 23:42:28 -08002507 self.assertEqual(0, len(static_mappings))
2508
Matus Fabianab7a8052017-11-28 04:29:41 -08002509 def test_interface_addr_identity_nat(self):
2510 """ Identity NAT with addresses from interface """
2511
2512 port = 53053
2513 self.vapi.nat44_add_interface_addr(self.pg7.sw_if_index)
2514 self.vapi.nat44_add_del_identity_mapping(
2515 sw_if_index=self.pg7.sw_if_index,
2516 port=port,
2517 protocol=IP_PROTOS.tcp,
2518 addr_only=0)
2519
2520 # identity mappings with external interface
2521 identity_mappings = self.vapi.nat44_identity_mapping_dump()
2522 self.assertEqual(1, len(identity_mappings))
2523 self.assertEqual(self.pg7.sw_if_index,
2524 identity_mappings[0].sw_if_index)
2525
2526 # configure interface address and check identity mappings
2527 self.pg7.config_ip4()
2528 identity_mappings = self.vapi.nat44_identity_mapping_dump()
Matus Fabianea2600a2018-03-28 04:06:26 -07002529 resolved = False
2530 self.assertEqual(2, len(identity_mappings))
2531 for sm in identity_mappings:
2532 if sm.sw_if_index == 0xFFFFFFFF:
2533 self.assertEqual(identity_mappings[0].ip_address,
2534 self.pg7.local_ip4n)
2535 self.assertEqual(port, identity_mappings[0].port)
2536 self.assertEqual(IP_PROTOS.tcp, identity_mappings[0].protocol)
2537 resolved = True
2538 self.assertTrue(resolved)
Matus Fabianab7a8052017-11-28 04:29:41 -08002539
2540 # remove interface address and check identity mappings
2541 self.pg7.unconfig_ip4()
2542 identity_mappings = self.vapi.nat44_identity_mapping_dump()
Matus Fabianea2600a2018-03-28 04:06:26 -07002543 self.assertEqual(1, len(identity_mappings))
2544 self.assertEqual(self.pg7.sw_if_index,
2545 identity_mappings[0].sw_if_index)
Matus Fabianab7a8052017-11-28 04:29:41 -08002546
Matus Fabianeea28d72017-01-13 04:15:54 -08002547 def test_ipfix_nat44_sess(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002548 """ IPFIX logging NAT44 session created/delted """
Matus Fabian6631e9c2017-05-24 01:52:20 -07002549 self.ipfix_domain_id = 10
2550 self.ipfix_src_port = 20202
2551 colector_port = 30303
2552 bind_layers(UDP, IPFIX, dport=30303)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002553 self.nat44_add_address(self.nat_addr)
2554 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2555 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2556 is_inside=0)
Matus Fabianeea28d72017-01-13 04:15:54 -08002557 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
2558 src_address=self.pg3.local_ip4n,
2559 path_mtu=512,
Matus Fabian6631e9c2017-05-24 01:52:20 -07002560 template_interval=10,
2561 collector_port=colector_port)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002562 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
2563 src_port=self.ipfix_src_port)
Matus Fabianeea28d72017-01-13 04:15:54 -08002564
2565 pkts = self.create_stream_in(self.pg0, self.pg1)
2566 self.pg0.add_stream(pkts)
2567 self.pg_enable_capture(self.pg_interfaces)
2568 self.pg_start()
2569 capture = self.pg1.get_capture(len(pkts))
2570 self.verify_capture_out(capture)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002571 self.nat44_add_address(self.nat_addr, is_add=0)
Matus Fabianeea28d72017-01-13 04:15:54 -08002572 self.vapi.cli("ipfix flush") # FIXME this should be an API call
Matus Fabiana431ad12018-01-04 04:03:14 -08002573 capture = self.pg3.get_capture(9)
Matus Fabianeea28d72017-01-13 04:15:54 -08002574 ipfix = IPFIXDecoder()
2575 # first load template
2576 for p in capture:
2577 self.assertTrue(p.haslayer(IPFIX))
Matus Fabian6631e9c2017-05-24 01:52:20 -07002578 self.assertEqual(p[IP].src, self.pg3.local_ip4)
2579 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
2580 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
2581 self.assertEqual(p[UDP].dport, colector_port)
2582 self.assertEqual(p[IPFIX].observationDomainID,
2583 self.ipfix_domain_id)
Matus Fabianeea28d72017-01-13 04:15:54 -08002584 if p.haslayer(Template):
2585 ipfix.add_template(p.getlayer(Template))
2586 # verify events in data set
2587 for p in capture:
2588 if p.haslayer(Data):
2589 data = ipfix.decode_data_set(p.getlayer(Set))
2590 self.verify_ipfix_nat44_ses(data)
2591
2592 def test_ipfix_addr_exhausted(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002593 """ IPFIX logging NAT addresses exhausted """
2594 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2595 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2596 is_inside=0)
Matus Fabianeea28d72017-01-13 04:15:54 -08002597 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
2598 src_address=self.pg3.local_ip4n,
2599 path_mtu=512,
2600 template_interval=10)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002601 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
2602 src_port=self.ipfix_src_port)
Matus Fabianeea28d72017-01-13 04:15:54 -08002603
2604 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
2605 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
2606 TCP(sport=3025))
2607 self.pg0.add_stream(p)
2608 self.pg_enable_capture(self.pg_interfaces)
2609 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07002610 self.pg1.assert_nothing_captured()
2611 sleep(1)
Matus Fabianeea28d72017-01-13 04:15:54 -08002612 self.vapi.cli("ipfix flush") # FIXME this should be an API call
Matus Fabiana431ad12018-01-04 04:03:14 -08002613 capture = self.pg3.get_capture(9)
Matus Fabianeea28d72017-01-13 04:15:54 -08002614 ipfix = IPFIXDecoder()
2615 # first load template
2616 for p in capture:
2617 self.assertTrue(p.haslayer(IPFIX))
Matus Fabian6631e9c2017-05-24 01:52:20 -07002618 self.assertEqual(p[IP].src, self.pg3.local_ip4)
2619 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
2620 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
2621 self.assertEqual(p[UDP].dport, 4739)
2622 self.assertEqual(p[IPFIX].observationDomainID,
2623 self.ipfix_domain_id)
Matus Fabianeea28d72017-01-13 04:15:54 -08002624 if p.haslayer(Template):
2625 ipfix.add_template(p.getlayer(Template))
2626 # verify events in data set
2627 for p in capture:
2628 if p.haslayer(Data):
2629 data = ipfix.decode_data_set(p.getlayer(Set))
2630 self.verify_ipfix_addr_exhausted(data)
2631
Matus Fabiana431ad12018-01-04 04:03:14 -08002632 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
2633 def test_ipfix_max_sessions(self):
2634 """ IPFIX logging maximum session entries exceeded """
2635 self.nat44_add_address(self.nat_addr)
2636 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2637 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2638 is_inside=0)
2639
2640 nat44_config = self.vapi.nat_show_config()
2641 max_sessions = 10 * nat44_config.translation_buckets
2642
2643 pkts = []
2644 for i in range(0, max_sessions):
2645 src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF)
2646 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2647 IP(src=src, dst=self.pg1.remote_ip4) /
2648 TCP(sport=1025))
2649 pkts.append(p)
2650 self.pg0.add_stream(pkts)
2651 self.pg_enable_capture(self.pg_interfaces)
2652 self.pg_start()
2653
2654 self.pg1.get_capture(max_sessions)
2655 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
2656 src_address=self.pg3.local_ip4n,
2657 path_mtu=512,
2658 template_interval=10)
2659 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
2660 src_port=self.ipfix_src_port)
2661
2662 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2663 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
2664 TCP(sport=1025))
2665 self.pg0.add_stream(p)
2666 self.pg_enable_capture(self.pg_interfaces)
2667 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07002668 self.pg1.assert_nothing_captured()
2669 sleep(1)
Matus Fabiana431ad12018-01-04 04:03:14 -08002670 self.vapi.cli("ipfix flush") # FIXME this should be an API call
2671 capture = self.pg3.get_capture(9)
2672 ipfix = IPFIXDecoder()
2673 # first load template
2674 for p in capture:
2675 self.assertTrue(p.haslayer(IPFIX))
2676 self.assertEqual(p[IP].src, self.pg3.local_ip4)
2677 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
2678 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
2679 self.assertEqual(p[UDP].dport, 4739)
2680 self.assertEqual(p[IPFIX].observationDomainID,
2681 self.ipfix_domain_id)
2682 if p.haslayer(Template):
2683 ipfix.add_template(p.getlayer(Template))
2684 # verify events in data set
2685 for p in capture:
2686 if p.haslayer(Data):
2687 data = ipfix.decode_data_set(p.getlayer(Set))
2688 self.verify_ipfix_max_sessions(data, max_sessions)
2689
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002690 def test_pool_addr_fib(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002691 """ NAT44 add pool addresses to FIB """
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002692 static_addr = '10.0.0.10'
Matus Fabian2ba92e32017-08-21 07:05:03 -07002693 self.nat44_add_address(self.nat_addr)
2694 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2695 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2696 is_inside=0)
2697 self.nat44_add_static_mapping(self.pg0.remote_ip4, static_addr)
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002698
Matus Fabian2ba92e32017-08-21 07:05:03 -07002699 # NAT44 address
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002700 p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002701 ARP(op=ARP.who_has, pdst=self.nat_addr,
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002702 psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac))
2703 self.pg1.add_stream(p)
2704 self.pg_enable_capture(self.pg_interfaces)
2705 self.pg_start()
2706 capture = self.pg1.get_capture(1)
2707 self.assertTrue(capture[0].haslayer(ARP))
2708 self.assertTrue(capture[0][ARP].op, ARP.is_at)
2709
2710 # 1:1 NAT address
2711 p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') /
2712 ARP(op=ARP.who_has, pdst=static_addr,
2713 psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac))
2714 self.pg1.add_stream(p)
2715 self.pg_enable_capture(self.pg_interfaces)
2716 self.pg_start()
2717 capture = self.pg1.get_capture(1)
2718 self.assertTrue(capture[0].haslayer(ARP))
2719 self.assertTrue(capture[0][ARP].op, ARP.is_at)
2720
Matus Fabian2ba92e32017-08-21 07:05:03 -07002721 # send ARP to non-NAT44 interface
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002722 p = (Ether(src=self.pg2.remote_mac, dst='ff:ff:ff:ff:ff:ff') /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002723 ARP(op=ARP.who_has, pdst=self.nat_addr,
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002724 psrc=self.pg2.remote_ip4, hwsrc=self.pg2.remote_mac))
2725 self.pg2.add_stream(p)
2726 self.pg_enable_capture(self.pg_interfaces)
2727 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07002728 self.pg1.assert_nothing_captured()
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002729
2730 # remove addresses and verify
Matus Fabian2ba92e32017-08-21 07:05:03 -07002731 self.nat44_add_address(self.nat_addr, is_add=0)
2732 self.nat44_add_static_mapping(self.pg0.remote_ip4, static_addr,
2733 is_add=0)
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002734
2735 p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') /
Matus Fabian2ba92e32017-08-21 07:05:03 -07002736 ARP(op=ARP.who_has, pdst=self.nat_addr,
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002737 psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac))
2738 self.pg1.add_stream(p)
2739 self.pg_enable_capture(self.pg_interfaces)
2740 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07002741 self.pg1.assert_nothing_captured()
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002742
2743 p = (Ether(src=self.pg1.remote_mac, dst='ff:ff:ff:ff:ff:ff') /
2744 ARP(op=ARP.who_has, pdst=static_addr,
2745 psrc=self.pg1.remote_ip4, hwsrc=self.pg1.remote_mac))
2746 self.pg1.add_stream(p)
2747 self.pg_enable_capture(self.pg_interfaces)
2748 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07002749 self.pg1.assert_nothing_captured()
Matus Fabiane1ae29a2017-01-27 00:47:58 -08002750
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002751 def test_vrf_mode(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002752 """ NAT44 tenant VRF aware address pool mode """
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002753
2754 vrf_id1 = 1
2755 vrf_id2 = 2
2756 nat_ip1 = "10.0.0.10"
2757 nat_ip2 = "10.0.0.11"
2758
2759 self.pg0.unconfig_ip4()
2760 self.pg1.unconfig_ip4()
Neale Ranns15002542017-09-10 04:39:11 -07002761 self.vapi.ip_table_add_del(vrf_id1, is_add=1)
2762 self.vapi.ip_table_add_del(vrf_id2, is_add=1)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002763 self.pg0.set_table_ip4(vrf_id1)
2764 self.pg1.set_table_ip4(vrf_id2)
2765 self.pg0.config_ip4()
2766 self.pg1.config_ip4()
Matus Fabian8008d7c2018-07-09 01:34:20 -07002767 self.pg0.resolve_arp()
2768 self.pg1.resolve_arp()
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002769
Matus Fabian2ba92e32017-08-21 07:05:03 -07002770 self.nat44_add_address(nat_ip1, vrf_id=vrf_id1)
2771 self.nat44_add_address(nat_ip2, vrf_id=vrf_id2)
2772 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2773 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index)
2774 self.vapi.nat44_interface_add_del_feature(self.pg2.sw_if_index,
2775 is_inside=0)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002776
Matus Fabian8008d7c2018-07-09 01:34:20 -07002777 try:
2778 # first VRF
2779 pkts = self.create_stream_in(self.pg0, self.pg2)
2780 self.pg0.add_stream(pkts)
2781 self.pg_enable_capture(self.pg_interfaces)
2782 self.pg_start()
2783 capture = self.pg2.get_capture(len(pkts))
2784 self.verify_capture_out(capture, nat_ip1)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002785
Matus Fabian8008d7c2018-07-09 01:34:20 -07002786 # second VRF
2787 pkts = self.create_stream_in(self.pg1, self.pg2)
2788 self.pg1.add_stream(pkts)
2789 self.pg_enable_capture(self.pg_interfaces)
2790 self.pg_start()
2791 capture = self.pg2.get_capture(len(pkts))
2792 self.verify_capture_out(capture, nat_ip2)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002793
Matus Fabian8008d7c2018-07-09 01:34:20 -07002794 finally:
2795 self.pg0.unconfig_ip4()
2796 self.pg1.unconfig_ip4()
2797 self.pg0.set_table_ip4(0)
2798 self.pg1.set_table_ip4(0)
2799 self.pg0.config_ip4()
2800 self.pg1.config_ip4()
2801 self.pg0.resolve_arp()
2802 self.pg1.resolve_arp()
2803 self.vapi.ip_table_add_del(vrf_id1, is_add=0)
2804 self.vapi.ip_table_add_del(vrf_id2, is_add=0)
Neale Ranns15002542017-09-10 04:39:11 -07002805
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002806 def test_vrf_feature_independent(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002807 """ NAT44 tenant VRF independent address pool mode """
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002808
2809 nat_ip1 = "10.0.0.10"
2810 nat_ip2 = "10.0.0.11"
2811
Matus Fabian2ba92e32017-08-21 07:05:03 -07002812 self.nat44_add_address(nat_ip1)
Matus Fabian51e759f2017-12-07 23:22:51 -08002813 self.nat44_add_address(nat_ip2, vrf_id=99)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002814 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2815 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index)
2816 self.vapi.nat44_interface_add_del_feature(self.pg2.sw_if_index,
2817 is_inside=0)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002818
2819 # first VRF
2820 pkts = self.create_stream_in(self.pg0, self.pg2)
2821 self.pg0.add_stream(pkts)
2822 self.pg_enable_capture(self.pg_interfaces)
2823 self.pg_start()
2824 capture = self.pg2.get_capture(len(pkts))
2825 self.verify_capture_out(capture, nat_ip1)
2826
2827 # second VRF
2828 pkts = self.create_stream_in(self.pg1, self.pg2)
2829 self.pg1.add_stream(pkts)
2830 self.pg_enable_capture(self.pg_interfaces)
2831 self.pg_start()
2832 capture = self.pg2.get_capture(len(pkts))
2833 self.verify_capture_out(capture, nat_ip1)
2834
Martin Gálik406eb1d2017-05-04 04:35:04 -07002835 def test_dynamic_ipless_interfaces(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002836 """ NAT44 interfaces without configured IP address """
Martin Gálik406eb1d2017-05-04 04:35:04 -07002837
2838 self.vapi.ip_neighbor_add_del(self.pg7.sw_if_index,
Ole Troan895b6e82017-10-20 13:28:20 +02002839 mactobinary(self.pg7.remote_mac),
Martin Gálik406eb1d2017-05-04 04:35:04 -07002840 self.pg7.remote_ip4n,
2841 is_static=1)
2842 self.vapi.ip_neighbor_add_del(self.pg8.sw_if_index,
Ole Troan895b6e82017-10-20 13:28:20 +02002843 mactobinary(self.pg8.remote_mac),
Martin Gálik406eb1d2017-05-04 04:35:04 -07002844 self.pg8.remote_ip4n,
2845 is_static=1)
2846
2847 self.vapi.ip_add_del_route(dst_address=self.pg7.remote_ip4n,
2848 dst_address_length=32,
2849 next_hop_address=self.pg7.remote_ip4n,
2850 next_hop_sw_if_index=self.pg7.sw_if_index)
2851 self.vapi.ip_add_del_route(dst_address=self.pg8.remote_ip4n,
2852 dst_address_length=32,
2853 next_hop_address=self.pg8.remote_ip4n,
2854 next_hop_sw_if_index=self.pg8.sw_if_index)
2855
Matus Fabian2ba92e32017-08-21 07:05:03 -07002856 self.nat44_add_address(self.nat_addr)
2857 self.vapi.nat44_interface_add_del_feature(self.pg7.sw_if_index)
2858 self.vapi.nat44_interface_add_del_feature(self.pg8.sw_if_index,
2859 is_inside=0)
Martin Gálik406eb1d2017-05-04 04:35:04 -07002860
2861 # in2out
2862 pkts = self.create_stream_in(self.pg7, self.pg8)
2863 self.pg7.add_stream(pkts)
2864 self.pg_enable_capture(self.pg_interfaces)
2865 self.pg_start()
2866 capture = self.pg8.get_capture(len(pkts))
2867 self.verify_capture_out(capture)
2868
2869 # out2in
Matus Fabian2ba92e32017-08-21 07:05:03 -07002870 pkts = self.create_stream_out(self.pg8, self.nat_addr)
Martin Gálik406eb1d2017-05-04 04:35:04 -07002871 self.pg8.add_stream(pkts)
2872 self.pg_enable_capture(self.pg_interfaces)
2873 self.pg_start()
2874 capture = self.pg7.get_capture(len(pkts))
2875 self.verify_capture_in(capture, self.pg7)
2876
2877 def test_static_ipless_interfaces(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002878 """ NAT44 interfaces without configured IP address - 1:1 NAT """
Martin Gálik406eb1d2017-05-04 04:35:04 -07002879
2880 self.vapi.ip_neighbor_add_del(self.pg7.sw_if_index,
Ole Troan895b6e82017-10-20 13:28:20 +02002881 mactobinary(self.pg7.remote_mac),
Martin Gálik406eb1d2017-05-04 04:35:04 -07002882 self.pg7.remote_ip4n,
2883 is_static=1)
2884 self.vapi.ip_neighbor_add_del(self.pg8.sw_if_index,
Ole Troan895b6e82017-10-20 13:28:20 +02002885 mactobinary(self.pg8.remote_mac),
Martin Gálik406eb1d2017-05-04 04:35:04 -07002886 self.pg8.remote_ip4n,
2887 is_static=1)
2888
2889 self.vapi.ip_add_del_route(dst_address=self.pg7.remote_ip4n,
2890 dst_address_length=32,
2891 next_hop_address=self.pg7.remote_ip4n,
2892 next_hop_sw_if_index=self.pg7.sw_if_index)
2893 self.vapi.ip_add_del_route(dst_address=self.pg8.remote_ip4n,
2894 dst_address_length=32,
2895 next_hop_address=self.pg8.remote_ip4n,
2896 next_hop_sw_if_index=self.pg8.sw_if_index)
2897
Matus Fabian2ba92e32017-08-21 07:05:03 -07002898 self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr)
2899 self.vapi.nat44_interface_add_del_feature(self.pg7.sw_if_index)
2900 self.vapi.nat44_interface_add_del_feature(self.pg8.sw_if_index,
2901 is_inside=0)
Martin Gálik406eb1d2017-05-04 04:35:04 -07002902
2903 # out2in
2904 pkts = self.create_stream_out(self.pg8)
2905 self.pg8.add_stream(pkts)
2906 self.pg_enable_capture(self.pg_interfaces)
2907 self.pg_start()
2908 capture = self.pg7.get_capture(len(pkts))
2909 self.verify_capture_in(capture, self.pg7)
2910
2911 # in2out
2912 pkts = self.create_stream_in(self.pg7, self.pg8)
2913 self.pg7.add_stream(pkts)
2914 self.pg_enable_capture(self.pg_interfaces)
2915 self.pg_start()
2916 capture = self.pg8.get_capture(len(pkts))
Matus Fabian2ba92e32017-08-21 07:05:03 -07002917 self.verify_capture_out(capture, self.nat_addr, True)
Martin Gálik406eb1d2017-05-04 04:35:04 -07002918
2919 def test_static_with_port_ipless_interfaces(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07002920 """ NAT44 interfaces without configured IP address - 1:1 NAPT """
Martin Gálik406eb1d2017-05-04 04:35:04 -07002921
2922 self.tcp_port_out = 30606
2923 self.udp_port_out = 30607
2924 self.icmp_id_out = 30608
2925
2926 self.vapi.ip_neighbor_add_del(self.pg7.sw_if_index,
Ole Troan895b6e82017-10-20 13:28:20 +02002927 mactobinary(self.pg7.remote_mac),
Martin Gálik406eb1d2017-05-04 04:35:04 -07002928 self.pg7.remote_ip4n,
2929 is_static=1)
2930 self.vapi.ip_neighbor_add_del(self.pg8.sw_if_index,
Ole Troan895b6e82017-10-20 13:28:20 +02002931 mactobinary(self.pg8.remote_mac),
Martin Gálik406eb1d2017-05-04 04:35:04 -07002932 self.pg8.remote_ip4n,
2933 is_static=1)
2934
2935 self.vapi.ip_add_del_route(dst_address=self.pg7.remote_ip4n,
2936 dst_address_length=32,
2937 next_hop_address=self.pg7.remote_ip4n,
2938 next_hop_sw_if_index=self.pg7.sw_if_index)
2939 self.vapi.ip_add_del_route(dst_address=self.pg8.remote_ip4n,
2940 dst_address_length=32,
2941 next_hop_address=self.pg8.remote_ip4n,
2942 next_hop_sw_if_index=self.pg8.sw_if_index)
2943
Matus Fabian2ba92e32017-08-21 07:05:03 -07002944 self.nat44_add_address(self.nat_addr)
2945 self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr,
2946 self.tcp_port_in, self.tcp_port_out,
2947 proto=IP_PROTOS.tcp)
2948 self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr,
2949 self.udp_port_in, self.udp_port_out,
2950 proto=IP_PROTOS.udp)
2951 self.nat44_add_static_mapping(self.pg7.remote_ip4, self.nat_addr,
2952 self.icmp_id_in, self.icmp_id_out,
2953 proto=IP_PROTOS.icmp)
2954 self.vapi.nat44_interface_add_del_feature(self.pg7.sw_if_index)
2955 self.vapi.nat44_interface_add_del_feature(self.pg8.sw_if_index,
2956 is_inside=0)
Martin Gálik406eb1d2017-05-04 04:35:04 -07002957
2958 # out2in
2959 pkts = self.create_stream_out(self.pg8)
2960 self.pg8.add_stream(pkts)
2961 self.pg_enable_capture(self.pg_interfaces)
2962 self.pg_start()
2963 capture = self.pg7.get_capture(len(pkts))
2964 self.verify_capture_in(capture, self.pg7)
2965
2966 # in2out
2967 pkts = self.create_stream_in(self.pg7, self.pg8)
2968 self.pg7.add_stream(pkts)
2969 self.pg_enable_capture(self.pg_interfaces)
2970 self.pg_start()
2971 capture = self.pg8.get_capture(len(pkts))
2972 self.verify_capture_out(capture)
2973
Matus Fabian328dbc82017-06-19 04:28:04 -07002974 def test_static_unknown_proto(self):
2975 """ 1:1 NAT translate packet with unknown protocol """
2976 nat_ip = "10.0.0.10"
Matus Fabian2ba92e32017-08-21 07:05:03 -07002977 self.nat44_add_static_mapping(self.pg0.remote_ip4, nat_ip)
2978 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
2979 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
2980 is_inside=0)
Matus Fabian328dbc82017-06-19 04:28:04 -07002981
2982 # in2out
2983 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
2984 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
2985 GRE() /
Matus Fabian1e8db5f2017-06-20 01:45:49 -07002986 IP(src=self.pg2.remote_ip4, dst=self.pg3.remote_ip4) /
Matus Fabian328dbc82017-06-19 04:28:04 -07002987 TCP(sport=1234, dport=1234))
2988 self.pg0.add_stream(p)
2989 self.pg_enable_capture(self.pg_interfaces)
2990 self.pg_start()
2991 p = self.pg1.get_capture(1)
2992 packet = p[0]
2993 try:
2994 self.assertEqual(packet[IP].src, nat_ip)
2995 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
2996 self.assertTrue(packet.haslayer(GRE))
Klement Sekerad81ae412018-05-16 10:52:54 +02002997 self.assert_packet_checksums_valid(packet)
Matus Fabian328dbc82017-06-19 04:28:04 -07002998 except:
2999 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3000 raise
3001
3002 # out2in
3003 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
3004 IP(src=self.pg1.remote_ip4, dst=nat_ip) /
3005 GRE() /
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003006 IP(src=self.pg3.remote_ip4, dst=self.pg2.remote_ip4) /
Matus Fabian328dbc82017-06-19 04:28:04 -07003007 TCP(sport=1234, dport=1234))
3008 self.pg1.add_stream(p)
3009 self.pg_enable_capture(self.pg_interfaces)
3010 self.pg_start()
3011 p = self.pg0.get_capture(1)
3012 packet = p[0]
3013 try:
3014 self.assertEqual(packet[IP].src, self.pg1.remote_ip4)
3015 self.assertEqual(packet[IP].dst, self.pg0.remote_ip4)
3016 self.assertTrue(packet.haslayer(GRE))
Klement Sekerad81ae412018-05-16 10:52:54 +02003017 self.assert_packet_checksums_valid(packet)
Matus Fabian328dbc82017-06-19 04:28:04 -07003018 except:
3019 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3020 raise
3021
Matus Fabian7968e6c2017-07-06 05:37:49 -07003022 def test_hairpinning_static_unknown_proto(self):
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003023 """ 1:1 NAT translate packet with unknown protocol - hairpinning """
3024
3025 host = self.pg0.remote_hosts[0]
3026 server = self.pg0.remote_hosts[1]
3027
3028 host_nat_ip = "10.0.0.10"
3029 server_nat_ip = "10.0.0.11"
3030
Matus Fabian2ba92e32017-08-21 07:05:03 -07003031 self.nat44_add_static_mapping(host.ip4, host_nat_ip)
3032 self.nat44_add_static_mapping(server.ip4, server_nat_ip)
3033 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3034 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3035 is_inside=0)
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003036
3037 # host to server
3038 p = (Ether(dst=self.pg0.local_mac, src=host.mac) /
3039 IP(src=host.ip4, dst=server_nat_ip) /
3040 GRE() /
3041 IP(src=self.pg2.remote_ip4, dst=self.pg3.remote_ip4) /
3042 TCP(sport=1234, dport=1234))
3043 self.pg0.add_stream(p)
3044 self.pg_enable_capture(self.pg_interfaces)
3045 self.pg_start()
3046 p = self.pg0.get_capture(1)
3047 packet = p[0]
3048 try:
3049 self.assertEqual(packet[IP].src, host_nat_ip)
3050 self.assertEqual(packet[IP].dst, server.ip4)
3051 self.assertTrue(packet.haslayer(GRE))
Klement Sekerad81ae412018-05-16 10:52:54 +02003052 self.assert_packet_checksums_valid(packet)
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003053 except:
3054 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3055 raise
3056
3057 # server to host
3058 p = (Ether(dst=self.pg0.local_mac, src=server.mac) /
3059 IP(src=server.ip4, dst=host_nat_ip) /
3060 GRE() /
3061 IP(src=self.pg3.remote_ip4, dst=self.pg2.remote_ip4) /
3062 TCP(sport=1234, dport=1234))
3063 self.pg0.add_stream(p)
3064 self.pg_enable_capture(self.pg_interfaces)
3065 self.pg_start()
3066 p = self.pg0.get_capture(1)
3067 packet = p[0]
3068 try:
3069 self.assertEqual(packet[IP].src, server_nat_ip)
3070 self.assertEqual(packet[IP].dst, host.ip4)
3071 self.assertTrue(packet.haslayer(GRE))
Klement Sekerad81ae412018-05-16 10:52:54 +02003072 self.assert_packet_checksums_valid(packet)
Matus Fabian1e8db5f2017-06-20 01:45:49 -07003073 except:
3074 self.logger.error(ppp("Unexpected or invalid packet:", packet))
3075 raise
3076
Matus Fabian93d84c92017-07-19 08:06:01 -07003077 def test_output_feature(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07003078 """ NAT44 interface output feature (in2out postrouting) """
3079 self.nat44_add_address(self.nat_addr)
3080 self.vapi.nat44_interface_add_del_output_feature(self.pg0.sw_if_index)
Matus Fabianb2d2fc72017-09-10 22:17:47 -07003081 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index)
3082 self.vapi.nat44_interface_add_del_output_feature(self.pg3.sw_if_index,
Matus Fabian2ba92e32017-08-21 07:05:03 -07003083 is_inside=0)
Matus Fabian93d84c92017-07-19 08:06:01 -07003084
3085 # in2out
Matus Fabianb2d2fc72017-09-10 22:17:47 -07003086 pkts = self.create_stream_in(self.pg0, self.pg3)
Matus Fabian93d84c92017-07-19 08:06:01 -07003087 self.pg0.add_stream(pkts)
3088 self.pg_enable_capture(self.pg_interfaces)
3089 self.pg_start()
Matus Fabianb2d2fc72017-09-10 22:17:47 -07003090 capture = self.pg3.get_capture(len(pkts))
Matus Fabian93d84c92017-07-19 08:06:01 -07003091 self.verify_capture_out(capture)
3092
3093 # out2in
Matus Fabianb2d2fc72017-09-10 22:17:47 -07003094 pkts = self.create_stream_out(self.pg3)
3095 self.pg3.add_stream(pkts)
Matus Fabian93d84c92017-07-19 08:06:01 -07003096 self.pg_enable_capture(self.pg_interfaces)
3097 self.pg_start()
3098 capture = self.pg0.get_capture(len(pkts))
3099 self.verify_capture_in(capture, self.pg0)
3100
Matus Fabianb2d2fc72017-09-10 22:17:47 -07003101 # from non-NAT interface to NAT inside interface
3102 pkts = self.create_stream_in(self.pg2, self.pg0)
3103 self.pg2.add_stream(pkts)
3104 self.pg_enable_capture(self.pg_interfaces)
3105 self.pg_start()
3106 capture = self.pg0.get_capture(len(pkts))
3107 self.verify_capture_no_translation(capture, self.pg2, self.pg0)
3108
Matus Fabian93d84c92017-07-19 08:06:01 -07003109 def test_output_feature_vrf_aware(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07003110 """ NAT44 interface output feature VRF aware (in2out postrouting) """
Matus Fabian93d84c92017-07-19 08:06:01 -07003111 nat_ip_vrf10 = "10.0.0.10"
3112 nat_ip_vrf20 = "10.0.0.20"
3113
3114 self.vapi.ip_add_del_route(dst_address=self.pg3.remote_ip4n,
3115 dst_address_length=32,
3116 next_hop_address=self.pg3.remote_ip4n,
3117 next_hop_sw_if_index=self.pg3.sw_if_index,
3118 table_id=10)
3119 self.vapi.ip_add_del_route(dst_address=self.pg3.remote_ip4n,
3120 dst_address_length=32,
3121 next_hop_address=self.pg3.remote_ip4n,
3122 next_hop_sw_if_index=self.pg3.sw_if_index,
3123 table_id=20)
3124
Matus Fabian2ba92e32017-08-21 07:05:03 -07003125 self.nat44_add_address(nat_ip_vrf10, vrf_id=10)
3126 self.nat44_add_address(nat_ip_vrf20, vrf_id=20)
3127 self.vapi.nat44_interface_add_del_output_feature(self.pg4.sw_if_index)
3128 self.vapi.nat44_interface_add_del_output_feature(self.pg6.sw_if_index)
3129 self.vapi.nat44_interface_add_del_output_feature(self.pg3.sw_if_index,
3130 is_inside=0)
Matus Fabian93d84c92017-07-19 08:06:01 -07003131
3132 # in2out VRF 10
3133 pkts = self.create_stream_in(self.pg4, self.pg3)
3134 self.pg4.add_stream(pkts)
3135 self.pg_enable_capture(self.pg_interfaces)
3136 self.pg_start()
3137 capture = self.pg3.get_capture(len(pkts))
3138 self.verify_capture_out(capture, nat_ip=nat_ip_vrf10)
3139
3140 # out2in VRF 10
3141 pkts = self.create_stream_out(self.pg3, dst_ip=nat_ip_vrf10)
3142 self.pg3.add_stream(pkts)
3143 self.pg_enable_capture(self.pg_interfaces)
3144 self.pg_start()
3145 capture = self.pg4.get_capture(len(pkts))
3146 self.verify_capture_in(capture, self.pg4)
3147
3148 # in2out VRF 20
3149 pkts = self.create_stream_in(self.pg6, self.pg3)
3150 self.pg6.add_stream(pkts)
3151 self.pg_enable_capture(self.pg_interfaces)
3152 self.pg_start()
3153 capture = self.pg3.get_capture(len(pkts))
3154 self.verify_capture_out(capture, nat_ip=nat_ip_vrf20)
3155
3156 # out2in VRF 20
3157 pkts = self.create_stream_out(self.pg3, dst_ip=nat_ip_vrf20)
3158 self.pg3.add_stream(pkts)
3159 self.pg_enable_capture(self.pg_interfaces)
3160 self.pg_start()
3161 capture = self.pg6.get_capture(len(pkts))
3162 self.verify_capture_in(capture, self.pg6)
3163
Matus Fabian161c59c2017-07-21 03:46:03 -07003164 def test_output_feature_hairpinning(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07003165 """ NAT44 interface output feature hairpinning (in2out postrouting) """
Matus Fabian93d84c92017-07-19 08:06:01 -07003166 host = self.pg0.remote_hosts[0]
3167 server = self.pg0.remote_hosts[1]
3168 host_in_port = 1234
3169 host_out_port = 0
3170 server_in_port = 5678
3171 server_out_port = 8765
3172
Matus Fabian2ba92e32017-08-21 07:05:03 -07003173 self.nat44_add_address(self.nat_addr)
3174 self.vapi.nat44_interface_add_del_output_feature(self.pg0.sw_if_index)
3175 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
3176 is_inside=0)
Matus Fabian93d84c92017-07-19 08:06:01 -07003177
3178 # add static mapping for server
Matus Fabian2ba92e32017-08-21 07:05:03 -07003179 self.nat44_add_static_mapping(server.ip4, self.nat_addr,
3180 server_in_port, server_out_port,
3181 proto=IP_PROTOS.tcp)
Matus Fabian93d84c92017-07-19 08:06:01 -07003182
3183 # send packet from host to server
3184 p = (Ether(src=host.mac, dst=self.pg0.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07003185 IP(src=host.ip4, dst=self.nat_addr) /
Matus Fabian93d84c92017-07-19 08:06:01 -07003186 TCP(sport=host_in_port, dport=server_out_port))
3187 self.pg0.add_stream(p)
3188 self.pg_enable_capture(self.pg_interfaces)
3189 self.pg_start()
3190 capture = self.pg0.get_capture(1)
3191 p = capture[0]
3192 try:
3193 ip = p[IP]
3194 tcp = p[TCP]
Matus Fabian2ba92e32017-08-21 07:05:03 -07003195 self.assertEqual(ip.src, self.nat_addr)
Matus Fabian93d84c92017-07-19 08:06:01 -07003196 self.assertEqual(ip.dst, server.ip4)
3197 self.assertNotEqual(tcp.sport, host_in_port)
3198 self.assertEqual(tcp.dport, server_in_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02003199 self.assert_packet_checksums_valid(p)
Matus Fabian93d84c92017-07-19 08:06:01 -07003200 host_out_port = tcp.sport
3201 except:
3202 self.logger.error(ppp("Unexpected or invalid packet:", p))
3203 raise
3204
3205 # send reply from server to host
3206 p = (Ether(src=server.mac, dst=self.pg0.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07003207 IP(src=server.ip4, dst=self.nat_addr) /
Matus Fabian93d84c92017-07-19 08:06:01 -07003208 TCP(sport=server_in_port, dport=host_out_port))
3209 self.pg0.add_stream(p)
3210 self.pg_enable_capture(self.pg_interfaces)
3211 self.pg_start()
3212 capture = self.pg0.get_capture(1)
3213 p = capture[0]
3214 try:
3215 ip = p[IP]
3216 tcp = p[TCP]
Matus Fabian2ba92e32017-08-21 07:05:03 -07003217 self.assertEqual(ip.src, self.nat_addr)
Matus Fabian93d84c92017-07-19 08:06:01 -07003218 self.assertEqual(ip.dst, host.ip4)
3219 self.assertEqual(tcp.sport, server_out_port)
3220 self.assertEqual(tcp.dport, host_in_port)
Klement Sekerad81ae412018-05-16 10:52:54 +02003221 self.assert_packet_checksums_valid(p)
Matus Fabian93d84c92017-07-19 08:06:01 -07003222 except:
Matus Fabiane82488f2018-01-18 03:38:45 -08003223 self.logger.error(ppp("Unexpected or invalid packet:", p))
Matus Fabian93d84c92017-07-19 08:06:01 -07003224 raise
3225
Matus Fabian36ea2d62017-10-24 04:13:49 -07003226 def test_one_armed_nat44(self):
3227 """ One armed NAT44 """
3228 remote_host = self.pg9.remote_hosts[0]
3229 local_host = self.pg9.remote_hosts[1]
3230 external_port = 0
3231
3232 self.nat44_add_address(self.nat_addr)
3233 self.vapi.nat44_interface_add_del_feature(self.pg9.sw_if_index)
3234 self.vapi.nat44_interface_add_del_feature(self.pg9.sw_if_index,
3235 is_inside=0)
3236
3237 # in2out
3238 p = (Ether(src=self.pg9.remote_mac, dst=self.pg9.local_mac) /
3239 IP(src=local_host.ip4, dst=remote_host.ip4) /
3240 TCP(sport=12345, dport=80))
3241 self.pg9.add_stream(p)
3242 self.pg_enable_capture(self.pg_interfaces)
3243 self.pg_start()
3244 capture = self.pg9.get_capture(1)
3245 p = capture[0]
3246 try:
3247 ip = p[IP]
3248 tcp = p[TCP]
3249 self.assertEqual(ip.src, self.nat_addr)
3250 self.assertEqual(ip.dst, remote_host.ip4)
3251 self.assertNotEqual(tcp.sport, 12345)
3252 external_port = tcp.sport
3253 self.assertEqual(tcp.dport, 80)
Klement Sekerad81ae412018-05-16 10:52:54 +02003254 self.assert_packet_checksums_valid(p)
Matus Fabian36ea2d62017-10-24 04:13:49 -07003255 except:
3256 self.logger.error(ppp("Unexpected or invalid packet:", p))
3257 raise
3258
3259 # out2in
3260 p = (Ether(src=self.pg9.remote_mac, dst=self.pg9.local_mac) /
3261 IP(src=remote_host.ip4, dst=self.nat_addr) /
3262 TCP(sport=80, dport=external_port))
3263 self.pg9.add_stream(p)
3264 self.pg_enable_capture(self.pg_interfaces)
3265 self.pg_start()
3266 capture = self.pg9.get_capture(1)
3267 p = capture[0]
3268 try:
3269 ip = p[IP]
3270 tcp = p[TCP]
3271 self.assertEqual(ip.src, remote_host.ip4)
3272 self.assertEqual(ip.dst, local_host.ip4)
3273 self.assertEqual(tcp.sport, 80)
3274 self.assertEqual(tcp.dport, 12345)
Klement Sekerad81ae412018-05-16 10:52:54 +02003275 self.assert_packet_checksums_valid(p)
Matus Fabian36ea2d62017-10-24 04:13:49 -07003276 except:
3277 self.logger.error(ppp("Unexpected or invalid packet:", p))
3278 raise
3279
Matus Fabian5ba86f72017-10-26 03:37:38 -07003280 def test_del_session(self):
3281 """ Delete NAT44 session """
3282 self.nat44_add_address(self.nat_addr)
3283 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3284 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3285 is_inside=0)
3286
3287 pkts = self.create_stream_in(self.pg0, self.pg1)
3288 self.pg0.add_stream(pkts)
3289 self.pg_enable_capture(self.pg_interfaces)
3290 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07003291 self.pg1.get_capture(len(pkts))
Matus Fabian5ba86f72017-10-26 03:37:38 -07003292
3293 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
3294 nsessions = len(sessions)
3295
3296 self.vapi.nat44_del_session(sessions[0].inside_ip_address,
3297 sessions[0].inside_port,
3298 sessions[0].protocol)
3299 self.vapi.nat44_del_session(sessions[1].outside_ip_address,
3300 sessions[1].outside_port,
3301 sessions[1].protocol,
3302 is_in=0)
3303
3304 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
3305 self.assertEqual(nsessions - len(sessions), 2)
3306
Matus Fabian68ba8802018-08-08 05:52:47 -07003307 self.vapi.nat44_del_session(sessions[0].inside_ip_address,
3308 sessions[0].inside_port,
3309 sessions[0].protocol)
3310
3311 self.verify_no_nat44_user()
3312
Matus Fabianefcd1e92017-08-15 06:59:19 -07003313 def test_set_get_reass(self):
3314 """ NAT44 set/get virtual fragmentation reassembly """
3315 reas_cfg1 = self.vapi.nat_get_reass()
3316
3317 self.vapi.nat_set_reass(timeout=reas_cfg1.ip4_timeout + 5,
3318 max_reass=reas_cfg1.ip4_max_reass * 2,
3319 max_frag=reas_cfg1.ip4_max_frag * 2)
3320
3321 reas_cfg2 = self.vapi.nat_get_reass()
3322
3323 self.assertEqual(reas_cfg1.ip4_timeout + 5, reas_cfg2.ip4_timeout)
3324 self.assertEqual(reas_cfg1.ip4_max_reass * 2, reas_cfg2.ip4_max_reass)
3325 self.assertEqual(reas_cfg1.ip4_max_frag * 2, reas_cfg2.ip4_max_frag)
3326
3327 self.vapi.nat_set_reass(drop_frag=1)
3328 self.assertTrue(self.vapi.nat_get_reass().ip4_drop_frag)
3329
3330 def test_frag_in_order(self):
3331 """ NAT44 translate fragments arriving in order """
Juraj Slobodafe0aa762018-07-23 12:22:54 +02003332
Matus Fabianefcd1e92017-08-15 06:59:19 -07003333 self.nat44_add_address(self.nat_addr)
3334 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3335 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3336 is_inside=0)
3337
Matus Fabianda41d722018-10-19 04:01:19 -07003338 self.frag_in_order(proto=IP_PROTOS.tcp)
3339 self.frag_in_order(proto=IP_PROTOS.udp)
3340 self.frag_in_order(proto=IP_PROTOS.icmp)
Matus Fabianefcd1e92017-08-15 06:59:19 -07003341
Matus Fabian111add72018-11-20 02:19:05 -08003342 def test_frag_forwarding(self):
3343 """ NAT44 forwarding fragment test """
3344 self.vapi.nat44_add_interface_addr(self.pg1.sw_if_index)
3345 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3346 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3347 is_inside=0)
3348 self.vapi.nat44_forwarding_enable_disable(1)
3349
3350 data = "A" * 16 + "B" * 16 + "C" * 3
3351 pkts = self.create_stream_frag(self.pg1,
3352 self.pg0.remote_ip4,
3353 4789,
3354 4789,
3355 data,
3356 proto=IP_PROTOS.udp)
3357 self.pg1.add_stream(pkts)
3358 self.pg_enable_capture(self.pg_interfaces)
3359 self.pg_start()
3360 frags = self.pg0.get_capture(len(pkts))
3361 p = self.reass_frags_and_verify(frags,
3362 self.pg1.remote_ip4,
3363 self.pg0.remote_ip4)
3364 self.assertEqual(p[UDP].sport, 4789)
3365 self.assertEqual(p[UDP].dport, 4789)
3366 self.assertEqual(data, p[Raw].load)
3367
Matus Fabianefcd1e92017-08-15 06:59:19 -07003368 def test_reass_hairpinning(self):
3369 """ NAT44 fragments hairpinning """
Juraj Slobodafe0aa762018-07-23 12:22:54 +02003370
Matus Fabianda41d722018-10-19 04:01:19 -07003371 self.server = self.pg0.remote_hosts[1]
3372 self.host_in_port = random.randint(1025, 65535)
3373 self.server_in_port = random.randint(1025, 65535)
3374 self.server_out_port = random.randint(1025, 65535)
Matus Fabianefcd1e92017-08-15 06:59:19 -07003375
3376 self.nat44_add_address(self.nat_addr)
3377 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3378 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3379 is_inside=0)
3380 # add static mapping for server
Matus Fabianda41d722018-10-19 04:01:19 -07003381 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr,
3382 self.server_in_port,
3383 self.server_out_port,
Matus Fabianefcd1e92017-08-15 06:59:19 -07003384 proto=IP_PROTOS.tcp)
Matus Fabianda41d722018-10-19 04:01:19 -07003385 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr,
3386 self.server_in_port,
3387 self.server_out_port,
3388 proto=IP_PROTOS.udp)
3389 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr)
Matus Fabianefcd1e92017-08-15 06:59:19 -07003390
Matus Fabianda41d722018-10-19 04:01:19 -07003391 self.reass_hairpinning(proto=IP_PROTOS.tcp)
3392 self.reass_hairpinning(proto=IP_PROTOS.udp)
3393 self.reass_hairpinning(proto=IP_PROTOS.icmp)
Matus Fabianefcd1e92017-08-15 06:59:19 -07003394
3395 def test_frag_out_of_order(self):
3396 """ NAT44 translate fragments arriving out of order """
Juraj Slobodafe0aa762018-07-23 12:22:54 +02003397
Matus Fabianefcd1e92017-08-15 06:59:19 -07003398 self.nat44_add_address(self.nat_addr)
3399 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3400 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3401 is_inside=0)
3402
Matus Fabianda41d722018-10-19 04:01:19 -07003403 self.frag_out_of_order(proto=IP_PROTOS.tcp)
3404 self.frag_out_of_order(proto=IP_PROTOS.udp)
3405 self.frag_out_of_order(proto=IP_PROTOS.icmp)
Matus Fabianefcd1e92017-08-15 06:59:19 -07003406
Matus Fabian27697102017-11-09 01:43:47 -08003407 def test_port_restricted(self):
3408 """ Port restricted NAT44 (MAP-E CE) """
3409 self.nat44_add_address(self.nat_addr)
3410 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3411 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3412 is_inside=0)
Matus Fabian5d28c7a2018-09-04 03:55:45 -07003413 self.vapi.nat_set_addr_and_port_alloc_alg(alg=1,
3414 psid_offset=6,
3415 psid_length=6,
3416 psid=10)
Matus Fabian27697102017-11-09 01:43:47 -08003417
3418 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
3419 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
3420 TCP(sport=4567, dport=22))
3421 self.pg0.add_stream(p)
3422 self.pg_enable_capture(self.pg_interfaces)
3423 self.pg_start()
3424 capture = self.pg1.get_capture(1)
3425 p = capture[0]
3426 try:
3427 ip = p[IP]
3428 tcp = p[TCP]
3429 self.assertEqual(ip.dst, self.pg1.remote_ip4)
3430 self.assertEqual(ip.src, self.nat_addr)
3431 self.assertEqual(tcp.dport, 22)
3432 self.assertNotEqual(tcp.sport, 4567)
3433 self.assertEqual((tcp.sport >> 6) & 63, 10)
Klement Sekerad81ae412018-05-16 10:52:54 +02003434 self.assert_packet_checksums_valid(p)
Matus Fabian27697102017-11-09 01:43:47 -08003435 except:
3436 self.logger.error(ppp("Unexpected or invalid packet:", p))
3437 raise
3438
Matus Fabian5d28c7a2018-09-04 03:55:45 -07003439 def test_port_range(self):
3440 """ External address port range """
3441 self.nat44_add_address(self.nat_addr)
3442 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3443 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3444 is_inside=0)
3445 self.vapi.nat_set_addr_and_port_alloc_alg(alg=2,
3446 start_port=1025,
3447 end_port=1027)
3448
3449 pkts = []
3450 for port in range(0, 5):
3451 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
3452 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
3453 TCP(sport=1125 + port))
3454 pkts.append(p)
3455 self.pg0.add_stream(pkts)
3456 self.pg_enable_capture(self.pg_interfaces)
3457 self.pg_start()
3458 capture = self.pg1.get_capture(3)
3459 for p in capture:
3460 tcp = p[TCP]
3461 self.assertGreaterEqual(tcp.sport, 1025)
3462 self.assertLessEqual(tcp.sport, 1027)
3463
Matus Fabiana6110b62018-06-13 05:39:07 -07003464 def test_ipfix_max_frags(self):
3465 """ IPFIX logging maximum fragments pending reassembly exceeded """
3466 self.nat44_add_address(self.nat_addr)
3467 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3468 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3469 is_inside=0)
Matus Fabiana7f8b222018-09-05 06:01:55 -07003470 self.vapi.nat_set_reass(max_frag=1)
Matus Fabiana6110b62018-06-13 05:39:07 -07003471 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
3472 src_address=self.pg3.local_ip4n,
3473 path_mtu=512,
3474 template_interval=10)
3475 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
3476 src_port=self.ipfix_src_port)
3477
3478 data = "A" * 4 + "B" * 16 + "C" * 3
3479 self.tcp_port_in = random.randint(1025, 65535)
3480 pkts = self.create_stream_frag(self.pg0,
3481 self.pg1.remote_ip4,
3482 self.tcp_port_in,
3483 20,
3484 data)
Matus Fabiana7f8b222018-09-05 06:01:55 -07003485 pkts.reverse()
3486 self.pg0.add_stream(pkts)
Matus Fabiana6110b62018-06-13 05:39:07 -07003487 self.pg_enable_capture(self.pg_interfaces)
3488 self.pg_start()
3489 self.pg1.assert_nothing_captured()
3490 sleep(1)
3491 self.vapi.cli("ipfix flush") # FIXME this should be an API call
3492 capture = self.pg3.get_capture(9)
3493 ipfix = IPFIXDecoder()
3494 # first load template
3495 for p in capture:
3496 self.assertTrue(p.haslayer(IPFIX))
3497 self.assertEqual(p[IP].src, self.pg3.local_ip4)
3498 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
3499 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
3500 self.assertEqual(p[UDP].dport, 4739)
3501 self.assertEqual(p[IPFIX].observationDomainID,
3502 self.ipfix_domain_id)
3503 if p.haslayer(Template):
3504 ipfix.add_template(p.getlayer(Template))
3505 # verify events in data set
3506 for p in capture:
3507 if p.haslayer(Data):
3508 data = ipfix.decode_data_set(p.getlayer(Set))
Matus Fabiana7f8b222018-09-05 06:01:55 -07003509 self.verify_ipfix_max_fragments_ip4(data, 1,
Matus Fabiana6110b62018-06-13 05:39:07 -07003510 self.pg0.remote_ip4n)
3511
Matus Fabian8008d7c2018-07-09 01:34:20 -07003512 def test_multiple_outside_vrf(self):
3513 """ Multiple outside VRF """
3514 vrf_id1 = 1
3515 vrf_id2 = 2
3516
3517 self.pg1.unconfig_ip4()
3518 self.pg2.unconfig_ip4()
3519 self.vapi.ip_table_add_del(vrf_id1, is_add=1)
3520 self.vapi.ip_table_add_del(vrf_id2, is_add=1)
3521 self.pg1.set_table_ip4(vrf_id1)
3522 self.pg2.set_table_ip4(vrf_id2)
3523 self.pg1.config_ip4()
3524 self.pg2.config_ip4()
3525 self.pg1.resolve_arp()
3526 self.pg2.resolve_arp()
3527
3528 self.nat44_add_address(self.nat_addr)
3529 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3530 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3531 is_inside=0)
3532 self.vapi.nat44_interface_add_del_feature(self.pg2.sw_if_index,
3533 is_inside=0)
3534
3535 try:
3536 # first VRF
3537 pkts = self.create_stream_in(self.pg0, self.pg1)
3538 self.pg0.add_stream(pkts)
3539 self.pg_enable_capture(self.pg_interfaces)
3540 self.pg_start()
3541 capture = self.pg1.get_capture(len(pkts))
3542 self.verify_capture_out(capture, self.nat_addr)
3543
3544 pkts = self.create_stream_out(self.pg1, self.nat_addr)
3545 self.pg1.add_stream(pkts)
3546 self.pg_enable_capture(self.pg_interfaces)
3547 self.pg_start()
3548 capture = self.pg0.get_capture(len(pkts))
3549 self.verify_capture_in(capture, self.pg0)
3550
3551 self.tcp_port_in = 60303
3552 self.udp_port_in = 60304
3553 self.icmp_id_in = 60305
3554
3555 # second VRF
3556 pkts = self.create_stream_in(self.pg0, self.pg2)
3557 self.pg0.add_stream(pkts)
3558 self.pg_enable_capture(self.pg_interfaces)
3559 self.pg_start()
3560 capture = self.pg2.get_capture(len(pkts))
3561 self.verify_capture_out(capture, self.nat_addr)
3562
3563 pkts = self.create_stream_out(self.pg2, self.nat_addr)
3564 self.pg2.add_stream(pkts)
3565 self.pg_enable_capture(self.pg_interfaces)
3566 self.pg_start()
3567 capture = self.pg0.get_capture(len(pkts))
3568 self.verify_capture_in(capture, self.pg0)
3569
3570 finally:
3571 self.pg1.unconfig_ip4()
3572 self.pg2.unconfig_ip4()
3573 self.pg1.set_table_ip4(0)
3574 self.pg2.set_table_ip4(0)
3575 self.pg1.config_ip4()
3576 self.pg2.config_ip4()
3577 self.pg1.resolve_arp()
3578 self.pg2.resolve_arp()
3579
Matus Fabian878c6462018-08-23 00:33:35 -07003580 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
3581 def test_session_timeout(self):
3582 """ NAT44 session timeouts """
3583 self.nat44_add_address(self.nat_addr)
3584 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3585 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3586 is_inside=0)
3587 self.vapi.nat_set_timeouts(udp=5)
3588
3589 max_sessions = 1000
3590 pkts = []
3591 for i in range(0, max_sessions):
3592 src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF)
3593 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
3594 IP(src=src, dst=self.pg1.remote_ip4) /
3595 UDP(sport=1025, dport=53))
3596 pkts.append(p)
3597 self.pg0.add_stream(pkts)
3598 self.pg_enable_capture(self.pg_interfaces)
3599 self.pg_start()
3600 self.pg1.get_capture(max_sessions)
3601
3602 sleep(6)
3603
3604 pkts = []
3605 for i in range(0, max_sessions):
3606 src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF)
3607 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
3608 IP(src=src, dst=self.pg1.remote_ip4) /
3609 UDP(sport=1026, dport=53))
3610 pkts.append(p)
3611 self.pg0.add_stream(pkts)
3612 self.pg_enable_capture(self.pg_interfaces)
3613 self.pg_start()
3614 self.pg1.get_capture(max_sessions)
3615
3616 nsessions = 0
3617 users = self.vapi.nat44_user_dump()
3618 for user in users:
3619 nsessions = nsessions + user.nsessions
3620 self.assertLess(nsessions, 2 * max_sessions)
3621
Matus Fabianbb4e0222018-09-13 02:36:25 -07003622 def test_mss_clamping(self):
3623 """ TCP MSS clamping """
3624 self.nat44_add_address(self.nat_addr)
3625 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3626 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3627 is_inside=0)
3628
3629 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
3630 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
3631 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
3632 flags="S", options=[('MSS', 1400)]))
3633
3634 self.vapi.nat_set_mss_clamping(enable=1, mss_value=1000)
3635 self.pg0.add_stream(p)
3636 self.pg_enable_capture(self.pg_interfaces)
3637 self.pg_start()
3638 capture = self.pg1.get_capture(1)
3639 # Negotiated MSS value greater than configured - changed
3640 self.verify_mss_value(capture[0], 1000)
3641
3642 self.vapi.nat_set_mss_clamping(enable=0)
3643 self.pg0.add_stream(p)
3644 self.pg_enable_capture(self.pg_interfaces)
3645 self.pg_start()
3646 capture = self.pg1.get_capture(1)
3647 # MSS clamping disabled - negotiated MSS unchanged
3648 self.verify_mss_value(capture[0], 1400)
3649
3650 self.vapi.nat_set_mss_clamping(enable=1, mss_value=1500)
3651 self.pg0.add_stream(p)
3652 self.pg_enable_capture(self.pg_interfaces)
3653 self.pg_start()
3654 capture = self.pg1.get_capture(1)
3655 # Negotiated MSS value smaller than configured - unchanged
3656 self.verify_mss_value(capture[0], 1400)
3657
Matus Fabiana6110b62018-06-13 05:39:07 -07003658 def tearDown(self):
3659 super(TestNAT44, self).tearDown()
3660 if not self.vpp_dead:
3661 self.logger.info(self.vapi.cli("show nat44 addresses"))
3662 self.logger.info(self.vapi.cli("show nat44 interfaces"))
3663 self.logger.info(self.vapi.cli("show nat44 static mappings"))
3664 self.logger.info(self.vapi.cli("show nat44 interface address"))
3665 self.logger.info(self.vapi.cli("show nat44 sessions detail"))
3666 self.logger.info(self.vapi.cli("show nat virtual-reassembly"))
3667 self.logger.info(self.vapi.cli("show nat44 hash tables detail"))
Matus Fabian878c6462018-08-23 00:33:35 -07003668 self.logger.info(self.vapi.cli("show nat timeouts"))
Matus Fabian5d28c7a2018-09-04 03:55:45 -07003669 self.logger.info(
3670 self.vapi.cli("show nat addr-port-assignment-alg"))
Matus Fabiana6110b62018-06-13 05:39:07 -07003671 self.clear_nat44()
3672 self.vapi.cli("clear logging")
3673
3674
3675class TestNAT44EndpointDependent(MethodHolder):
3676 """ Endpoint-Dependent mapping and filtering test cases """
3677
3678 @classmethod
3679 def setUpConstants(cls):
3680 super(TestNAT44EndpointDependent, cls).setUpConstants()
3681 cls.vpp_cmdline.extend(["nat", "{", "endpoint-dependent", "}"])
3682
3683 @classmethod
3684 def setUpClass(cls):
3685 super(TestNAT44EndpointDependent, cls).setUpClass()
3686 cls.vapi.cli("set log class nat level debug")
3687 try:
3688 cls.tcp_port_in = 6303
3689 cls.tcp_port_out = 6303
3690 cls.udp_port_in = 6304
3691 cls.udp_port_out = 6304
3692 cls.icmp_id_in = 6305
3693 cls.icmp_id_out = 6305
3694 cls.nat_addr = '10.0.0.3'
3695 cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
3696 cls.ipfix_src_port = 4739
3697 cls.ipfix_domain_id = 1
3698 cls.tcp_external_port = 80
3699
Matus Fabian8008d7c2018-07-09 01:34:20 -07003700 cls.create_pg_interfaces(range(7))
Matus Fabiana6110b62018-06-13 05:39:07 -07003701 cls.interfaces = list(cls.pg_interfaces[0:3])
3702
3703 for i in cls.interfaces:
3704 i.admin_up()
3705 i.config_ip4()
3706 i.resolve_arp()
3707
3708 cls.pg0.generate_remote_hosts(3)
3709 cls.pg0.configure_ipv4_neighbors()
3710
3711 cls.pg3.admin_up()
3712
3713 cls.pg4.generate_remote_hosts(2)
3714 cls.pg4.config_ip4()
3715 ip_addr_n = socket.inet_pton(socket.AF_INET, "10.0.0.1")
3716 cls.vapi.sw_interface_add_del_address(cls.pg4.sw_if_index,
3717 ip_addr_n,
3718 24)
3719 cls.pg4.admin_up()
3720 cls.pg4.resolve_arp()
3721 cls.pg4._remote_hosts[1]._ip4 = cls.pg4._remote_hosts[0]._ip4
3722 cls.pg4.resolve_arp()
3723
Matus Fabian8008d7c2018-07-09 01:34:20 -07003724 zero_ip4n = socket.inet_pton(socket.AF_INET, "0.0.0.0")
3725 cls.vapi.ip_table_add_del(1, is_add=1)
3726
3727 cls.pg5._local_ip4 = "10.1.1.1"
3728 cls.pg5._local_ip4n = socket.inet_pton(socket.AF_INET,
3729 cls.pg5.local_ip4)
3730 cls.pg5._remote_hosts[0]._ip4 = "10.1.1.2"
3731 cls.pg5._remote_hosts[0]._ip4n = socket.inet_pton(
3732 socket.AF_INET, cls.pg5.remote_ip4)
3733 cls.pg5.set_table_ip4(1)
3734 cls.pg5.config_ip4()
3735 cls.pg5.admin_up()
3736 cls.vapi.ip_add_del_route(dst_address=cls.pg5.remote_ip4n,
3737 dst_address_length=32,
3738 table_id=1,
3739 next_hop_sw_if_index=cls.pg5.sw_if_index,
3740 next_hop_address=zero_ip4n)
3741
3742 cls.pg6._local_ip4 = "10.1.2.1"
3743 cls.pg6._local_ip4n = socket.inet_pton(socket.AF_INET,
3744 cls.pg6.local_ip4)
3745 cls.pg6._remote_hosts[0]._ip4 = "10.1.2.2"
3746 cls.pg6._remote_hosts[0]._ip4n = socket.inet_pton(
3747 socket.AF_INET, cls.pg6.remote_ip4)
3748 cls.pg6.set_table_ip4(1)
3749 cls.pg6.config_ip4()
3750 cls.pg6.admin_up()
3751 cls.vapi.ip_add_del_route(dst_address=cls.pg6.remote_ip4n,
3752 dst_address_length=32,
3753 table_id=1,
3754 next_hop_sw_if_index=cls.pg6.sw_if_index,
3755 next_hop_address=zero_ip4n)
3756
3757 cls.vapi.ip_add_del_route(dst_address=cls.pg6.remote_ip4n,
3758 dst_address_length=16,
3759 next_hop_address=zero_ip4n,
3760 table_id=0,
3761 next_hop_table_id=1)
3762 cls.vapi.ip_add_del_route(dst_address=zero_ip4n,
3763 dst_address_length=0,
3764 next_hop_address=zero_ip4n,
3765 table_id=1,
3766 next_hop_table_id=0)
3767 cls.vapi.ip_add_del_route(dst_address=zero_ip4n,
3768 dst_address_length=0,
3769 table_id=0,
3770 next_hop_sw_if_index=cls.pg1.sw_if_index,
3771 next_hop_address=cls.pg1.local_ip4n)
3772
3773 cls.pg5.resolve_arp()
3774 cls.pg6.resolve_arp()
3775
Matus Fabiana6110b62018-06-13 05:39:07 -07003776 except Exception:
3777 super(TestNAT44EndpointDependent, cls).tearDownClass()
3778 raise
3779
Juraj Slobodafe0aa762018-07-23 12:22:54 +02003780 def test_frag_in_order(self):
3781 """ NAT44 translate fragments arriving in order """
3782 self.nat44_add_address(self.nat_addr)
3783 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3784 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3785 is_inside=0)
3786 self.frag_in_order(proto=IP_PROTOS.tcp)
3787 self.frag_in_order(proto=IP_PROTOS.udp)
3788 self.frag_in_order(proto=IP_PROTOS.icmp)
3789
3790 def test_frag_in_order_dont_translate(self):
3791 """ NAT44 don't translate fragments arriving in order """
3792 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3793 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3794 is_inside=0)
3795 self.vapi.nat44_forwarding_enable_disable(enable=True)
3796 self.frag_in_order(proto=IP_PROTOS.tcp, dont_translate=True)
3797
3798 def test_frag_out_of_order(self):
3799 """ NAT44 translate fragments arriving out of order """
3800 self.nat44_add_address(self.nat_addr)
3801 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3802 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3803 is_inside=0)
3804 self.frag_out_of_order(proto=IP_PROTOS.tcp)
3805 self.frag_out_of_order(proto=IP_PROTOS.udp)
3806 self.frag_out_of_order(proto=IP_PROTOS.icmp)
3807
3808 def test_frag_out_of_order_dont_translate(self):
3809 """ NAT44 don't translate fragments arriving out of order """
3810 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3811 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3812 is_inside=0)
3813 self.vapi.nat44_forwarding_enable_disable(enable=True)
3814 self.frag_out_of_order(proto=IP_PROTOS.tcp, dont_translate=True)
3815
3816 def test_frag_in_order_in_plus_out(self):
3817 """ in+out interface fragments in order """
3818 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3819 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
3820 is_inside=0)
3821 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index)
3822 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3823 is_inside=0)
3824
3825 self.server = self.pg1.remote_hosts[0]
3826
3827 self.server_in_addr = self.server.ip4
3828 self.server_out_addr = '11.11.11.11'
3829 self.server_in_port = random.randint(1025, 65535)
3830 self.server_out_port = random.randint(1025, 65535)
3831
3832 self.nat44_add_address(self.server_out_addr)
3833
3834 # add static mappings for server
3835 self.nat44_add_static_mapping(self.server_in_addr,
3836 self.server_out_addr,
3837 self.server_in_port,
3838 self.server_out_port,
3839 proto=IP_PROTOS.tcp)
3840 self.nat44_add_static_mapping(self.server_in_addr,
3841 self.server_out_addr,
3842 self.server_in_port,
3843 self.server_out_port,
3844 proto=IP_PROTOS.udp)
3845 self.nat44_add_static_mapping(self.server_in_addr,
3846 self.server_out_addr,
3847 proto=IP_PROTOS.icmp)
3848
3849 self.vapi.nat_set_reass(timeout=10)
3850
3851 self.frag_in_order_in_plus_out(proto=IP_PROTOS.tcp)
3852 self.frag_in_order_in_plus_out(proto=IP_PROTOS.udp)
3853 self.frag_in_order_in_plus_out(proto=IP_PROTOS.icmp)
3854
3855 def test_frag_out_of_order_in_plus_out(self):
3856 """ in+out interface fragments out of order """
3857 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3858 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
3859 is_inside=0)
3860 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index)
3861 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3862 is_inside=0)
3863
3864 self.server = self.pg1.remote_hosts[0]
3865
3866 self.server_in_addr = self.server.ip4
3867 self.server_out_addr = '11.11.11.11'
3868 self.server_in_port = random.randint(1025, 65535)
3869 self.server_out_port = random.randint(1025, 65535)
3870
3871 self.nat44_add_address(self.server_out_addr)
3872
3873 # add static mappings for server
3874 self.nat44_add_static_mapping(self.server_in_addr,
3875 self.server_out_addr,
3876 self.server_in_port,
3877 self.server_out_port,
3878 proto=IP_PROTOS.tcp)
3879 self.nat44_add_static_mapping(self.server_in_addr,
3880 self.server_out_addr,
3881 self.server_in_port,
3882 self.server_out_port,
3883 proto=IP_PROTOS.udp)
3884 self.nat44_add_static_mapping(self.server_in_addr,
3885 self.server_out_addr,
3886 proto=IP_PROTOS.icmp)
3887
3888 self.vapi.nat_set_reass(timeout=10)
3889
3890 self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.tcp)
3891 self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.udp)
3892 self.frag_out_of_order_in_plus_out(proto=IP_PROTOS.icmp)
3893
3894 def test_reass_hairpinning(self):
3895 """ NAT44 fragments hairpinning """
3896 self.server = self.pg0.remote_hosts[1]
3897 self.host_in_port = random.randint(1025, 65535)
3898 self.server_in_port = random.randint(1025, 65535)
3899 self.server_out_port = random.randint(1025, 65535)
3900
3901 self.nat44_add_address(self.nat_addr)
3902 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3903 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3904 is_inside=0)
3905 # add static mapping for server
3906 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr,
3907 self.server_in_port,
3908 self.server_out_port,
3909 proto=IP_PROTOS.tcp)
3910 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr,
3911 self.server_in_port,
3912 self.server_out_port,
3913 proto=IP_PROTOS.udp)
Matus Fabianda41d722018-10-19 04:01:19 -07003914 self.nat44_add_static_mapping(self.server.ip4, self.nat_addr)
Juraj Slobodafe0aa762018-07-23 12:22:54 +02003915
3916 self.reass_hairpinning(proto=IP_PROTOS.tcp)
3917 self.reass_hairpinning(proto=IP_PROTOS.udp)
3918 self.reass_hairpinning(proto=IP_PROTOS.icmp)
3919
Matus Fabiana6110b62018-06-13 05:39:07 -07003920 def test_dynamic(self):
3921 """ NAT44 dynamic translation test """
3922
3923 self.nat44_add_address(self.nat_addr)
3924 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3925 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3926 is_inside=0)
3927
Matus Fabian69ce30d2018-08-22 01:27:10 -07003928 nat_config = self.vapi.nat_show_config()
3929 self.assertEqual(1, nat_config.endpoint_dependent)
3930
Matus Fabiana6110b62018-06-13 05:39:07 -07003931 # in2out
3932 pkts = self.create_stream_in(self.pg0, self.pg1)
3933 self.pg0.add_stream(pkts)
3934 self.pg_enable_capture(self.pg_interfaces)
3935 self.pg_start()
3936 capture = self.pg1.get_capture(len(pkts))
3937 self.verify_capture_out(capture)
3938
3939 # out2in
3940 pkts = self.create_stream_out(self.pg1)
3941 self.pg1.add_stream(pkts)
3942 self.pg_enable_capture(self.pg_interfaces)
3943 self.pg_start()
3944 capture = self.pg0.get_capture(len(pkts))
3945 self.verify_capture_in(capture, self.pg0)
3946
3947 def test_forwarding(self):
3948 """ NAT44 forwarding test """
3949
3950 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
3951 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
3952 is_inside=0)
3953 self.vapi.nat44_forwarding_enable_disable(1)
3954
3955 real_ip = self.pg0.remote_ip4n
3956 alias_ip = self.nat_addr_n
3957 self.vapi.nat44_add_del_static_mapping(local_ip=real_ip,
3958 external_ip=alias_ip)
3959
3960 try:
3961 # in2out - static mapping match
3962
3963 pkts = self.create_stream_out(self.pg1)
3964 self.pg1.add_stream(pkts)
3965 self.pg_enable_capture(self.pg_interfaces)
3966 self.pg_start()
3967 capture = self.pg0.get_capture(len(pkts))
3968 self.verify_capture_in(capture, self.pg0)
3969
3970 pkts = self.create_stream_in(self.pg0, self.pg1)
3971 self.pg0.add_stream(pkts)
3972 self.pg_enable_capture(self.pg_interfaces)
3973 self.pg_start()
3974 capture = self.pg1.get_capture(len(pkts))
3975 self.verify_capture_out(capture, same_port=True)
3976
3977 # in2out - no static mapping match
3978
3979 host0 = self.pg0.remote_hosts[0]
3980 self.pg0.remote_hosts[0] = self.pg0.remote_hosts[1]
3981 try:
3982 pkts = self.create_stream_out(self.pg1,
3983 dst_ip=self.pg0.remote_ip4,
3984 use_inside_ports=True)
3985 self.pg1.add_stream(pkts)
3986 self.pg_enable_capture(self.pg_interfaces)
3987 self.pg_start()
3988 capture = self.pg0.get_capture(len(pkts))
3989 self.verify_capture_in(capture, self.pg0)
3990
3991 pkts = self.create_stream_in(self.pg0, self.pg1)
3992 self.pg0.add_stream(pkts)
3993 self.pg_enable_capture(self.pg_interfaces)
3994 self.pg_start()
3995 capture = self.pg1.get_capture(len(pkts))
3996 self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4,
3997 same_port=True)
3998 finally:
3999 self.pg0.remote_hosts[0] = host0
4000
4001 user = self.pg0.remote_hosts[1]
4002 sessions = self.vapi.nat44_user_session_dump(user.ip4n, 0)
4003 self.assertEqual(len(sessions), 3)
4004 self.assertTrue(sessions[0].ext_host_valid)
4005 self.vapi.nat44_del_session(
4006 sessions[0].inside_ip_address,
4007 sessions[0].inside_port,
4008 sessions[0].protocol,
4009 ext_host_address=sessions[0].ext_host_address,
4010 ext_host_port=sessions[0].ext_host_port)
4011 sessions = self.vapi.nat44_user_session_dump(user.ip4n, 0)
4012 self.assertEqual(len(sessions), 2)
4013
4014 finally:
4015 self.vapi.nat44_forwarding_enable_disable(0)
4016 self.vapi.nat44_add_del_static_mapping(local_ip=real_ip,
4017 external_ip=alias_ip,
4018 is_add=0)
4019
4020 def test_static_lb(self):
4021 """ NAT44 local service load balancing """
4022 external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr)
4023 external_port = 80
4024 local_port = 8080
4025 server1 = self.pg0.remote_hosts[0]
4026 server2 = self.pg0.remote_hosts[1]
4027
4028 locals = [{'addr': server1.ip4n,
4029 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004030 'probability': 70,
4031 'vrf_id': 0},
Matus Fabiana6110b62018-06-13 05:39:07 -07004032 {'addr': server2.ip4n,
4033 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004034 'probability': 30,
4035 'vrf_id': 0}]
Matus Fabiana6110b62018-06-13 05:39:07 -07004036
4037 self.nat44_add_address(self.nat_addr)
4038 self.vapi.nat44_add_del_lb_static_mapping(external_addr_n,
4039 external_port,
4040 IP_PROTOS.tcp,
4041 local_num=len(locals),
4042 locals=locals)
4043 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4044 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4045 is_inside=0)
4046
4047 # from client to service
4048 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4049 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4050 TCP(sport=12345, dport=external_port))
4051 self.pg1.add_stream(p)
4052 self.pg_enable_capture(self.pg_interfaces)
4053 self.pg_start()
4054 capture = self.pg0.get_capture(1)
4055 p = capture[0]
4056 server = None
4057 try:
4058 ip = p[IP]
4059 tcp = p[TCP]
4060 self.assertIn(ip.dst, [server1.ip4, server2.ip4])
4061 if ip.dst == server1.ip4:
4062 server = server1
4063 else:
4064 server = server2
4065 self.assertEqual(tcp.dport, local_port)
4066 self.assert_packet_checksums_valid(p)
4067 except:
4068 self.logger.error(ppp("Unexpected or invalid packet:", p))
4069 raise
4070
4071 # from service back to client
4072 p = (Ether(src=server.mac, dst=self.pg0.local_mac) /
4073 IP(src=server.ip4, dst=self.pg1.remote_ip4) /
4074 TCP(sport=local_port, dport=12345))
4075 self.pg0.add_stream(p)
4076 self.pg_enable_capture(self.pg_interfaces)
4077 self.pg_start()
4078 capture = self.pg1.get_capture(1)
4079 p = capture[0]
4080 try:
4081 ip = p[IP]
4082 tcp = p[TCP]
4083 self.assertEqual(ip.src, self.nat_addr)
4084 self.assertEqual(tcp.sport, external_port)
4085 self.assert_packet_checksums_valid(p)
4086 except:
4087 self.logger.error(ppp("Unexpected or invalid packet:", p))
4088 raise
4089
4090 sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0)
4091 self.assertEqual(len(sessions), 1)
4092 self.assertTrue(sessions[0].ext_host_valid)
4093 self.vapi.nat44_del_session(
4094 sessions[0].inside_ip_address,
4095 sessions[0].inside_port,
4096 sessions[0].protocol,
4097 ext_host_address=sessions[0].ext_host_address,
4098 ext_host_port=sessions[0].ext_host_port)
4099 sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0)
4100 self.assertEqual(len(sessions), 0)
4101
4102 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
4103 def test_static_lb_multi_clients(self):
4104 """ NAT44 local service load balancing - multiple clients"""
4105
4106 external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr)
4107 external_port = 80
4108 local_port = 8080
4109 server1 = self.pg0.remote_hosts[0]
4110 server2 = self.pg0.remote_hosts[1]
4111
4112 locals = [{'addr': server1.ip4n,
4113 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004114 'probability': 90,
4115 'vrf_id': 0},
Matus Fabiana6110b62018-06-13 05:39:07 -07004116 {'addr': server2.ip4n,
4117 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004118 'probability': 10,
4119 'vrf_id': 0}]
Matus Fabiana6110b62018-06-13 05:39:07 -07004120
4121 self.nat44_add_address(self.nat_addr)
4122 self.vapi.nat44_add_del_lb_static_mapping(external_addr_n,
4123 external_port,
4124 IP_PROTOS.tcp,
4125 local_num=len(locals),
4126 locals=locals)
4127 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4128 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4129 is_inside=0)
4130
4131 server1_n = 0
4132 server2_n = 0
4133 clients = ip4_range(self.pg1.remote_ip4, 10, 50)
4134 pkts = []
4135 for client in clients:
4136 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4137 IP(src=client, dst=self.nat_addr) /
4138 TCP(sport=12345, dport=external_port))
4139 pkts.append(p)
4140 self.pg1.add_stream(pkts)
4141 self.pg_enable_capture(self.pg_interfaces)
4142 self.pg_start()
4143 capture = self.pg0.get_capture(len(pkts))
4144 for p in capture:
4145 if p[IP].dst == server1.ip4:
4146 server1_n += 1
4147 else:
4148 server2_n += 1
4149 self.assertTrue(server1_n > server2_n)
4150
4151 def test_static_lb_2(self):
4152 """ NAT44 local service load balancing (asymmetrical rule) """
4153 external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr)
4154 external_port = 80
4155 local_port = 8080
4156 server1 = self.pg0.remote_hosts[0]
4157 server2 = self.pg0.remote_hosts[1]
4158
4159 locals = [{'addr': server1.ip4n,
4160 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004161 'probability': 70,
4162 'vrf_id': 0},
Matus Fabiana6110b62018-06-13 05:39:07 -07004163 {'addr': server2.ip4n,
4164 'port': local_port,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004165 'probability': 30,
4166 'vrf_id': 0}]
Matus Fabiana6110b62018-06-13 05:39:07 -07004167
4168 self.vapi.nat44_forwarding_enable_disable(1)
4169 self.vapi.nat44_add_del_lb_static_mapping(external_addr_n,
4170 external_port,
4171 IP_PROTOS.tcp,
4172 out2in_only=1,
4173 local_num=len(locals),
4174 locals=locals)
4175 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4176 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4177 is_inside=0)
4178
4179 # from client to service
4180 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4181 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4182 TCP(sport=12345, dport=external_port))
4183 self.pg1.add_stream(p)
4184 self.pg_enable_capture(self.pg_interfaces)
4185 self.pg_start()
4186 capture = self.pg0.get_capture(1)
4187 p = capture[0]
4188 server = None
4189 try:
4190 ip = p[IP]
4191 tcp = p[TCP]
4192 self.assertIn(ip.dst, [server1.ip4, server2.ip4])
4193 if ip.dst == server1.ip4:
4194 server = server1
4195 else:
4196 server = server2
4197 self.assertEqual(tcp.dport, local_port)
4198 self.assert_packet_checksums_valid(p)
4199 except:
4200 self.logger.error(ppp("Unexpected or invalid packet:", p))
4201 raise
4202
4203 # from service back to client
4204 p = (Ether(src=server.mac, dst=self.pg0.local_mac) /
4205 IP(src=server.ip4, dst=self.pg1.remote_ip4) /
4206 TCP(sport=local_port, dport=12345))
4207 self.pg0.add_stream(p)
4208 self.pg_enable_capture(self.pg_interfaces)
4209 self.pg_start()
4210 capture = self.pg1.get_capture(1)
4211 p = capture[0]
4212 try:
4213 ip = p[IP]
4214 tcp = p[TCP]
4215 self.assertEqual(ip.src, self.nat_addr)
4216 self.assertEqual(tcp.sport, external_port)
4217 self.assert_packet_checksums_valid(p)
4218 except:
4219 self.logger.error(ppp("Unexpected or invalid packet:", p))
4220 raise
4221
4222 # from client to server (no translation)
4223 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4224 IP(src=self.pg1.remote_ip4, dst=server1.ip4) /
4225 TCP(sport=12346, dport=local_port))
4226 self.pg1.add_stream(p)
4227 self.pg_enable_capture(self.pg_interfaces)
4228 self.pg_start()
4229 capture = self.pg0.get_capture(1)
4230 p = capture[0]
4231 server = None
4232 try:
4233 ip = p[IP]
4234 tcp = p[TCP]
4235 self.assertEqual(ip.dst, server1.ip4)
4236 self.assertEqual(tcp.dport, local_port)
4237 self.assert_packet_checksums_valid(p)
4238 except:
4239 self.logger.error(ppp("Unexpected or invalid packet:", p))
4240 raise
4241
4242 # from service back to client (no translation)
4243 p = (Ether(src=server1.mac, dst=self.pg0.local_mac) /
4244 IP(src=server1.ip4, dst=self.pg1.remote_ip4) /
4245 TCP(sport=local_port, dport=12346))
4246 self.pg0.add_stream(p)
4247 self.pg_enable_capture(self.pg_interfaces)
4248 self.pg_start()
4249 capture = self.pg1.get_capture(1)
4250 p = capture[0]
4251 try:
4252 ip = p[IP]
4253 tcp = p[TCP]
4254 self.assertEqual(ip.src, server1.ip4)
4255 self.assertEqual(tcp.sport, local_port)
4256 self.assert_packet_checksums_valid(p)
4257 except:
4258 self.logger.error(ppp("Unexpected or invalid packet:", p))
4259 raise
4260
Matus Fabianea5b5be2018-09-03 05:02:23 -07004261 def test_lb_affinity(self):
4262 """ NAT44 local service load balancing affinity """
4263 external_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr)
4264 external_port = 80
4265 local_port = 8080
4266 server1 = self.pg0.remote_hosts[0]
4267 server2 = self.pg0.remote_hosts[1]
4268
4269 locals = [{'addr': server1.ip4n,
4270 'port': local_port,
4271 'probability': 50,
4272 'vrf_id': 0},
4273 {'addr': server2.ip4n,
4274 'port': local_port,
4275 'probability': 50,
4276 'vrf_id': 0}]
4277
4278 self.nat44_add_address(self.nat_addr)
4279 self.vapi.nat44_add_del_lb_static_mapping(external_addr_n,
4280 external_port,
4281 IP_PROTOS.tcp,
4282 affinity=10800,
4283 local_num=len(locals),
4284 locals=locals)
4285 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4286 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4287 is_inside=0)
4288
4289 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
4290 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4291 TCP(sport=1025, dport=external_port))
4292 self.pg1.add_stream(p)
4293 self.pg_enable_capture(self.pg_interfaces)
4294 self.pg_start()
4295 capture = self.pg0.get_capture(1)
4296 backend = capture[0][IP].dst
4297
4298 sessions = self.vapi.nat44_user_session_dump(
4299 socket.inet_pton(socket.AF_INET, backend), 0)
4300 self.assertEqual(len(sessions), 1)
4301 self.assertTrue(sessions[0].ext_host_valid)
4302 self.vapi.nat44_del_session(
4303 sessions[0].inside_ip_address,
4304 sessions[0].inside_port,
4305 sessions[0].protocol,
4306 ext_host_address=sessions[0].ext_host_address,
4307 ext_host_port=sessions[0].ext_host_port)
4308
4309 pkts = []
4310 for port in range(1030, 1100):
4311 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
4312 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4313 TCP(sport=port, dport=external_port))
4314 pkts.append(p)
4315 self.pg1.add_stream(pkts)
4316 self.pg_enable_capture(self.pg_interfaces)
4317 self.pg_start()
4318 capture = self.pg0.get_capture(len(pkts))
4319 for p in capture:
4320 self.assertEqual(p[IP].dst, backend)
4321
Matus Fabiana6110b62018-06-13 05:39:07 -07004322 def test_unknown_proto(self):
4323 """ NAT44 translate packet with unknown protocol """
4324 self.nat44_add_address(self.nat_addr)
4325 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4326 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4327 is_inside=0)
4328
4329 # in2out
4330 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
4331 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
4332 TCP(sport=self.tcp_port_in, dport=20))
4333 self.pg0.add_stream(p)
4334 self.pg_enable_capture(self.pg_interfaces)
4335 self.pg_start()
4336 p = self.pg1.get_capture(1)
4337
4338 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
4339 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
4340 GRE() /
4341 IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) /
4342 TCP(sport=1234, dport=1234))
4343 self.pg0.add_stream(p)
4344 self.pg_enable_capture(self.pg_interfaces)
4345 self.pg_start()
4346 p = self.pg1.get_capture(1)
4347 packet = p[0]
4348 try:
4349 self.assertEqual(packet[IP].src, self.nat_addr)
4350 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
4351 self.assertTrue(packet.haslayer(GRE))
4352 self.assert_packet_checksums_valid(packet)
4353 except:
4354 self.logger.error(ppp("Unexpected or invalid packet:", packet))
4355 raise
4356
4357 # out2in
4358 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
4359 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4360 GRE() /
4361 IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) /
4362 TCP(sport=1234, dport=1234))
4363 self.pg1.add_stream(p)
4364 self.pg_enable_capture(self.pg_interfaces)
4365 self.pg_start()
4366 p = self.pg0.get_capture(1)
4367 packet = p[0]
4368 try:
4369 self.assertEqual(packet[IP].src, self.pg1.remote_ip4)
4370 self.assertEqual(packet[IP].dst, self.pg0.remote_ip4)
4371 self.assertTrue(packet.haslayer(GRE))
4372 self.assert_packet_checksums_valid(packet)
4373 except:
4374 self.logger.error(ppp("Unexpected or invalid packet:", packet))
4375 raise
4376
4377 def test_hairpinning_unknown_proto(self):
4378 """ NAT44 translate packet with unknown protocol - hairpinning """
4379 host = self.pg0.remote_hosts[0]
4380 server = self.pg0.remote_hosts[1]
4381 host_in_port = 1234
4382 server_out_port = 8765
4383 server_nat_ip = "10.0.0.11"
4384
4385 self.nat44_add_address(self.nat_addr)
4386 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4387 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4388 is_inside=0)
4389
4390 # add static mapping for server
4391 self.nat44_add_static_mapping(server.ip4, server_nat_ip)
4392
4393 # host to server
4394 p = (Ether(src=host.mac, dst=self.pg0.local_mac) /
4395 IP(src=host.ip4, dst=server_nat_ip) /
4396 TCP(sport=host_in_port, dport=server_out_port))
4397 self.pg0.add_stream(p)
4398 self.pg_enable_capture(self.pg_interfaces)
4399 self.pg_start()
4400 self.pg0.get_capture(1)
4401
4402 p = (Ether(dst=self.pg0.local_mac, src=host.mac) /
4403 IP(src=host.ip4, dst=server_nat_ip) /
4404 GRE() /
4405 IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) /
4406 TCP(sport=1234, dport=1234))
4407 self.pg0.add_stream(p)
4408 self.pg_enable_capture(self.pg_interfaces)
4409 self.pg_start()
4410 p = self.pg0.get_capture(1)
4411 packet = p[0]
4412 try:
4413 self.assertEqual(packet[IP].src, self.nat_addr)
4414 self.assertEqual(packet[IP].dst, server.ip4)
4415 self.assertTrue(packet.haslayer(GRE))
4416 self.assert_packet_checksums_valid(packet)
4417 except:
4418 self.logger.error(ppp("Unexpected or invalid packet:", packet))
4419 raise
4420
4421 # server to host
4422 p = (Ether(dst=self.pg0.local_mac, src=server.mac) /
4423 IP(src=server.ip4, dst=self.nat_addr) /
4424 GRE() /
4425 IP(src=self.pg2.remote_ip4, dst=self.pg2.remote_ip4) /
4426 TCP(sport=1234, dport=1234))
4427 self.pg0.add_stream(p)
4428 self.pg_enable_capture(self.pg_interfaces)
4429 self.pg_start()
4430 p = self.pg0.get_capture(1)
4431 packet = p[0]
4432 try:
4433 self.assertEqual(packet[IP].src, server_nat_ip)
4434 self.assertEqual(packet[IP].dst, host.ip4)
4435 self.assertTrue(packet.haslayer(GRE))
4436 self.assert_packet_checksums_valid(packet)
4437 except:
4438 self.logger.error(ppp("Unexpected or invalid packet:", packet))
4439 raise
4440
4441 def test_output_feature_and_service(self):
4442 """ NAT44 interface output feature and services """
4443 external_addr = '1.2.3.4'
4444 external_port = 80
4445 local_port = 8080
4446
4447 self.vapi.nat44_forwarding_enable_disable(1)
4448 self.nat44_add_address(self.nat_addr)
4449 self.vapi.nat44_add_del_identity_mapping(ip=self.pg1.remote_ip4n)
4450 self.nat44_add_static_mapping(self.pg0.remote_ip4, external_addr,
4451 local_port, external_port,
4452 proto=IP_PROTOS.tcp, out2in_only=1)
4453 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4454 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
4455 is_inside=0)
4456 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
4457 is_inside=0)
4458
4459 # from client to service
4460 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4461 IP(src=self.pg1.remote_ip4, dst=external_addr) /
4462 TCP(sport=12345, dport=external_port))
4463 self.pg1.add_stream(p)
4464 self.pg_enable_capture(self.pg_interfaces)
4465 self.pg_start()
4466 capture = self.pg0.get_capture(1)
4467 p = capture[0]
4468 try:
4469 ip = p[IP]
4470 tcp = p[TCP]
4471 self.assertEqual(ip.dst, self.pg0.remote_ip4)
4472 self.assertEqual(tcp.dport, local_port)
4473 self.assert_packet_checksums_valid(p)
4474 except:
4475 self.logger.error(ppp("Unexpected or invalid packet:", p))
4476 raise
4477
4478 # from service back to client
4479 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
4480 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
4481 TCP(sport=local_port, dport=12345))
4482 self.pg0.add_stream(p)
4483 self.pg_enable_capture(self.pg_interfaces)
4484 self.pg_start()
4485 capture = self.pg1.get_capture(1)
4486 p = capture[0]
4487 try:
4488 ip = p[IP]
4489 tcp = p[TCP]
4490 self.assertEqual(ip.src, external_addr)
4491 self.assertEqual(tcp.sport, external_port)
4492 self.assert_packet_checksums_valid(p)
4493 except:
4494 self.logger.error(ppp("Unexpected or invalid packet:", p))
4495 raise
4496
4497 # from local network host to external network
4498 pkts = self.create_stream_in(self.pg0, self.pg1)
4499 self.pg0.add_stream(pkts)
4500 self.pg_enable_capture(self.pg_interfaces)
4501 self.pg_start()
4502 capture = self.pg1.get_capture(len(pkts))
4503 self.verify_capture_out(capture)
4504 pkts = self.create_stream_in(self.pg0, self.pg1)
4505 self.pg0.add_stream(pkts)
4506 self.pg_enable_capture(self.pg_interfaces)
4507 self.pg_start()
4508 capture = self.pg1.get_capture(len(pkts))
4509 self.verify_capture_out(capture)
4510
4511 # from external network back to local network host
4512 pkts = self.create_stream_out(self.pg1)
4513 self.pg1.add_stream(pkts)
4514 self.pg_enable_capture(self.pg_interfaces)
4515 self.pg_start()
4516 capture = self.pg0.get_capture(len(pkts))
4517 self.verify_capture_in(capture, self.pg0)
4518
4519 def test_output_feature_and_service2(self):
4520 """ NAT44 interface output feature and service host direct access """
4521 self.vapi.nat44_forwarding_enable_disable(1)
4522 self.nat44_add_address(self.nat_addr)
4523 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
4524 is_inside=0)
4525
4526 # session initiaded from service host - translate
4527 pkts = self.create_stream_in(self.pg0, self.pg1)
4528 self.pg0.add_stream(pkts)
4529 self.pg_enable_capture(self.pg_interfaces)
4530 self.pg_start()
4531 capture = self.pg1.get_capture(len(pkts))
4532 self.verify_capture_out(capture)
4533
4534 pkts = self.create_stream_out(self.pg1)
4535 self.pg1.add_stream(pkts)
4536 self.pg_enable_capture(self.pg_interfaces)
4537 self.pg_start()
4538 capture = self.pg0.get_capture(len(pkts))
4539 self.verify_capture_in(capture, self.pg0)
4540
4541 # session initiaded from remote host - do not translate
4542 self.tcp_port_in = 60303
4543 self.udp_port_in = 60304
4544 self.icmp_id_in = 60305
4545 pkts = self.create_stream_out(self.pg1,
4546 self.pg0.remote_ip4,
4547 use_inside_ports=True)
4548 self.pg1.add_stream(pkts)
4549 self.pg_enable_capture(self.pg_interfaces)
4550 self.pg_start()
4551 capture = self.pg0.get_capture(len(pkts))
4552 self.verify_capture_in(capture, self.pg0)
4553
4554 pkts = self.create_stream_in(self.pg0, self.pg1)
4555 self.pg0.add_stream(pkts)
4556 self.pg_enable_capture(self.pg_interfaces)
4557 self.pg_start()
4558 capture = self.pg1.get_capture(len(pkts))
4559 self.verify_capture_out(capture, nat_ip=self.pg0.remote_ip4,
4560 same_port=True)
4561
4562 def test_output_feature_and_service3(self):
4563 """ NAT44 interface output feature and DST NAT """
4564 external_addr = '1.2.3.4'
4565 external_port = 80
4566 local_port = 8080
4567
4568 self.vapi.nat44_forwarding_enable_disable(1)
4569 self.nat44_add_address(self.nat_addr)
4570 self.nat44_add_static_mapping(self.pg1.remote_ip4, external_addr,
4571 local_port, external_port,
4572 proto=IP_PROTOS.tcp, out2in_only=1)
4573 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4574 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
4575 is_inside=0)
4576 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
4577 is_inside=0)
4578
4579 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
4580 IP(src=self.pg0.remote_ip4, dst=external_addr) /
4581 TCP(sport=12345, dport=external_port))
4582 self.pg0.add_stream(p)
4583 self.pg_enable_capture(self.pg_interfaces)
4584 self.pg_start()
4585 capture = self.pg1.get_capture(1)
4586 p = capture[0]
4587 try:
4588 ip = p[IP]
4589 tcp = p[TCP]
4590 self.assertEqual(ip.src, self.pg0.remote_ip4)
4591 self.assertEqual(tcp.sport, 12345)
4592 self.assertEqual(ip.dst, self.pg1.remote_ip4)
4593 self.assertEqual(tcp.dport, local_port)
4594 self.assert_packet_checksums_valid(p)
4595 except:
4596 self.logger.error(ppp("Unexpected or invalid packet:", p))
4597 raise
4598
4599 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4600 IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) /
4601 TCP(sport=local_port, dport=12345))
4602 self.pg1.add_stream(p)
4603 self.pg_enable_capture(self.pg_interfaces)
4604 self.pg_start()
4605 capture = self.pg0.get_capture(1)
4606 p = capture[0]
4607 try:
4608 ip = p[IP]
4609 tcp = p[TCP]
4610 self.assertEqual(ip.src, external_addr)
4611 self.assertEqual(tcp.sport, external_port)
4612 self.assertEqual(ip.dst, self.pg0.remote_ip4)
4613 self.assertEqual(tcp.dport, 12345)
4614 self.assert_packet_checksums_valid(p)
4615 except:
4616 self.logger.error(ppp("Unexpected or invalid packet:", p))
4617 raise
4618
Matus Fabian182e37e2018-08-14 04:21:26 -07004619 def test_next_src_nat(self):
4620 """ On way back forward packet to nat44-in2out node. """
4621 twice_nat_addr = '10.0.1.3'
4622 external_port = 80
4623 local_port = 8080
4624 post_twice_nat_port = 0
4625
4626 self.vapi.nat44_forwarding_enable_disable(1)
4627 self.nat44_add_address(twice_nat_addr, twice_nat=1)
4628 self.nat44_add_static_mapping(self.pg6.remote_ip4, self.pg1.remote_ip4,
4629 local_port, external_port,
4630 proto=IP_PROTOS.tcp, out2in_only=1,
4631 self_twice_nat=1, vrf_id=1)
4632 self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index,
4633 is_inside=0)
4634
4635 p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) /
4636 IP(src=self.pg6.remote_ip4, dst=self.pg1.remote_ip4) /
4637 TCP(sport=12345, dport=external_port))
4638 self.pg6.add_stream(p)
4639 self.pg_enable_capture(self.pg_interfaces)
4640 self.pg_start()
4641 capture = self.pg6.get_capture(1)
4642 p = capture[0]
4643 try:
4644 ip = p[IP]
4645 tcp = p[TCP]
4646 self.assertEqual(ip.src, twice_nat_addr)
4647 self.assertNotEqual(tcp.sport, 12345)
4648 post_twice_nat_port = tcp.sport
4649 self.assertEqual(ip.dst, self.pg6.remote_ip4)
4650 self.assertEqual(tcp.dport, local_port)
4651 self.assert_packet_checksums_valid(p)
4652 except:
4653 self.logger.error(ppp("Unexpected or invalid packet:", p))
4654 raise
4655
4656 p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) /
4657 IP(src=self.pg6.remote_ip4, dst=twice_nat_addr) /
4658 TCP(sport=local_port, dport=post_twice_nat_port))
4659 self.pg6.add_stream(p)
4660 self.pg_enable_capture(self.pg_interfaces)
4661 self.pg_start()
4662 capture = self.pg6.get_capture(1)
4663 p = capture[0]
4664 try:
4665 ip = p[IP]
4666 tcp = p[TCP]
4667 self.assertEqual(ip.src, self.pg1.remote_ip4)
4668 self.assertEqual(tcp.sport, external_port)
4669 self.assertEqual(ip.dst, self.pg6.remote_ip4)
4670 self.assertEqual(tcp.dport, 12345)
4671 self.assert_packet_checksums_valid(p)
4672 except:
4673 self.logger.error(ppp("Unexpected or invalid packet:", p))
4674 raise
4675
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004676 def twice_nat_common(self, self_twice_nat=False, same_pg=False, lb=False,
4677 client_id=None):
Matus Fabianb932d262017-12-18 05:38:24 -08004678 twice_nat_addr = '10.0.1.3'
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004679
Matus Fabianb932d262017-12-18 05:38:24 -08004680 port_in = 8080
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004681 if lb:
4682 if not same_pg:
4683 port_in1 = port_in
4684 port_in2 = port_in
4685 else:
4686 port_in1 = port_in+1
4687 port_in2 = port_in+2
4688
Matus Fabianb932d262017-12-18 05:38:24 -08004689 port_out = 80
4690 eh_port_out = 4567
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004691
4692 server1 = self.pg0.remote_hosts[0]
4693 server2 = self.pg0.remote_hosts[1]
4694 if lb and same_pg:
4695 server2 = server1
4696 if not lb:
4697 server = server1
4698
4699 pg0 = self.pg0
4700 if same_pg:
4701 pg1 = self.pg0
4702 else:
4703 pg1 = self.pg1
4704
4705 eh_translate = ((not self_twice_nat) or (not lb and same_pg) or
4706 client_id == 1)
4707
Matus Fabianb932d262017-12-18 05:38:24 -08004708 self.nat44_add_address(self.nat_addr)
4709 self.nat44_add_address(twice_nat_addr, twice_nat=1)
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004710 if not lb:
4711 self.nat44_add_static_mapping(pg0.remote_ip4, self.nat_addr,
4712 port_in, port_out,
4713 proto=IP_PROTOS.tcp,
4714 twice_nat=int(not self_twice_nat),
4715 self_twice_nat=int(self_twice_nat))
4716 else:
4717 locals = [{'addr': server1.ip4n,
4718 'port': port_in1,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004719 'probability': 50,
4720 'vrf_id': 0},
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004721 {'addr': server2.ip4n,
4722 'port': port_in2,
Matus Fabianc6c0d2a2018-07-19 22:45:25 -07004723 'probability': 50,
4724 'vrf_id': 0}]
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004725 out_addr_n = socket.inet_pton(socket.AF_INET, self.nat_addr)
4726 self.vapi.nat44_add_del_lb_static_mapping(out_addr_n,
4727 port_out,
4728 IP_PROTOS.tcp,
4729 twice_nat=int(
4730 not self_twice_nat),
4731 self_twice_nat=int(
4732 self_twice_nat),
4733 local_num=len(locals),
4734 locals=locals)
4735 self.vapi.nat44_interface_add_del_feature(pg0.sw_if_index)
4736 self.vapi.nat44_interface_add_del_feature(pg1.sw_if_index,
Matus Fabianb932d262017-12-18 05:38:24 -08004737 is_inside=0)
4738
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004739 if same_pg:
4740 if not lb:
4741 client = server
4742 else:
4743 assert client_id is not None
4744 if client_id == 1:
4745 client = self.pg0.remote_hosts[0]
4746 elif client_id == 2:
4747 client = self.pg0.remote_hosts[1]
4748 else:
4749 client = pg1.remote_hosts[0]
4750 p = (Ether(src=pg1.remote_mac, dst=pg1.local_mac) /
4751 IP(src=client.ip4, dst=self.nat_addr) /
Matus Fabianb932d262017-12-18 05:38:24 -08004752 TCP(sport=eh_port_out, dport=port_out))
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004753 pg1.add_stream(p)
Matus Fabianb932d262017-12-18 05:38:24 -08004754 self.pg_enable_capture(self.pg_interfaces)
4755 self.pg_start()
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004756 capture = pg0.get_capture(1)
Matus Fabianb932d262017-12-18 05:38:24 -08004757 p = capture[0]
4758 try:
4759 ip = p[IP]
4760 tcp = p[TCP]
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004761 if lb:
4762 if ip.dst == server1.ip4:
4763 server = server1
4764 port_in = port_in1
4765 else:
4766 server = server2
4767 port_in = port_in2
4768 self.assertEqual(ip.dst, server.ip4)
4769 if lb and same_pg:
4770 self.assertIn(tcp.dport, [port_in1, port_in2])
4771 else:
4772 self.assertEqual(tcp.dport, port_in)
4773 if eh_translate:
4774 self.assertEqual(ip.src, twice_nat_addr)
4775 self.assertNotEqual(tcp.sport, eh_port_out)
4776 else:
4777 self.assertEqual(ip.src, client.ip4)
4778 self.assertEqual(tcp.sport, eh_port_out)
4779 eh_addr_in = ip.src
Matus Fabianb932d262017-12-18 05:38:24 -08004780 eh_port_in = tcp.sport
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004781 saved_port_in = tcp.dport
Klement Sekerad81ae412018-05-16 10:52:54 +02004782 self.assert_packet_checksums_valid(p)
Matus Fabianb932d262017-12-18 05:38:24 -08004783 except:
4784 self.logger.error(ppp("Unexpected or invalid packet:", p))
4785 raise
4786
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004787 p = (Ether(src=server.mac, dst=pg0.local_mac) /
4788 IP(src=server.ip4, dst=eh_addr_in) /
4789 TCP(sport=saved_port_in, dport=eh_port_in))
4790 pg0.add_stream(p)
Matus Fabianb932d262017-12-18 05:38:24 -08004791 self.pg_enable_capture(self.pg_interfaces)
4792 self.pg_start()
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004793 capture = pg1.get_capture(1)
Matus Fabianb932d262017-12-18 05:38:24 -08004794 p = capture[0]
4795 try:
4796 ip = p[IP]
4797 tcp = p[TCP]
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004798 self.assertEqual(ip.dst, client.ip4)
Matus Fabianb932d262017-12-18 05:38:24 -08004799 self.assertEqual(ip.src, self.nat_addr)
4800 self.assertEqual(tcp.dport, eh_port_out)
4801 self.assertEqual(tcp.sport, port_out)
Klement Sekerad81ae412018-05-16 10:52:54 +02004802 self.assert_packet_checksums_valid(p)
Matus Fabianb932d262017-12-18 05:38:24 -08004803 except:
4804 self.logger.error(ppp("Unexpected or invalid packet:", p))
4805 raise
4806
Matus Fabian70a26ac2018-05-14 06:20:28 -07004807 if eh_translate:
4808 sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0)
4809 self.assertEqual(len(sessions), 1)
4810 self.assertTrue(sessions[0].ext_host_valid)
4811 self.assertTrue(sessions[0].is_twicenat)
4812 self.vapi.nat44_del_session(
4813 sessions[0].inside_ip_address,
4814 sessions[0].inside_port,
4815 sessions[0].protocol,
4816 ext_host_address=sessions[0].ext_host_nat_address,
4817 ext_host_port=sessions[0].ext_host_nat_port)
4818 sessions = self.vapi.nat44_user_session_dump(server.ip4n, 0)
4819 self.assertEqual(len(sessions), 0)
4820
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004821 def test_twice_nat(self):
4822 """ Twice NAT44 """
4823 self.twice_nat_common()
4824
4825 def test_self_twice_nat_positive(self):
4826 """ Self Twice NAT44 (positive test) """
4827 self.twice_nat_common(self_twice_nat=True, same_pg=True)
4828
4829 def test_self_twice_nat_negative(self):
4830 """ Self Twice NAT44 (negative test) """
4831 self.twice_nat_common(self_twice_nat=True)
4832
Matus Fabianb932d262017-12-18 05:38:24 -08004833 def test_twice_nat_lb(self):
4834 """ Twice NAT44 local service load balancing """
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004835 self.twice_nat_common(lb=True)
Matus Fabianb932d262017-12-18 05:38:24 -08004836
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004837 def test_self_twice_nat_lb_positive(self):
4838 """ Self Twice NAT44 local service load balancing (positive test) """
4839 self.twice_nat_common(lb=True, self_twice_nat=True, same_pg=True,
4840 client_id=1)
Matus Fabianb932d262017-12-18 05:38:24 -08004841
Juraj Sloboda1e5c07d2018-04-10 13:51:54 +02004842 def test_self_twice_nat_lb_negative(self):
4843 """ Self Twice NAT44 local service load balancing (negative test) """
4844 self.twice_nat_common(lb=True, self_twice_nat=True, same_pg=True,
4845 client_id=2)
Matus Fabianb932d262017-12-18 05:38:24 -08004846
4847 def test_twice_nat_interface_addr(self):
4848 """ Acquire twice NAT44 addresses from interface """
Matus Fabiana6110b62018-06-13 05:39:07 -07004849 self.vapi.nat44_add_interface_addr(self.pg3.sw_if_index, twice_nat=1)
Matus Fabianb932d262017-12-18 05:38:24 -08004850
4851 # no address in NAT pool
4852 adresses = self.vapi.nat44_address_dump()
4853 self.assertEqual(0, len(adresses))
4854
4855 # configure interface address and check NAT address pool
Matus Fabiana6110b62018-06-13 05:39:07 -07004856 self.pg3.config_ip4()
Matus Fabianb932d262017-12-18 05:38:24 -08004857 adresses = self.vapi.nat44_address_dump()
4858 self.assertEqual(1, len(adresses))
Matus Fabiana6110b62018-06-13 05:39:07 -07004859 self.assertEqual(adresses[0].ip_address[0:4], self.pg3.local_ip4n)
Matus Fabianb932d262017-12-18 05:38:24 -08004860 self.assertEqual(adresses[0].twice_nat, 1)
4861
4862 # remove interface address and check NAT address pool
Matus Fabiana6110b62018-06-13 05:39:07 -07004863 self.pg3.unconfig_ip4()
Matus Fabianb932d262017-12-18 05:38:24 -08004864 adresses = self.vapi.nat44_address_dump()
4865 self.assertEqual(0, len(adresses))
4866
Matus Fabian6c01dce2018-11-16 04:41:31 -08004867 def test_tcp_close(self):
4868 """ Close TCP session from inside network - output feature """
4869 self.vapi.nat44_forwarding_enable_disable(1)
4870 self.nat44_add_address(self.pg1.local_ip4)
4871 twice_nat_addr = '10.0.1.3'
4872 service_ip = '192.168.16.150'
4873 self.nat44_add_address(twice_nat_addr, twice_nat=1)
4874 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4875 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
4876 is_inside=0)
4877 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
4878 is_inside=0)
4879 self.nat44_add_static_mapping(self.pg0.remote_ip4,
4880 service_ip,
4881 80,
4882 80,
4883 proto=IP_PROTOS.tcp,
4884 out2in_only=1,
4885 twice_nat=1)
4886 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
4887 start_sessnum = len(sessions)
4888
4889 # SYN packet out->in
4890 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4891 IP(src=self.pg1.remote_ip4, dst=service_ip) /
4892 TCP(sport=33898, dport=80, flags="S"))
4893 self.pg1.add_stream(p)
4894 self.pg_enable_capture(self.pg_interfaces)
4895 self.pg_start()
4896 capture = self.pg0.get_capture(1)
4897 p = capture[0]
4898 tcp_port = p[TCP].sport
4899
4900 # SYN + ACK packet in->out
4901 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
4902 IP(src=self.pg0.remote_ip4, dst=twice_nat_addr) /
4903 TCP(sport=80, dport=tcp_port, flags="SA"))
4904 self.pg0.add_stream(p)
4905 self.pg_enable_capture(self.pg_interfaces)
4906 self.pg_start()
4907 self.pg1.get_capture(1)
4908
4909 # ACK packet out->in
4910 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4911 IP(src=self.pg1.remote_ip4, dst=service_ip) /
4912 TCP(sport=33898, dport=80, flags="A"))
4913 self.pg1.add_stream(p)
4914 self.pg_enable_capture(self.pg_interfaces)
4915 self.pg_start()
4916 self.pg0.get_capture(1)
4917
4918 # FIN packet in -> out
4919 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
4920 IP(src=self.pg0.remote_ip4, dst=twice_nat_addr) /
4921 TCP(sport=80, dport=tcp_port, flags="FA", seq=100, ack=300))
4922 self.pg0.add_stream(p)
4923 self.pg_enable_capture(self.pg_interfaces)
4924 self.pg_start()
4925 self.pg1.get_capture(1)
4926
4927 # FIN+ACK packet out -> in
4928 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4929 IP(src=self.pg1.remote_ip4, dst=service_ip) /
4930 TCP(sport=33898, dport=80, flags="FA", seq=300, ack=101))
4931 self.pg1.add_stream(p)
4932 self.pg_enable_capture(self.pg_interfaces)
4933 self.pg_start()
4934 self.pg0.get_capture(1)
4935
4936 # ACK packet in -> out
4937 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
4938 IP(src=self.pg0.remote_ip4, dst=twice_nat_addr) /
4939 TCP(sport=80, dport=tcp_port, flags="A", seq=101, ack=301))
4940 self.pg0.add_stream(p)
4941 self.pg_enable_capture(self.pg_interfaces)
4942 self.pg_start()
4943 self.pg1.get_capture(1)
4944
4945 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n,
4946 0)
4947 self.assertEqual(len(sessions) - start_sessnum, 0)
4948
Matus Fabianebdf1902018-05-04 03:57:42 -07004949 def test_tcp_session_close_in(self):
4950 """ Close TCP session from inside network """
Matus Fabian229c1aa2018-05-28 04:09:52 -07004951 self.tcp_port_out = 10505
Matus Fabianebdf1902018-05-04 03:57:42 -07004952 self.nat44_add_address(self.nat_addr)
Matus Fabian229c1aa2018-05-28 04:09:52 -07004953 self.nat44_add_static_mapping(self.pg0.remote_ip4,
4954 self.nat_addr,
4955 self.tcp_port_in,
4956 self.tcp_port_out,
4957 proto=IP_PROTOS.tcp,
4958 twice_nat=1)
Matus Fabianebdf1902018-05-04 03:57:42 -07004959 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
4960 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
4961 is_inside=0)
4962
4963 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
4964 start_sessnum = len(sessions)
4965
4966 self.initiate_tcp_session(self.pg0, self.pg1)
4967
Matus Fabian229c1aa2018-05-28 04:09:52 -07004968 # FIN packet in -> out
4969 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
4970 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
4971 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
4972 flags="FA", seq=100, ack=300))
4973 self.pg0.add_stream(p)
4974 self.pg_enable_capture(self.pg_interfaces)
4975 self.pg_start()
4976 self.pg1.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07004977
Matus Fabian229c1aa2018-05-28 04:09:52 -07004978 pkts = []
Matus Fabianebdf1902018-05-04 03:57:42 -07004979
Matus Fabian229c1aa2018-05-28 04:09:52 -07004980 # ACK packet out -> in
4981 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4982 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4983 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
4984 flags="A", seq=300, ack=101))
4985 pkts.append(p)
Matus Fabianebdf1902018-05-04 03:57:42 -07004986
Matus Fabian229c1aa2018-05-28 04:09:52 -07004987 # FIN packet out -> in
4988 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
4989 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
4990 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
4991 flags="FA", seq=300, ack=101))
4992 pkts.append(p)
Matus Fabianebdf1902018-05-04 03:57:42 -07004993
Matus Fabian229c1aa2018-05-28 04:09:52 -07004994 self.pg1.add_stream(pkts)
4995 self.pg_enable_capture(self.pg_interfaces)
4996 self.pg_start()
4997 self.pg0.get_capture(2)
Matus Fabianebdf1902018-05-04 03:57:42 -07004998
Matus Fabian229c1aa2018-05-28 04:09:52 -07004999 # ACK packet in -> out
5000 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5001 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5002 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5003 flags="A", seq=101, ack=301))
5004 self.pg0.add_stream(p)
5005 self.pg_enable_capture(self.pg_interfaces)
5006 self.pg_start()
5007 self.pg1.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005008
Matus Fabian229c1aa2018-05-28 04:09:52 -07005009 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n,
5010 0)
5011 self.assertEqual(len(sessions) - start_sessnum, 0)
Matus Fabianebdf1902018-05-04 03:57:42 -07005012
5013 def test_tcp_session_close_out(self):
5014 """ Close TCP session from outside network """
Matus Fabian229c1aa2018-05-28 04:09:52 -07005015 self.tcp_port_out = 10505
Matus Fabianebdf1902018-05-04 03:57:42 -07005016 self.nat44_add_address(self.nat_addr)
Matus Fabian229c1aa2018-05-28 04:09:52 -07005017 self.nat44_add_static_mapping(self.pg0.remote_ip4,
5018 self.nat_addr,
5019 self.tcp_port_in,
5020 self.tcp_port_out,
5021 proto=IP_PROTOS.tcp,
5022 twice_nat=1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005023 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5024 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5025 is_inside=0)
5026
5027 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
5028 start_sessnum = len(sessions)
5029
5030 self.initiate_tcp_session(self.pg0, self.pg1)
5031
Matus Fabian229c1aa2018-05-28 04:09:52 -07005032 # FIN packet out -> in
5033 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5034 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5035 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
5036 flags="FA", seq=100, ack=300))
5037 self.pg1.add_stream(p)
5038 self.pg_enable_capture(self.pg_interfaces)
5039 self.pg_start()
5040 self.pg0.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005041
Matus Fabian229c1aa2018-05-28 04:09:52 -07005042 # FIN+ACK packet in -> out
5043 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5044 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5045 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5046 flags="FA", seq=300, ack=101))
Matus Fabianebdf1902018-05-04 03:57:42 -07005047
Matus Fabian229c1aa2018-05-28 04:09:52 -07005048 self.pg0.add_stream(p)
5049 self.pg_enable_capture(self.pg_interfaces)
5050 self.pg_start()
5051 self.pg1.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005052
Matus Fabian229c1aa2018-05-28 04:09:52 -07005053 # ACK packet out -> in
5054 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5055 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5056 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
5057 flags="A", seq=101, ack=301))
5058 self.pg1.add_stream(p)
5059 self.pg_enable_capture(self.pg_interfaces)
5060 self.pg_start()
5061 self.pg0.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005062
Matus Fabian229c1aa2018-05-28 04:09:52 -07005063 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n,
5064 0)
5065 self.assertEqual(len(sessions) - start_sessnum, 0)
Matus Fabianebdf1902018-05-04 03:57:42 -07005066
5067 def test_tcp_session_close_simultaneous(self):
5068 """ Close TCP session from inside network """
Matus Fabian229c1aa2018-05-28 04:09:52 -07005069 self.tcp_port_out = 10505
Matus Fabianebdf1902018-05-04 03:57:42 -07005070 self.nat44_add_address(self.nat_addr)
Matus Fabian229c1aa2018-05-28 04:09:52 -07005071 self.nat44_add_static_mapping(self.pg0.remote_ip4,
5072 self.nat_addr,
5073 self.tcp_port_in,
5074 self.tcp_port_out,
5075 proto=IP_PROTOS.tcp,
5076 twice_nat=1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005077 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5078 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5079 is_inside=0)
5080
5081 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n, 0)
5082 start_sessnum = len(sessions)
5083
5084 self.initiate_tcp_session(self.pg0, self.pg1)
5085
Matus Fabian229c1aa2018-05-28 04:09:52 -07005086 # FIN packet in -> out
5087 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5088 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5089 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5090 flags="FA", seq=100, ack=300))
5091 self.pg0.add_stream(p)
5092 self.pg_enable_capture(self.pg_interfaces)
5093 self.pg_start()
5094 self.pg1.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005095
Matus Fabian229c1aa2018-05-28 04:09:52 -07005096 # FIN packet out -> in
5097 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5098 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5099 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
5100 flags="FA", seq=300, ack=100))
5101 self.pg1.add_stream(p)
5102 self.pg_enable_capture(self.pg_interfaces)
5103 self.pg_start()
5104 self.pg0.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005105
Matus Fabian229c1aa2018-05-28 04:09:52 -07005106 # ACK packet in -> out
5107 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5108 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5109 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5110 flags="A", seq=101, ack=301))
5111 self.pg0.add_stream(p)
5112 self.pg_enable_capture(self.pg_interfaces)
5113 self.pg_start()
5114 self.pg1.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005115
Matus Fabian229c1aa2018-05-28 04:09:52 -07005116 # ACK packet out -> in
5117 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5118 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5119 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
5120 flags="A", seq=301, ack=101))
5121 self.pg1.add_stream(p)
5122 self.pg_enable_capture(self.pg_interfaces)
5123 self.pg_start()
5124 self.pg0.get_capture(1)
Matus Fabianebdf1902018-05-04 03:57:42 -07005125
Matus Fabian229c1aa2018-05-28 04:09:52 -07005126 sessions = self.vapi.nat44_user_session_dump(self.pg0.remote_ip4n,
5127 0)
5128 self.assertEqual(len(sessions) - start_sessnum, 0)
Matus Fabianebdf1902018-05-04 03:57:42 -07005129
Matus Fabiana6110b62018-06-13 05:39:07 -07005130 def test_one_armed_nat44_static(self):
5131 """ One armed NAT44 and 1:1 NAPT asymmetrical rule """
5132 remote_host = self.pg4.remote_hosts[0]
5133 local_host = self.pg4.remote_hosts[1]
5134 external_port = 80
5135 local_port = 8080
5136 eh_port_in = 0
5137
5138 self.vapi.nat44_forwarding_enable_disable(1)
5139 self.nat44_add_address(self.nat_addr, twice_nat=1)
5140 self.nat44_add_static_mapping(local_host.ip4, self.nat_addr,
5141 local_port, external_port,
5142 proto=IP_PROTOS.tcp, out2in_only=1,
5143 twice_nat=1)
5144 self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index)
5145 self.vapi.nat44_interface_add_del_feature(self.pg4.sw_if_index,
5146 is_inside=0)
5147
5148 # from client to service
5149 p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) /
5150 IP(src=remote_host.ip4, dst=self.nat_addr) /
5151 TCP(sport=12345, dport=external_port))
5152 self.pg4.add_stream(p)
5153 self.pg_enable_capture(self.pg_interfaces)
5154 self.pg_start()
5155 capture = self.pg4.get_capture(1)
5156 p = capture[0]
5157 try:
5158 ip = p[IP]
5159 tcp = p[TCP]
5160 self.assertEqual(ip.dst, local_host.ip4)
5161 self.assertEqual(ip.src, self.nat_addr)
5162 self.assertEqual(tcp.dport, local_port)
5163 self.assertNotEqual(tcp.sport, 12345)
5164 eh_port_in = tcp.sport
5165 self.assert_packet_checksums_valid(p)
5166 except:
5167 self.logger.error(ppp("Unexpected or invalid packet:", p))
5168 raise
5169
5170 # from service back to client
5171 p = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) /
5172 IP(src=local_host.ip4, dst=self.nat_addr) /
5173 TCP(sport=local_port, dport=eh_port_in))
5174 self.pg4.add_stream(p)
5175 self.pg_enable_capture(self.pg_interfaces)
5176 self.pg_start()
5177 capture = self.pg4.get_capture(1)
5178 p = capture[0]
5179 try:
5180 ip = p[IP]
5181 tcp = p[TCP]
5182 self.assertEqual(ip.src, self.nat_addr)
5183 self.assertEqual(ip.dst, remote_host.ip4)
5184 self.assertEqual(tcp.sport, external_port)
5185 self.assertEqual(tcp.dport, 12345)
5186 self.assert_packet_checksums_valid(p)
5187 except:
5188 self.logger.error(ppp("Unexpected or invalid packet:", p))
5189 raise
5190
5191 def test_static_with_port_out2(self):
5192 """ 1:1 NAPT asymmetrical rule """
5193
5194 external_port = 80
5195 local_port = 8080
5196
5197 self.vapi.nat44_forwarding_enable_disable(1)
5198 self.nat44_add_static_mapping(self.pg0.remote_ip4, self.nat_addr,
5199 local_port, external_port,
5200 proto=IP_PROTOS.tcp, out2in_only=1)
5201 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5202 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5203 is_inside=0)
5204
5205 # from client to service
5206 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5207 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5208 TCP(sport=12345, dport=external_port))
5209 self.pg1.add_stream(p)
5210 self.pg_enable_capture(self.pg_interfaces)
5211 self.pg_start()
5212 capture = self.pg0.get_capture(1)
5213 p = capture[0]
5214 try:
5215 ip = p[IP]
5216 tcp = p[TCP]
5217 self.assertEqual(ip.dst, self.pg0.remote_ip4)
5218 self.assertEqual(tcp.dport, local_port)
5219 self.assert_packet_checksums_valid(p)
5220 except:
5221 self.logger.error(ppp("Unexpected or invalid packet:", p))
5222 raise
5223
5224 # ICMP error
5225 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
5226 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5227 ICMP(type=11) / capture[0][IP])
5228 self.pg0.add_stream(p)
5229 self.pg_enable_capture(self.pg_interfaces)
5230 self.pg_start()
5231 capture = self.pg1.get_capture(1)
5232 p = capture[0]
5233 try:
5234 self.assertEqual(p[IP].src, self.nat_addr)
5235 inner = p[IPerror]
5236 self.assertEqual(inner.dst, self.nat_addr)
5237 self.assertEqual(inner[TCPerror].dport, external_port)
5238 except:
5239 self.logger.error(ppp("Unexpected or invalid packet:", p))
5240 raise
5241
5242 # from service back to client
5243 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5244 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5245 TCP(sport=local_port, dport=12345))
5246 self.pg0.add_stream(p)
5247 self.pg_enable_capture(self.pg_interfaces)
5248 self.pg_start()
5249 capture = self.pg1.get_capture(1)
5250 p = capture[0]
5251 try:
5252 ip = p[IP]
5253 tcp = p[TCP]
5254 self.assertEqual(ip.src, self.nat_addr)
5255 self.assertEqual(tcp.sport, external_port)
5256 self.assert_packet_checksums_valid(p)
5257 except:
5258 self.logger.error(ppp("Unexpected or invalid packet:", p))
5259 raise
5260
5261 # ICMP error
5262 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
5263 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5264 ICMP(type=11) / capture[0][IP])
5265 self.pg1.add_stream(p)
5266 self.pg_enable_capture(self.pg_interfaces)
5267 self.pg_start()
5268 capture = self.pg0.get_capture(1)
5269 p = capture[0]
5270 try:
5271 self.assertEqual(p[IP].dst, self.pg0.remote_ip4)
5272 inner = p[IPerror]
5273 self.assertEqual(inner.src, self.pg0.remote_ip4)
5274 self.assertEqual(inner[TCPerror].sport, local_port)
5275 except:
5276 self.logger.error(ppp("Unexpected or invalid packet:", p))
5277 raise
5278
5279 # from client to server (no translation)
5280 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5281 IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) /
5282 TCP(sport=12346, dport=local_port))
5283 self.pg1.add_stream(p)
5284 self.pg_enable_capture(self.pg_interfaces)
5285 self.pg_start()
5286 capture = self.pg0.get_capture(1)
5287 p = capture[0]
5288 try:
5289 ip = p[IP]
5290 tcp = p[TCP]
5291 self.assertEqual(ip.dst, self.pg0.remote_ip4)
5292 self.assertEqual(tcp.dport, local_port)
5293 self.assert_packet_checksums_valid(p)
5294 except:
5295 self.logger.error(ppp("Unexpected or invalid packet:", p))
5296 raise
5297
5298 # from service back to client (no translation)
5299 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5300 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5301 TCP(sport=local_port, dport=12346))
5302 self.pg0.add_stream(p)
5303 self.pg_enable_capture(self.pg_interfaces)
5304 self.pg_start()
5305 capture = self.pg1.get_capture(1)
5306 p = capture[0]
5307 try:
5308 ip = p[IP]
5309 tcp = p[TCP]
5310 self.assertEqual(ip.src, self.pg0.remote_ip4)
5311 self.assertEqual(tcp.sport, local_port)
5312 self.assert_packet_checksums_valid(p)
5313 except:
5314 self.logger.error(ppp("Unexpected or invalid packet:", p))
5315 raise
5316
Matus Fabian235a47e2018-06-25 16:42:36 -07005317 def test_output_feature(self):
5318 """ NAT44 interface output feature (in2out postrouting) """
5319 self.vapi.nat44_forwarding_enable_disable(1)
5320 self.nat44_add_address(self.nat_addr)
5321 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
5322 is_inside=0)
5323 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
5324 is_inside=0)
5325
5326 # in2out
5327 pkts = self.create_stream_in(self.pg0, self.pg1)
5328 self.pg0.add_stream(pkts)
5329 self.pg_enable_capture(self.pg_interfaces)
5330 self.pg_start()
5331 capture = self.pg1.get_capture(len(pkts))
5332 self.verify_capture_out(capture)
5333
5334 # out2in
5335 pkts = self.create_stream_out(self.pg1)
5336 self.pg1.add_stream(pkts)
5337 self.pg_enable_capture(self.pg_interfaces)
5338 self.pg_start()
5339 capture = self.pg0.get_capture(len(pkts))
5340 self.verify_capture_in(capture, self.pg0)
5341
Matus Fabian8008d7c2018-07-09 01:34:20 -07005342 def test_multiple_vrf(self):
5343 """ Multiple VRF setup """
5344 external_addr = '1.2.3.4'
5345 external_port = 80
5346 local_port = 8080
5347 port = 0
5348
5349 self.vapi.nat44_forwarding_enable_disable(1)
5350 self.nat44_add_address(self.nat_addr)
5351 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5352 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
5353 is_inside=0)
5354 self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
5355 is_inside=0)
5356 self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index)
5357 self.vapi.nat44_interface_add_del_feature(self.pg5.sw_if_index,
5358 is_inside=0)
5359 self.vapi.nat44_interface_add_del_feature(self.pg6.sw_if_index,
5360 is_inside=0)
5361 self.nat44_add_static_mapping(self.pg5.remote_ip4, external_addr,
5362 local_port, external_port, vrf_id=1,
5363 proto=IP_PROTOS.tcp, out2in_only=1)
5364 self.nat44_add_static_mapping(
5365 self.pg0.remote_ip4, external_sw_if_index=self.pg0.sw_if_index,
5366 local_port=local_port, vrf_id=0, external_port=external_port,
5367 proto=IP_PROTOS.tcp, out2in_only=1)
5368
5369 # from client to service (both VRF1)
5370 p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) /
5371 IP(src=self.pg6.remote_ip4, dst=external_addr) /
5372 TCP(sport=12345, dport=external_port))
5373 self.pg6.add_stream(p)
5374 self.pg_enable_capture(self.pg_interfaces)
5375 self.pg_start()
5376 capture = self.pg5.get_capture(1)
5377 p = capture[0]
5378 try:
5379 ip = p[IP]
5380 tcp = p[TCP]
5381 self.assertEqual(ip.dst, self.pg5.remote_ip4)
5382 self.assertEqual(tcp.dport, local_port)
5383 self.assert_packet_checksums_valid(p)
5384 except:
5385 self.logger.error(ppp("Unexpected or invalid packet:", p))
5386 raise
5387
5388 # from service back to client (both VRF1)
5389 p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
5390 IP(src=self.pg5.remote_ip4, dst=self.pg6.remote_ip4) /
5391 TCP(sport=local_port, dport=12345))
5392 self.pg5.add_stream(p)
5393 self.pg_enable_capture(self.pg_interfaces)
5394 self.pg_start()
5395 capture = self.pg6.get_capture(1)
5396 p = capture[0]
5397 try:
5398 ip = p[IP]
5399 tcp = p[TCP]
5400 self.assertEqual(ip.src, external_addr)
5401 self.assertEqual(tcp.sport, external_port)
5402 self.assert_packet_checksums_valid(p)
5403 except:
5404 self.logger.error(ppp("Unexpected or invalid packet:", p))
5405 raise
5406
5407 # dynamic NAT from VRF1 to VRF0 (output-feature)
5408 p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
5409 IP(src=self.pg5.remote_ip4, dst=self.pg1.remote_ip4) /
5410 TCP(sport=2345, dport=22))
5411 self.pg5.add_stream(p)
5412 self.pg_enable_capture(self.pg_interfaces)
5413 self.pg_start()
5414 capture = self.pg1.get_capture(1)
5415 p = capture[0]
5416 try:
5417 ip = p[IP]
5418 tcp = p[TCP]
5419 self.assertEqual(ip.src, self.nat_addr)
5420 self.assertNotEqual(tcp.sport, 2345)
5421 self.assert_packet_checksums_valid(p)
5422 port = tcp.sport
5423 except:
5424 self.logger.error(ppp("Unexpected or invalid packet:", p))
5425 raise
5426
5427 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
5428 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
5429 TCP(sport=22, dport=port))
5430 self.pg1.add_stream(p)
5431 self.pg_enable_capture(self.pg_interfaces)
5432 self.pg_start()
5433 capture = self.pg5.get_capture(1)
5434 p = capture[0]
5435 try:
5436 ip = p[IP]
5437 tcp = p[TCP]
5438 self.assertEqual(ip.dst, self.pg5.remote_ip4)
5439 self.assertEqual(tcp.dport, 2345)
5440 self.assert_packet_checksums_valid(p)
5441 except:
5442 self.logger.error(ppp("Unexpected or invalid packet:", p))
5443 raise
5444
5445 # from client VRF1 to service VRF0
5446 p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) /
5447 IP(src=self.pg6.remote_ip4, dst=self.pg0.local_ip4) /
5448 TCP(sport=12346, dport=external_port))
5449 self.pg6.add_stream(p)
5450 self.pg_enable_capture(self.pg_interfaces)
5451 self.pg_start()
5452 capture = self.pg0.get_capture(1)
5453 p = capture[0]
5454 try:
5455 ip = p[IP]
5456 tcp = p[TCP]
5457 self.assertEqual(ip.dst, self.pg0.remote_ip4)
5458 self.assertEqual(tcp.dport, local_port)
5459 self.assert_packet_checksums_valid(p)
5460 except:
5461 self.logger.error(ppp("Unexpected or invalid packet:", p))
5462 raise
5463
5464 # from service VRF0 back to client VRF1
5465 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5466 IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) /
5467 TCP(sport=local_port, dport=12346))
5468 self.pg0.add_stream(p)
5469 self.pg_enable_capture(self.pg_interfaces)
5470 self.pg_start()
5471 capture = self.pg6.get_capture(1)
5472 p = capture[0]
5473 try:
5474 ip = p[IP]
5475 tcp = p[TCP]
5476 self.assertEqual(ip.src, self.pg0.local_ip4)
5477 self.assertEqual(tcp.sport, external_port)
5478 self.assert_packet_checksums_valid(p)
5479 except:
5480 self.logger.error(ppp("Unexpected or invalid packet:", p))
5481 raise
5482
5483 # from client VRF0 to service VRF1
5484 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5485 IP(src=self.pg0.remote_ip4, dst=external_addr) /
5486 TCP(sport=12347, dport=external_port))
5487 self.pg0.add_stream(p)
5488 self.pg_enable_capture(self.pg_interfaces)
5489 self.pg_start()
5490 capture = self.pg5.get_capture(1)
5491 p = capture[0]
5492 try:
5493 ip = p[IP]
5494 tcp = p[TCP]
5495 self.assertEqual(ip.dst, self.pg5.remote_ip4)
5496 self.assertEqual(tcp.dport, local_port)
5497 self.assert_packet_checksums_valid(p)
5498 except:
5499 self.logger.error(ppp("Unexpected or invalid packet:", p))
5500 raise
5501
5502 # from service VRF1 back to client VRF0
5503 p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
5504 IP(src=self.pg5.remote_ip4, dst=self.pg0.remote_ip4) /
5505 TCP(sport=local_port, dport=12347))
5506 self.pg5.add_stream(p)
5507 self.pg_enable_capture(self.pg_interfaces)
5508 self.pg_start()
5509 capture = self.pg0.get_capture(1)
5510 p = capture[0]
5511 try:
5512 ip = p[IP]
5513 tcp = p[TCP]
5514 self.assertEqual(ip.src, external_addr)
5515 self.assertEqual(tcp.sport, external_port)
5516 self.assert_packet_checksums_valid(p)
5517 except:
5518 self.logger.error(ppp("Unexpected or invalid packet:", p))
5519 raise
5520
5521 # from client to server (both VRF1, no translation)
5522 p = (Ether(src=self.pg6.remote_mac, dst=self.pg6.local_mac) /
5523 IP(src=self.pg6.remote_ip4, dst=self.pg5.remote_ip4) /
5524 TCP(sport=12348, dport=local_port))
5525 self.pg6.add_stream(p)
5526 self.pg_enable_capture(self.pg_interfaces)
5527 self.pg_start()
5528 capture = self.pg5.get_capture(1)
5529 p = capture[0]
5530 try:
5531 ip = p[IP]
5532 tcp = p[TCP]
5533 self.assertEqual(ip.dst, self.pg5.remote_ip4)
5534 self.assertEqual(tcp.dport, local_port)
5535 self.assert_packet_checksums_valid(p)
5536 except:
5537 self.logger.error(ppp("Unexpected or invalid packet:", p))
5538 raise
5539
5540 # from server back to client (both VRF1, no translation)
5541 p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
5542 IP(src=self.pg5.remote_ip4, dst=self.pg6.remote_ip4) /
5543 TCP(sport=local_port, dport=12348))
5544 self.pg5.add_stream(p)
5545 self.pg_enable_capture(self.pg_interfaces)
5546 self.pg_start()
5547 capture = self.pg6.get_capture(1)
5548 p = capture[0]
5549 try:
5550 ip = p[IP]
5551 tcp = p[TCP]
5552 self.assertEqual(ip.src, self.pg5.remote_ip4)
5553 self.assertEqual(tcp.sport, local_port)
5554 self.assert_packet_checksums_valid(p)
5555 except:
5556 self.logger.error(ppp("Unexpected or invalid packet:", p))
5557 raise
5558
5559 # from client VRF1 to server VRF0 (no translation)
5560 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5561 IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) /
5562 TCP(sport=local_port, dport=12349))
5563 self.pg0.add_stream(p)
5564 self.pg_enable_capture(self.pg_interfaces)
5565 self.pg_start()
5566 capture = self.pg6.get_capture(1)
5567 p = capture[0]
5568 try:
5569 ip = p[IP]
5570 tcp = p[TCP]
5571 self.assertEqual(ip.src, self.pg0.remote_ip4)
5572 self.assertEqual(tcp.sport, local_port)
5573 self.assert_packet_checksums_valid(p)
5574 except:
5575 self.logger.error(ppp("Unexpected or invalid packet:", p))
5576 raise
5577
5578 # from server VRF0 back to client VRF1 (no translation)
5579 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5580 IP(src=self.pg0.remote_ip4, dst=self.pg6.remote_ip4) /
5581 TCP(sport=local_port, dport=12349))
5582 self.pg0.add_stream(p)
5583 self.pg_enable_capture(self.pg_interfaces)
5584 self.pg_start()
5585 capture = self.pg6.get_capture(1)
5586 p = capture[0]
5587 try:
5588 ip = p[IP]
5589 tcp = p[TCP]
5590 self.assertEqual(ip.src, self.pg0.remote_ip4)
5591 self.assertEqual(tcp.sport, local_port)
5592 self.assert_packet_checksums_valid(p)
5593 except:
5594 self.logger.error(ppp("Unexpected or invalid packet:", p))
5595 raise
5596
5597 # from client VRF0 to server VRF1 (no translation)
5598 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5599 IP(src=self.pg0.remote_ip4, dst=self.pg5.remote_ip4) /
5600 TCP(sport=12344, dport=local_port))
5601 self.pg0.add_stream(p)
5602 self.pg_enable_capture(self.pg_interfaces)
5603 self.pg_start()
5604 capture = self.pg5.get_capture(1)
5605 p = capture[0]
5606 try:
5607 ip = p[IP]
5608 tcp = p[TCP]
5609 self.assertEqual(ip.dst, self.pg5.remote_ip4)
5610 self.assertEqual(tcp.dport, local_port)
5611 self.assert_packet_checksums_valid(p)
5612 except:
5613 self.logger.error(ppp("Unexpected or invalid packet:", p))
5614 raise
5615
5616 # from server VRF1 back to client VRF0 (no translation)
5617 p = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
5618 IP(src=self.pg5.remote_ip4, dst=self.pg0.remote_ip4) /
5619 TCP(sport=local_port, dport=12344))
5620 self.pg5.add_stream(p)
5621 self.pg_enable_capture(self.pg_interfaces)
5622 self.pg_start()
5623 capture = self.pg0.get_capture(1)
5624 p = capture[0]
5625 try:
5626 ip = p[IP]
5627 tcp = p[TCP]
5628 self.assertEqual(ip.src, self.pg5.remote_ip4)
5629 self.assertEqual(tcp.sport, local_port)
5630 self.assert_packet_checksums_valid(p)
5631 except:
5632 self.logger.error(ppp("Unexpected or invalid packet:", p))
5633 raise
5634
Matus Fabian878c6462018-08-23 00:33:35 -07005635 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
5636 def test_session_timeout(self):
5637 """ NAT44 session timeouts """
5638 self.nat44_add_address(self.nat_addr)
5639 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5640 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5641 is_inside=0)
5642 self.vapi.nat_set_timeouts(icmp=5)
5643
5644 max_sessions = 1000
5645 pkts = []
5646 for i in range(0, max_sessions):
5647 src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF)
5648 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
5649 IP(src=src, dst=self.pg1.remote_ip4) /
5650 ICMP(id=1025, type='echo-request'))
5651 pkts.append(p)
5652 self.pg0.add_stream(pkts)
5653 self.pg_enable_capture(self.pg_interfaces)
5654 self.pg_start()
5655 self.pg1.get_capture(max_sessions)
5656
5657 sleep(10)
5658
5659 pkts = []
5660 for i in range(0, max_sessions):
5661 src = "10.10.%u.%u" % ((i & 0xFF00) >> 8, i & 0xFF)
5662 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
5663 IP(src=src, dst=self.pg1.remote_ip4) /
5664 ICMP(id=1026, type='echo-request'))
5665 pkts.append(p)
5666 self.pg0.add_stream(pkts)
5667 self.pg_enable_capture(self.pg_interfaces)
5668 self.pg_start()
5669 self.pg1.get_capture(max_sessions)
5670
5671 nsessions = 0
5672 users = self.vapi.nat44_user_dump()
5673 for user in users:
5674 nsessions = nsessions + user.nsessions
5675 self.assertLess(nsessions, 2 * max_sessions)
5676
5677 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
Matus Fabian15e8e682018-11-21 04:53:10 -08005678 def test_session_rst_timeout(self):
5679 """ NAT44 session RST timeouts """
5680 self.nat44_add_address(self.nat_addr)
5681 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5682 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5683 is_inside=0)
5684 self.vapi.nat_set_timeouts(tcp_transitory=5)
5685
5686 nat44_config = self.vapi.nat_show_config()
5687
5688 self.initiate_tcp_session(self.pg0, self.pg1)
5689 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5690 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5691 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
5692 flags="R"))
5693 self.pg0.add_stream(p)
5694 self.pg_enable_capture(self.pg_interfaces)
5695 self.pg_start()
5696 self.pg1.get_capture(1)
5697
5698 pkts_num = nat44_config.max_translations_per_user - 1
5699 pkts = []
5700 for i in range(0, pkts_num):
5701 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
5702 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5703 UDP(sport=1025 + i, dport=53))
5704 pkts.append(p)
5705 self.pg0.add_stream(pkts)
5706 self.pg_enable_capture(self.pg_interfaces)
5707 self.pg_start()
5708 self.pg1.get_capture(pkts_num)
5709
5710 sleep(6)
5711
5712 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5713 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5714 TCP(sport=self.tcp_port_in + 1, dport=self.tcp_external_port + 1,
5715 flags="S"))
5716 self.pg0.add_stream(p)
5717 self.pg_enable_capture(self.pg_interfaces)
5718 self.pg_start()
5719 self.pg1.get_capture(1)
5720
5721 nsessions = 0
5722 users = self.vapi.nat44_user_dump()
5723 self.assertEqual(len(users), 1)
5724 self.assertEqual(users[0].ip_address, self.pg0.remote_ip4n)
5725 self.assertEqual(users[0].nsessions,
5726 nat44_config.max_translations_per_user)
5727
5728 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
Matus Fabian878c6462018-08-23 00:33:35 -07005729 def test_session_limit_per_user(self):
5730 """ Maximum sessions per user limit """
5731 self.nat44_add_address(self.nat_addr)
5732 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5733 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
5734 is_inside=0)
5735 self.vapi.set_ipfix_exporter(collector_address=self.pg2.remote_ip4n,
5736 src_address=self.pg2.local_ip4n,
5737 path_mtu=512,
5738 template_interval=10)
Matus Fabian8fdc0152018-09-24 04:41:28 -07005739 self.vapi.nat_set_timeouts(udp=5)
Matus Fabian878c6462018-08-23 00:33:35 -07005740
5741 # get maximum number of translations per user
5742 nat44_config = self.vapi.nat_show_config()
5743
5744 pkts = []
5745 for port in range(0, nat44_config.max_translations_per_user):
5746 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5747 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5748 UDP(sport=1025 + port, dport=1025 + port))
5749 pkts.append(p)
5750
5751 self.pg0.add_stream(pkts)
5752 self.pg_enable_capture(self.pg_interfaces)
5753 self.pg_start()
5754 capture = self.pg1.get_capture(len(pkts))
5755
5756 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
5757 src_port=self.ipfix_src_port)
5758
5759 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5760 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5761 UDP(sport=3001, dport=3002))
5762 self.pg0.add_stream(p)
5763 self.pg_enable_capture(self.pg_interfaces)
5764 self.pg_start()
5765 capture = self.pg1.assert_nothing_captured()
5766
5767 # verify IPFIX logging
5768 self.vapi.cli("ipfix flush") # FIXME this should be an API call
5769 sleep(1)
5770 capture = self.pg2.get_capture(10)
5771 ipfix = IPFIXDecoder()
5772 # first load template
5773 for p in capture:
5774 self.assertTrue(p.haslayer(IPFIX))
5775 if p.haslayer(Template):
5776 ipfix.add_template(p.getlayer(Template))
5777 # verify events in data set
5778 for p in capture:
5779 if p.haslayer(Data):
5780 data = ipfix.decode_data_set(p.getlayer(Set))
5781 self.verify_ipfix_max_entries_per_user(
5782 data,
5783 nat44_config.max_translations_per_user,
5784 self.pg0.remote_ip4n)
5785
Matus Fabian8fdc0152018-09-24 04:41:28 -07005786 sleep(6)
5787 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
5788 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
5789 UDP(sport=3001, dport=3002))
5790 self.pg0.add_stream(p)
5791 self.pg_enable_capture(self.pg_interfaces)
5792 self.pg_start()
5793 self.pg1.get_capture(1)
5794
Matus Fabiande886752016-12-07 03:38:19 -08005795 def tearDown(self):
Matus Fabiana6110b62018-06-13 05:39:07 -07005796 super(TestNAT44EndpointDependent, self).tearDown()
Matus Fabiande886752016-12-07 03:38:19 -08005797 if not self.vpp_dead:
Matus Fabian82119542018-01-25 01:13:22 -08005798 self.logger.info(self.vapi.cli("show nat44 addresses"))
5799 self.logger.info(self.vapi.cli("show nat44 interfaces"))
5800 self.logger.info(self.vapi.cli("show nat44 static mappings"))
5801 self.logger.info(self.vapi.cli("show nat44 interface address"))
5802 self.logger.info(self.vapi.cli("show nat44 sessions detail"))
Matus Fabian229c1aa2018-05-28 04:09:52 -07005803 self.logger.info(self.vapi.cli("show nat44 hash tables detail"))
Matus Fabian878c6462018-08-23 00:33:35 -07005804 self.logger.info(self.vapi.cli("show nat timeouts"))
Matus Fabian2ba92e32017-08-21 07:05:03 -07005805 self.clear_nat44()
Matus Fabian229c1aa2018-05-28 04:09:52 -07005806 self.vapi.cli("clear logging")
Matus Fabiande886752016-12-07 03:38:19 -08005807
Matus Fabianeea28d72017-01-13 04:15:54 -08005808
Juraj Slobodacba69362017-12-19 02:09:32 +01005809class TestNAT44Out2InDPO(MethodHolder):
5810 """ NAT44 Test Cases using out2in DPO """
5811
5812 @classmethod
5813 def setUpConstants(cls):
5814 super(TestNAT44Out2InDPO, cls).setUpConstants()
5815 cls.vpp_cmdline.extend(["nat", "{", "out2in dpo", "}"])
5816
5817 @classmethod
5818 def setUpClass(cls):
5819 super(TestNAT44Out2InDPO, cls).setUpClass()
Matus Fabian229c1aa2018-05-28 04:09:52 -07005820 cls.vapi.cli("set log class nat level debug")
Juraj Slobodacba69362017-12-19 02:09:32 +01005821
5822 try:
5823 cls.tcp_port_in = 6303
5824 cls.tcp_port_out = 6303
5825 cls.udp_port_in = 6304
5826 cls.udp_port_out = 6304
5827 cls.icmp_id_in = 6305
5828 cls.icmp_id_out = 6305
5829 cls.nat_addr = '10.0.0.3'
5830 cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
5831 cls.dst_ip4 = '192.168.70.1'
5832
5833 cls.create_pg_interfaces(range(2))
5834
5835 cls.pg0.admin_up()
5836 cls.pg0.config_ip4()
5837 cls.pg0.resolve_arp()
5838
5839 cls.pg1.admin_up()
5840 cls.pg1.config_ip6()
5841 cls.pg1.resolve_ndp()
5842
5843 cls.vapi.ip_add_del_route(is_ipv6=True, dst_address='\x00'*16,
5844 dst_address_length=0,
5845 next_hop_address=cls.pg1.remote_ip6n,
5846 next_hop_sw_if_index=cls.pg1.sw_if_index)
5847
5848 except Exception:
5849 super(TestNAT44Out2InDPO, cls).tearDownClass()
5850 raise
5851
5852 def configure_xlat(self):
5853 self.dst_ip6_pfx = '1:2:3::'
5854 self.dst_ip6_pfx_n = socket.inet_pton(socket.AF_INET6,
5855 self.dst_ip6_pfx)
5856 self.dst_ip6_pfx_len = 96
5857 self.src_ip6_pfx = '4:5:6::'
5858 self.src_ip6_pfx_n = socket.inet_pton(socket.AF_INET6,
5859 self.src_ip6_pfx)
5860 self.src_ip6_pfx_len = 96
5861 self.vapi.map_add_domain(self.dst_ip6_pfx_n, self.dst_ip6_pfx_len,
5862 self.src_ip6_pfx_n, self.src_ip6_pfx_len,
5863 '\x00\x00\x00\x00', 0, is_translation=1,
5864 is_rfc6052=1)
5865
5866 def test_464xlat_ce(self):
5867 """ Test 464XLAT CE with NAT44 """
5868
Matus Fabian69ce30d2018-08-22 01:27:10 -07005869 nat_config = self.vapi.nat_show_config()
5870 self.assertEqual(1, nat_config.out2in_dpo)
5871
Juraj Slobodacba69362017-12-19 02:09:32 +01005872 self.configure_xlat()
5873
5874 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
5875 self.vapi.nat44_add_del_address_range(self.nat_addr_n, self.nat_addr_n)
5876
5877 out_src_ip6 = self.compose_ip6(self.dst_ip4, self.dst_ip6_pfx,
5878 self.dst_ip6_pfx_len)
5879 out_dst_ip6 = self.compose_ip6(self.nat_addr, self.src_ip6_pfx,
5880 self.src_ip6_pfx_len)
5881
5882 try:
5883 pkts = self.create_stream_in(self.pg0, self.pg1, self.dst_ip4)
5884 self.pg0.add_stream(pkts)
5885 self.pg_enable_capture(self.pg_interfaces)
5886 self.pg_start()
5887 capture = self.pg1.get_capture(len(pkts))
5888 self.verify_capture_out_ip6(capture, nat_ip=out_dst_ip6,
5889 dst_ip=out_src_ip6)
5890
5891 pkts = self.create_stream_out_ip6(self.pg1, out_src_ip6,
5892 out_dst_ip6)
5893 self.pg1.add_stream(pkts)
5894 self.pg_enable_capture(self.pg_interfaces)
5895 self.pg_start()
5896 capture = self.pg0.get_capture(len(pkts))
5897 self.verify_capture_in(capture, self.pg0)
5898 finally:
5899 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index,
5900 is_add=0)
5901 self.vapi.nat44_add_del_address_range(self.nat_addr_n,
5902 self.nat_addr_n, is_add=0)
5903
5904 def test_464xlat_ce_no_nat(self):
5905 """ Test 464XLAT CE without NAT44 """
5906
5907 self.configure_xlat()
5908
5909 out_src_ip6 = self.compose_ip6(self.dst_ip4, self.dst_ip6_pfx,
5910 self.dst_ip6_pfx_len)
5911 out_dst_ip6 = self.compose_ip6(self.pg0.remote_ip4, self.src_ip6_pfx,
5912 self.src_ip6_pfx_len)
5913
5914 pkts = self.create_stream_in(self.pg0, self.pg1, self.dst_ip4)
5915 self.pg0.add_stream(pkts)
5916 self.pg_enable_capture(self.pg_interfaces)
5917 self.pg_start()
5918 capture = self.pg1.get_capture(len(pkts))
5919 self.verify_capture_out_ip6(capture, dst_ip=out_src_ip6,
5920 nat_ip=out_dst_ip6, same_port=True)
5921
5922 pkts = self.create_stream_out_ip6(self.pg1, out_src_ip6, out_dst_ip6)
5923 self.pg1.add_stream(pkts)
5924 self.pg_enable_capture(self.pg_interfaces)
5925 self.pg_start()
5926 capture = self.pg0.get_capture(len(pkts))
5927 self.verify_capture_in(capture, self.pg0)
5928
5929
Martin Gálikd7f75cd2017-03-27 06:02:47 -07005930class TestDeterministicNAT(MethodHolder):
Matus Fabian066f0342017-02-10 03:48:01 -08005931 """ Deterministic NAT Test Cases """
5932
5933 @classmethod
5934 def setUpConstants(cls):
5935 super(TestDeterministicNAT, cls).setUpConstants()
Matus Fabian2ba92e32017-08-21 07:05:03 -07005936 cls.vpp_cmdline.extend(["nat", "{", "deterministic", "}"])
Matus Fabian066f0342017-02-10 03:48:01 -08005937
5938 @classmethod
5939 def setUpClass(cls):
5940 super(TestDeterministicNAT, cls).setUpClass()
Matus Fabian229c1aa2018-05-28 04:09:52 -07005941 cls.vapi.cli("set log class nat level debug")
Matus Fabian066f0342017-02-10 03:48:01 -08005942
5943 try:
Martin Gálik977c1cb2017-03-30 23:21:51 -07005944 cls.tcp_port_in = 6303
Martin Gálik9806eae2017-04-25 01:25:08 -07005945 cls.tcp_external_port = 6303
Martin Gálik977c1cb2017-03-30 23:21:51 -07005946 cls.udp_port_in = 6304
Martin Gálik9806eae2017-04-25 01:25:08 -07005947 cls.udp_external_port = 6304
Martin Gálik977c1cb2017-03-30 23:21:51 -07005948 cls.icmp_id_in = 6305
Matus Fabian2ba92e32017-08-21 07:05:03 -07005949 cls.nat_addr = '10.0.0.3'
Martin Gálik977c1cb2017-03-30 23:21:51 -07005950
Matus Fabian2f2db1c2017-04-18 05:29:59 -07005951 cls.create_pg_interfaces(range(3))
Matus Fabian066f0342017-02-10 03:48:01 -08005952 cls.interfaces = list(cls.pg_interfaces)
5953
5954 for i in cls.interfaces:
5955 i.admin_up()
5956 i.config_ip4()
5957 i.resolve_arp()
5958
Martin Gálik977c1cb2017-03-30 23:21:51 -07005959 cls.pg0.generate_remote_hosts(2)
5960 cls.pg0.configure_ipv4_neighbors()
5961
Matus Fabian066f0342017-02-10 03:48:01 -08005962 except Exception:
5963 super(TestDeterministicNAT, cls).tearDownClass()
5964 raise
5965
Martin Gálik977c1cb2017-03-30 23:21:51 -07005966 def create_stream_in(self, in_if, out_if, ttl=64):
5967 """
5968 Create packet stream for inside network
5969
5970 :param in_if: Inside interface
5971 :param out_if: Outside interface
5972 :param ttl: TTL of generated packets
5973 """
5974 pkts = []
5975 # TCP
5976 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
5977 IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=ttl) /
Martin Gálik9806eae2017-04-25 01:25:08 -07005978 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07005979 pkts.append(p)
5980
5981 # UDP
5982 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
5983 IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=ttl) /
Martin Gálik9806eae2017-04-25 01:25:08 -07005984 UDP(sport=self.udp_port_in, dport=self.udp_external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07005985 pkts.append(p)
5986
5987 # ICMP
5988 p = (Ether(dst=in_if.local_mac, src=in_if.remote_mac) /
5989 IP(src=in_if.remote_ip4, dst=out_if.remote_ip4, ttl=ttl) /
5990 ICMP(id=self.icmp_id_in, type='echo-request'))
5991 pkts.append(p)
5992
5993 return pkts
5994
5995 def create_stream_out(self, out_if, dst_ip=None, ttl=64):
5996 """
5997 Create packet stream for outside network
5998
5999 :param out_if: Outside interface
Matus Fabian2ba92e32017-08-21 07:05:03 -07006000 :param dst_ip: Destination IP address (Default use global NAT address)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006001 :param ttl: TTL of generated packets
6002 """
6003 if dst_ip is None:
Matus Fabian2ba92e32017-08-21 07:05:03 -07006004 dst_ip = self.nat_addr
Martin Gálik977c1cb2017-03-30 23:21:51 -07006005 pkts = []
6006 # TCP
6007 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
6008 IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006009 TCP(dport=self.tcp_port_out, sport=self.tcp_external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006010 pkts.append(p)
6011
6012 # UDP
6013 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
6014 IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006015 UDP(dport=self.udp_port_out, sport=self.udp_external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006016 pkts.append(p)
6017
6018 # ICMP
6019 p = (Ether(dst=out_if.local_mac, src=out_if.remote_mac) /
6020 IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl) /
6021 ICMP(id=self.icmp_external_id, type='echo-reply'))
6022 pkts.append(p)
6023
6024 return pkts
6025
Matus Fabian05ca4a32018-09-04 23:45:13 -07006026 def verify_capture_out(self, capture, nat_ip=None):
Martin Gálik977c1cb2017-03-30 23:21:51 -07006027 """
6028 Verify captured packets on outside network
6029
6030 :param capture: Captured packets
Matus Fabian2ba92e32017-08-21 07:05:03 -07006031 :param nat_ip: Translated IP address (Default use global NAT address)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006032 :param same_port: Sorce port number is not translated (Default False)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006033 """
6034 if nat_ip is None:
Matus Fabian2ba92e32017-08-21 07:05:03 -07006035 nat_ip = self.nat_addr
Martin Gálik977c1cb2017-03-30 23:21:51 -07006036 for packet in capture:
6037 try:
6038 self.assertEqual(packet[IP].src, nat_ip)
6039 if packet.haslayer(TCP):
Martin Gálik9806eae2017-04-25 01:25:08 -07006040 self.tcp_port_out = packet[TCP].sport
Martin Gálik977c1cb2017-03-30 23:21:51 -07006041 elif packet.haslayer(UDP):
Martin Gálik9806eae2017-04-25 01:25:08 -07006042 self.udp_port_out = packet[UDP].sport
Martin Gálik977c1cb2017-03-30 23:21:51 -07006043 else:
6044 self.icmp_external_id = packet[ICMP].id
6045 except:
6046 self.logger.error(ppp("Unexpected or invalid packet "
6047 "(outside network):", packet))
6048 raise
6049
Matus Fabian066f0342017-02-10 03:48:01 -08006050 def test_deterministic_mode(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006051 """ NAT plugin run deterministic mode """
Matus Fabian066f0342017-02-10 03:48:01 -08006052 in_addr = '172.16.255.0'
6053 out_addr = '172.17.255.50'
6054 in_addr_t = '172.16.255.20'
6055 in_addr_n = socket.inet_aton(in_addr)
6056 out_addr_n = socket.inet_aton(out_addr)
6057 in_addr_t_n = socket.inet_aton(in_addr_t)
6058 in_plen = 24
6059 out_plen = 32
6060
Matus Fabian2ba92e32017-08-21 07:05:03 -07006061 nat_config = self.vapi.nat_show_config()
6062 self.assertEqual(1, nat_config.deterministic)
Matus Fabian066f0342017-02-10 03:48:01 -08006063
Matus Fabian2ba92e32017-08-21 07:05:03 -07006064 self.vapi.nat_det_add_del_map(in_addr_n, in_plen, out_addr_n, out_plen)
Matus Fabian066f0342017-02-10 03:48:01 -08006065
Matus Fabian2ba92e32017-08-21 07:05:03 -07006066 rep1 = self.vapi.nat_det_forward(in_addr_t_n)
Matus Fabian066f0342017-02-10 03:48:01 -08006067 self.assertEqual(rep1.out_addr[:4], out_addr_n)
Matus Fabian2ba92e32017-08-21 07:05:03 -07006068 rep2 = self.vapi.nat_det_reverse(out_addr_n, rep1.out_port_hi)
Matus Fabian066f0342017-02-10 03:48:01 -08006069 self.assertEqual(rep2.in_addr[:4], in_addr_t_n)
6070
Matus Fabian2ba92e32017-08-21 07:05:03 -07006071 deterministic_mappings = self.vapi.nat_det_map_dump()
Martin17a75cb2017-03-08 05:53:20 -08006072 self.assertEqual(len(deterministic_mappings), 1)
6073 dsm = deterministic_mappings[0]
6074 self.assertEqual(in_addr_n, dsm.in_addr[:4])
6075 self.assertEqual(in_plen, dsm.in_plen)
6076 self.assertEqual(out_addr_n, dsm.out_addr[:4])
6077 self.assertEqual(out_plen, dsm.out_plen)
6078
Matus Fabian2ba92e32017-08-21 07:05:03 -07006079 self.clear_nat_det()
6080 deterministic_mappings = self.vapi.nat_det_map_dump()
Martinb616e9f2017-03-14 02:25:45 -07006081 self.assertEqual(len(deterministic_mappings), 0)
6082
Matus Fabian6a0946f2017-04-12 03:36:13 -07006083 def test_set_timeouts(self):
6084 """ Set deterministic NAT timeouts """
Matus Fabian878c6462018-08-23 00:33:35 -07006085 timeouts_before = self.vapi.nat_get_timeouts()
Matus Fabian6a0946f2017-04-12 03:36:13 -07006086
Matus Fabian878c6462018-08-23 00:33:35 -07006087 self.vapi.nat_set_timeouts(timeouts_before.udp + 10,
6088 timeouts_before.tcp_established + 10,
6089 timeouts_before.tcp_transitory + 10,
6090 timeouts_before.icmp + 10)
Matus Fabian6a0946f2017-04-12 03:36:13 -07006091
Matus Fabian878c6462018-08-23 00:33:35 -07006092 timeouts_after = self.vapi.nat_get_timeouts()
Matus Fabian6a0946f2017-04-12 03:36:13 -07006093
6094 self.assertNotEqual(timeouts_before.udp, timeouts_after.udp)
6095 self.assertNotEqual(timeouts_before.icmp, timeouts_after.icmp)
6096 self.assertNotEqual(timeouts_before.tcp_established,
6097 timeouts_after.tcp_established)
6098 self.assertNotEqual(timeouts_before.tcp_transitory,
6099 timeouts_after.tcp_transitory)
6100
Martin Gálik977c1cb2017-03-30 23:21:51 -07006101 def test_det_in(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006102 """ Deterministic NAT translation test (TCP, UDP, ICMP) """
Martin Gálik977c1cb2017-03-30 23:21:51 -07006103
6104 nat_ip = "10.0.0.10"
Martin Gálik977c1cb2017-03-30 23:21:51 -07006105
Matus Fabian2ba92e32017-08-21 07:05:03 -07006106 self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n,
6107 32,
6108 socket.inet_aton(nat_ip),
6109 32)
6110 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6111 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6112 is_inside=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006113
6114 # in2out
6115 pkts = self.create_stream_in(self.pg0, self.pg1)
6116 self.pg0.add_stream(pkts)
6117 self.pg_enable_capture(self.pg_interfaces)
6118 self.pg_start()
6119 capture = self.pg1.get_capture(len(pkts))
6120 self.verify_capture_out(capture, nat_ip)
6121
6122 # out2in
6123 pkts = self.create_stream_out(self.pg1, nat_ip)
6124 self.pg1.add_stream(pkts)
6125 self.pg_enable_capture(self.pg_interfaces)
6126 self.pg_start()
6127 capture = self.pg0.get_capture(len(pkts))
6128 self.verify_capture_in(capture, self.pg0)
6129
Martin Gálik9806eae2017-04-25 01:25:08 -07006130 # session dump test
Matus Fabian2ba92e32017-08-21 07:05:03 -07006131 sessions = self.vapi.nat_det_session_dump(self.pg0.remote_ip4n)
Martin Gálik9806eae2017-04-25 01:25:08 -07006132 self.assertEqual(len(sessions), 3)
6133
6134 # TCP session
6135 s = sessions[0]
6136 self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n)
6137 self.assertEqual(s.in_port, self.tcp_port_in)
6138 self.assertEqual(s.out_port, self.tcp_port_out)
6139 self.assertEqual(s.ext_port, self.tcp_external_port)
6140
6141 # UDP session
6142 s = sessions[1]
6143 self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n)
6144 self.assertEqual(s.in_port, self.udp_port_in)
6145 self.assertEqual(s.out_port, self.udp_port_out)
6146 self.assertEqual(s.ext_port, self.udp_external_port)
6147
6148 # ICMP session
6149 s = sessions[2]
6150 self.assertEqual(s.ext_addr[:4], self.pg1.remote_ip4n)
6151 self.assertEqual(s.in_port, self.icmp_id_in)
6152 self.assertEqual(s.out_port, self.icmp_external_id)
6153
Martin Gálik977c1cb2017-03-30 23:21:51 -07006154 def test_multiple_users(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006155 """ Deterministic NAT multiple users """
Martin Gálik977c1cb2017-03-30 23:21:51 -07006156
6157 nat_ip = "10.0.0.10"
6158 port_in = 80
Martin Gálik9806eae2017-04-25 01:25:08 -07006159 external_port = 6303
Martin Gálik977c1cb2017-03-30 23:21:51 -07006160
6161 host0 = self.pg0.remote_hosts[0]
6162 host1 = self.pg0.remote_hosts[1]
6163
Matus Fabian2ba92e32017-08-21 07:05:03 -07006164 self.vapi.nat_det_add_del_map(host0.ip4n,
6165 24,
6166 socket.inet_aton(nat_ip),
6167 32)
6168 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6169 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6170 is_inside=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006171
6172 # host0 to out
6173 p = (Ether(src=host0.mac, dst=self.pg0.local_mac) /
6174 IP(src=host0.ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006175 TCP(sport=port_in, dport=external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006176 self.pg0.add_stream(p)
6177 self.pg_enable_capture(self.pg_interfaces)
6178 self.pg_start()
6179 capture = self.pg1.get_capture(1)
6180 p = capture[0]
6181 try:
6182 ip = p[IP]
6183 tcp = p[TCP]
6184 self.assertEqual(ip.src, nat_ip)
6185 self.assertEqual(ip.dst, self.pg1.remote_ip4)
Martin Gálik9806eae2017-04-25 01:25:08 -07006186 self.assertEqual(tcp.dport, external_port)
6187 port_out0 = tcp.sport
Martin Gálik977c1cb2017-03-30 23:21:51 -07006188 except:
6189 self.logger.error(ppp("Unexpected or invalid packet:", p))
6190 raise
6191
6192 # host1 to out
6193 p = (Ether(src=host1.mac, dst=self.pg0.local_mac) /
6194 IP(src=host1.ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006195 TCP(sport=port_in, dport=external_port))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006196 self.pg0.add_stream(p)
6197 self.pg_enable_capture(self.pg_interfaces)
6198 self.pg_start()
6199 capture = self.pg1.get_capture(1)
6200 p = capture[0]
6201 try:
6202 ip = p[IP]
6203 tcp = p[TCP]
6204 self.assertEqual(ip.src, nat_ip)
6205 self.assertEqual(ip.dst, self.pg1.remote_ip4)
Martin Gálik9806eae2017-04-25 01:25:08 -07006206 self.assertEqual(tcp.dport, external_port)
6207 port_out1 = tcp.sport
Martin Gálik977c1cb2017-03-30 23:21:51 -07006208 except:
6209 self.logger.error(ppp("Unexpected or invalid packet:", p))
6210 raise
6211
Matus Fabian2ba92e32017-08-21 07:05:03 -07006212 dms = self.vapi.nat_det_map_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006213 self.assertEqual(1, len(dms))
6214 self.assertEqual(2, dms[0].ses_num)
6215
6216 # out to host0
6217 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
6218 IP(src=self.pg1.remote_ip4, dst=nat_ip) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006219 TCP(sport=external_port, dport=port_out0))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006220 self.pg1.add_stream(p)
6221 self.pg_enable_capture(self.pg_interfaces)
6222 self.pg_start()
6223 capture = self.pg0.get_capture(1)
6224 p = capture[0]
6225 try:
6226 ip = p[IP]
6227 tcp = p[TCP]
6228 self.assertEqual(ip.src, self.pg1.remote_ip4)
6229 self.assertEqual(ip.dst, host0.ip4)
6230 self.assertEqual(tcp.dport, port_in)
Martin Gálik9806eae2017-04-25 01:25:08 -07006231 self.assertEqual(tcp.sport, external_port)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006232 except:
6233 self.logger.error(ppp("Unexpected or invalid packet:", p))
6234 raise
6235
6236 # out to host1
6237 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
6238 IP(src=self.pg1.remote_ip4, dst=nat_ip) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006239 TCP(sport=external_port, dport=port_out1))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006240 self.pg1.add_stream(p)
6241 self.pg_enable_capture(self.pg_interfaces)
6242 self.pg_start()
6243 capture = self.pg0.get_capture(1)
6244 p = capture[0]
6245 try:
6246 ip = p[IP]
6247 tcp = p[TCP]
6248 self.assertEqual(ip.src, self.pg1.remote_ip4)
6249 self.assertEqual(ip.dst, host1.ip4)
6250 self.assertEqual(tcp.dport, port_in)
Martin Gálik9806eae2017-04-25 01:25:08 -07006251 self.assertEqual(tcp.sport, external_port)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006252 except:
6253 self.logger.error(ppp("Unexpected or invalid packet", p))
6254 raise
6255
Martin Gálik6bc8c642017-04-19 01:12:27 -07006256 # session close api test
Matus Fabian2ba92e32017-08-21 07:05:03 -07006257 self.vapi.nat_det_close_session_out(socket.inet_aton(nat_ip),
6258 port_out1,
Martin Gálik6bc8c642017-04-19 01:12:27 -07006259 self.pg1.remote_ip4n,
Martin Gálik9806eae2017-04-25 01:25:08 -07006260 external_port)
Matus Fabian2ba92e32017-08-21 07:05:03 -07006261 dms = self.vapi.nat_det_map_dump()
6262 self.assertEqual(dms[0].ses_num, 1)
6263
6264 self.vapi.nat_det_close_session_in(host0.ip4n,
6265 port_in,
6266 self.pg1.remote_ip4n,
6267 external_port)
6268 dms = self.vapi.nat_det_map_dump()
Martin Gálik6bc8c642017-04-19 01:12:27 -07006269 self.assertEqual(dms[0].ses_num, 0)
6270
Martin Gálik977c1cb2017-03-30 23:21:51 -07006271 def test_tcp_session_close_detection_in(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006272 """ Deterministic NAT TCP session close from inside network """
6273 self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n,
6274 32,
6275 socket.inet_aton(self.nat_addr),
6276 32)
6277 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6278 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6279 is_inside=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006280
6281 self.initiate_tcp_session(self.pg0, self.pg1)
6282
6283 # close the session from inside
6284 try:
6285 # FIN packet in -> out
6286 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6287 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006288 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006289 flags="F"))
6290 self.pg0.add_stream(p)
6291 self.pg_enable_capture(self.pg_interfaces)
6292 self.pg_start()
6293 self.pg1.get_capture(1)
6294
6295 pkts = []
6296
6297 # ACK packet out -> in
6298 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07006299 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006300 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006301 flags="A"))
6302 pkts.append(p)
6303
6304 # FIN packet out -> in
6305 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07006306 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006307 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006308 flags="F"))
6309 pkts.append(p)
6310
6311 self.pg1.add_stream(pkts)
6312 self.pg_enable_capture(self.pg_interfaces)
6313 self.pg_start()
6314 self.pg0.get_capture(2)
6315
6316 # ACK packet in -> out
6317 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6318 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006319 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006320 flags="A"))
6321 self.pg0.add_stream(p)
6322 self.pg_enable_capture(self.pg_interfaces)
6323 self.pg_start()
6324 self.pg1.get_capture(1)
6325
Matus Fabian2ba92e32017-08-21 07:05:03 -07006326 # Check if deterministic NAT44 closed the session
6327 dms = self.vapi.nat_det_map_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006328 self.assertEqual(0, dms[0].ses_num)
6329 except:
6330 self.logger.error("TCP session termination failed")
6331 raise
6332
6333 def test_tcp_session_close_detection_out(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006334 """ Deterministic NAT TCP session close from outside network """
6335 self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n,
6336 32,
6337 socket.inet_aton(self.nat_addr),
6338 32)
6339 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6340 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6341 is_inside=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006342
6343 self.initiate_tcp_session(self.pg0, self.pg1)
6344
6345 # close the session from outside
6346 try:
6347 # FIN packet out -> in
6348 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07006349 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006350 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006351 flags="F"))
6352 self.pg1.add_stream(p)
6353 self.pg_enable_capture(self.pg_interfaces)
6354 self.pg_start()
6355 self.pg0.get_capture(1)
6356
6357 pkts = []
6358
6359 # ACK packet in -> out
6360 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6361 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006362 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006363 flags="A"))
6364 pkts.append(p)
6365
6366 # ACK packet in -> out
6367 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6368 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006369 TCP(sport=self.tcp_port_in, dport=self.tcp_external_port,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006370 flags="F"))
6371 pkts.append(p)
6372
6373 self.pg0.add_stream(pkts)
6374 self.pg_enable_capture(self.pg_interfaces)
6375 self.pg_start()
6376 self.pg1.get_capture(2)
6377
6378 # ACK packet out -> in
6379 p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
Matus Fabian2ba92e32017-08-21 07:05:03 -07006380 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
Martin Gálik9806eae2017-04-25 01:25:08 -07006381 TCP(sport=self.tcp_external_port, dport=self.tcp_port_out,
Martin Gálik977c1cb2017-03-30 23:21:51 -07006382 flags="A"))
6383 self.pg1.add_stream(p)
6384 self.pg_enable_capture(self.pg_interfaces)
6385 self.pg_start()
6386 self.pg0.get_capture(1)
6387
Matus Fabian2ba92e32017-08-21 07:05:03 -07006388 # Check if deterministic NAT44 closed the session
6389 dms = self.vapi.nat_det_map_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006390 self.assertEqual(0, dms[0].ses_num)
6391 except:
6392 self.logger.error("TCP session termination failed")
6393 raise
6394
6395 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
6396 def test_session_timeout(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006397 """ Deterministic NAT session timeouts """
6398 self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n,
6399 32,
6400 socket.inet_aton(self.nat_addr),
6401 32)
6402 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6403 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6404 is_inside=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006405
6406 self.initiate_tcp_session(self.pg0, self.pg1)
Matus Fabian878c6462018-08-23 00:33:35 -07006407 self.vapi.nat_set_timeouts(5, 5, 5, 5)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006408 pkts = self.create_stream_in(self.pg0, self.pg1)
6409 self.pg0.add_stream(pkts)
6410 self.pg_enable_capture(self.pg_interfaces)
6411 self.pg_start()
6412 capture = self.pg1.get_capture(len(pkts))
6413 sleep(15)
6414
Matus Fabian2ba92e32017-08-21 07:05:03 -07006415 dms = self.vapi.nat_det_map_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006416 self.assertEqual(0, dms[0].ses_num)
6417
Matus Fabian7c0aecc2017-07-03 01:21:38 -07006418 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
Martin Gálik977c1cb2017-03-30 23:21:51 -07006419 def test_session_limit_per_user(self):
Matus Fabian2ba92e32017-08-21 07:05:03 -07006420 """ Deterministic NAT maximum sessions per user limit """
6421 self.vapi.nat_det_add_del_map(self.pg0.remote_ip4n,
6422 32,
6423 socket.inet_aton(self.nat_addr),
6424 32)
6425 self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
6426 self.vapi.nat44_interface_add_del_feature(self.pg1.sw_if_index,
6427 is_inside=0)
Matus Fabian2f2db1c2017-04-18 05:29:59 -07006428 self.vapi.set_ipfix_exporter(collector_address=self.pg2.remote_ip4n,
6429 src_address=self.pg2.local_ip4n,
6430 path_mtu=512,
6431 template_interval=10)
Matus Fabian2ba92e32017-08-21 07:05:03 -07006432 self.vapi.nat_ipfix()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006433
6434 pkts = []
6435 for port in range(1025, 2025):
6436 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6437 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
6438 UDP(sport=port, dport=port))
6439 pkts.append(p)
6440
6441 self.pg0.add_stream(pkts)
6442 self.pg_enable_capture(self.pg_interfaces)
6443 self.pg_start()
6444 capture = self.pg1.get_capture(len(pkts))
6445
6446 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
6447 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
Martin Gálikf7e655d2017-04-27 02:13:26 -07006448 UDP(sport=3001, dport=3002))
Martin Gálik977c1cb2017-03-30 23:21:51 -07006449 self.pg0.add_stream(p)
6450 self.pg_enable_capture(self.pg_interfaces)
6451 self.pg_start()
6452 capture = self.pg1.assert_nothing_captured()
6453
Martin Gálikf7e655d2017-04-27 02:13:26 -07006454 # verify ICMP error packet
6455 capture = self.pg0.get_capture(1)
6456 p = capture[0]
6457 self.assertTrue(p.haslayer(ICMP))
6458 icmp = p[ICMP]
6459 self.assertEqual(icmp.type, 3)
6460 self.assertEqual(icmp.code, 1)
6461 self.assertTrue(icmp.haslayer(IPerror))
6462 inner_ip = icmp[IPerror]
6463 self.assertEqual(inner_ip[UDPerror].sport, 3001)
6464 self.assertEqual(inner_ip[UDPerror].dport, 3002)
6465
Matus Fabian2ba92e32017-08-21 07:05:03 -07006466 dms = self.vapi.nat_det_map_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006467
6468 self.assertEqual(1000, dms[0].ses_num)
6469
Matus Fabian2f2db1c2017-04-18 05:29:59 -07006470 # verify IPFIX logging
6471 self.vapi.cli("ipfix flush") # FIXME this should be an API call
Matus Fabian7c0aecc2017-07-03 01:21:38 -07006472 sleep(1)
Matus Fabian2f2db1c2017-04-18 05:29:59 -07006473 capture = self.pg2.get_capture(2)
6474 ipfix = IPFIXDecoder()
6475 # first load template
6476 for p in capture:
6477 self.assertTrue(p.haslayer(IPFIX))
6478 if p.haslayer(Template):
6479 ipfix.add_template(p.getlayer(Template))
6480 # verify events in data set
6481 for p in capture:
6482 if p.haslayer(Data):
6483 data = ipfix.decode_data_set(p.getlayer(Set))
Matus Fabian878c6462018-08-23 00:33:35 -07006484 self.verify_ipfix_max_entries_per_user(data,
6485 1000,
6486 self.pg0.remote_ip4n)
Matus Fabian2f2db1c2017-04-18 05:29:59 -07006487
Matus Fabian2ba92e32017-08-21 07:05:03 -07006488 def clear_nat_det(self):
Martin17a75cb2017-03-08 05:53:20 -08006489 """
Matus Fabian2ba92e32017-08-21 07:05:03 -07006490 Clear deterministic NAT configuration.
Martin17a75cb2017-03-08 05:53:20 -08006491 """
Matus Fabian2ba92e32017-08-21 07:05:03 -07006492 self.vapi.nat_ipfix(enable=0)
Matus Fabian878c6462018-08-23 00:33:35 -07006493 self.vapi.nat_set_timeouts()
Matus Fabian2ba92e32017-08-21 07:05:03 -07006494 deterministic_mappings = self.vapi.nat_det_map_dump()
Martin17a75cb2017-03-08 05:53:20 -08006495 for dsm in deterministic_mappings:
Matus Fabian2ba92e32017-08-21 07:05:03 -07006496 self.vapi.nat_det_add_del_map(dsm.in_addr,
6497 dsm.in_plen,
6498 dsm.out_addr,
6499 dsm.out_plen,
6500 is_add=0)
Martin17a75cb2017-03-08 05:53:20 -08006501
Matus Fabian2ba92e32017-08-21 07:05:03 -07006502 interfaces = self.vapi.nat44_interface_dump()
Martin Gálik977c1cb2017-03-30 23:21:51 -07006503 for intf in interfaces:
Matus Fabian2ba92e32017-08-21 07:05:03 -07006504 self.vapi.nat44_interface_add_del_feature(intf.sw_if_index,
6505 intf.is_inside,
6506 is_add=0)
Martin Gálik977c1cb2017-03-30 23:21:51 -07006507
Matus Fabian066f0342017-02-10 03:48:01 -08006508 def tearDown(self):
6509 super(TestDeterministicNAT, self).tearDown()
6510 if not self.vpp_dead:
Matus Fabian82119542018-01-25 01:13:22 -08006511 self.logger.info(self.vapi.cli("show nat44 interfaces"))
Matus Fabian878c6462018-08-23 00:33:35 -07006512 self.logger.info(self.vapi.cli("show nat timeouts"))
Matus Fabian82119542018-01-25 01:13:22 -08006513 self.logger.info(
6514 self.vapi.cli("show nat44 deterministic mappings"))
6515 self.logger.info(
Matus Fabian82119542018-01-25 01:13:22 -08006516 self.vapi.cli("show nat44 deterministic sessions"))
Matus Fabian2ba92e32017-08-21 07:05:03 -07006517 self.clear_nat_det()
Matus Fabian066f0342017-02-10 03:48:01 -08006518
Matus Fabian06596c52017-06-06 04:53:28 -07006519
6520class TestNAT64(MethodHolder):
6521 """ NAT64 Test Cases """
6522
6523 @classmethod
Matus Fabiana431ad12018-01-04 04:03:14 -08006524 def setUpConstants(cls):
6525 super(TestNAT64, cls).setUpConstants()
6526 cls.vpp_cmdline.extend(["nat", "{", "nat64 bib hash buckets 128",
6527 "nat64 st hash buckets 256", "}"])
6528
6529 @classmethod
Matus Fabian06596c52017-06-06 04:53:28 -07006530 def setUpClass(cls):
6531 super(TestNAT64, cls).setUpClass()
6532
6533 try:
6534 cls.tcp_port_in = 6303
6535 cls.tcp_port_out = 6303
6536 cls.udp_port_in = 6304
6537 cls.udp_port_out = 6304
6538 cls.icmp_id_in = 6305
6539 cls.icmp_id_out = 6305
6540 cls.nat_addr = '10.0.0.3'
6541 cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
Matus Fabian029f3d22017-06-15 02:28:50 -07006542 cls.vrf1_id = 10
6543 cls.vrf1_nat_addr = '10.0.10.3'
6544 cls.vrf1_nat_addr_n = socket.inet_pton(socket.AF_INET,
6545 cls.vrf1_nat_addr)
Matus Fabiana431ad12018-01-04 04:03:14 -08006546 cls.ipfix_src_port = 4739
6547 cls.ipfix_domain_id = 1
Matus Fabian06596c52017-06-06 04:53:28 -07006548
Juraj Slobodac746a152018-07-09 02:36:37 +02006549 cls.create_pg_interfaces(range(6))
Matus Fabian06596c52017-06-06 04:53:28 -07006550 cls.ip6_interfaces = list(cls.pg_interfaces[0:1])
Matus Fabian029f3d22017-06-15 02:28:50 -07006551 cls.ip6_interfaces.append(cls.pg_interfaces[2])
Matus Fabian06596c52017-06-06 04:53:28 -07006552 cls.ip4_interfaces = list(cls.pg_interfaces[1:2])
6553
Neale Ranns15002542017-09-10 04:39:11 -07006554 cls.vapi.ip_table_add_del(cls.vrf1_id, is_add=1, is_ipv6=1)
6555
Matus Fabian029f3d22017-06-15 02:28:50 -07006556 cls.pg_interfaces[2].set_table_ip6(cls.vrf1_id)
6557
6558 cls.pg0.generate_remote_hosts(2)
6559
Matus Fabian06596c52017-06-06 04:53:28 -07006560 for i in cls.ip6_interfaces:
6561 i.admin_up()
6562 i.config_ip6()
Matus Fabian029f3d22017-06-15 02:28:50 -07006563 i.configure_ipv6_neighbors()
Matus Fabian06596c52017-06-06 04:53:28 -07006564
6565 for i in cls.ip4_interfaces:
6566 i.admin_up()
6567 i.config_ip4()
6568 i.resolve_arp()
6569
Matus Fabian36ea2d62017-10-24 04:13:49 -07006570 cls.pg3.admin_up()
6571 cls.pg3.config_ip4()
6572 cls.pg3.resolve_arp()
6573 cls.pg3.config_ip6()
6574 cls.pg3.configure_ipv6_neighbors()
6575
Juraj Slobodac746a152018-07-09 02:36:37 +02006576 cls.pg5.admin_up()
6577 cls.pg5.config_ip6()
6578
Matus Fabian06596c52017-06-06 04:53:28 -07006579 except Exception:
6580 super(TestNAT64, cls).tearDownClass()
6581 raise
6582
Juraj Slobodac746a152018-07-09 02:36:37 +02006583 def test_nat64_inside_interface_handles_neighbor_advertisement(self):
6584 """ NAT64 inside interface handles Neighbor Advertisement """
6585
6586 self.vapi.nat64_add_del_interface(self.pg5.sw_if_index)
6587
6588 # Try to send ping
6589 ping = (Ether(dst=self.pg5.local_mac, src=self.pg5.remote_mac) /
6590 IPv6(src=self.pg5.remote_ip6, dst=self.pg5.local_ip6) /
6591 ICMPv6EchoRequest())
6592 pkts = [ping]
6593 self.pg5.add_stream(pkts)
6594 self.pg_enable_capture(self.pg_interfaces)
6595 self.pg_start()
6596
6597 # Wait for Neighbor Solicitation
6598 capture = self.pg5.get_capture(len(pkts))
Juraj Slobodac746a152018-07-09 02:36:37 +02006599 packet = capture[0]
6600 try:
6601 self.assertEqual(packet[IPv6].src, self.pg5.local_ip6)
6602 self.assertTrue(packet.haslayer(ICMPv6ND_NS))
6603 tgt = packet[ICMPv6ND_NS].tgt
6604 except:
6605 self.logger.error(ppp("Unexpected or invalid packet:", packet))
6606 raise
6607
6608 # Send Neighbor Advertisement
6609 p = (Ether(dst=self.pg5.local_mac, src=self.pg5.remote_mac) /
6610 IPv6(src=self.pg5.remote_ip6, dst=self.pg5.local_ip6) /
6611 ICMPv6ND_NA(tgt=tgt) /
6612 ICMPv6NDOptDstLLAddr(lladdr=self.pg5.remote_mac))
6613 pkts = [p]
6614 self.pg5.add_stream(pkts)
6615 self.pg_enable_capture(self.pg_interfaces)
6616 self.pg_start()
6617
6618 # Try to send ping again
6619 pkts = [ping]
6620 self.pg5.add_stream(pkts)
6621 self.pg_enable_capture(self.pg_interfaces)
6622 self.pg_start()
6623
6624 # Wait for ping reply
6625 capture = self.pg5.get_capture(len(pkts))
Juraj Slobodac746a152018-07-09 02:36:37 +02006626 packet = capture[0]
6627 try:
6628 self.assertEqual(packet[IPv6].src, self.pg5.local_ip6)
6629 self.assertEqual(packet[IPv6].dst, self.pg5.remote_ip6)
6630 self.assertTrue(packet.haslayer(ICMPv6EchoReply))
6631 except:
6632 self.logger.error(ppp("Unexpected or invalid packet:", packet))
6633 raise
6634
Matus Fabian06596c52017-06-06 04:53:28 -07006635 def test_pool(self):
6636 """ Add/delete address to NAT64 pool """
6637 nat_addr = socket.inet_pton(socket.AF_INET, '1.2.3.4')
6638
6639 self.vapi.nat64_add_del_pool_addr_range(nat_addr, nat_addr)
6640
6641 addresses = self.vapi.nat64_pool_addr_dump()
6642 self.assertEqual(len(addresses), 1)
6643 self.assertEqual(addresses[0].address, nat_addr)
6644
6645 self.vapi.nat64_add_del_pool_addr_range(nat_addr, nat_addr, is_add=0)
6646
6647 addresses = self.vapi.nat64_pool_addr_dump()
6648 self.assertEqual(len(addresses), 0)
6649
6650 def test_interface(self):
6651 """ Enable/disable NAT64 feature on the interface """
6652 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
6653 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
6654
6655 interfaces = self.vapi.nat64_interface_dump()
6656 self.assertEqual(len(interfaces), 2)
6657 pg0_found = False
6658 pg1_found = False
6659 for intf in interfaces:
6660 if intf.sw_if_index == self.pg0.sw_if_index:
6661 self.assertEqual(intf.is_inside, 1)
6662 pg0_found = True
6663 elif intf.sw_if_index == self.pg1.sw_if_index:
6664 self.assertEqual(intf.is_inside, 0)
6665 pg1_found = True
6666 self.assertTrue(pg0_found)
6667 self.assertTrue(pg1_found)
6668
6669 features = self.vapi.cli("show interface features pg0")
6670 self.assertNotEqual(features.find('nat64-in2out'), -1)
6671 features = self.vapi.cli("show interface features pg1")
6672 self.assertNotEqual(features.find('nat64-out2in'), -1)
6673
6674 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index, is_add=0)
6675 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_add=0)
6676
6677 interfaces = self.vapi.nat64_interface_dump()
6678 self.assertEqual(len(interfaces), 0)
6679
6680 def test_static_bib(self):
6681 """ Add/delete static BIB entry """
6682 in_addr = socket.inet_pton(socket.AF_INET6,
6683 '2001:db8:85a3::8a2e:370:7334')
6684 out_addr = socket.inet_pton(socket.AF_INET, '10.1.1.3')
6685 in_port = 1234
6686 out_port = 5678
6687 proto = IP_PROTOS.tcp
6688
6689 self.vapi.nat64_add_del_static_bib(in_addr,
6690 out_addr,
6691 in_port,
6692 out_port,
6693 proto)
6694 bib = self.vapi.nat64_bib_dump(IP_PROTOS.tcp)
6695 static_bib_num = 0
6696 for bibe in bib:
6697 if bibe.is_static:
6698 static_bib_num += 1
6699 self.assertEqual(bibe.i_addr, in_addr)
6700 self.assertEqual(bibe.o_addr, out_addr)
6701 self.assertEqual(bibe.i_port, in_port)
6702 self.assertEqual(bibe.o_port, out_port)
6703 self.assertEqual(static_bib_num, 1)
6704
6705 self.vapi.nat64_add_del_static_bib(in_addr,
6706 out_addr,
6707 in_port,
6708 out_port,
6709 proto,
6710 is_add=0)
6711 bib = self.vapi.nat64_bib_dump(IP_PROTOS.tcp)
6712 static_bib_num = 0
6713 for bibe in bib:
6714 if bibe.is_static:
6715 static_bib_num += 1
6716 self.assertEqual(static_bib_num, 0)
6717
6718 def test_set_timeouts(self):
6719 """ Set NAT64 timeouts """
6720 # verify default values
Matus Fabian878c6462018-08-23 00:33:35 -07006721 timeouts = self.vapi.nat_get_timeouts()
Matus Fabian06596c52017-06-06 04:53:28 -07006722 self.assertEqual(timeouts.udp, 300)
6723 self.assertEqual(timeouts.icmp, 60)
Matus Fabian878c6462018-08-23 00:33:35 -07006724 self.assertEqual(timeouts.tcp_transitory, 240)
6725 self.assertEqual(timeouts.tcp_established, 7440)
Matus Fabian06596c52017-06-06 04:53:28 -07006726
6727 # set and verify custom values
Matus Fabian878c6462018-08-23 00:33:35 -07006728 self.vapi.nat_set_timeouts(udp=200, icmp=30, tcp_transitory=250,
6729 tcp_established=7450)
6730 timeouts = self.vapi.nat_get_timeouts()
Matus Fabian06596c52017-06-06 04:53:28 -07006731 self.assertEqual(timeouts.udp, 200)
6732 self.assertEqual(timeouts.icmp, 30)
Matus Fabian878c6462018-08-23 00:33:35 -07006733 self.assertEqual(timeouts.tcp_transitory, 250)
6734 self.assertEqual(timeouts.tcp_established, 7450)
Matus Fabian06596c52017-06-06 04:53:28 -07006735
6736 def test_dynamic(self):
6737 """ NAT64 dynamic translation test """
6738 self.tcp_port_in = 6303
6739 self.udp_port_in = 6304
6740 self.icmp_id_in = 6305
6741
6742 ses_num_start = self.nat64_get_ses_num()
6743
6744 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
6745 self.nat_addr_n)
6746 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
6747 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
6748
6749 # in2out
6750 pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
6751 self.pg0.add_stream(pkts)
6752 self.pg_enable_capture(self.pg_interfaces)
6753 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07006754 capture = self.pg1.get_capture(len(pkts))
6755 self.verify_capture_out(capture, nat_ip=self.nat_addr,
Matus Fabian06596c52017-06-06 04:53:28 -07006756 dst_ip=self.pg1.remote_ip4)
6757
6758 # out2in
6759 pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
6760 self.pg1.add_stream(pkts)
6761 self.pg_enable_capture(self.pg_interfaces)
6762 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07006763 capture = self.pg0.get_capture(len(pkts))
Matus Fabian06596c52017-06-06 04:53:28 -07006764 ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4]))
6765 self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6)
6766
6767 # in2out
6768 pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
6769 self.pg0.add_stream(pkts)
6770 self.pg_enable_capture(self.pg_interfaces)
6771 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07006772 capture = self.pg1.get_capture(len(pkts))
6773 self.verify_capture_out(capture, nat_ip=self.nat_addr,
Matus Fabian06596c52017-06-06 04:53:28 -07006774 dst_ip=self.pg1.remote_ip4)
6775
6776 # out2in
6777 pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
6778 self.pg1.add_stream(pkts)
6779 self.pg_enable_capture(self.pg_interfaces)
6780 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07006781 capture = self.pg0.get_capture(len(pkts))
Matus Fabian06596c52017-06-06 04:53:28 -07006782 self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6)
6783
6784 ses_num_end = self.nat64_get_ses_num()
6785
6786 self.assertEqual(ses_num_end - ses_num_start, 3)
6787
Matus Fabian029f3d22017-06-15 02:28:50 -07006788 # tenant with specific VRF
6789 self.vapi.nat64_add_del_pool_addr_range(self.vrf1_nat_addr_n,
6790 self.vrf1_nat_addr_n,
6791 vrf_id=self.vrf1_id)
6792 self.vapi.nat64_add_del_interface(self.pg2.sw_if_index)
6793
6794 pkts = self.create_stream_in_ip6(self.pg2, self.pg1)
6795 self.pg2.add_stream(pkts)
6796 self.pg_enable_capture(self.pg_interfaces)
6797 self.pg_start()
6798 capture = self.pg1.get_capture(len(pkts))
6799 self.verify_capture_out(capture, nat_ip=self.vrf1_nat_addr,
6800 dst_ip=self.pg1.remote_ip4)
6801
6802 pkts = self.create_stream_out(self.pg1, dst_ip=self.vrf1_nat_addr)
6803 self.pg1.add_stream(pkts)
6804 self.pg_enable_capture(self.pg_interfaces)
6805 self.pg_start()
6806 capture = self.pg2.get_capture(len(pkts))
6807 self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg2.remote_ip6)
6808
Matus Fabian06596c52017-06-06 04:53:28 -07006809 def test_static(self):
6810 """ NAT64 static translation test """
6811 self.tcp_port_in = 60303
6812 self.udp_port_in = 60304
6813 self.icmp_id_in = 60305
6814 self.tcp_port_out = 60303
6815 self.udp_port_out = 60304
6816 self.icmp_id_out = 60305
6817
6818 ses_num_start = self.nat64_get_ses_num()
6819
6820 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
6821 self.nat_addr_n)
6822 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
6823 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
6824
6825 self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n,
6826 self.nat_addr_n,
6827 self.tcp_port_in,
6828 self.tcp_port_out,
6829 IP_PROTOS.tcp)
6830 self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n,
6831 self.nat_addr_n,
6832 self.udp_port_in,
6833 self.udp_port_out,
6834 IP_PROTOS.udp)
6835 self.vapi.nat64_add_del_static_bib(self.pg0.remote_ip6n,
6836 self.nat_addr_n,
6837 self.icmp_id_in,
6838 self.icmp_id_out,
6839 IP_PROTOS.icmp)
6840
6841 # in2out
6842 pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
6843 self.pg0.add_stream(pkts)
6844 self.pg_enable_capture(self.pg_interfaces)
6845 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07006846 capture = self.pg1.get_capture(len(pkts))
6847 self.verify_capture_out(capture, nat_ip=self.nat_addr,
Matus Fabian06596c52017-06-06 04:53:28 -07006848 dst_ip=self.pg1.remote_ip4, same_port=True)
6849
6850 # out2in
6851 pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
6852 self.pg1.add_stream(pkts)
6853 self.pg_enable_capture(self.pg_interfaces)
6854 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07006855 capture = self.pg0.get_capture(len(pkts))
Matus Fabian06596c52017-06-06 04:53:28 -07006856 ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4]))
6857 self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6)
6858
6859 ses_num_end = self.nat64_get_ses_num()
6860
6861 self.assertEqual(ses_num_end - ses_num_start, 3)
6862
6863 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
6864 def test_session_timeout(self):
6865 """ NAT64 session timeout """
6866 self.icmp_id_in = 1234
6867 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
6868 self.nat_addr_n)
6869 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
6870 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
Matus Fabian878c6462018-08-23 00:33:35 -07006871 self.vapi.nat_set_timeouts(icmp=5, tcp_transitory=5, tcp_established=5)
Matus Fabian06596c52017-06-06 04:53:28 -07006872
6873 pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
6874 self.pg0.add_stream(pkts)
6875 self.pg_enable_capture(self.pg_interfaces)
6876 self.pg_start()
Matus Fabian029f3d22017-06-15 02:28:50 -07006877 capture = self.pg1.get_capture(len(pkts))
Matus Fabian06596c52017-06-06 04:53:28 -07006878
6879 ses_num_before_timeout = self.nat64_get_ses_num()
6880
6881 sleep(15)
6882
Matus Fabian8fed4242018-08-14 05:14:55 -07006883 # ICMP and TCP session after timeout
Matus Fabian06596c52017-06-06 04:53:28 -07006884 ses_num_after_timeout = self.nat64_get_ses_num()
Matus Fabian8fed4242018-08-14 05:14:55 -07006885 self.assertEqual(ses_num_before_timeout - ses_num_after_timeout, 2)
Matus Fabian06596c52017-06-06 04:53:28 -07006886
Matus Fabian732036d2017-06-08 05:24:28 -07006887 def test_icmp_error(self):
6888 """ NAT64 ICMP Error message translation """
6889 self.tcp_port_in = 6303
6890 self.udp_port_in = 6304
6891 self.icmp_id_in = 6305
6892
Matus Fabian732036d2017-06-08 05:24:28 -07006893 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
6894 self.nat_addr_n)
6895 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
6896 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
6897
6898 # send some packets to create sessions
6899 pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
6900 self.pg0.add_stream(pkts)
6901 self.pg_enable_capture(self.pg_interfaces)
6902 self.pg_start()
6903 capture_ip4 = self.pg1.get_capture(len(pkts))
Matus Fabian029f3d22017-06-15 02:28:50 -07006904 self.verify_capture_out(capture_ip4,
Matus Fabian732036d2017-06-08 05:24:28 -07006905 nat_ip=self.nat_addr,
6906 dst_ip=self.pg1.remote_ip4)
6907
6908 pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
6909 self.pg1.add_stream(pkts)
6910 self.pg_enable_capture(self.pg_interfaces)
6911 self.pg_start()
6912 capture_ip6 = self.pg0.get_capture(len(pkts))
6913 ip = IPv6(src=''.join(['64:ff9b::', self.pg1.remote_ip4]))
6914 self.verify_capture_in_ip6(capture_ip6, ip[IPv6].src,
6915 self.pg0.remote_ip6)
6916
6917 # in2out
6918 pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
6919 IPv6(src=self.pg0.remote_ip6, dst=ip[IPv6].src) /
6920 ICMPv6DestUnreach(code=1) /
6921 packet[IPv6] for packet in capture_ip6]
6922 self.pg0.add_stream(pkts)
6923 self.pg_enable_capture(self.pg_interfaces)
6924 self.pg_start()
6925 capture = self.pg1.get_capture(len(pkts))
6926 for packet in capture:
6927 try:
6928 self.assertEqual(packet[IP].src, self.nat_addr)
6929 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
6930 self.assertEqual(packet[ICMP].type, 3)
6931 self.assertEqual(packet[ICMP].code, 13)
6932 inner = packet[IPerror]
6933 self.assertEqual(inner.src, self.pg1.remote_ip4)
6934 self.assertEqual(inner.dst, self.nat_addr)
Klement Sekerad81ae412018-05-16 10:52:54 +02006935 self.assert_packet_checksums_valid(packet)
Matus Fabian732036d2017-06-08 05:24:28 -07006936 if inner.haslayer(TCPerror):
6937 self.assertEqual(inner[TCPerror].dport, self.tcp_port_out)
6938 elif inner.haslayer(UDPerror):
6939 self.assertEqual(inner[UDPerror].dport, self.udp_port_out)
6940 else:
6941 self.assertEqual(inner[ICMPerror].id, self.icmp_id_out)
6942 except:
6943 self.logger.error(ppp("Unexpected or invalid packet:", packet))
6944 raise
6945
6946 # out2in
6947 pkts = [Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
6948 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
6949 ICMP(type=3, code=13) /
6950 packet[IP] for packet in capture_ip4]
6951 self.pg1.add_stream(pkts)
6952 self.pg_enable_capture(self.pg_interfaces)
6953 self.pg_start()
6954 capture = self.pg0.get_capture(len(pkts))
6955 for packet in capture:
6956 try:
6957 self.assertEqual(packet[IPv6].src, ip.src)
6958 self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6)
6959 icmp = packet[ICMPv6DestUnreach]
6960 self.assertEqual(icmp.code, 1)
6961 inner = icmp[IPerror6]
6962 self.assertEqual(inner.src, self.pg0.remote_ip6)
6963 self.assertEqual(inner.dst, ip.src)
Klement Sekerad81ae412018-05-16 10:52:54 +02006964 self.assert_icmpv6_checksum_valid(packet)
Matus Fabian732036d2017-06-08 05:24:28 -07006965 if inner.haslayer(TCPerror):
6966 self.assertEqual(inner[TCPerror].sport, self.tcp_port_in)
6967 elif inner.haslayer(UDPerror):
6968 self.assertEqual(inner[UDPerror].sport, self.udp_port_in)
6969 else:
6970 self.assertEqual(inner[ICMPv6EchoRequest].id,
6971 self.icmp_id_in)
6972 except:
6973 self.logger.error(ppp("Unexpected or invalid packet:", packet))
6974 raise
6975
Matus Fabian029f3d22017-06-15 02:28:50 -07006976 def test_hairpinning(self):
6977 """ NAT64 hairpinning """
6978
6979 client = self.pg0.remote_hosts[0]
6980 server = self.pg0.remote_hosts[1]
6981 server_tcp_in_port = 22
6982 server_tcp_out_port = 4022
6983 server_udp_in_port = 23
6984 server_udp_out_port = 4023
6985 client_tcp_in_port = 1234
6986 client_udp_in_port = 1235
6987 client_tcp_out_port = 0
6988 client_udp_out_port = 0
6989 ip = IPv6(src=''.join(['64:ff9b::', self.nat_addr]))
6990 nat_addr_ip6 = ip.src
6991
6992 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
6993 self.nat_addr_n)
6994 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
6995 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
6996
6997 self.vapi.nat64_add_del_static_bib(server.ip6n,
6998 self.nat_addr_n,
6999 server_tcp_in_port,
7000 server_tcp_out_port,
7001 IP_PROTOS.tcp)
7002 self.vapi.nat64_add_del_static_bib(server.ip6n,
7003 self.nat_addr_n,
7004 server_udp_in_port,
7005 server_udp_out_port,
7006 IP_PROTOS.udp)
7007
7008 # client to server
7009 pkts = []
7010 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7011 IPv6(src=client.ip6, dst=nat_addr_ip6) /
7012 TCP(sport=client_tcp_in_port, dport=server_tcp_out_port))
7013 pkts.append(p)
7014 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7015 IPv6(src=client.ip6, dst=nat_addr_ip6) /
7016 UDP(sport=client_udp_in_port, dport=server_udp_out_port))
7017 pkts.append(p)
7018 self.pg0.add_stream(pkts)
7019 self.pg_enable_capture(self.pg_interfaces)
7020 self.pg_start()
7021 capture = self.pg0.get_capture(len(pkts))
7022 for packet in capture:
7023 try:
7024 self.assertEqual(packet[IPv6].src, nat_addr_ip6)
7025 self.assertEqual(packet[IPv6].dst, server.ip6)
Klement Sekerad81ae412018-05-16 10:52:54 +02007026 self.assert_packet_checksums_valid(packet)
Matus Fabian029f3d22017-06-15 02:28:50 -07007027 if packet.haslayer(TCP):
7028 self.assertNotEqual(packet[TCP].sport, client_tcp_in_port)
7029 self.assertEqual(packet[TCP].dport, server_tcp_in_port)
Matus Fabian029f3d22017-06-15 02:28:50 -07007030 client_tcp_out_port = packet[TCP].sport
7031 else:
7032 self.assertNotEqual(packet[UDP].sport, client_udp_in_port)
7033 self.assertEqual(packet[UDP].dport, server_udp_in_port)
Matus Fabian029f3d22017-06-15 02:28:50 -07007034 client_udp_out_port = packet[UDP].sport
7035 except:
7036 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7037 raise
7038
7039 # server to client
7040 pkts = []
7041 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7042 IPv6(src=server.ip6, dst=nat_addr_ip6) /
7043 TCP(sport=server_tcp_in_port, dport=client_tcp_out_port))
7044 pkts.append(p)
7045 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7046 IPv6(src=server.ip6, dst=nat_addr_ip6) /
7047 UDP(sport=server_udp_in_port, dport=client_udp_out_port))
7048 pkts.append(p)
7049 self.pg0.add_stream(pkts)
7050 self.pg_enable_capture(self.pg_interfaces)
7051 self.pg_start()
7052 capture = self.pg0.get_capture(len(pkts))
7053 for packet in capture:
7054 try:
7055 self.assertEqual(packet[IPv6].src, nat_addr_ip6)
7056 self.assertEqual(packet[IPv6].dst, client.ip6)
Klement Sekerad81ae412018-05-16 10:52:54 +02007057 self.assert_packet_checksums_valid(packet)
Matus Fabian029f3d22017-06-15 02:28:50 -07007058 if packet.haslayer(TCP):
7059 self.assertEqual(packet[TCP].sport, server_tcp_out_port)
7060 self.assertEqual(packet[TCP].dport, client_tcp_in_port)
Matus Fabian029f3d22017-06-15 02:28:50 -07007061 else:
7062 self.assertEqual(packet[UDP].sport, server_udp_out_port)
7063 self.assertEqual(packet[UDP].dport, client_udp_in_port)
Matus Fabian029f3d22017-06-15 02:28:50 -07007064 except:
7065 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7066 raise
7067
7068 # ICMP error
7069 pkts = []
7070 pkts = [Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7071 IPv6(src=client.ip6, dst=nat_addr_ip6) /
7072 ICMPv6DestUnreach(code=1) /
7073 packet[IPv6] for packet in capture]
7074 self.pg0.add_stream(pkts)
7075 self.pg_enable_capture(self.pg_interfaces)
7076 self.pg_start()
7077 capture = self.pg0.get_capture(len(pkts))
7078 for packet in capture:
7079 try:
7080 self.assertEqual(packet[IPv6].src, nat_addr_ip6)
7081 self.assertEqual(packet[IPv6].dst, server.ip6)
7082 icmp = packet[ICMPv6DestUnreach]
7083 self.assertEqual(icmp.code, 1)
7084 inner = icmp[IPerror6]
7085 self.assertEqual(inner.src, server.ip6)
7086 self.assertEqual(inner.dst, nat_addr_ip6)
Klement Sekerad81ae412018-05-16 10:52:54 +02007087 self.assert_packet_checksums_valid(packet)
Matus Fabian029f3d22017-06-15 02:28:50 -07007088 if inner.haslayer(TCPerror):
7089 self.assertEqual(inner[TCPerror].sport, server_tcp_in_port)
7090 self.assertEqual(inner[TCPerror].dport,
7091 client_tcp_out_port)
7092 else:
7093 self.assertEqual(inner[UDPerror].sport, server_udp_in_port)
7094 self.assertEqual(inner[UDPerror].dport,
7095 client_udp_out_port)
7096 except:
7097 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7098 raise
7099
Matus Fabian428dc912017-06-21 06:15:18 -07007100 def test_prefix(self):
7101 """ NAT64 Network-Specific Prefix """
7102
7103 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7104 self.nat_addr_n)
7105 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7106 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7107 self.vapi.nat64_add_del_pool_addr_range(self.vrf1_nat_addr_n,
7108 self.vrf1_nat_addr_n,
7109 vrf_id=self.vrf1_id)
7110 self.vapi.nat64_add_del_interface(self.pg2.sw_if_index)
7111
7112 # Add global prefix
7113 global_pref64 = "2001:db8::"
7114 global_pref64_n = socket.inet_pton(socket.AF_INET6, global_pref64)
7115 global_pref64_len = 32
7116 self.vapi.nat64_add_del_prefix(global_pref64_n, global_pref64_len)
7117
7118 prefix = self.vapi.nat64_prefix_dump()
7119 self.assertEqual(len(prefix), 1)
7120 self.assertEqual(prefix[0].prefix, global_pref64_n)
7121 self.assertEqual(prefix[0].prefix_len, global_pref64_len)
7122 self.assertEqual(prefix[0].vrf_id, 0)
7123
7124 # Add tenant specific prefix
7125 vrf1_pref64 = "2001:db8:122:300::"
7126 vrf1_pref64_n = socket.inet_pton(socket.AF_INET6, vrf1_pref64)
7127 vrf1_pref64_len = 56
7128 self.vapi.nat64_add_del_prefix(vrf1_pref64_n,
7129 vrf1_pref64_len,
7130 vrf_id=self.vrf1_id)
7131 prefix = self.vapi.nat64_prefix_dump()
7132 self.assertEqual(len(prefix), 2)
7133
7134 # Global prefix
7135 pkts = self.create_stream_in_ip6(self.pg0,
7136 self.pg1,
7137 pref=global_pref64,
7138 plen=global_pref64_len)
7139 self.pg0.add_stream(pkts)
7140 self.pg_enable_capture(self.pg_interfaces)
7141 self.pg_start()
7142 capture = self.pg1.get_capture(len(pkts))
7143 self.verify_capture_out(capture, nat_ip=self.nat_addr,
7144 dst_ip=self.pg1.remote_ip4)
7145
7146 pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
7147 self.pg1.add_stream(pkts)
7148 self.pg_enable_capture(self.pg_interfaces)
7149 self.pg_start()
7150 capture = self.pg0.get_capture(len(pkts))
7151 dst_ip = self.compose_ip6(self.pg1.remote_ip4,
7152 global_pref64,
7153 global_pref64_len)
7154 self.verify_capture_in_ip6(capture, dst_ip, self.pg0.remote_ip6)
7155
7156 # Tenant specific prefix
7157 pkts = self.create_stream_in_ip6(self.pg2,
7158 self.pg1,
7159 pref=vrf1_pref64,
7160 plen=vrf1_pref64_len)
7161 self.pg2.add_stream(pkts)
7162 self.pg_enable_capture(self.pg_interfaces)
7163 self.pg_start()
7164 capture = self.pg1.get_capture(len(pkts))
7165 self.verify_capture_out(capture, nat_ip=self.vrf1_nat_addr,
7166 dst_ip=self.pg1.remote_ip4)
7167
7168 pkts = self.create_stream_out(self.pg1, dst_ip=self.vrf1_nat_addr)
7169 self.pg1.add_stream(pkts)
7170 self.pg_enable_capture(self.pg_interfaces)
7171 self.pg_start()
7172 capture = self.pg2.get_capture(len(pkts))
7173 dst_ip = self.compose_ip6(self.pg1.remote_ip4,
7174 vrf1_pref64,
7175 vrf1_pref64_len)
7176 self.verify_capture_in_ip6(capture, dst_ip, self.pg2.remote_ip6)
7177
Matus Fabianf8cd5812017-07-11 03:55:02 -07007178 def test_unknown_proto(self):
Matus Fabian7968e6c2017-07-06 05:37:49 -07007179 """ NAT64 translate packet with unknown protocol """
7180
7181 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7182 self.nat_addr_n)
7183 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7184 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7185 remote_ip6 = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96)
7186
7187 # in2out
7188 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7189 IPv6(src=self.pg0.remote_ip6, dst=remote_ip6) /
7190 TCP(sport=self.tcp_port_in, dport=20))
7191 self.pg0.add_stream(p)
7192 self.pg_enable_capture(self.pg_interfaces)
7193 self.pg_start()
7194 p = self.pg1.get_capture(1)
7195
7196 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabianf8cd5812017-07-11 03:55:02 -07007197 IPv6(src=self.pg0.remote_ip6, dst=remote_ip6, nh=47) /
Matus Fabian7968e6c2017-07-06 05:37:49 -07007198 GRE() /
7199 IP(src=self.pg2.local_ip4, dst=self.pg2.remote_ip4) /
7200 TCP(sport=1234, dport=1234))
7201 self.pg0.add_stream(p)
7202 self.pg_enable_capture(self.pg_interfaces)
7203 self.pg_start()
7204 p = self.pg1.get_capture(1)
7205 packet = p[0]
7206 try:
7207 self.assertEqual(packet[IP].src, self.nat_addr)
7208 self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
7209 self.assertTrue(packet.haslayer(GRE))
Klement Sekerad81ae412018-05-16 10:52:54 +02007210 self.assert_packet_checksums_valid(packet)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007211 except:
7212 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7213 raise
7214
7215 # out2in
7216 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
7217 IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
7218 GRE() /
7219 IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) /
7220 TCP(sport=1234, dport=1234))
7221 self.pg1.add_stream(p)
7222 self.pg_enable_capture(self.pg_interfaces)
7223 self.pg_start()
7224 p = self.pg0.get_capture(1)
7225 packet = p[0]
7226 try:
7227 self.assertEqual(packet[IPv6].src, remote_ip6)
Matus Fabianf8cd5812017-07-11 03:55:02 -07007228 self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6)
7229 self.assertEqual(packet[IPv6].nh, 47)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007230 except:
7231 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7232 raise
7233
Matus Fabianf8cd5812017-07-11 03:55:02 -07007234 def test_hairpinning_unknown_proto(self):
Matus Fabian7968e6c2017-07-06 05:37:49 -07007235 """ NAT64 translate packet with unknown protocol - hairpinning """
7236
7237 client = self.pg0.remote_hosts[0]
7238 server = self.pg0.remote_hosts[1]
7239 server_tcp_in_port = 22
7240 server_tcp_out_port = 4022
7241 client_tcp_in_port = 1234
Matus Fabianf8cd5812017-07-11 03:55:02 -07007242 client_tcp_out_port = 1235
7243 server_nat_ip = "10.0.0.100"
7244 client_nat_ip = "10.0.0.110"
7245 server_nat_ip_n = socket.inet_pton(socket.AF_INET, server_nat_ip)
7246 client_nat_ip_n = socket.inet_pton(socket.AF_INET, client_nat_ip)
7247 server_nat_ip6 = self.compose_ip6(server_nat_ip, '64:ff9b::', 96)
7248 client_nat_ip6 = self.compose_ip6(client_nat_ip, '64:ff9b::', 96)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007249
Matus Fabianf8cd5812017-07-11 03:55:02 -07007250 self.vapi.nat64_add_del_pool_addr_range(server_nat_ip_n,
7251 client_nat_ip_n)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007252 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7253 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7254
7255 self.vapi.nat64_add_del_static_bib(server.ip6n,
Matus Fabianf8cd5812017-07-11 03:55:02 -07007256 server_nat_ip_n,
Matus Fabian7968e6c2017-07-06 05:37:49 -07007257 server_tcp_in_port,
7258 server_tcp_out_port,
7259 IP_PROTOS.tcp)
7260
Matus Fabianf8cd5812017-07-11 03:55:02 -07007261 self.vapi.nat64_add_del_static_bib(server.ip6n,
7262 server_nat_ip_n,
7263 0,
7264 0,
7265 IP_PROTOS.gre)
7266
7267 self.vapi.nat64_add_del_static_bib(client.ip6n,
7268 client_nat_ip_n,
7269 client_tcp_in_port,
7270 client_tcp_out_port,
7271 IP_PROTOS.tcp)
7272
Matus Fabian7968e6c2017-07-06 05:37:49 -07007273 # client to server
7274 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabianf8cd5812017-07-11 03:55:02 -07007275 IPv6(src=client.ip6, dst=server_nat_ip6) /
Matus Fabian7968e6c2017-07-06 05:37:49 -07007276 TCP(sport=client_tcp_in_port, dport=server_tcp_out_port))
7277 self.pg0.add_stream(p)
7278 self.pg_enable_capture(self.pg_interfaces)
7279 self.pg_start()
7280 p = self.pg0.get_capture(1)
7281
7282 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabianf8cd5812017-07-11 03:55:02 -07007283 IPv6(src=client.ip6, dst=server_nat_ip6, nh=IP_PROTOS.gre) /
Matus Fabian7968e6c2017-07-06 05:37:49 -07007284 GRE() /
7285 IP(src=self.pg2.local_ip4, dst=self.pg2.remote_ip4) /
7286 TCP(sport=1234, dport=1234))
7287 self.pg0.add_stream(p)
7288 self.pg_enable_capture(self.pg_interfaces)
7289 self.pg_start()
7290 p = self.pg0.get_capture(1)
7291 packet = p[0]
7292 try:
Matus Fabianf8cd5812017-07-11 03:55:02 -07007293 self.assertEqual(packet[IPv6].src, client_nat_ip6)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007294 self.assertEqual(packet[IPv6].dst, server.ip6)
Matus Fabianf8cd5812017-07-11 03:55:02 -07007295 self.assertEqual(packet[IPv6].nh, IP_PROTOS.gre)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007296 except:
7297 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7298 raise
7299
7300 # server to client
7301 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Matus Fabianf8cd5812017-07-11 03:55:02 -07007302 IPv6(src=server.ip6, dst=client_nat_ip6, nh=IP_PROTOS.gre) /
Matus Fabian7968e6c2017-07-06 05:37:49 -07007303 GRE() /
7304 IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) /
7305 TCP(sport=1234, dport=1234))
7306 self.pg0.add_stream(p)
7307 self.pg_enable_capture(self.pg_interfaces)
7308 self.pg_start()
7309 p = self.pg0.get_capture(1)
7310 packet = p[0]
7311 try:
Matus Fabianf8cd5812017-07-11 03:55:02 -07007312 self.assertEqual(packet[IPv6].src, server_nat_ip6)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007313 self.assertEqual(packet[IPv6].dst, client.ip6)
Matus Fabianf8cd5812017-07-11 03:55:02 -07007314 self.assertEqual(packet[IPv6].nh, IP_PROTOS.gre)
Matus Fabian7968e6c2017-07-06 05:37:49 -07007315 except:
7316 self.logger.error(ppp("Unexpected or invalid packet:", packet))
7317 raise
7318
Matus Fabian36ea2d62017-10-24 04:13:49 -07007319 def test_one_armed_nat64(self):
7320 """ One armed NAT64 """
7321 external_port = 0
7322 remote_host_ip6 = self.compose_ip6(self.pg3.remote_ip4,
7323 '64:ff9b::',
7324 96)
7325
7326 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7327 self.nat_addr_n)
7328 self.vapi.nat64_add_del_interface(self.pg3.sw_if_index)
7329 self.vapi.nat64_add_del_interface(self.pg3.sw_if_index, is_inside=0)
7330
7331 # in2out
7332 p = (Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) /
7333 IPv6(src=self.pg3.remote_ip6, dst=remote_host_ip6) /
7334 TCP(sport=12345, dport=80))
7335 self.pg3.add_stream(p)
7336 self.pg_enable_capture(self.pg_interfaces)
7337 self.pg_start()
7338 capture = self.pg3.get_capture(1)
7339 p = capture[0]
7340 try:
7341 ip = p[IP]
7342 tcp = p[TCP]
7343 self.assertEqual(ip.src, self.nat_addr)
7344 self.assertEqual(ip.dst, self.pg3.remote_ip4)
7345 self.assertNotEqual(tcp.sport, 12345)
7346 external_port = tcp.sport
7347 self.assertEqual(tcp.dport, 80)
Klement Sekerad81ae412018-05-16 10:52:54 +02007348 self.assert_packet_checksums_valid(p)
Matus Fabian36ea2d62017-10-24 04:13:49 -07007349 except:
7350 self.logger.error(ppp("Unexpected or invalid packet:", p))
7351 raise
7352
7353 # out2in
7354 p = (Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) /
7355 IP(src=self.pg3.remote_ip4, dst=self.nat_addr) /
7356 TCP(sport=80, dport=external_port))
7357 self.pg3.add_stream(p)
7358 self.pg_enable_capture(self.pg_interfaces)
7359 self.pg_start()
7360 capture = self.pg3.get_capture(1)
7361 p = capture[0]
7362 try:
7363 ip = p[IPv6]
7364 tcp = p[TCP]
7365 self.assertEqual(ip.src, remote_host_ip6)
7366 self.assertEqual(ip.dst, self.pg3.remote_ip6)
7367 self.assertEqual(tcp.sport, 80)
7368 self.assertEqual(tcp.dport, 12345)
Klement Sekerad81ae412018-05-16 10:52:54 +02007369 self.assert_packet_checksums_valid(p)
Matus Fabian36ea2d62017-10-24 04:13:49 -07007370 except:
7371 self.logger.error(ppp("Unexpected or invalid packet:", p))
7372 raise
7373
Matus Fabianefcd1e92017-08-15 06:59:19 -07007374 def test_frag_in_order(self):
7375 """ NAT64 translate fragments arriving in order """
7376 self.tcp_port_in = random.randint(1025, 65535)
7377
7378 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7379 self.nat_addr_n)
7380 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7381 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7382
7383 reass = self.vapi.nat_reass_dump()
7384 reass_n_start = len(reass)
7385
7386 # in2out
7387 data = 'a' * 200
7388 pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4,
7389 self.tcp_port_in, 20, data)
7390 self.pg0.add_stream(pkts)
7391 self.pg_enable_capture(self.pg_interfaces)
7392 self.pg_start()
7393 frags = self.pg1.get_capture(len(pkts))
7394 p = self.reass_frags_and_verify(frags,
7395 self.nat_addr,
7396 self.pg1.remote_ip4)
7397 self.assertEqual(p[TCP].dport, 20)
7398 self.assertNotEqual(p[TCP].sport, self.tcp_port_in)
7399 self.tcp_port_out = p[TCP].sport
7400 self.assertEqual(data, p[Raw].load)
7401
7402 # out2in
7403 data = "A" * 4 + "b" * 16 + "C" * 3
7404 pkts = self.create_stream_frag(self.pg1,
7405 self.nat_addr,
7406 20,
7407 self.tcp_port_out,
7408 data)
7409 self.pg1.add_stream(pkts)
7410 self.pg_enable_capture(self.pg_interfaces)
7411 self.pg_start()
7412 frags = self.pg0.get_capture(len(pkts))
7413 src = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96)
7414 p = self.reass_frags_and_verify_ip6(frags, src, self.pg0.remote_ip6)
7415 self.assertEqual(p[TCP].sport, 20)
7416 self.assertEqual(p[TCP].dport, self.tcp_port_in)
7417 self.assertEqual(data, p[Raw].load)
7418
7419 reass = self.vapi.nat_reass_dump()
7420 reass_n_end = len(reass)
7421
7422 self.assertEqual(reass_n_end - reass_n_start, 2)
7423
7424 def test_reass_hairpinning(self):
7425 """ NAT64 fragments hairpinning """
7426 data = 'a' * 200
Matus Fabianefcd1e92017-08-15 06:59:19 -07007427 server = self.pg0.remote_hosts[1]
7428 server_in_port = random.randint(1025, 65535)
7429 server_out_port = random.randint(1025, 65535)
7430 client_in_port = random.randint(1025, 65535)
7431 ip = IPv6(src=''.join(['64:ff9b::', self.nat_addr]))
7432 nat_addr_ip6 = ip.src
7433
7434 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7435 self.nat_addr_n)
7436 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7437 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7438
7439 # add static BIB entry for server
7440 self.vapi.nat64_add_del_static_bib(server.ip6n,
7441 self.nat_addr_n,
7442 server_in_port,
7443 server_out_port,
7444 IP_PROTOS.tcp)
7445
7446 # send packet from host to server
7447 pkts = self.create_stream_frag_ip6(self.pg0,
7448 self.nat_addr,
7449 client_in_port,
7450 server_out_port,
7451 data)
7452 self.pg0.add_stream(pkts)
7453 self.pg_enable_capture(self.pg_interfaces)
7454 self.pg_start()
7455 frags = self.pg0.get_capture(len(pkts))
7456 p = self.reass_frags_and_verify_ip6(frags, nat_addr_ip6, server.ip6)
7457 self.assertNotEqual(p[TCP].sport, client_in_port)
7458 self.assertEqual(p[TCP].dport, server_in_port)
7459 self.assertEqual(data, p[Raw].load)
7460
7461 def test_frag_out_of_order(self):
7462 """ NAT64 translate fragments arriving out of order """
7463 self.tcp_port_in = random.randint(1025, 65535)
7464
7465 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7466 self.nat_addr_n)
7467 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7468 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7469
7470 # in2out
7471 data = 'a' * 200
7472 pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4,
7473 self.tcp_port_in, 20, data)
7474 pkts.reverse()
7475 self.pg0.add_stream(pkts)
7476 self.pg_enable_capture(self.pg_interfaces)
7477 self.pg_start()
7478 frags = self.pg1.get_capture(len(pkts))
7479 p = self.reass_frags_and_verify(frags,
7480 self.nat_addr,
7481 self.pg1.remote_ip4)
7482 self.assertEqual(p[TCP].dport, 20)
7483 self.assertNotEqual(p[TCP].sport, self.tcp_port_in)
7484 self.tcp_port_out = p[TCP].sport
7485 self.assertEqual(data, p[Raw].load)
7486
7487 # out2in
7488 data = "A" * 4 + "B" * 16 + "C" * 3
7489 pkts = self.create_stream_frag(self.pg1,
7490 self.nat_addr,
7491 20,
7492 self.tcp_port_out,
7493 data)
7494 pkts.reverse()
7495 self.pg1.add_stream(pkts)
7496 self.pg_enable_capture(self.pg_interfaces)
7497 self.pg_start()
7498 frags = self.pg0.get_capture(len(pkts))
7499 src = self.compose_ip6(self.pg1.remote_ip4, '64:ff9b::', 96)
7500 p = self.reass_frags_and_verify_ip6(frags, src, self.pg0.remote_ip6)
7501 self.assertEqual(p[TCP].sport, 20)
7502 self.assertEqual(p[TCP].dport, self.tcp_port_in)
7503 self.assertEqual(data, p[Raw].load)
7504
Matus Fabian0938dcf2017-11-08 01:59:38 -08007505 def test_interface_addr(self):
7506 """ Acquire NAT64 pool addresses from interface """
7507 self.vapi.nat64_add_interface_addr(self.pg4.sw_if_index)
7508
7509 # no address in NAT64 pool
7510 adresses = self.vapi.nat44_address_dump()
7511 self.assertEqual(0, len(adresses))
7512
7513 # configure interface address and check NAT64 address pool
7514 self.pg4.config_ip4()
7515 addresses = self.vapi.nat64_pool_addr_dump()
7516 self.assertEqual(len(addresses), 1)
7517 self.assertEqual(addresses[0].address, self.pg4.local_ip4n)
7518
7519 # remove interface address and check NAT64 address pool
7520 self.pg4.unconfig_ip4()
7521 addresses = self.vapi.nat64_pool_addr_dump()
7522 self.assertEqual(0, len(adresses))
7523
Matus Fabiana431ad12018-01-04 04:03:14 -08007524 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
7525 def test_ipfix_max_bibs_sessions(self):
7526 """ IPFIX logging maximum session and BIB entries exceeded """
7527 max_bibs = 1280
7528 max_sessions = 2560
7529 remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4,
7530 '64:ff9b::',
7531 96)
7532
7533 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7534 self.nat_addr_n)
7535 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7536 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7537
7538 pkts = []
7539 src = ""
7540 for i in range(0, max_bibs):
7541 src = "fd01:aa::%x" % (i)
7542 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
7543 IPv6(src=src, dst=remote_host_ip6) /
7544 TCP(sport=12345, dport=80))
7545 pkts.append(p)
7546 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
7547 IPv6(src=src, dst=remote_host_ip6) /
7548 TCP(sport=12345, dport=22))
7549 pkts.append(p)
7550 self.pg0.add_stream(pkts)
7551 self.pg_enable_capture(self.pg_interfaces)
7552 self.pg_start()
7553 self.pg1.get_capture(max_sessions)
7554
7555 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
7556 src_address=self.pg3.local_ip4n,
7557 path_mtu=512,
7558 template_interval=10)
7559 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
7560 src_port=self.ipfix_src_port)
7561
7562 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
7563 IPv6(src=src, dst=remote_host_ip6) /
7564 TCP(sport=12345, dport=25))
7565 self.pg0.add_stream(p)
7566 self.pg_enable_capture(self.pg_interfaces)
7567 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07007568 self.pg1.assert_nothing_captured()
7569 sleep(1)
Matus Fabiana431ad12018-01-04 04:03:14 -08007570 self.vapi.cli("ipfix flush") # FIXME this should be an API call
7571 capture = self.pg3.get_capture(9)
7572 ipfix = IPFIXDecoder()
7573 # first load template
7574 for p in capture:
7575 self.assertTrue(p.haslayer(IPFIX))
7576 self.assertEqual(p[IP].src, self.pg3.local_ip4)
7577 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
7578 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
7579 self.assertEqual(p[UDP].dport, 4739)
7580 self.assertEqual(p[IPFIX].observationDomainID,
7581 self.ipfix_domain_id)
7582 if p.haslayer(Template):
7583 ipfix.add_template(p.getlayer(Template))
7584 # verify events in data set
7585 for p in capture:
7586 if p.haslayer(Data):
7587 data = ipfix.decode_data_set(p.getlayer(Set))
7588 self.verify_ipfix_max_sessions(data, max_sessions)
7589
7590 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
7591 IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6) /
7592 TCP(sport=12345, dport=80))
7593 self.pg0.add_stream(p)
7594 self.pg_enable_capture(self.pg_interfaces)
7595 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07007596 self.pg1.assert_nothing_captured()
7597 sleep(1)
Matus Fabiana431ad12018-01-04 04:03:14 -08007598 self.vapi.cli("ipfix flush") # FIXME this should be an API call
7599 capture = self.pg3.get_capture(1)
7600 # verify events in data set
7601 for p in capture:
7602 self.assertTrue(p.haslayer(IPFIX))
7603 self.assertEqual(p[IP].src, self.pg3.local_ip4)
7604 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
7605 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
7606 self.assertEqual(p[UDP].dport, 4739)
7607 self.assertEqual(p[IPFIX].observationDomainID,
7608 self.ipfix_domain_id)
7609 if p.haslayer(Data):
7610 data = ipfix.decode_data_set(p.getlayer(Set))
7611 self.verify_ipfix_max_bibs(data, max_bibs)
7612
7613 def test_ipfix_max_frags(self):
7614 """ IPFIX logging maximum fragments pending reassembly exceeded """
7615 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7616 self.nat_addr_n)
7617 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7618 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
Matus Fabiana7f8b222018-09-05 06:01:55 -07007619 self.vapi.nat_set_reass(max_frag=1, is_ip6=1)
Matus Fabiana431ad12018-01-04 04:03:14 -08007620 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
7621 src_address=self.pg3.local_ip4n,
7622 path_mtu=512,
7623 template_interval=10)
7624 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
7625 src_port=self.ipfix_src_port)
7626
7627 data = 'a' * 200
7628 pkts = self.create_stream_frag_ip6(self.pg0, self.pg1.remote_ip4,
7629 self.tcp_port_in, 20, data)
Matus Fabiana7f8b222018-09-05 06:01:55 -07007630 pkts.reverse()
7631 self.pg0.add_stream(pkts)
Matus Fabiana431ad12018-01-04 04:03:14 -08007632 self.pg_enable_capture(self.pg_interfaces)
7633 self.pg_start()
Matus Fabian229c1aa2018-05-28 04:09:52 -07007634 self.pg1.assert_nothing_captured()
7635 sleep(1)
Matus Fabiana431ad12018-01-04 04:03:14 -08007636 self.vapi.cli("ipfix flush") # FIXME this should be an API call
7637 capture = self.pg3.get_capture(9)
7638 ipfix = IPFIXDecoder()
7639 # first load template
7640 for p in capture:
7641 self.assertTrue(p.haslayer(IPFIX))
7642 self.assertEqual(p[IP].src, self.pg3.local_ip4)
7643 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
7644 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
7645 self.assertEqual(p[UDP].dport, 4739)
7646 self.assertEqual(p[IPFIX].observationDomainID,
7647 self.ipfix_domain_id)
7648 if p.haslayer(Template):
7649 ipfix.add_template(p.getlayer(Template))
7650 # verify events in data set
7651 for p in capture:
7652 if p.haslayer(Data):
7653 data = ipfix.decode_data_set(p.getlayer(Set))
Matus Fabiana7f8b222018-09-05 06:01:55 -07007654 self.verify_ipfix_max_fragments_ip6(data, 1,
Matus Fabiana431ad12018-01-04 04:03:14 -08007655 self.pg0.remote_ip6n)
7656
7657 def test_ipfix_bib_ses(self):
7658 """ IPFIX logging NAT64 BIB/session create and delete events """
7659 self.tcp_port_in = random.randint(1025, 65535)
7660 remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4,
7661 '64:ff9b::',
7662 96)
7663
7664 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7665 self.nat_addr_n)
7666 self.vapi.nat64_add_del_interface(self.pg0.sw_if_index)
7667 self.vapi.nat64_add_del_interface(self.pg1.sw_if_index, is_inside=0)
7668 self.vapi.set_ipfix_exporter(collector_address=self.pg3.remote_ip4n,
7669 src_address=self.pg3.local_ip4n,
7670 path_mtu=512,
7671 template_interval=10)
7672 self.vapi.nat_ipfix(domain_id=self.ipfix_domain_id,
7673 src_port=self.ipfix_src_port)
7674
7675 # Create
7676 p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
7677 IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6) /
7678 TCP(sport=self.tcp_port_in, dport=25))
7679 self.pg0.add_stream(p)
7680 self.pg_enable_capture(self.pg_interfaces)
7681 self.pg_start()
7682 p = self.pg1.get_capture(1)
7683 self.tcp_port_out = p[0][TCP].sport
7684 self.vapi.cli("ipfix flush") # FIXME this should be an API call
7685 capture = self.pg3.get_capture(10)
7686 ipfix = IPFIXDecoder()
7687 # first load template
7688 for p in capture:
7689 self.assertTrue(p.haslayer(IPFIX))
7690 self.assertEqual(p[IP].src, self.pg3.local_ip4)
7691 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
7692 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
7693 self.assertEqual(p[UDP].dport, 4739)
7694 self.assertEqual(p[IPFIX].observationDomainID,
7695 self.ipfix_domain_id)
7696 if p.haslayer(Template):
7697 ipfix.add_template(p.getlayer(Template))
7698 # verify events in data set
7699 for p in capture:
7700 if p.haslayer(Data):
7701 data = ipfix.decode_data_set(p.getlayer(Set))
7702 if ord(data[0][230]) == 10:
7703 self.verify_ipfix_bib(data, 1, self.pg0.remote_ip6n)
7704 elif ord(data[0][230]) == 6:
7705 self.verify_ipfix_nat64_ses(data,
7706 1,
7707 self.pg0.remote_ip6n,
7708 self.pg1.remote_ip4,
7709 25)
7710 else:
7711 self.logger.error(ppp("Unexpected or invalid packet: ", p))
7712
7713 # Delete
7714 self.pg_enable_capture(self.pg_interfaces)
7715 self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n,
7716 self.nat_addr_n,
7717 is_add=0)
7718 self.vapi.cli("ipfix flush") # FIXME this should be an API call
7719 capture = self.pg3.get_capture(2)
7720 # verify events in data set
7721 for p in capture:
7722 self.assertTrue(p.haslayer(IPFIX))
7723 self.assertEqual(p[IP].src, self.pg3.local_ip4)
7724 self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
7725 self.assertEqual(p[UDP].sport, self.ipfix_src_port)
7726 self.assertEqual(p[UDP].dport, 4739)
7727 self.assertEqual(p[IPFIX].observationDomainID,
7728 self.ipfix_domain_id)
7729 if p.haslayer(Data):
7730 data = ipfix.decode_data_set(p.getlayer(Set))
7731 if ord(data[0][230]) == 11:
7732 self.verify_ipfix_bib(data, 0, self.pg0.remote_ip6n)
7733 elif ord(data[0][230]) == 7:
7734 self.verify_ipfix_nat64_ses(data,
7735 0,
7736 self.pg0.remote_ip6n,
7737 self.pg1.remote_ip4,
7738 25)
7739 else:
7740 self.logger.error(ppp("Unexpected or invalid packet: ", p))
7741
Matus Fabian06596c52017-06-06 04:53:28 -07007742 def nat64_get_ses_num(self):
7743 """
7744 Return number of active NAT64 sessions.
7745 """
Matus Fabianab9a59c2017-08-16 05:37:36 -07007746 st = self.vapi.nat64_st_dump()
7747 return len(st)
Matus Fabian06596c52017-06-06 04:53:28 -07007748
7749 def clear_nat64(self):
7750 """
7751 Clear NAT64 configuration.
7752 """
Matus Fabiana431ad12018-01-04 04:03:14 -08007753 self.vapi.nat_ipfix(enable=0, src_port=self.ipfix_src_port,
7754 domain_id=self.ipfix_domain_id)
7755 self.ipfix_src_port = 4739
7756 self.ipfix_domain_id = 1
7757
Matus Fabian878c6462018-08-23 00:33:35 -07007758 self.vapi.nat_set_timeouts()
Matus Fabian06596c52017-06-06 04:53:28 -07007759
7760 interfaces = self.vapi.nat64_interface_dump()
7761 for intf in interfaces:
Matus Fabian36ea2d62017-10-24 04:13:49 -07007762 if intf.is_inside > 1:
7763 self.vapi.nat64_add_del_interface(intf.sw_if_index,
7764 0,
7765 is_add=0)
Matus Fabian06596c52017-06-06 04:53:28 -07007766 self.vapi.nat64_add_del_interface(intf.sw_if_index,
7767 intf.is_inside,
7768 is_add=0)
7769
Matus Fabiana431ad12018-01-04 04:03:14 -08007770 bib = self.vapi.nat64_bib_dump(255)
Matus Fabian06596c52017-06-06 04:53:28 -07007771 for bibe in bib:
7772 if bibe.is_static:
7773 self.vapi.nat64_add_del_static_bib(bibe.i_addr,
7774 bibe.o_addr,
7775 bibe.i_port,
7776 bibe.o_port,
7777 bibe.proto,
7778 bibe.vrf_id,
7779 is_add=0)
7780
7781 adresses = self.vapi.nat64_pool_addr_dump()
7782 for addr in adresses:
7783 self.vapi.nat64_add_del_pool_addr_range(addr.address,
7784 addr.address,
Matus Fabian029f3d22017-06-15 02:28:50 -07007785 vrf_id=addr.vrf_id,
Matus Fabian06596c52017-06-06 04:53:28 -07007786 is_add=0)
7787
Matus Fabian428dc912017-06-21 06:15:18 -07007788 prefixes = self.vapi.nat64_prefix_dump()
7789 for prefix in prefixes:
7790 self.vapi.nat64_add_del_prefix(prefix.prefix,
7791 prefix.prefix_len,
7792 vrf_id=prefix.vrf_id,
7793 is_add=0)
7794
Matus Fabian06596c52017-06-06 04:53:28 -07007795 def tearDown(self):
7796 super(TestNAT64, self).tearDown()
7797 if not self.vpp_dead:
7798 self.logger.info(self.vapi.cli("show nat64 pool"))
7799 self.logger.info(self.vapi.cli("show nat64 interfaces"))
Matus Fabian428dc912017-06-21 06:15:18 -07007800 self.logger.info(self.vapi.cli("show nat64 prefix"))
Matus Fabianab9a59c2017-08-16 05:37:36 -07007801 self.logger.info(self.vapi.cli("show nat64 bib all"))
7802 self.logger.info(self.vapi.cli("show nat64 session table all"))
Matus Fabianefcd1e92017-08-15 06:59:19 -07007803 self.logger.info(self.vapi.cli("show nat virtual-reassembly"))
Matus Fabian06596c52017-06-06 04:53:28 -07007804 self.clear_nat64()
7805
Matus Fabian8ebe6252017-11-06 05:04:53 -08007806
7807class TestDSlite(MethodHolder):
7808 """ DS-Lite Test Cases """
7809
7810 @classmethod
7811 def setUpClass(cls):
7812 super(TestDSlite, cls).setUpClass()
7813
7814 try:
7815 cls.nat_addr = '10.0.0.3'
7816 cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
7817
7818 cls.create_pg_interfaces(range(2))
7819 cls.pg0.admin_up()
7820 cls.pg0.config_ip4()
7821 cls.pg0.resolve_arp()
7822 cls.pg1.admin_up()
7823 cls.pg1.config_ip6()
7824 cls.pg1.generate_remote_hosts(2)
7825 cls.pg1.configure_ipv6_neighbors()
7826
7827 except Exception:
7828 super(TestDSlite, cls).tearDownClass()
7829 raise
7830
7831 def test_dslite(self):
7832 """ Test DS-Lite """
Matus Fabian69ce30d2018-08-22 01:27:10 -07007833 nat_config = self.vapi.nat_show_config()
7834 self.assertEqual(0, nat_config.dslite_ce)
7835
Matus Fabian8ebe6252017-11-06 05:04:53 -08007836 self.vapi.dslite_add_del_pool_addr_range(self.nat_addr_n,
7837 self.nat_addr_n)
7838 aftr_ip4 = '192.0.0.1'
7839 aftr_ip4_n = socket.inet_pton(socket.AF_INET, aftr_ip4)
7840 aftr_ip6 = '2001:db8:85a3::8a2e:370:1'
7841 aftr_ip6_n = socket.inet_pton(socket.AF_INET6, aftr_ip6)
7842 self.vapi.dslite_set_aftr_addr(aftr_ip6_n, aftr_ip4_n)
7843
7844 # UDP
7845 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
7846 IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[0].ip6) /
7847 IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
7848 UDP(sport=20000, dport=10000))
7849 self.pg1.add_stream(p)
7850 self.pg_enable_capture(self.pg_interfaces)
7851 self.pg_start()
7852 capture = self.pg0.get_capture(1)
7853 capture = capture[0]
7854 self.assertFalse(capture.haslayer(IPv6))
7855 self.assertEqual(capture[IP].src, self.nat_addr)
7856 self.assertEqual(capture[IP].dst, self.pg0.remote_ip4)
7857 self.assertNotEqual(capture[UDP].sport, 20000)
7858 self.assertEqual(capture[UDP].dport, 10000)
Klement Sekerad81ae412018-05-16 10:52:54 +02007859 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08007860 out_port = capture[UDP].sport
7861
7862 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7863 IP(dst=self.nat_addr, src=self.pg0.remote_ip4) /
7864 UDP(sport=10000, dport=out_port))
7865 self.pg0.add_stream(p)
7866 self.pg_enable_capture(self.pg_interfaces)
7867 self.pg_start()
7868 capture = self.pg1.get_capture(1)
7869 capture = capture[0]
7870 self.assertEqual(capture[IPv6].src, aftr_ip6)
7871 self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[0].ip6)
7872 self.assertEqual(capture[IP].src, self.pg0.remote_ip4)
7873 self.assertEqual(capture[IP].dst, '192.168.1.1')
7874 self.assertEqual(capture[UDP].sport, 10000)
7875 self.assertEqual(capture[UDP].dport, 20000)
Klement Sekerad81ae412018-05-16 10:52:54 +02007876 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08007877
7878 # TCP
7879 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
7880 IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[1].ip6) /
7881 IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
7882 TCP(sport=20001, dport=10001))
7883 self.pg1.add_stream(p)
7884 self.pg_enable_capture(self.pg_interfaces)
7885 self.pg_start()
7886 capture = self.pg0.get_capture(1)
7887 capture = capture[0]
7888 self.assertFalse(capture.haslayer(IPv6))
7889 self.assertEqual(capture[IP].src, self.nat_addr)
7890 self.assertEqual(capture[IP].dst, self.pg0.remote_ip4)
7891 self.assertNotEqual(capture[TCP].sport, 20001)
7892 self.assertEqual(capture[TCP].dport, 10001)
Klement Sekerad81ae412018-05-16 10:52:54 +02007893 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08007894 out_port = capture[TCP].sport
7895
7896 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7897 IP(dst=self.nat_addr, src=self.pg0.remote_ip4) /
7898 TCP(sport=10001, dport=out_port))
7899 self.pg0.add_stream(p)
7900 self.pg_enable_capture(self.pg_interfaces)
7901 self.pg_start()
7902 capture = self.pg1.get_capture(1)
7903 capture = capture[0]
7904 self.assertEqual(capture[IPv6].src, aftr_ip6)
7905 self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6)
7906 self.assertEqual(capture[IP].src, self.pg0.remote_ip4)
7907 self.assertEqual(capture[IP].dst, '192.168.1.1')
7908 self.assertEqual(capture[TCP].sport, 10001)
7909 self.assertEqual(capture[TCP].dport, 20001)
Klement Sekerad81ae412018-05-16 10:52:54 +02007910 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08007911
7912 # ICMP
7913 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
7914 IPv6(dst=aftr_ip6, src=self.pg1.remote_hosts[1].ip6) /
7915 IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
7916 ICMP(id=4000, type='echo-request'))
7917 self.pg1.add_stream(p)
7918 self.pg_enable_capture(self.pg_interfaces)
7919 self.pg_start()
7920 capture = self.pg0.get_capture(1)
7921 capture = capture[0]
7922 self.assertFalse(capture.haslayer(IPv6))
7923 self.assertEqual(capture[IP].src, self.nat_addr)
7924 self.assertEqual(capture[IP].dst, self.pg0.remote_ip4)
7925 self.assertNotEqual(capture[ICMP].id, 4000)
Klement Sekerad81ae412018-05-16 10:52:54 +02007926 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08007927 out_id = capture[ICMP].id
7928
7929 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
7930 IP(dst=self.nat_addr, src=self.pg0.remote_ip4) /
7931 ICMP(id=out_id, type='echo-reply'))
7932 self.pg0.add_stream(p)
7933 self.pg_enable_capture(self.pg_interfaces)
7934 self.pg_start()
7935 capture = self.pg1.get_capture(1)
7936 capture = capture[0]
7937 self.assertEqual(capture[IPv6].src, aftr_ip6)
7938 self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6)
7939 self.assertEqual(capture[IP].src, self.pg0.remote_ip4)
7940 self.assertEqual(capture[IP].dst, '192.168.1.1')
7941 self.assertEqual(capture[ICMP].id, 4000)
Klement Sekerad81ae412018-05-16 10:52:54 +02007942 self.assert_packet_checksums_valid(capture)
Matus Fabian8ebe6252017-11-06 05:04:53 -08007943
Matus Fabian331acc62017-12-08 03:38:51 -08007944 # ping DS-Lite AFTR tunnel endpoint address
7945 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
7946 IPv6(src=self.pg1.remote_hosts[1].ip6, dst=aftr_ip6) /
7947 ICMPv6EchoRequest())
7948 self.pg1.add_stream(p)
7949 self.pg_enable_capture(self.pg_interfaces)
7950 self.pg_start()
7951 capture = self.pg1.get_capture(1)
Matus Fabian331acc62017-12-08 03:38:51 -08007952 capture = capture[0]
7953 self.assertEqual(capture[IPv6].src, aftr_ip6)
7954 self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[1].ip6)
7955 self.assertTrue(capture.haslayer(ICMPv6EchoReply))
7956
Matus Fabian8ebe6252017-11-06 05:04:53 -08007957 def tearDown(self):
7958 super(TestDSlite, self).tearDown()
7959 if not self.vpp_dead:
7960 self.logger.info(self.vapi.cli("show dslite pool"))
7961 self.logger.info(
7962 self.vapi.cli("show dslite aftr-tunnel-endpoint-address"))
7963 self.logger.info(self.vapi.cli("show dslite sessions"))
7964
Juraj Slobodac5c6a332018-01-09 16:08:32 +01007965
7966class TestDSliteCE(MethodHolder):
7967 """ DS-Lite CE Test Cases """
7968
7969 @classmethod
7970 def setUpConstants(cls):
7971 super(TestDSliteCE, cls).setUpConstants()
7972 cls.vpp_cmdline.extend(["nat", "{", "dslite ce", "}"])
7973
7974 @classmethod
7975 def setUpClass(cls):
7976 super(TestDSliteCE, cls).setUpClass()
7977
7978 try:
7979 cls.create_pg_interfaces(range(2))
7980 cls.pg0.admin_up()
7981 cls.pg0.config_ip4()
7982 cls.pg0.resolve_arp()
7983 cls.pg1.admin_up()
7984 cls.pg1.config_ip6()
7985 cls.pg1.generate_remote_hosts(1)
7986 cls.pg1.configure_ipv6_neighbors()
7987
7988 except Exception:
7989 super(TestDSliteCE, cls).tearDownClass()
7990 raise
7991
7992 def test_dslite_ce(self):
7993 """ Test DS-Lite CE """
7994
Matus Fabian69ce30d2018-08-22 01:27:10 -07007995 nat_config = self.vapi.nat_show_config()
7996 self.assertEqual(1, nat_config.dslite_ce)
7997
Juraj Slobodac5c6a332018-01-09 16:08:32 +01007998 b4_ip4 = '192.0.0.2'
7999 b4_ip4_n = socket.inet_pton(socket.AF_INET, b4_ip4)
8000 b4_ip6 = '2001:db8:62aa::375e:f4c1:1'
8001 b4_ip6_n = socket.inet_pton(socket.AF_INET6, b4_ip6)
8002 self.vapi.dslite_set_b4_addr(b4_ip6_n, b4_ip4_n)
8003
8004 aftr_ip4 = '192.0.0.1'
8005 aftr_ip4_n = socket.inet_pton(socket.AF_INET, aftr_ip4)
8006 aftr_ip6 = '2001:db8:85a3::8a2e:370:1'
8007 aftr_ip6_n = socket.inet_pton(socket.AF_INET6, aftr_ip6)
8008 self.vapi.dslite_set_aftr_addr(aftr_ip6_n, aftr_ip4_n)
8009
8010 self.vapi.ip_add_del_route(dst_address=aftr_ip6_n,
8011 dst_address_length=128,
8012 next_hop_address=self.pg1.remote_ip6n,
8013 next_hop_sw_if_index=self.pg1.sw_if_index,
8014 is_ipv6=1)
8015
8016 # UDP encapsulation
8017 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8018 IP(dst=self.pg1.remote_ip4, src=self.pg0.remote_ip4) /
8019 UDP(sport=10000, dport=20000))
8020 self.pg0.add_stream(p)
8021 self.pg_enable_capture(self.pg_interfaces)
8022 self.pg_start()
8023 capture = self.pg1.get_capture(1)
8024 capture = capture[0]
8025 self.assertEqual(capture[IPv6].src, b4_ip6)
8026 self.assertEqual(capture[IPv6].dst, aftr_ip6)
8027 self.assertEqual(capture[IP].src, self.pg0.remote_ip4)
8028 self.assertEqual(capture[IP].dst, self.pg1.remote_ip4)
8029 self.assertEqual(capture[UDP].sport, 10000)
8030 self.assertEqual(capture[UDP].dport, 20000)
Klement Sekerad81ae412018-05-16 10:52:54 +02008031 self.assert_packet_checksums_valid(capture)
Juraj Slobodac5c6a332018-01-09 16:08:32 +01008032
8033 # UDP decapsulation
8034 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8035 IPv6(dst=b4_ip6, src=aftr_ip6) /
8036 IP(dst=self.pg0.remote_ip4, src=self.pg1.remote_ip4) /
8037 UDP(sport=20000, dport=10000))
8038 self.pg1.add_stream(p)
8039 self.pg_enable_capture(self.pg_interfaces)
8040 self.pg_start()
8041 capture = self.pg0.get_capture(1)
8042 capture = capture[0]
8043 self.assertFalse(capture.haslayer(IPv6))
8044 self.assertEqual(capture[IP].src, self.pg1.remote_ip4)
8045 self.assertEqual(capture[IP].dst, self.pg0.remote_ip4)
8046 self.assertEqual(capture[UDP].sport, 20000)
8047 self.assertEqual(capture[UDP].dport, 10000)
Klement Sekerad81ae412018-05-16 10:52:54 +02008048 self.assert_packet_checksums_valid(capture)
Juraj Slobodac5c6a332018-01-09 16:08:32 +01008049
8050 # ping DS-Lite B4 tunnel endpoint address
8051 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8052 IPv6(src=self.pg1.remote_hosts[0].ip6, dst=b4_ip6) /
8053 ICMPv6EchoRequest())
8054 self.pg1.add_stream(p)
8055 self.pg_enable_capture(self.pg_interfaces)
8056 self.pg_start()
8057 capture = self.pg1.get_capture(1)
Juraj Slobodac5c6a332018-01-09 16:08:32 +01008058 capture = capture[0]
8059 self.assertEqual(capture[IPv6].src, b4_ip6)
8060 self.assertEqual(capture[IPv6].dst, self.pg1.remote_hosts[0].ip6)
8061 self.assertTrue(capture.haslayer(ICMPv6EchoReply))
8062
8063 def tearDown(self):
8064 super(TestDSliteCE, self).tearDown()
8065 if not self.vpp_dead:
8066 self.logger.info(
8067 self.vapi.cli("show dslite aftr-tunnel-endpoint-address"))
8068 self.logger.info(
8069 self.vapi.cli("show dslite b4-tunnel-endpoint-address"))
8070
Matus Fabianf2a23cc2018-01-22 03:41:53 -08008071
8072class TestNAT66(MethodHolder):
8073 """ NAT66 Test Cases """
8074
8075 @classmethod
8076 def setUpClass(cls):
8077 super(TestNAT66, cls).setUpClass()
8078
8079 try:
8080 cls.nat_addr = 'fd01:ff::2'
8081 cls.nat_addr_n = socket.inet_pton(socket.AF_INET6, cls.nat_addr)
8082
8083 cls.create_pg_interfaces(range(2))
8084 cls.interfaces = list(cls.pg_interfaces)
8085
8086 for i in cls.interfaces:
8087 i.admin_up()
8088 i.config_ip6()
8089 i.configure_ipv6_neighbors()
8090
8091 except Exception:
8092 super(TestNAT66, cls).tearDownClass()
8093 raise
8094
8095 def test_static(self):
8096 """ 1:1 NAT66 test """
8097 self.vapi.nat66_add_del_interface(self.pg0.sw_if_index)
8098 self.vapi.nat66_add_del_interface(self.pg1.sw_if_index, is_inside=0)
8099 self.vapi.nat66_add_del_static_mapping(self.pg0.remote_ip6n,
8100 self.nat_addr_n)
8101
8102 # in2out
8103 pkts = []
8104 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8105 IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
8106 TCP())
8107 pkts.append(p)
8108 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8109 IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
8110 UDP())
8111 pkts.append(p)
8112 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8113 IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
8114 ICMPv6EchoRequest())
8115 pkts.append(p)
8116 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8117 IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
8118 GRE() / IP() / TCP())
8119 pkts.append(p)
8120 self.pg0.add_stream(pkts)
8121 self.pg_enable_capture(self.pg_interfaces)
8122 self.pg_start()
8123 capture = self.pg1.get_capture(len(pkts))
8124 for packet in capture:
8125 try:
8126 self.assertEqual(packet[IPv6].src, self.nat_addr)
8127 self.assertEqual(packet[IPv6].dst, self.pg1.remote_ip6)
Klement Sekerad81ae412018-05-16 10:52:54 +02008128 self.assert_packet_checksums_valid(packet)
Matus Fabianf2a23cc2018-01-22 03:41:53 -08008129 except:
8130 self.logger.error(ppp("Unexpected or invalid packet:", packet))
8131 raise
8132
8133 # out2in
8134 pkts = []
8135 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8136 IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
8137 TCP())
8138 pkts.append(p)
8139 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8140 IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
8141 UDP())
8142 pkts.append(p)
8143 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8144 IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
8145 ICMPv6EchoReply())
8146 pkts.append(p)
8147 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
8148 IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
8149 GRE() / IP() / TCP())
8150 pkts.append(p)
8151 self.pg1.add_stream(pkts)
8152 self.pg_enable_capture(self.pg_interfaces)
8153 self.pg_start()
8154 capture = self.pg0.get_capture(len(pkts))
8155 for packet in capture:
8156 try:
8157 self.assertEqual(packet[IPv6].src, self.pg1.remote_ip6)
8158 self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6)
Klement Sekerad81ae412018-05-16 10:52:54 +02008159 self.assert_packet_checksums_valid(packet)
Matus Fabianf2a23cc2018-01-22 03:41:53 -08008160 except:
8161 self.logger.error(ppp("Unexpected or invalid packet:", packet))
8162 raise
8163
8164 sm = self.vapi.nat66_static_mapping_dump()
8165 self.assertEqual(len(sm), 1)
8166 self.assertEqual(sm[0].total_pkts, 8)
8167
Juraj Sloboda9341e342018-04-13 12:00:46 +02008168 def test_check_no_translate(self):
8169 """ NAT66 translate only when egress interface is outside interface """
8170 self.vapi.nat66_add_del_interface(self.pg0.sw_if_index)
8171 self.vapi.nat66_add_del_interface(self.pg1.sw_if_index)
8172 self.vapi.nat66_add_del_static_mapping(self.pg0.remote_ip6n,
8173 self.nat_addr_n)
8174
8175 # in2out
8176 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
8177 IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
8178 UDP())
8179 self.pg0.add_stream([p])
8180 self.pg_enable_capture(self.pg_interfaces)
8181 self.pg_start()
8182 capture = self.pg1.get_capture(1)
8183 packet = capture[0]
8184 try:
8185 self.assertEqual(packet[IPv6].src, self.pg0.remote_ip6)
8186 self.assertEqual(packet[IPv6].dst, self.pg1.remote_ip6)
8187 except:
8188 self.logger.error(ppp("Unexpected or invalid packet:", packet))
8189 raise
8190
Matus Fabianf2a23cc2018-01-22 03:41:53 -08008191 def clear_nat66(self):
8192 """
8193 Clear NAT66 configuration.
8194 """
8195 interfaces = self.vapi.nat66_interface_dump()
8196 for intf in interfaces:
8197 self.vapi.nat66_add_del_interface(intf.sw_if_index,
8198 intf.is_inside,
8199 is_add=0)
8200
8201 static_mappings = self.vapi.nat66_static_mapping_dump()
8202 for sm in static_mappings:
8203 self.vapi.nat66_add_del_static_mapping(sm.local_ip_address,
8204 sm.external_ip_address,
8205 sm.vrf_id,
8206 is_add=0)
8207
8208 def tearDown(self):
8209 super(TestNAT66, self).tearDown()
8210 if not self.vpp_dead:
8211 self.logger.info(self.vapi.cli("show nat66 interfaces"))
8212 self.logger.info(self.vapi.cli("show nat66 static mappings"))
8213 self.clear_nat66()
8214
Klement Sekerad81ae412018-05-16 10:52:54 +02008215
Matus Fabiande886752016-12-07 03:38:19 -08008216if __name__ == '__main__':
8217 unittest.main(testRunner=VppTestRunner)