blob: 1caed0dd932a61866fc7977c9b7f33ba4ca123ff [file] [log] [blame]
Klement Sekera31da2e32018-06-24 22:49:55 +02001import unittest
Klement Sekera611864f2018-09-26 11:19:00 +02002import socket
Neale Ranns80f6fd52019-04-16 02:41:34 +00003import struct
Klement Sekera31da2e32018-06-24 22:49:55 +02004
Neale Ranns53f526b2019-02-25 14:32:02 +00005from scapy.layers.inet import IP, ICMP, TCP, UDP
Damjan Mariona829b132019-04-24 23:39:16 +02006from scapy.layers.ipsec import SecurityAssociation, ESP
snaramre5d4b8912019-12-13 23:39:35 +00007from scapy.layers.l2 import Ether
Paul Vinciguerra582eac52020-04-03 12:18:40 -04008from scapy.packet import raw, Raw
Neale Ranns02950402019-12-20 00:54:57 +00009from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest, IPv6ExtHdrHopByHop, \
10 IPv6ExtHdrFragment, IPv6ExtHdrDestOpt
11
Klement Sekera31da2e32018-06-24 22:49:55 +020012
13from framework import VppTestCase, VppTestRunner
Neale Ranns14046982019-07-29 14:49:52 +000014from util import ppp, reassemble4, fragment_rfc791, fragment_rfc8200
Neale Ranns17dcec02019-01-09 21:22:20 -080015from vpp_papi import VppEnum
Klement Sekera31da2e32018-06-24 22:49:55 +020016
17
Klement Sekera611864f2018-09-26 11:19:00 +020018class IPsecIPv4Params(object):
Neale Ranns17dcec02019-01-09 21:22:20 -080019
Klement Sekera611864f2018-09-26 11:19:00 +020020 addr_type = socket.AF_INET
21 addr_any = "0.0.0.0"
22 addr_bcast = "255.255.255.255"
23 addr_len = 32
24 is_ipv6 = 0
Klement Sekera611864f2018-09-26 11:19:00 +020025
Neale Ranns17dcec02019-01-09 21:22:20 -080026 def __init__(self):
27 self.remote_tun_if_host = '1.1.1.1'
Neale Ranns987aea82019-03-27 13:40:35 +000028 self.remote_tun_if_host6 = '1111::1'
Klement Sekera611864f2018-09-26 11:19:00 +020029
Neale Ranns28287212019-12-16 00:53:11 +000030 self.scapy_tun_sa_id = 100
Neale Ranns17dcec02019-01-09 21:22:20 -080031 self.scapy_tun_spi = 1001
Neale Ranns28287212019-12-16 00:53:11 +000032 self.vpp_tun_sa_id = 200
Neale Ranns17dcec02019-01-09 21:22:20 -080033 self.vpp_tun_spi = 1000
Klement Sekera611864f2018-09-26 11:19:00 +020034
Neale Ranns28287212019-12-16 00:53:11 +000035 self.scapy_tra_sa_id = 300
Neale Ranns17dcec02019-01-09 21:22:20 -080036 self.scapy_tra_spi = 2001
Neale Ranns28287212019-12-16 00:53:11 +000037 self.vpp_tra_sa_id = 400
Neale Ranns17dcec02019-01-09 21:22:20 -080038 self.vpp_tra_spi = 2000
Klement Sekera611864f2018-09-26 11:19:00 +020039
Neale Ranns17dcec02019-01-09 21:22:20 -080040 self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t.
41 IPSEC_API_INTEG_ALG_SHA1_96)
42 self.auth_algo = 'HMAC-SHA1-96' # scapy name
Ole Troan64e978b2019-10-17 21:40:36 +020043 self.auth_key = b'C91KUR9GYMm5GfkEvNjX'
Neale Ranns17dcec02019-01-09 21:22:20 -080044
45 self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t.
46 IPSEC_API_CRYPTO_ALG_AES_CBC_128)
47 self.crypt_algo = 'AES-CBC' # scapy name
Ole Troan64e978b2019-10-17 21:40:36 +020048 self.crypt_key = b'JPjyOWBeVEQiMe7h'
Neale Ranns80f6fd52019-04-16 02:41:34 +000049 self.salt = 0
Neale Ranns53f526b2019-02-25 14:32:02 +000050 self.flags = 0
51 self.nat_header = None
Klement Sekera611864f2018-09-26 11:19:00 +020052
53
54class IPsecIPv6Params(object):
Neale Ranns17dcec02019-01-09 21:22:20 -080055
Klement Sekera611864f2018-09-26 11:19:00 +020056 addr_type = socket.AF_INET6
57 addr_any = "0::0"
58 addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
59 addr_len = 128
60 is_ipv6 = 1
Klement Sekera611864f2018-09-26 11:19:00 +020061
Neale Ranns17dcec02019-01-09 21:22:20 -080062 def __init__(self):
63 self.remote_tun_if_host = '1111:1111:1111:1111:1111:1111:1111:1111'
Neale Ranns987aea82019-03-27 13:40:35 +000064 self.remote_tun_if_host4 = '1.1.1.1'
Klement Sekera611864f2018-09-26 11:19:00 +020065
Neale Ranns28287212019-12-16 00:53:11 +000066 self.scapy_tun_sa_id = 500
Neale Ranns17dcec02019-01-09 21:22:20 -080067 self.scapy_tun_spi = 3001
Neale Ranns28287212019-12-16 00:53:11 +000068 self.vpp_tun_sa_id = 600
Neale Ranns17dcec02019-01-09 21:22:20 -080069 self.vpp_tun_spi = 3000
Klement Sekera611864f2018-09-26 11:19:00 +020070
Neale Ranns28287212019-12-16 00:53:11 +000071 self.scapy_tra_sa_id = 700
Neale Ranns17dcec02019-01-09 21:22:20 -080072 self.scapy_tra_spi = 4001
Neale Ranns28287212019-12-16 00:53:11 +000073 self.vpp_tra_sa_id = 800
Neale Ranns17dcec02019-01-09 21:22:20 -080074 self.vpp_tra_spi = 4000
Klement Sekera611864f2018-09-26 11:19:00 +020075
Neale Ranns17dcec02019-01-09 21:22:20 -080076 self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t.
Neale Ranns1091c4a2019-04-08 14:48:23 +000077 IPSEC_API_INTEG_ALG_SHA1_96)
78 self.auth_algo = 'HMAC-SHA1-96' # scapy name
Ole Troan64e978b2019-10-17 21:40:36 +020079 self.auth_key = b'C91KUR9GYMm5GfkEvNjX'
Neale Ranns17dcec02019-01-09 21:22:20 -080080
81 self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t.
Neale Ranns4f33c802019-04-10 12:39:10 +000082 IPSEC_API_CRYPTO_ALG_AES_CBC_128)
Neale Ranns17dcec02019-01-09 21:22:20 -080083 self.crypt_algo = 'AES-CBC' # scapy name
Ole Troan64e978b2019-10-17 21:40:36 +020084 self.crypt_key = b'JPjyOWBeVEQiMe7h'
Neale Ranns80f6fd52019-04-16 02:41:34 +000085 self.salt = 0
Neale Ranns53f526b2019-02-25 14:32:02 +000086 self.flags = 0
87 self.nat_header = None
Klement Sekera611864f2018-09-26 11:19:00 +020088
89
Neale Ranns12989b52019-09-26 16:20:19 +000090def mk_scapy_crypt_key(p):
Neale Ranns6afaae12019-07-17 15:07:14 +000091 if p.crypt_algo == "AES-GCM":
92 return p.crypt_key + struct.pack("!I", p.salt)
93 else:
94 return p.crypt_key
95
96
Neale Ranns2ac885c2019-03-20 18:24:43 +000097def config_tun_params(p, encryption_type, tun_if):
98 ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
snaramre5d4b8912019-12-13 23:39:35 +000099 esn_en = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.
100 IPSEC_API_SAD_FLAG_USE_ESN))
Neale Rannsf3a66222020-01-02 05:04:00 +0000101 p.tun_dst = tun_if.remote_addr[p.addr_type]
102 p.tun_src = tun_if.local_addr[p.addr_type]
Neale Ranns12989b52019-09-26 16:20:19 +0000103 crypt_key = mk_scapy_crypt_key(p)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000104 p.scapy_tun_sa = SecurityAssociation(
105 encryption_type, spi=p.vpp_tun_spi,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000106 crypt_algo=p.crypt_algo,
107 crypt_key=crypt_key,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000108 auth_algo=p.auth_algo, auth_key=p.auth_key,
109 tunnel_header=ip_class_by_addr_type[p.addr_type](
Neale Rannsf3a66222020-01-02 05:04:00 +0000110 src=p.tun_dst,
111 dst=p.tun_src),
Neale Ranns3833ffd2019-03-21 14:34:09 +0000112 nat_t_header=p.nat_header,
snaramre5d4b8912019-12-13 23:39:35 +0000113 esn_en=esn_en)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000114 p.vpp_tun_sa = SecurityAssociation(
115 encryption_type, spi=p.scapy_tun_spi,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000116 crypt_algo=p.crypt_algo,
117 crypt_key=crypt_key,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000118 auth_algo=p.auth_algo, auth_key=p.auth_key,
119 tunnel_header=ip_class_by_addr_type[p.addr_type](
Neale Rannsf3a66222020-01-02 05:04:00 +0000120 dst=p.tun_dst,
121 src=p.tun_src),
Neale Ranns3833ffd2019-03-21 14:34:09 +0000122 nat_t_header=p.nat_header,
snaramre5d4b8912019-12-13 23:39:35 +0000123 esn_en=esn_en)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000124
125
126def config_tra_params(p, encryption_type):
snaramre5d4b8912019-12-13 23:39:35 +0000127 esn_en = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.
128 IPSEC_API_SAD_FLAG_USE_ESN))
Neale Ranns12989b52019-09-26 16:20:19 +0000129 crypt_key = mk_scapy_crypt_key(p)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000130 p.scapy_tra_sa = SecurityAssociation(
131 encryption_type,
132 spi=p.vpp_tra_spi,
133 crypt_algo=p.crypt_algo,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000134 crypt_key=crypt_key,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000135 auth_algo=p.auth_algo,
136 auth_key=p.auth_key,
Neale Ranns3833ffd2019-03-21 14:34:09 +0000137 nat_t_header=p.nat_header,
snaramre5d4b8912019-12-13 23:39:35 +0000138 esn_en=esn_en)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000139 p.vpp_tra_sa = SecurityAssociation(
140 encryption_type,
141 spi=p.scapy_tra_spi,
142 crypt_algo=p.crypt_algo,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000143 crypt_key=crypt_key,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000144 auth_algo=p.auth_algo,
145 auth_key=p.auth_key,
Neale Ranns3833ffd2019-03-21 14:34:09 +0000146 nat_t_header=p.nat_header,
snaramre5d4b8912019-12-13 23:39:35 +0000147 esn_en=esn_en)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000148
149
Klement Sekera31da2e32018-06-24 22:49:55 +0200150class TemplateIpsec(VppTestCase):
151 """
152 TRANSPORT MODE:
153
154 ------ encrypt ---
155 |tra_if| <-------> |VPP|
156 ------ decrypt ---
157
158 TUNNEL MODE:
159
160 ------ encrypt --- plain ---
161 |tun_if| <------- |VPP| <------ |pg1|
162 ------ --- ---
163
164 ------ decrypt --- plain ---
165 |tun_if| -------> |VPP| ------> |pg1|
166 ------ --- ---
167 """
Neale Ranns4f33c802019-04-10 12:39:10 +0000168 tun_spd_id = 1
169 tra_spd_id = 2
Klement Sekera31da2e32018-06-24 22:49:55 +0200170
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800171 def ipsec_select_backend(self):
Klement Sekerab4d30532018-11-08 13:00:02 +0100172 """ empty method to be overloaded when necessary """
173 pass
174
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700175 @classmethod
176 def setUpClass(cls):
177 super(TemplateIpsec, cls).setUpClass()
178
179 @classmethod
180 def tearDownClass(cls):
181 super(TemplateIpsec, cls).tearDownClass()
182
Neale Ranns3833ffd2019-03-21 14:34:09 +0000183 def setup_params(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800184 self.ipv4_params = IPsecIPv4Params()
185 self.ipv6_params = IPsecIPv6Params()
186 self.params = {self.ipv4_params.addr_type: self.ipv4_params,
187 self.ipv6_params.addr_type: self.ipv6_params}
188
Neale Ranns4f33c802019-04-10 12:39:10 +0000189 def config_interfaces(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800190 self.create_pg_interfaces(range(3))
191 self.interfaces = list(self.pg_interfaces)
192 for i in self.interfaces:
Klement Sekera31da2e32018-06-24 22:49:55 +0200193 i.admin_up()
194 i.config_ip4()
195 i.resolve_arp()
Klement Sekera611864f2018-09-26 11:19:00 +0200196 i.config_ip6()
197 i.resolve_ndp()
Neale Ranns4f33c802019-04-10 12:39:10 +0000198
199 def setUp(self):
200 super(TemplateIpsec, self).setUp()
201
202 self.setup_params()
203
204 self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t.
205 IPSEC_API_PROTO_ESP)
206 self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t.
207 IPSEC_API_PROTO_AH)
208
209 self.config_interfaces()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700210
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800211 self.ipsec_select_backend()
Klement Sekera31da2e32018-06-24 22:49:55 +0200212
Neale Ranns4f33c802019-04-10 12:39:10 +0000213 def unconfig_interfaces(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800214 for i in self.interfaces:
215 i.admin_down()
216 i.unconfig_ip4()
217 i.unconfig_ip6()
218
Neale Ranns4f33c802019-04-10 12:39:10 +0000219 def tearDown(self):
220 super(TemplateIpsec, self).tearDown()
221
222 self.unconfig_interfaces()
223
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700224 def show_commands_at_teardown(self):
225 self.logger.info(self.vapi.cli("show hardware"))
Klement Sekera31da2e32018-06-24 22:49:55 +0200226
Neale Ranns28287212019-12-16 00:53:11 +0000227 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1,
Neale Rannsd7603d92019-03-28 08:56:10 +0000228 payload_size=54):
Klement Sekera31da2e32018-06-24 22:49:55 +0200229 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000230 sa.encrypt(IP(src=src, dst=dst) /
Ole Troan8b76c232019-10-21 20:55:13 +0200231 ICMP() / Raw(b'X' * payload_size))
Klement Sekera31da2e32018-06-24 22:49:55 +0200232 for i in range(count)]
233
Neale Ranns28287212019-12-16 00:53:11 +0000234 def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1,
Neale Rannsd7603d92019-03-28 08:56:10 +0000235 payload_size=54):
Klement Sekera611864f2018-09-26 11:19:00 +0200236 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
237 sa.encrypt(IPv6(src=src, dst=dst) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000238 ICMPv6EchoRequest(id=0, seq=1,
239 data='X' * payload_size))
Klement Sekera611864f2018-09-26 11:19:00 +0200240 for i in range(count)]
241
Neale Rannsd7603d92019-03-28 08:56:10 +0000242 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=54):
Klement Sekera31da2e32018-06-24 22:49:55 +0200243 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
Ole Troan8b76c232019-10-21 20:55:13 +0200244 IP(src=src, dst=dst) / ICMP() / Raw(b'X' * payload_size)
Klement Sekera31da2e32018-06-24 22:49:55 +0200245 for i in range(count)]
246
Neale Rannsd7603d92019-03-28 08:56:10 +0000247 def gen_pkts6(self, sw_intf, src, dst, count=1, payload_size=54):
Klement Sekera611864f2018-09-26 11:19:00 +0200248 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
249 IPv6(src=src, dst=dst) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000250 ICMPv6EchoRequest(id=0, seq=1, data='X' * payload_size)
Klement Sekera611864f2018-09-26 11:19:00 +0200251 for i in range(count)]
252
Klement Sekera31da2e32018-06-24 22:49:55 +0200253
Neale Ranns4f33c802019-04-10 12:39:10 +0000254class IpsecTcp(object):
255 def verify_tcp_checksum(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200256 self.vapi.cli("test http server")
Klement Sekera611864f2018-09-26 11:19:00 +0200257 p = self.params[socket.AF_INET]
Klement Sekera31da2e32018-06-24 22:49:55 +0200258 send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
Neale Ranns2ac885c2019-03-20 18:24:43 +0000259 p.scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host,
260 dst=self.tun_if.local_ip4) /
261 TCP(flags='S', dport=80)))
Klement Sekera31da2e32018-06-24 22:49:55 +0200262 self.logger.debug(ppp("Sending packet:", send))
Klement Sekera611864f2018-09-26 11:19:00 +0200263 recv = self.send_and_expect(self.tun_if, [send], self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200264 recv = recv[0]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000265 decrypted = p.vpp_tun_sa.decrypt(recv[IP])
Klement Sekera31da2e32018-06-24 22:49:55 +0200266 self.assert_packet_checksums_valid(decrypted)
267
268
Neale Ranns4f33c802019-04-10 12:39:10 +0000269class IpsecTcpTests(IpsecTcp):
270 def test_tcp_checksum(self):
271 """ verify checksum correctness for vpp generated packets """
272 self.verify_tcp_checksum()
273
274
275class IpsecTra4(object):
276 """ verify methods for Transport v4 """
Neale Ranns6afaae12019-07-17 15:07:14 +0000277 def verify_tra_anti_replay(self):
Neale Rannsde847272018-11-28 01:38:34 -0800278 p = self.params[socket.AF_INET]
snaramre5d4b8912019-12-13 23:39:35 +0000279 esn_en = p.vpp_tra_sa.esn_en
Neale Rannsde847272018-11-28 01:38:34 -0800280
Neale Ranns6afaae12019-07-17 15:07:14 +0000281 seq_cycle_node_name = ('/err/%s/sequence number cycled' %
282 self.tra4_encrypt_node_name)
283 replay_node_name = ('/err/%s/SA replayed packet' %
284 self.tra4_decrypt_node_name)
285 if ESP == self.encryption_type and p.crypt_algo == "AES-GCM":
286 hash_failed_node_name = ('/err/%s/ESP decryption failed' %
287 self.tra4_decrypt_node_name)
288 else:
289 hash_failed_node_name = ('/err/%s/Integrity check failed' %
290 self.tra4_decrypt_node_name)
291 replay_count = self.statistics.get_err_counter(replay_node_name)
292 hash_failed_count = self.statistics.get_err_counter(
293 hash_failed_node_name)
294 seq_cycle_count = self.statistics.get_err_counter(seq_cycle_node_name)
Neale Rannsde847272018-11-28 01:38:34 -0800295
Neale Ranns6afaae12019-07-17 15:07:14 +0000296 if ESP == self.encryption_type:
297 undersize_node_name = ('/err/%s/undersized packet' %
298 self.tra4_decrypt_node_name)
299 undersize_count = self.statistics.get_err_counter(
300 undersize_node_name)
301
302 #
303 # send packets with seq numbers 1->34
304 # this means the window size is still in Case B (see RFC4303
305 # Appendix A)
306 #
307 # for reasons i haven't investigated Scapy won't create a packet with
308 # seq_num=0
309 #
310 pkts = [(Ether(src=self.tra_if.remote_mac,
311 dst=self.tra_if.local_mac) /
312 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
313 dst=self.tra_if.local_ip4) /
314 ICMP(),
315 seq_num=seq))
316 for seq in range(1, 34)]
317 recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if)
318
319 # replayed packets are dropped
320 self.send_and_assert_no_replies(self.tra_if, pkts)
321 replay_count += len(pkts)
322 self.assert_error_counter_equal(replay_node_name, replay_count)
323
324 #
Neale Ranns3b9374f2019-08-01 04:45:15 -0700325 # now send a batch of packets all with the same sequence number
326 # the first packet in the batch is legitimate, the rest bogus
327 #
328 pkts = (Ether(src=self.tra_if.remote_mac,
329 dst=self.tra_if.local_mac) /
330 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
331 dst=self.tra_if.local_ip4) /
332 ICMP(),
333 seq_num=35))
334 recv_pkts = self.send_and_expect(self.tra_if, pkts * 8,
335 self.tra_if, n_rx=1)
336 replay_count += 7
337 self.assert_error_counter_equal(replay_node_name, replay_count)
338
339 #
Neale Ranns6afaae12019-07-17 15:07:14 +0000340 # now move the window over to 257 (more than one byte) and into Case A
341 #
Neale Rannsde847272018-11-28 01:38:34 -0800342 pkt = (Ether(src=self.tra_if.remote_mac,
343 dst=self.tra_if.local_mac) /
344 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
345 dst=self.tra_if.local_ip4) /
346 ICMP(),
Neale Ranns6afaae12019-07-17 15:07:14 +0000347 seq_num=257))
Neale Rannsde847272018-11-28 01:38:34 -0800348 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
349
Neale Ranns3833ffd2019-03-21 14:34:09 +0000350 # replayed packets are dropped
351 self.send_and_assert_no_replies(self.tra_if, pkt * 3)
Neale Ranns6afaae12019-07-17 15:07:14 +0000352 replay_count += 3
353 self.assert_error_counter_equal(replay_node_name, replay_count)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000354
Neale Rannsde847272018-11-28 01:38:34 -0800355 # the window size is 64 packets
356 # in window are still accepted
357 pkt = (Ether(src=self.tra_if.remote_mac,
358 dst=self.tra_if.local_mac) /
359 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
360 dst=self.tra_if.local_ip4) /
361 ICMP(),
Neale Ranns6afaae12019-07-17 15:07:14 +0000362 seq_num=200))
Neale Rannsde847272018-11-28 01:38:34 -0800363 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
364
Neale Rannsde847272018-11-28 01:38:34 -0800365 # a packet that does not decrypt does not move the window forward
366 bogus_sa = SecurityAssociation(self.encryption_type,
Damjan Mariona829b132019-04-24 23:39:16 +0200367 p.vpp_tra_spi,
368 crypt_algo=p.crypt_algo,
Neale Ranns12989b52019-09-26 16:20:19 +0000369 crypt_key=mk_scapy_crypt_key(p)[::-1],
Damjan Mariona829b132019-04-24 23:39:16 +0200370 auth_algo=p.auth_algo,
371 auth_key=p.auth_key[::-1])
Neale Rannsde847272018-11-28 01:38:34 -0800372 pkt = (Ether(src=self.tra_if.remote_mac,
373 dst=self.tra_if.local_mac) /
374 bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4,
375 dst=self.tra_if.local_ip4) /
376 ICMP(),
377 seq_num=350))
378 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
379
Neale Ranns6afaae12019-07-17 15:07:14 +0000380 hash_failed_count += 17
381 self.assert_error_counter_equal(hash_failed_node_name,
382 hash_failed_count)
Neale Rannsde847272018-11-28 01:38:34 -0800383
Damjan Mariona829b132019-04-24 23:39:16 +0200384 # a malformed 'runt' packet
385 # created by a mis-constructed SA
Neale Ranns2cdcd0c2019-08-27 12:26:14 +0000386 if (ESP == self.encryption_type and p.crypt_algo != "NULL"):
Damjan Mariona829b132019-04-24 23:39:16 +0200387 bogus_sa = SecurityAssociation(self.encryption_type,
388 p.vpp_tra_spi)
389 pkt = (Ether(src=self.tra_if.remote_mac,
390 dst=self.tra_if.local_mac) /
391 bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4,
392 dst=self.tra_if.local_ip4) /
393 ICMP(),
394 seq_num=350))
395 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
396
Neale Ranns6afaae12019-07-17 15:07:14 +0000397 undersize_count += 17
398 self.assert_error_counter_equal(undersize_node_name,
399 undersize_count)
Damjan Mariona829b132019-04-24 23:39:16 +0200400
Neale Rannsde847272018-11-28 01:38:34 -0800401 # which we can determine since this packet is still in the window
402 pkt = (Ether(src=self.tra_if.remote_mac,
403 dst=self.tra_if.local_mac) /
404 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
405 dst=self.tra_if.local_ip4) /
406 ICMP(),
407 seq_num=234))
Klement Sekera14d7e902018-12-10 13:46:09 +0100408 self.send_and_expect(self.tra_if, [pkt], self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800409
Neale Ranns6afaae12019-07-17 15:07:14 +0000410 #
Neale Ranns3833ffd2019-03-21 14:34:09 +0000411 # out of window are dropped
Neale Ranns6afaae12019-07-17 15:07:14 +0000412 # this is Case B. So VPP will consider this to be a high seq num wrap
413 # and so the decrypt attempt will fail
414 #
Neale Ranns3833ffd2019-03-21 14:34:09 +0000415 pkt = (Ether(src=self.tra_if.remote_mac,
416 dst=self.tra_if.local_mac) /
417 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
418 dst=self.tra_if.local_ip4) /
419 ICMP(),
420 seq_num=17))
421 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
Neale Ranns00a44202019-03-21 16:36:28 +0000422
snaramre5d4b8912019-12-13 23:39:35 +0000423 if esn_en:
Neale Ranns3833ffd2019-03-21 14:34:09 +0000424 # an out of window error with ESN looks like a high sequence
425 # wrap. but since it isn't then the verify will fail.
Neale Ranns6afaae12019-07-17 15:07:14 +0000426 hash_failed_count += 17
427 self.assert_error_counter_equal(hash_failed_node_name,
428 hash_failed_count)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000429
430 else:
Neale Ranns6afaae12019-07-17 15:07:14 +0000431 replay_count += 17
432 self.assert_error_counter_equal(replay_node_name,
433 replay_count)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000434
Neale Ranns6afaae12019-07-17 15:07:14 +0000435 # valid packet moves the window over to 258
Neale Ranns00a44202019-03-21 16:36:28 +0000436 pkt = (Ether(src=self.tra_if.remote_mac,
437 dst=self.tra_if.local_mac) /
438 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
439 dst=self.tra_if.local_ip4) /
440 ICMP(),
Neale Ranns6afaae12019-07-17 15:07:14 +0000441 seq_num=258))
Neale Ranns3833ffd2019-03-21 14:34:09 +0000442 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
443 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
444
Neale Ranns6afaae12019-07-17 15:07:14 +0000445 #
446 # move VPP's SA TX seq-num to just before the seq-number wrap.
447 # then fire in a packet that VPP should drop on TX because it
448 # causes the TX seq number to wrap; unless we're using extened sequence
449 # numbers.
450 #
Neale Ranns3833ffd2019-03-21 14:34:09 +0000451 self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.scapy_tra_sa_id)
Neale Ranns6afaae12019-07-17 15:07:14 +0000452 self.logger.info(self.vapi.ppcli("show ipsec sa 0"))
453 self.logger.info(self.vapi.ppcli("show ipsec sa 1"))
Neale Ranns3833ffd2019-03-21 14:34:09 +0000454
Neale Ranns6afaae12019-07-17 15:07:14 +0000455 pkts = [(Ether(src=self.tra_if.remote_mac,
456 dst=self.tra_if.local_mac) /
457 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
458 dst=self.tra_if.local_ip4) /
459 ICMP(),
460 seq_num=seq))
461 for seq in range(259, 280)]
Neale Ranns3833ffd2019-03-21 14:34:09 +0000462
snaramre5d4b8912019-12-13 23:39:35 +0000463 if esn_en:
Neale Ranns6afaae12019-07-17 15:07:14 +0000464 rxs = self.send_and_expect(self.tra_if, pkts, self.tra_if)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000465
Neale Ranns6afaae12019-07-17 15:07:14 +0000466 #
467 # in order for scapy to decrypt its SA's high order number needs
468 # to wrap
469 #
470 p.vpp_tra_sa.seq_num = 0x100000000
471 for rx in rxs:
472 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
473
474 #
475 # wrap scapy's TX high sequence number. VPP is in case B, so it
476 # will consider this a high seq wrap also.
477 # The low seq num we set it to will place VPP's RX window in Case A
478 #
Neale Ranns3833ffd2019-03-21 14:34:09 +0000479 p.scapy_tra_sa.seq_num = 0x100000005
480 pkt = (Ether(src=self.tra_if.remote_mac,
481 dst=self.tra_if.local_mac) /
482 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
483 dst=self.tra_if.local_ip4) /
484 ICMP(),
485 seq_num=0x100000005))
486 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000487 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
Neale Ranns6afaae12019-07-17 15:07:14 +0000488
489 #
490 # A packet that has seq num between (2^32-64) and 5 is within
491 # the window
492 #
493 p.scapy_tra_sa.seq_num = 0xfffffffd
494 pkt = (Ether(src=self.tra_if.remote_mac,
495 dst=self.tra_if.local_mac) /
496 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
497 dst=self.tra_if.local_ip4) /
498 ICMP(),
499 seq_num=0xfffffffd))
500 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
501 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
502
503 #
504 # While in case A we cannot wrap the high sequence number again
505 # becuase VPP will consider this packet to be one that moves the
506 # window forward
507 #
508 pkt = (Ether(src=self.tra_if.remote_mac,
509 dst=self.tra_if.local_mac) /
510 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
511 dst=self.tra_if.local_ip4) /
512 ICMP(),
513 seq_num=0x200000999))
514 self.send_and_assert_no_replies(self.tra_if, [pkt], self.tra_if)
515
516 hash_failed_count += 1
517 self.assert_error_counter_equal(hash_failed_node_name,
518 hash_failed_count)
519
520 #
521 # but if we move the wondow forward to case B, then we can wrap
522 # again
523 #
524 p.scapy_tra_sa.seq_num = 0x100000555
525 pkt = (Ether(src=self.tra_if.remote_mac,
526 dst=self.tra_if.local_mac) /
527 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
528 dst=self.tra_if.local_ip4) /
529 ICMP(),
530 seq_num=0x100000555))
531 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
532 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
533
534 p.scapy_tra_sa.seq_num = 0x200000444
535 pkt = (Ether(src=self.tra_if.remote_mac,
536 dst=self.tra_if.local_mac) /
537 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
538 dst=self.tra_if.local_ip4) /
539 ICMP(),
540 seq_num=0x200000444))
541 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
542 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
543
Neale Ranns3833ffd2019-03-21 14:34:09 +0000544 else:
Neale Ranns6afaae12019-07-17 15:07:14 +0000545 #
546 # without ESN TX sequence numbers can't wrap and packets are
547 # dropped from here on out.
548 #
549 self.send_and_assert_no_replies(self.tra_if, pkts)
550 seq_cycle_count += len(pkts)
551 self.assert_error_counter_equal(seq_cycle_node_name,
552 seq_cycle_count)
Neale Ranns00a44202019-03-21 16:36:28 +0000553
Neale Rannsde847272018-11-28 01:38:34 -0800554 # move the security-associations seq number on to the last we used
Neale Ranns00a44202019-03-21 16:36:28 +0000555 self.vapi.cli("test ipsec sa %d seq 0x15f" % p.scapy_tra_sa_id)
Neale Rannsde847272018-11-28 01:38:34 -0800556 p.scapy_tra_sa.seq_num = 351
557 p.vpp_tra_sa.seq_num = 351
558
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000559 def verify_tra_basic4(self, count=1, payload_size=54):
Klement Sekera31da2e32018-06-24 22:49:55 +0200560 """ ipsec v4 transport basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100561 self.vapi.cli("clear errors")
Neale Ranns6afaae12019-07-17 15:07:14 +0000562 self.vapi.cli("clear ipsec sa")
Klement Sekera31da2e32018-06-24 22:49:55 +0200563 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200564 p = self.params[socket.AF_INET]
Neale Ranns28287212019-12-16 00:53:11 +0000565 send_pkts = self.gen_encrypt_pkts(p, p.scapy_tra_sa, self.tra_if,
Klement Sekera31da2e32018-06-24 22:49:55 +0200566 src=self.tra_if.remote_ip4,
567 dst=self.tra_if.local_ip4,
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000568 count=count,
569 payload_size=payload_size)
Klement Sekera31da2e32018-06-24 22:49:55 +0200570 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
Klement Sekera611864f2018-09-26 11:19:00 +0200571 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800572 for rx in recv_pkts:
Neale Ranns1b582b82019-04-18 19:49:13 -0700573 self.assertEqual(len(rx) - len(Ether()), rx[IP].len)
574 self.assert_packet_checksums_valid(rx)
Klement Sekera611864f2018-09-26 11:19:00 +0200575 try:
Neale Rannsde847272018-11-28 01:38:34 -0800576 decrypted = p.vpp_tra_sa.decrypt(rx[IP])
Klement Sekera611864f2018-09-26 11:19:00 +0200577 self.assert_packet_checksums_valid(decrypted)
578 except:
Neale Rannsde847272018-11-28 01:38:34 -0800579 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200580 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200581 finally:
582 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -0400583 self.logger.info(self.vapi.ppcli("show ipsec all"))
Klement Sekera31da2e32018-06-24 22:49:55 +0200584
Neale Rannseba31ec2019-02-17 18:04:27 +0000585 pkts = p.tra_sa_in.get_stats()['packets']
586 self.assertEqual(pkts, count,
587 "incorrect SA in counts: expected %d != %d" %
588 (count, pkts))
589 pkts = p.tra_sa_out.get_stats()['packets']
590 self.assertEqual(pkts, count,
591 "incorrect SA out counts: expected %d != %d" %
592 (count, pkts))
593
Klement Sekera10d066e2018-11-13 11:12:57 +0100594 self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count)
595 self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count)
596
Neale Ranns4f33c802019-04-10 12:39:10 +0000597
598class IpsecTra4Tests(IpsecTra4):
599 """ UT test methods for Transport v4 """
600 def test_tra_anti_replay(self):
Paul Vinciguerra6e20e032019-12-27 00:19:23 -0500601 """ ipsec v4 transport anti-replay test """
Neale Ranns6afaae12019-07-17 15:07:14 +0000602 self.verify_tra_anti_replay()
Neale Ranns4f33c802019-04-10 12:39:10 +0000603
604 def test_tra_basic(self, count=1):
605 """ ipsec v4 transport basic test """
606 self.verify_tra_basic4(count=1)
607
Klement Sekera31da2e32018-06-24 22:49:55 +0200608 def test_tra_burst(self):
609 """ ipsec v4 transport burst test """
Neale Ranns4f33c802019-04-10 12:39:10 +0000610 self.verify_tra_basic4(count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200611
Neale Ranns53f526b2019-02-25 14:32:02 +0000612
Neale Ranns4f33c802019-04-10 12:39:10 +0000613class IpsecTra6(object):
614 """ verify methods for Transport v6 """
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000615 def verify_tra_basic6(self, count=1, payload_size=54):
Klement Sekera10d066e2018-11-13 11:12:57 +0100616 self.vapi.cli("clear errors")
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000617 self.vapi.cli("clear ipsec sa")
Klement Sekera31da2e32018-06-24 22:49:55 +0200618 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200619 p = self.params[socket.AF_INET6]
Neale Ranns28287212019-12-16 00:53:11 +0000620 send_pkts = self.gen_encrypt_pkts6(p, p.scapy_tra_sa, self.tra_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200621 src=self.tra_if.remote_ip6,
622 dst=self.tra_if.local_ip6,
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000623 count=count,
624 payload_size=payload_size)
Klement Sekera611864f2018-09-26 11:19:00 +0200625 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
626 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800627 for rx in recv_pkts:
Neale Rannsd207fd72019-04-18 17:18:12 -0700628 self.assertEqual(len(rx) - len(Ether()) - len(IPv6()),
629 rx[IPv6].plen)
Klement Sekera611864f2018-09-26 11:19:00 +0200630 try:
Neale Rannsde847272018-11-28 01:38:34 -0800631 decrypted = p.vpp_tra_sa.decrypt(rx[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200632 self.assert_packet_checksums_valid(decrypted)
633 except:
Neale Rannsde847272018-11-28 01:38:34 -0800634 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200635 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200636 finally:
637 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -0400638 self.logger.info(self.vapi.ppcli("show ipsec all"))
Klement Sekera31da2e32018-06-24 22:49:55 +0200639
Neale Rannseba31ec2019-02-17 18:04:27 +0000640 pkts = p.tra_sa_in.get_stats()['packets']
641 self.assertEqual(pkts, count,
642 "incorrect SA in counts: expected %d != %d" %
643 (count, pkts))
644 pkts = p.tra_sa_out.get_stats()['packets']
645 self.assertEqual(pkts, count,
646 "incorrect SA out counts: expected %d != %d" %
647 (count, pkts))
Klement Sekera10d066e2018-11-13 11:12:57 +0100648 self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count)
649 self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count)
650
Neale Ranns02950402019-12-20 00:54:57 +0000651 def gen_encrypt_pkts_ext_hdrs6(self, sa, sw_intf, src, dst, count=1,
652 payload_size=54):
653 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
654 sa.encrypt(IPv6(src=src, dst=dst) /
655 ICMPv6EchoRequest(id=0, seq=1,
656 data='X' * payload_size))
657 for i in range(count)]
658
659 def gen_pkts_ext_hdrs6(self, sw_intf, src, dst, count=1, payload_size=54):
660 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
661 IPv6(src=src, dst=dst) /
662 IPv6ExtHdrHopByHop() /
663 IPv6ExtHdrFragment(id=2, offset=200) /
664 Raw(b'\xff' * 200)
665 for i in range(count)]
666
667 def verify_tra_encrypted6(self, p, sa, rxs):
668 decrypted = []
669 for rx in rxs:
670 self.assert_packet_checksums_valid(rx)
671 try:
672 decrypt_pkt = p.vpp_tra_sa.decrypt(rx[IPv6])
673 decrypted.append(decrypt_pkt)
674 self.assert_equal(decrypt_pkt.src, self.tra_if.local_ip6)
675 self.assert_equal(decrypt_pkt.dst, self.tra_if.remote_ip6)
676 except:
677 self.logger.debug(ppp("Unexpected packet:", rx))
678 try:
679 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
680 except:
681 pass
682 raise
683 return decrypted
684
685 def verify_tra_66_ext_hdrs(self, p):
686 count = 63
687
688 #
689 # check we can decrypt with options
690 #
691 tx = self.gen_encrypt_pkts_ext_hdrs6(p.scapy_tra_sa, self.tra_if,
692 src=self.tra_if.remote_ip6,
693 dst=self.tra_if.local_ip6,
694 count=count)
695 self.send_and_expect(self.tra_if, tx, self.tra_if)
696
697 #
698 # injecting a packet from ourselves to be routed of box is a hack
699 # but it matches an outbout policy, alors je ne regrette rien
700 #
701
702 # one extension before ESP
703 tx = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
704 IPv6(src=self.tra_if.local_ip6,
705 dst=self.tra_if.remote_ip6) /
706 IPv6ExtHdrFragment(id=2, offset=200) /
707 Raw(b'\xff' * 200))
708
709 rxs = self.send_and_expect(self.pg2, [tx], self.tra_if)
710 dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs)
711
712 for dc in dcs:
713 # for reasons i'm not going to investigate scapy does not
714 # created the correct headers after decrypt. but reparsing
715 # the ipv6 packet fixes it
716 dc = IPv6(raw(dc[IPv6]))
717 self.assert_equal(dc[IPv6ExtHdrFragment].id, 2)
718
719 # two extensions before ESP
720 tx = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
721 IPv6(src=self.tra_if.local_ip6,
722 dst=self.tra_if.remote_ip6) /
723 IPv6ExtHdrHopByHop() /
724 IPv6ExtHdrFragment(id=2, offset=200) /
725 Raw(b'\xff' * 200))
726
727 rxs = self.send_and_expect(self.pg2, [tx], self.tra_if)
728 dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs)
729
730 for dc in dcs:
731 dc = IPv6(raw(dc[IPv6]))
732 self.assertTrue(dc[IPv6ExtHdrHopByHop])
733 self.assert_equal(dc[IPv6ExtHdrFragment].id, 2)
734
735 # two extensions before ESP, one after
736 tx = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
737 IPv6(src=self.tra_if.local_ip6,
738 dst=self.tra_if.remote_ip6) /
739 IPv6ExtHdrHopByHop() /
740 IPv6ExtHdrFragment(id=2, offset=200) /
741 IPv6ExtHdrDestOpt() /
742 Raw(b'\xff' * 200))
743
744 rxs = self.send_and_expect(self.pg2, [tx], self.tra_if)
745 dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs)
746
747 for dc in dcs:
748 dc = IPv6(raw(dc[IPv6]))
749 self.assertTrue(dc[IPv6ExtHdrDestOpt])
750 self.assertTrue(dc[IPv6ExtHdrHopByHop])
751 self.assert_equal(dc[IPv6ExtHdrFragment].id, 2)
752
Neale Ranns4f33c802019-04-10 12:39:10 +0000753
754class IpsecTra6Tests(IpsecTra6):
755 """ UT test methods for Transport v6 """
756 def test_tra_basic6(self):
757 """ ipsec v6 transport basic test """
758 self.verify_tra_basic6(count=1)
759
Klement Sekera611864f2018-09-26 11:19:00 +0200760 def test_tra_burst6(self):
761 """ ipsec v6 transport burst test """
Neale Ranns4f33c802019-04-10 12:39:10 +0000762 self.verify_tra_basic6(count=257)
Klement Sekera31da2e32018-06-24 22:49:55 +0200763
Klement Sekera611864f2018-09-26 11:19:00 +0200764
Neale Ranns02950402019-12-20 00:54:57 +0000765class IpsecTra6ExtTests(IpsecTra6):
766 def test_tra_ext_hdrs_66(self):
767 """ ipsec 6o6 tra extension headers test """
768 self.verify_tra_66_ext_hdrs(self.params[socket.AF_INET6])
769
770
Neale Ranns53f526b2019-02-25 14:32:02 +0000771class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests):
Neale Ranns4f33c802019-04-10 12:39:10 +0000772 """ UT test methods for Transport v6 and v4"""
Neale Ranns53f526b2019-02-25 14:32:02 +0000773 pass
774
775
Neale Ranns2ac885c2019-03-20 18:24:43 +0000776class IpsecTun4(object):
Neale Ranns4f33c802019-04-10 12:39:10 +0000777 """ verify methods for Tunnel v4 """
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000778 def verify_counters4(self, p, count, n_frags=None, worker=None):
Klement Sekera6aa58b72019-05-16 14:34:55 +0200779 if not n_frags:
780 n_frags = count
Neale Ranns987aea82019-03-27 13:40:35 +0000781 if (hasattr(p, "spd_policy_in_any")):
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000782 pkts = p.spd_policy_in_any.get_stats(worker)['packets']
Neale Ranns987aea82019-03-27 13:40:35 +0000783 self.assertEqual(pkts, count,
784 "incorrect SPD any policy: expected %d != %d" %
785 (count, pkts))
786
787 if (hasattr(p, "tun_sa_in")):
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000788 pkts = p.tun_sa_in.get_stats(worker)['packets']
Neale Ranns987aea82019-03-27 13:40:35 +0000789 self.assertEqual(pkts, count,
790 "incorrect SA in counts: expected %d != %d" %
791 (count, pkts))
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000792 pkts = p.tun_sa_out.get_stats(worker)['packets']
Neale Ranns987aea82019-03-27 13:40:35 +0000793 self.assertEqual(pkts, count,
794 "incorrect SA out counts: expected %d != %d" %
795 (count, pkts))
796
Klement Sekera6aa58b72019-05-16 14:34:55 +0200797 self.assert_packet_counter_equal(self.tun4_encrypt_node_name, n_frags)
Neale Ranns987aea82019-03-27 13:40:35 +0000798 self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count)
799
Neale Rannsf05e7322019-03-29 20:23:58 +0000800 def verify_decrypted(self, p, rxs):
801 for rx in rxs:
802 self.assert_equal(rx[IP].src, p.remote_tun_if_host)
803 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
804 self.assert_packet_checksums_valid(rx)
805
806 def verify_encrypted(self, p, sa, rxs):
807 decrypt_pkts = []
808 for rx in rxs:
Neale Ranns41afb332019-07-16 06:19:35 -0700809 if p.nat_header:
810 self.assertEqual(rx[UDP].dport, 4500)
Neale Ranns1b582b82019-04-18 19:49:13 -0700811 self.assert_packet_checksums_valid(rx)
812 self.assertEqual(len(rx) - len(Ether()), rx[IP].len)
Neale Rannsf05e7322019-03-29 20:23:58 +0000813 try:
814 decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IP])
815 if not decrypt_pkt.haslayer(IP):
816 decrypt_pkt = IP(decrypt_pkt[Raw].load)
817 decrypt_pkts.append(decrypt_pkt)
818 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
819 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
820 except:
821 self.logger.debug(ppp("Unexpected packet:", rx))
822 try:
823 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
824 except:
825 pass
826 raise
827 pkts = reassemble4(decrypt_pkts)
828 for pkt in pkts:
829 self.assert_packet_checksums_valid(pkt)
830
Neale Rannsd7603d92019-03-28 08:56:10 +0000831 def verify_tun_44(self, p, count=1, payload_size=64, n_rx=None):
Klement Sekera10d066e2018-11-13 11:12:57 +0100832 self.vapi.cli("clear errors")
Neale Ranns02950402019-12-20 00:54:57 +0000833 self.vapi.cli("clear ipsec counters")
Neale Ranns28287212019-12-16 00:53:11 +0000834 self.vapi.cli("clear ipsec sa")
Neale Rannsd7603d92019-03-28 08:56:10 +0000835 if not n_rx:
836 n_rx = count
Klement Sekera31da2e32018-06-24 22:49:55 +0200837 try:
Neale Ranns28287212019-12-16 00:53:11 +0000838 send_pkts = self.gen_encrypt_pkts(p, p.scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200839 src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200840 dst=self.pg1.remote_ip4,
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000841 count=count,
842 payload_size=payload_size)
Klement Sekera611864f2018-09-26 11:19:00 +0200843 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Neale Rannsf05e7322019-03-29 20:23:58 +0000844 self.verify_decrypted(p, recv_pkts)
845
Klement Sekera31da2e32018-06-24 22:49:55 +0200846 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
Neale Rannsd7603d92019-03-28 08:56:10 +0000847 dst=p.remote_tun_if_host, count=count,
848 payload_size=payload_size)
849 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
850 self.tun_if, n_rx)
Neale Rannsf05e7322019-03-29 20:23:58 +0000851 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
852
Neale Rannsf3a66222020-01-02 05:04:00 +0000853 for rx in recv_pkts:
854 self.assertEqual(rx[IP].src, p.tun_src)
855 self.assertEqual(rx[IP].dst, p.tun_dst)
856
Klement Sekera31da2e32018-06-24 22:49:55 +0200857 finally:
858 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -0400859 self.logger.info(self.vapi.ppcli("show ipsec all"))
Klement Sekera31da2e32018-06-24 22:49:55 +0200860
Neale Ranns02950402019-12-20 00:54:57 +0000861 self.logger.info(self.vapi.ppcli("show ipsec sa 0"))
862 self.logger.info(self.vapi.ppcli("show ipsec sa 4"))
Klement Sekera6aa58b72019-05-16 14:34:55 +0200863 self.verify_counters4(p, count, n_rx)
Neale Rannseba31ec2019-02-17 18:04:27 +0000864
Neale Ranns28287212019-12-16 00:53:11 +0000865 def verify_tun_dropped_44(self, p, count=1, payload_size=64, n_rx=None):
866 self.vapi.cli("clear errors")
867 if not n_rx:
868 n_rx = count
869 try:
870 send_pkts = self.gen_encrypt_pkts(p, p.scapy_tun_sa, self.tun_if,
871 src=p.remote_tun_if_host,
872 dst=self.pg1.remote_ip4,
873 count=count)
874 self.send_and_assert_no_replies(self.tun_if, send_pkts)
875
876 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
877 dst=p.remote_tun_if_host, count=count,
878 payload_size=payload_size)
879 self.send_and_assert_no_replies(self.pg1, send_pkts)
880
881 finally:
882 self.logger.info(self.vapi.ppcli("show error"))
883 self.logger.info(self.vapi.ppcli("show ipsec all"))
884
Neale Ranns14046982019-07-29 14:49:52 +0000885 def verify_tun_reass_44(self, p):
886 self.vapi.cli("clear errors")
887 self.vapi.ip_reassembly_enable_disable(
888 sw_if_index=self.tun_if.sw_if_index, enable_ip4=True)
889
890 try:
Neale Ranns28287212019-12-16 00:53:11 +0000891 send_pkts = self.gen_encrypt_pkts(p, p.scapy_tun_sa, self.tun_if,
Neale Ranns14046982019-07-29 14:49:52 +0000892 src=p.remote_tun_if_host,
893 dst=self.pg1.remote_ip4,
894 payload_size=1900,
895 count=1)
896 send_pkts = fragment_rfc791(send_pkts[0], 1400)
897 recv_pkts = self.send_and_expect(self.tun_if, send_pkts,
898 self.pg1, n_rx=1)
899 self.verify_decrypted(p, recv_pkts)
900
901 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
902 dst=p.remote_tun_if_host, count=1)
903 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
904 self.tun_if)
905 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
906
907 finally:
908 self.logger.info(self.vapi.ppcli("show error"))
909 self.logger.info(self.vapi.ppcli("show ipsec all"))
910
911 self.verify_counters4(p, 1, 1)
912 self.vapi.ip_reassembly_enable_disable(
913 sw_if_index=self.tun_if.sw_if_index, enable_ip4=False)
914
Neale Ranns987aea82019-03-27 13:40:35 +0000915 def verify_tun_64(self, p, count=1):
916 self.vapi.cli("clear errors")
917 try:
Neale Ranns28287212019-12-16 00:53:11 +0000918 send_pkts = self.gen_encrypt_pkts6(p, p.scapy_tun_sa, self.tun_if,
Neale Ranns987aea82019-03-27 13:40:35 +0000919 src=p.remote_tun_if_host6,
920 dst=self.pg1.remote_ip6,
921 count=count)
922 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
923 for recv_pkt in recv_pkts:
924 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host6)
925 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
926 self.assert_packet_checksums_valid(recv_pkt)
927 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
928 dst=p.remote_tun_if_host6, count=count)
929 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
930 for recv_pkt in recv_pkts:
931 try:
932 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP])
933 if not decrypt_pkt.haslayer(IPv6):
934 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
935 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
936 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host6)
937 self.assert_packet_checksums_valid(decrypt_pkt)
938 except:
939 self.logger.error(ppp("Unexpected packet:", recv_pkt))
940 try:
941 self.logger.debug(
942 ppp("Decrypted packet:", decrypt_pkt))
943 except:
944 pass
945 raise
946 finally:
947 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -0400948 self.logger.info(self.vapi.ppcli("show ipsec all"))
Neale Rannseba31ec2019-02-17 18:04:27 +0000949
Klement Sekera6aa58b72019-05-16 14:34:55 +0200950 self.verify_counters4(p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +0100951
Neale Ranns41afb332019-07-16 06:19:35 -0700952 def verify_keepalive(self, p):
953 pkt = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
954 IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4) /
955 UDP(sport=333, dport=4500) /
Ole Troan8b76c232019-10-21 20:55:13 +0200956 Raw(b'\xff'))
Neale Ranns41afb332019-07-16 06:19:35 -0700957 self.send_and_assert_no_replies(self.tun_if, pkt*31)
958 self.assert_error_counter_equal(
959 '/err/%s/NAT Keepalive' % self.tun4_input_node, 31)
960
961 pkt = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
962 IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4) /
963 UDP(sport=333, dport=4500) /
Ole Troan8b76c232019-10-21 20:55:13 +0200964 Raw(b'\xfe'))
Neale Ranns41afb332019-07-16 06:19:35 -0700965 self.send_and_assert_no_replies(self.tun_if, pkt*31)
966 self.assert_error_counter_equal(
967 '/err/%s/Too Short' % self.tun4_input_node, 31)
968
Neale Ranns2ac885c2019-03-20 18:24:43 +0000969
970class IpsecTun4Tests(IpsecTun4):
Neale Ranns4f33c802019-04-10 12:39:10 +0000971 """ UT test methods for Tunnel v4 """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000972 def test_tun_basic44(self):
973 """ ipsec 4o4 tunnel basic test """
974 self.verify_tun_44(self.params[socket.AF_INET], count=1)
Neale Ranns02950402019-12-20 00:54:57 +0000975 self.tun_if.admin_down()
976 self.tun_if.resolve_arp()
977 self.tun_if.admin_up()
978 self.verify_tun_44(self.params[socket.AF_INET], count=1)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000979
Neale Ranns14046982019-07-29 14:49:52 +0000980 def test_tun_reass_basic44(self):
981 """ ipsec 4o4 tunnel basic reassembly test """
982 self.verify_tun_reass_44(self.params[socket.AF_INET])
983
Klement Sekera611864f2018-09-26 11:19:00 +0200984 def test_tun_burst44(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200985 """ ipsec 4o4 tunnel burst test """
Neale Ranns02950402019-12-20 00:54:57 +0000986 self.verify_tun_44(self.params[socket.AF_INET], count=127)
987
988
Neale Ranns2ac885c2019-03-20 18:24:43 +0000989class IpsecTun6(object):
Neale Ranns4f33c802019-04-10 12:39:10 +0000990 """ verify methods for Tunnel v6 """
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000991 def verify_counters6(self, p_in, p_out, count, worker=None):
Neale Rannsc87b66c2019-02-07 07:26:12 -0800992 if (hasattr(p_in, "tun_sa_in")):
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000993 pkts = p_in.tun_sa_in.get_stats(worker)['packets']
Neale Ranns987aea82019-03-27 13:40:35 +0000994 self.assertEqual(pkts, count,
995 "incorrect SA in counts: expected %d != %d" %
996 (count, pkts))
Neale Rannsc87b66c2019-02-07 07:26:12 -0800997 if (hasattr(p_out, "tun_sa_out")):
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000998 pkts = p_out.tun_sa_out.get_stats(worker)['packets']
Neale Ranns987aea82019-03-27 13:40:35 +0000999 self.assertEqual(pkts, count,
1000 "incorrect SA out counts: expected %d != %d" %
1001 (count, pkts))
1002 self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count)
1003 self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count)
1004
Neale Rannsc87b66c2019-02-07 07:26:12 -08001005 def verify_decrypted6(self, p, rxs):
1006 for rx in rxs:
1007 self.assert_equal(rx[IPv6].src, p.remote_tun_if_host)
1008 self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6)
1009 self.assert_packet_checksums_valid(rx)
1010
1011 def verify_encrypted6(self, p, sa, rxs):
1012 for rx in rxs:
1013 self.assert_packet_checksums_valid(rx)
1014 self.assertEqual(len(rx) - len(Ether()) - len(IPv6()),
1015 rx[IPv6].plen)
1016 try:
1017 decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IPv6])
1018 if not decrypt_pkt.haslayer(IPv6):
1019 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
1020 self.assert_packet_checksums_valid(decrypt_pkt)
1021 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
1022 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
1023 except:
1024 self.logger.debug(ppp("Unexpected packet:", rx))
1025 try:
1026 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
1027 except:
1028 pass
1029 raise
1030
1031 def verify_drop_tun_66(self, p_in, count=1, payload_size=64):
Klement Sekera10d066e2018-11-13 11:12:57 +01001032 self.vapi.cli("clear errors")
Neale Rannsc87b66c2019-02-07 07:26:12 -08001033 self.vapi.cli("clear ipsec sa")
1034
Neale Ranns28287212019-12-16 00:53:11 +00001035 send_pkts = self.gen_encrypt_pkts6(p_in, p_in.scapy_tun_sa,
1036 self.tun_if,
Neale Rannsc87b66c2019-02-07 07:26:12 -08001037 src=p_in.remote_tun_if_host,
1038 dst=self.pg1.remote_ip6,
1039 count=count)
1040 self.send_and_assert_no_replies(self.tun_if, send_pkts)
1041 self.logger.info(self.vapi.cli("sh punt stats"))
1042
1043 def verify_tun_66(self, p_in, p_out=None, count=1, payload_size=64):
1044 self.vapi.cli("clear errors")
1045 self.vapi.cli("clear ipsec sa")
1046 if not p_out:
1047 p_out = p_in
Klement Sekera31da2e32018-06-24 22:49:55 +02001048 try:
Neale Ranns28287212019-12-16 00:53:11 +00001049 send_pkts = self.gen_encrypt_pkts6(p_in, p_in.scapy_tun_sa,
1050 self.tun_if,
Neale Rannsc87b66c2019-02-07 07:26:12 -08001051 src=p_in.remote_tun_if_host,
Klement Sekera611864f2018-09-26 11:19:00 +02001052 dst=self.pg1.remote_ip6,
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001053 count=count,
1054 payload_size=payload_size)
Klement Sekera611864f2018-09-26 11:19:00 +02001055 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Neale Rannsc87b66c2019-02-07 07:26:12 -08001056 self.verify_decrypted6(p_in, recv_pkts)
1057
Klement Sekera611864f2018-09-26 11:19:00 +02001058 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
Neale Rannsc87b66c2019-02-07 07:26:12 -08001059 dst=p_out.remote_tun_if_host,
1060 count=count,
1061 payload_size=payload_size)
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001062 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
Neale Rannsc87b66c2019-02-07 07:26:12 -08001063 self.verify_encrypted6(p_out, p_out.vpp_tun_sa, recv_pkts)
1064
Neale Rannsf3a66222020-01-02 05:04:00 +00001065 for rx in recv_pkts:
1066 self.assertEqual(rx[IPv6].src, p_out.tun_src)
1067 self.assertEqual(rx[IPv6].dst, p_out.tun_dst)
1068
Klement Sekera31da2e32018-06-24 22:49:55 +02001069 finally:
1070 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -04001071 self.logger.info(self.vapi.ppcli("show ipsec all"))
Neale Rannsc87b66c2019-02-07 07:26:12 -08001072 self.verify_counters6(p_in, p_out, count)
Klement Sekera31da2e32018-06-24 22:49:55 +02001073
Neale Ranns14046982019-07-29 14:49:52 +00001074 def verify_tun_reass_66(self, p):
1075 self.vapi.cli("clear errors")
1076 self.vapi.ip_reassembly_enable_disable(
1077 sw_if_index=self.tun_if.sw_if_index, enable_ip6=True)
1078
1079 try:
Neale Ranns28287212019-12-16 00:53:11 +00001080 send_pkts = self.gen_encrypt_pkts6(p, p.scapy_tun_sa, self.tun_if,
Neale Ranns14046982019-07-29 14:49:52 +00001081 src=p.remote_tun_if_host,
1082 dst=self.pg1.remote_ip6,
1083 count=1,
Neale Ranns02950402019-12-20 00:54:57 +00001084 payload_size=1850)
Neale Ranns14046982019-07-29 14:49:52 +00001085 send_pkts = fragment_rfc8200(send_pkts[0], 1, 1400, self.logger)
1086 recv_pkts = self.send_and_expect(self.tun_if, send_pkts,
1087 self.pg1, n_rx=1)
1088 self.verify_decrypted6(p, recv_pkts)
1089
1090 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
1091 dst=p.remote_tun_if_host,
1092 count=1,
1093 payload_size=64)
1094 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
1095 self.tun_if)
1096 self.verify_encrypted6(p, p.vpp_tun_sa, recv_pkts)
1097 finally:
1098 self.logger.info(self.vapi.ppcli("show error"))
1099 self.logger.info(self.vapi.ppcli("show ipsec all"))
1100 self.verify_counters6(p, p, 1)
1101 self.vapi.ip_reassembly_enable_disable(
1102 sw_if_index=self.tun_if.sw_if_index, enable_ip6=False)
1103
Neale Ranns987aea82019-03-27 13:40:35 +00001104 def verify_tun_46(self, p, count=1):
1105 """ ipsec 4o6 tunnel basic test """
1106 self.vapi.cli("clear errors")
1107 try:
Neale Ranns28287212019-12-16 00:53:11 +00001108 send_pkts = self.gen_encrypt_pkts(p, p.scapy_tun_sa, self.tun_if,
Neale Ranns987aea82019-03-27 13:40:35 +00001109 src=p.remote_tun_if_host4,
1110 dst=self.pg1.remote_ip4,
1111 count=count)
1112 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
1113 for recv_pkt in recv_pkts:
1114 self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host4)
1115 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
1116 self.assert_packet_checksums_valid(recv_pkt)
1117 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
1118 dst=p.remote_tun_if_host4,
1119 count=count)
1120 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
1121 for recv_pkt in recv_pkts:
1122 try:
1123 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6])
1124 if not decrypt_pkt.haslayer(IP):
1125 decrypt_pkt = IP(decrypt_pkt[Raw].load)
1126 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
1127 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host4)
1128 self.assert_packet_checksums_valid(decrypt_pkt)
1129 except:
1130 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
1131 try:
1132 self.logger.debug(ppp("Decrypted packet:",
1133 decrypt_pkt))
1134 except:
1135 pass
1136 raise
1137 finally:
1138 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -04001139 self.logger.info(self.vapi.ppcli("show ipsec all"))
Neale Rannsc87b66c2019-02-07 07:26:12 -08001140 self.verify_counters6(p, p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +01001141
Neale Ranns2ac885c2019-03-20 18:24:43 +00001142
1143class IpsecTun6Tests(IpsecTun6):
Neale Ranns4f33c802019-04-10 12:39:10 +00001144 """ UT test methods for Tunnel v6 """
Neale Ranns2ac885c2019-03-20 18:24:43 +00001145
1146 def test_tun_basic66(self):
1147 """ ipsec 6o6 tunnel basic test """
1148 self.verify_tun_66(self.params[socket.AF_INET6], count=1)
1149
Neale Ranns14046982019-07-29 14:49:52 +00001150 def test_tun_reass_basic66(self):
1151 """ ipsec 6o6 tunnel basic reassembly test """
1152 self.verify_tun_reass_66(self.params[socket.AF_INET6])
1153
Klement Sekera611864f2018-09-26 11:19:00 +02001154 def test_tun_burst66(self):
1155 """ ipsec 6o6 tunnel burst test """
Neale Ranns2ac885c2019-03-20 18:24:43 +00001156 self.verify_tun_66(self.params[socket.AF_INET6], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +02001157
1158
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001159class IpsecTun6HandoffTests(IpsecTun6):
1160 """ UT test methods for Tunnel v6 with multiple workers """
1161 worker_config = "workers 2"
1162
1163 def test_tun_handoff_66(self):
1164 """ ipsec 6o6 tunnel worker hand-off test """
1165 N_PKTS = 15
1166 p = self.params[socket.AF_INET6]
1167
1168 # inject alternately on worker 0 and 1. all counts on the SA
1169 # should be against worker 0
1170 for worker in [0, 1, 0, 1]:
Neale Ranns28287212019-12-16 00:53:11 +00001171 send_pkts = self.gen_encrypt_pkts6(p, p.scapy_tun_sa, self.tun_if,
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001172 src=p.remote_tun_if_host,
1173 dst=self.pg1.remote_ip6,
1174 count=N_PKTS)
1175 recv_pkts = self.send_and_expect(self.tun_if, send_pkts,
1176 self.pg1, worker=worker)
1177 self.verify_decrypted6(p, recv_pkts)
1178
1179 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
1180 dst=p.remote_tun_if_host,
1181 count=N_PKTS)
1182 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
1183 self.tun_if, worker=worker)
1184 self.verify_encrypted6(p, p.vpp_tun_sa, recv_pkts)
1185
1186 # all counts against the first worker that was used
1187 self.verify_counters6(p, p, 4*N_PKTS, worker=0)
1188
1189
1190class IpsecTun4HandoffTests(IpsecTun4):
1191 """ UT test methods for Tunnel v4 with multiple workers """
1192 worker_config = "workers 2"
1193
1194 def test_tun_handooff_44(self):
1195 """ ipsec 4o4 tunnel worker hand-off test """
1196 N_PKTS = 15
1197 p = self.params[socket.AF_INET]
1198
1199 # inject alternately on worker 0 and 1. all counts on the SA
1200 # should be against worker 0
1201 for worker in [0, 1, 0, 1]:
Neale Ranns28287212019-12-16 00:53:11 +00001202 send_pkts = self.gen_encrypt_pkts(p, p.scapy_tun_sa, self.tun_if,
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001203 src=p.remote_tun_if_host,
1204 dst=self.pg1.remote_ip4,
1205 count=N_PKTS)
1206 recv_pkts = self.send_and_expect(self.tun_if, send_pkts,
1207 self.pg1, worker=worker)
1208 self.verify_decrypted(p, recv_pkts)
1209
1210 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
1211 dst=p.remote_tun_if_host,
1212 count=N_PKTS)
1213 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
1214 self.tun_if, worker=worker)
1215 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
1216
1217 # all counts against the first worker that was used
1218 self.verify_counters4(p, 4*N_PKTS, worker=0)
1219
1220
Neale Ranns53f526b2019-02-25 14:32:02 +00001221class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests):
Neale Ranns4f33c802019-04-10 12:39:10 +00001222 """ UT test methods for Tunnel v6 & v4 """
Klement Sekera611864f2018-09-26 11:19:00 +02001223 pass
1224
Klement Sekera31da2e32018-06-24 22:49:55 +02001225
1226if __name__ == '__main__':
1227 unittest.main(testRunner=VppTestRunner)