blob: 2eeb63c16d1ddeb77576013477a766baa4133270 [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
8from scapy.packet import 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 Ranns17dcec02019-01-09 21:22:20 -080030 self.scapy_tun_sa_id = 10
31 self.scapy_tun_spi = 1001
32 self.vpp_tun_sa_id = 20
33 self.vpp_tun_spi = 1000
Klement Sekera611864f2018-09-26 11:19:00 +020034
Neale Ranns17dcec02019-01-09 21:22:20 -080035 self.scapy_tra_sa_id = 30
36 self.scapy_tra_spi = 2001
37 self.vpp_tra_sa_id = 40
38 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 Ranns17dcec02019-01-09 21:22:20 -080066 self.scapy_tun_sa_id = 50
67 self.scapy_tun_spi = 3001
68 self.vpp_tun_sa_id = 60
69 self.vpp_tun_spi = 3000
Klement Sekera611864f2018-09-26 11:19:00 +020070
Neale Ranns17dcec02019-01-09 21:22:20 -080071 self.scapy_tra_sa_id = 70
72 self.scapy_tra_spi = 4001
73 self.vpp_tra_sa_id = 80
74 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 Rannsd7603d92019-03-28 08:56:10 +0000227 def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1,
228 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 Rannsd7603d92019-03-28 08:56:10 +0000234 def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1,
235 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
Neale Ranns4f33c802019-04-10 12:39:10 +0000559 def verify_tra_basic4(self, count=1):
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 Rannsde847272018-11-28 01:38:34 -0800565 send_pkts = self.gen_encrypt_pkts(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,
568 count=count)
569 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
Klement Sekera611864f2018-09-26 11:19:00 +0200570 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800571 for rx in recv_pkts:
Neale Ranns1b582b82019-04-18 19:49:13 -0700572 self.assertEqual(len(rx) - len(Ether()), rx[IP].len)
573 self.assert_packet_checksums_valid(rx)
Klement Sekera611864f2018-09-26 11:19:00 +0200574 try:
Neale Rannsde847272018-11-28 01:38:34 -0800575 decrypted = p.vpp_tra_sa.decrypt(rx[IP])
Klement Sekera611864f2018-09-26 11:19:00 +0200576 self.assert_packet_checksums_valid(decrypted)
577 except:
Neale Rannsde847272018-11-28 01:38:34 -0800578 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200579 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200580 finally:
581 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -0400582 self.logger.info(self.vapi.ppcli("show ipsec all"))
Klement Sekera31da2e32018-06-24 22:49:55 +0200583
Neale Rannseba31ec2019-02-17 18:04:27 +0000584 pkts = p.tra_sa_in.get_stats()['packets']
585 self.assertEqual(pkts, count,
586 "incorrect SA in counts: expected %d != %d" %
587 (count, pkts))
588 pkts = p.tra_sa_out.get_stats()['packets']
589 self.assertEqual(pkts, count,
590 "incorrect SA out counts: expected %d != %d" %
591 (count, pkts))
592
Klement Sekera10d066e2018-11-13 11:12:57 +0100593 self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count)
594 self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count)
595
Neale Ranns4f33c802019-04-10 12:39:10 +0000596
597class IpsecTra4Tests(IpsecTra4):
598 """ UT test methods for Transport v4 """
599 def test_tra_anti_replay(self):
Paul Vinciguerra6e20e032019-12-27 00:19:23 -0500600 """ ipsec v4 transport anti-replay test """
Neale Ranns6afaae12019-07-17 15:07:14 +0000601 self.verify_tra_anti_replay()
Neale Ranns4f33c802019-04-10 12:39:10 +0000602
603 def test_tra_basic(self, count=1):
604 """ ipsec v4 transport basic test """
605 self.verify_tra_basic4(count=1)
606
Klement Sekera31da2e32018-06-24 22:49:55 +0200607 def test_tra_burst(self):
608 """ ipsec v4 transport burst test """
Neale Ranns4f33c802019-04-10 12:39:10 +0000609 self.verify_tra_basic4(count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200610
Neale Ranns53f526b2019-02-25 14:32:02 +0000611
Neale Ranns4f33c802019-04-10 12:39:10 +0000612class IpsecTra6(object):
613 """ verify methods for Transport v6 """
614 def verify_tra_basic6(self, count=1):
Klement Sekera10d066e2018-11-13 11:12:57 +0100615 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200616 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200617 p = self.params[socket.AF_INET6]
Neale Rannsde847272018-11-28 01:38:34 -0800618 send_pkts = self.gen_encrypt_pkts6(p.scapy_tra_sa, self.tra_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200619 src=self.tra_if.remote_ip6,
620 dst=self.tra_if.local_ip6,
621 count=count)
622 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
623 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800624 for rx in recv_pkts:
Neale Rannsd207fd72019-04-18 17:18:12 -0700625 self.assertEqual(len(rx) - len(Ether()) - len(IPv6()),
626 rx[IPv6].plen)
Klement Sekera611864f2018-09-26 11:19:00 +0200627 try:
Neale Rannsde847272018-11-28 01:38:34 -0800628 decrypted = p.vpp_tra_sa.decrypt(rx[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200629 self.assert_packet_checksums_valid(decrypted)
630 except:
Neale Rannsde847272018-11-28 01:38:34 -0800631 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200632 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200633 finally:
634 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -0400635 self.logger.info(self.vapi.ppcli("show ipsec all"))
Klement Sekera31da2e32018-06-24 22:49:55 +0200636
Neale Rannseba31ec2019-02-17 18:04:27 +0000637 pkts = p.tra_sa_in.get_stats()['packets']
638 self.assertEqual(pkts, count,
639 "incorrect SA in counts: expected %d != %d" %
640 (count, pkts))
641 pkts = p.tra_sa_out.get_stats()['packets']
642 self.assertEqual(pkts, count,
643 "incorrect SA out counts: expected %d != %d" %
644 (count, pkts))
Klement Sekera10d066e2018-11-13 11:12:57 +0100645 self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count)
646 self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count)
647
Neale Ranns02950402019-12-20 00:54:57 +0000648 def gen_encrypt_pkts_ext_hdrs6(self, sa, sw_intf, src, dst, count=1,
649 payload_size=54):
650 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
651 sa.encrypt(IPv6(src=src, dst=dst) /
652 ICMPv6EchoRequest(id=0, seq=1,
653 data='X' * payload_size))
654 for i in range(count)]
655
656 def gen_pkts_ext_hdrs6(self, sw_intf, src, dst, count=1, payload_size=54):
657 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
658 IPv6(src=src, dst=dst) /
659 IPv6ExtHdrHopByHop() /
660 IPv6ExtHdrFragment(id=2, offset=200) /
661 Raw(b'\xff' * 200)
662 for i in range(count)]
663
664 def verify_tra_encrypted6(self, p, sa, rxs):
665 decrypted = []
666 for rx in rxs:
667 self.assert_packet_checksums_valid(rx)
668 try:
669 decrypt_pkt = p.vpp_tra_sa.decrypt(rx[IPv6])
670 decrypted.append(decrypt_pkt)
671 self.assert_equal(decrypt_pkt.src, self.tra_if.local_ip6)
672 self.assert_equal(decrypt_pkt.dst, self.tra_if.remote_ip6)
673 except:
674 self.logger.debug(ppp("Unexpected packet:", rx))
675 try:
676 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
677 except:
678 pass
679 raise
680 return decrypted
681
682 def verify_tra_66_ext_hdrs(self, p):
683 count = 63
684
685 #
686 # check we can decrypt with options
687 #
688 tx = self.gen_encrypt_pkts_ext_hdrs6(p.scapy_tra_sa, self.tra_if,
689 src=self.tra_if.remote_ip6,
690 dst=self.tra_if.local_ip6,
691 count=count)
692 self.send_and_expect(self.tra_if, tx, self.tra_if)
693
694 #
695 # injecting a packet from ourselves to be routed of box is a hack
696 # but it matches an outbout policy, alors je ne regrette rien
697 #
698
699 # one extension before ESP
700 tx = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
701 IPv6(src=self.tra_if.local_ip6,
702 dst=self.tra_if.remote_ip6) /
703 IPv6ExtHdrFragment(id=2, offset=200) /
704 Raw(b'\xff' * 200))
705
706 rxs = self.send_and_expect(self.pg2, [tx], self.tra_if)
707 dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs)
708
709 for dc in dcs:
710 # for reasons i'm not going to investigate scapy does not
711 # created the correct headers after decrypt. but reparsing
712 # the ipv6 packet fixes it
713 dc = IPv6(raw(dc[IPv6]))
714 self.assert_equal(dc[IPv6ExtHdrFragment].id, 2)
715
716 # two extensions before ESP
717 tx = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
718 IPv6(src=self.tra_if.local_ip6,
719 dst=self.tra_if.remote_ip6) /
720 IPv6ExtHdrHopByHop() /
721 IPv6ExtHdrFragment(id=2, offset=200) /
722 Raw(b'\xff' * 200))
723
724 rxs = self.send_and_expect(self.pg2, [tx], self.tra_if)
725 dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs)
726
727 for dc in dcs:
728 dc = IPv6(raw(dc[IPv6]))
729 self.assertTrue(dc[IPv6ExtHdrHopByHop])
730 self.assert_equal(dc[IPv6ExtHdrFragment].id, 2)
731
732 # two extensions before ESP, one after
733 tx = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
734 IPv6(src=self.tra_if.local_ip6,
735 dst=self.tra_if.remote_ip6) /
736 IPv6ExtHdrHopByHop() /
737 IPv6ExtHdrFragment(id=2, offset=200) /
738 IPv6ExtHdrDestOpt() /
739 Raw(b'\xff' * 200))
740
741 rxs = self.send_and_expect(self.pg2, [tx], self.tra_if)
742 dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs)
743
744 for dc in dcs:
745 dc = IPv6(raw(dc[IPv6]))
746 self.assertTrue(dc[IPv6ExtHdrDestOpt])
747 self.assertTrue(dc[IPv6ExtHdrHopByHop])
748 self.assert_equal(dc[IPv6ExtHdrFragment].id, 2)
749
Neale Ranns4f33c802019-04-10 12:39:10 +0000750
751class IpsecTra6Tests(IpsecTra6):
752 """ UT test methods for Transport v6 """
753 def test_tra_basic6(self):
754 """ ipsec v6 transport basic test """
755 self.verify_tra_basic6(count=1)
756
Klement Sekera611864f2018-09-26 11:19:00 +0200757 def test_tra_burst6(self):
758 """ ipsec v6 transport burst test """
Neale Ranns4f33c802019-04-10 12:39:10 +0000759 self.verify_tra_basic6(count=257)
Klement Sekera31da2e32018-06-24 22:49:55 +0200760
Klement Sekera611864f2018-09-26 11:19:00 +0200761
Neale Ranns02950402019-12-20 00:54:57 +0000762class IpsecTra6ExtTests(IpsecTra6):
763 def test_tra_ext_hdrs_66(self):
764 """ ipsec 6o6 tra extension headers test """
765 self.verify_tra_66_ext_hdrs(self.params[socket.AF_INET6])
766
767
Neale Ranns53f526b2019-02-25 14:32:02 +0000768class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests):
Neale Ranns4f33c802019-04-10 12:39:10 +0000769 """ UT test methods for Transport v6 and v4"""
Neale Ranns53f526b2019-02-25 14:32:02 +0000770 pass
771
772
Neale Ranns2ac885c2019-03-20 18:24:43 +0000773class IpsecTun4(object):
Neale Ranns4f33c802019-04-10 12:39:10 +0000774 """ verify methods for Tunnel v4 """
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000775 def verify_counters4(self, p, count, n_frags=None, worker=None):
Klement Sekera6aa58b72019-05-16 14:34:55 +0200776 if not n_frags:
777 n_frags = count
Neale Ranns987aea82019-03-27 13:40:35 +0000778 if (hasattr(p, "spd_policy_in_any")):
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000779 pkts = p.spd_policy_in_any.get_stats(worker)['packets']
Neale Ranns987aea82019-03-27 13:40:35 +0000780 self.assertEqual(pkts, count,
781 "incorrect SPD any policy: expected %d != %d" %
782 (count, pkts))
783
784 if (hasattr(p, "tun_sa_in")):
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000785 pkts = p.tun_sa_in.get_stats(worker)['packets']
Neale Ranns987aea82019-03-27 13:40:35 +0000786 self.assertEqual(pkts, count,
787 "incorrect SA in counts: expected %d != %d" %
788 (count, pkts))
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000789 pkts = p.tun_sa_out.get_stats(worker)['packets']
Neale Ranns987aea82019-03-27 13:40:35 +0000790 self.assertEqual(pkts, count,
791 "incorrect SA out counts: expected %d != %d" %
792 (count, pkts))
793
Klement Sekera6aa58b72019-05-16 14:34:55 +0200794 self.assert_packet_counter_equal(self.tun4_encrypt_node_name, n_frags)
Neale Ranns987aea82019-03-27 13:40:35 +0000795 self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count)
796
Neale Rannsf05e7322019-03-29 20:23:58 +0000797 def verify_decrypted(self, p, rxs):
798 for rx in rxs:
799 self.assert_equal(rx[IP].src, p.remote_tun_if_host)
800 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
801 self.assert_packet_checksums_valid(rx)
802
803 def verify_encrypted(self, p, sa, rxs):
804 decrypt_pkts = []
805 for rx in rxs:
Neale Ranns41afb332019-07-16 06:19:35 -0700806 if p.nat_header:
807 self.assertEqual(rx[UDP].dport, 4500)
Neale Ranns1b582b82019-04-18 19:49:13 -0700808 self.assert_packet_checksums_valid(rx)
809 self.assertEqual(len(rx) - len(Ether()), rx[IP].len)
Neale Rannsf05e7322019-03-29 20:23:58 +0000810 try:
811 decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IP])
812 if not decrypt_pkt.haslayer(IP):
813 decrypt_pkt = IP(decrypt_pkt[Raw].load)
814 decrypt_pkts.append(decrypt_pkt)
815 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
816 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
817 except:
818 self.logger.debug(ppp("Unexpected packet:", rx))
819 try:
820 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
821 except:
822 pass
823 raise
824 pkts = reassemble4(decrypt_pkts)
825 for pkt in pkts:
826 self.assert_packet_checksums_valid(pkt)
827
Neale Rannsd7603d92019-03-28 08:56:10 +0000828 def verify_tun_44(self, p, count=1, payload_size=64, n_rx=None):
Klement Sekera10d066e2018-11-13 11:12:57 +0100829 self.vapi.cli("clear errors")
Neale Ranns02950402019-12-20 00:54:57 +0000830 self.vapi.cli("clear ipsec counters")
Neale Rannsd7603d92019-03-28 08:56:10 +0000831 if not n_rx:
832 n_rx = count
Klement Sekera31da2e32018-06-24 22:49:55 +0200833 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000834 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200835 src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200836 dst=self.pg1.remote_ip4,
837 count=count)
Klement Sekera611864f2018-09-26 11:19:00 +0200838 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Neale Rannsf05e7322019-03-29 20:23:58 +0000839 self.verify_decrypted(p, recv_pkts)
840
Klement Sekera31da2e32018-06-24 22:49:55 +0200841 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
Neale Rannsd7603d92019-03-28 08:56:10 +0000842 dst=p.remote_tun_if_host, count=count,
843 payload_size=payload_size)
844 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
845 self.tun_if, n_rx)
Neale Rannsf05e7322019-03-29 20:23:58 +0000846 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
847
Neale Rannsf3a66222020-01-02 05:04:00 +0000848 for rx in recv_pkts:
849 self.assertEqual(rx[IP].src, p.tun_src)
850 self.assertEqual(rx[IP].dst, p.tun_dst)
851
Klement Sekera31da2e32018-06-24 22:49:55 +0200852 finally:
853 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -0400854 self.logger.info(self.vapi.ppcli("show ipsec all"))
Klement Sekera31da2e32018-06-24 22:49:55 +0200855
Neale Ranns02950402019-12-20 00:54:57 +0000856 self.logger.info(self.vapi.ppcli("show ipsec sa 0"))
857 self.logger.info(self.vapi.ppcli("show ipsec sa 4"))
Klement Sekera6aa58b72019-05-16 14:34:55 +0200858 self.verify_counters4(p, count, n_rx)
Neale Rannseba31ec2019-02-17 18:04:27 +0000859
Neale Ranns02950402019-12-20 00:54:57 +0000860 """ verify methods for Transport v4 """
861 def verify_tun_44_bad_packet_sizes(self, p):
862 # with a buffer size of 2048, 1989 bytes of payload
863 # means there isn't space to insert the ESP header
864 N_PKTS = 63
865 for p_siz in [1989, 8500]:
866 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
867 src=p.remote_tun_if_host,
868 dst=self.pg1.remote_ip4,
869 count=N_PKTS,
870 payload_size=p_siz)
871 self.send_and_assert_no_replies(self.tun_if, send_pkts)
872 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
873 dst=p.remote_tun_if_host, count=N_PKTS,
874 payload_size=p_siz)
875 self.send_and_assert_no_replies(self.pg1, send_pkts,
876 self.tun_if)
877
878 # both large packets on decrpyt count against chained buffers
879 # the 9000 bytes one does on encrypt
880 self.assertEqual(2 * N_PKTS,
881 self.statistics.get_err_counter(
882 '/err/%s/chained buffers (packet dropped)' %
883 self.tun4_decrypt_node_name))
884 self.assertEqual(N_PKTS,
885 self.statistics.get_err_counter(
886 '/err/%s/chained buffers (packet dropped)' %
887 self.tun4_encrypt_node_name))
888
889 # on encrypt the 1989 size is no trailer space
890 self.assertEqual(N_PKTS,
891 self.statistics.get_err_counter(
892 '/err/%s/no trailer space (packet dropped)' %
893 self.tun4_encrypt_node_name))
894
Neale Ranns14046982019-07-29 14:49:52 +0000895 def verify_tun_reass_44(self, p):
896 self.vapi.cli("clear errors")
897 self.vapi.ip_reassembly_enable_disable(
898 sw_if_index=self.tun_if.sw_if_index, enable_ip4=True)
899
900 try:
Neale Ranns14046982019-07-29 14:49:52 +0000901 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
902 src=p.remote_tun_if_host,
903 dst=self.pg1.remote_ip4,
904 payload_size=1900,
905 count=1)
906 send_pkts = fragment_rfc791(send_pkts[0], 1400)
907 recv_pkts = self.send_and_expect(self.tun_if, send_pkts,
908 self.pg1, n_rx=1)
909 self.verify_decrypted(p, recv_pkts)
910
911 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
912 dst=p.remote_tun_if_host, count=1)
913 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
914 self.tun_if)
915 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
916
917 finally:
918 self.logger.info(self.vapi.ppcli("show error"))
919 self.logger.info(self.vapi.ppcli("show ipsec all"))
920
921 self.verify_counters4(p, 1, 1)
922 self.vapi.ip_reassembly_enable_disable(
923 sw_if_index=self.tun_if.sw_if_index, enable_ip4=False)
924
Neale Ranns987aea82019-03-27 13:40:35 +0000925 def verify_tun_64(self, p, count=1):
926 self.vapi.cli("clear errors")
927 try:
Neale Ranns987aea82019-03-27 13:40:35 +0000928 send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if,
929 src=p.remote_tun_if_host6,
930 dst=self.pg1.remote_ip6,
931 count=count)
932 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
933 for recv_pkt in recv_pkts:
934 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host6)
935 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
936 self.assert_packet_checksums_valid(recv_pkt)
937 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
938 dst=p.remote_tun_if_host6, count=count)
939 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
940 for recv_pkt in recv_pkts:
941 try:
942 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP])
943 if not decrypt_pkt.haslayer(IPv6):
944 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
945 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
946 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host6)
947 self.assert_packet_checksums_valid(decrypt_pkt)
948 except:
949 self.logger.error(ppp("Unexpected packet:", recv_pkt))
950 try:
951 self.logger.debug(
952 ppp("Decrypted packet:", decrypt_pkt))
953 except:
954 pass
955 raise
956 finally:
957 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -0400958 self.logger.info(self.vapi.ppcli("show ipsec all"))
Neale Rannseba31ec2019-02-17 18:04:27 +0000959
Klement Sekera6aa58b72019-05-16 14:34:55 +0200960 self.verify_counters4(p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +0100961
Neale Ranns41afb332019-07-16 06:19:35 -0700962 def verify_keepalive(self, p):
963 pkt = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
964 IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4) /
965 UDP(sport=333, dport=4500) /
Ole Troan8b76c232019-10-21 20:55:13 +0200966 Raw(b'\xff'))
Neale Ranns41afb332019-07-16 06:19:35 -0700967 self.send_and_assert_no_replies(self.tun_if, pkt*31)
968 self.assert_error_counter_equal(
969 '/err/%s/NAT Keepalive' % self.tun4_input_node, 31)
970
971 pkt = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
972 IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4) /
973 UDP(sport=333, dport=4500) /
Ole Troan8b76c232019-10-21 20:55:13 +0200974 Raw(b'\xfe'))
Neale Ranns41afb332019-07-16 06:19:35 -0700975 self.send_and_assert_no_replies(self.tun_if, pkt*31)
976 self.assert_error_counter_equal(
977 '/err/%s/Too Short' % self.tun4_input_node, 31)
978
Neale Ranns2ac885c2019-03-20 18:24:43 +0000979
980class IpsecTun4Tests(IpsecTun4):
Neale Ranns4f33c802019-04-10 12:39:10 +0000981 """ UT test methods for Tunnel v4 """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000982 def test_tun_basic44(self):
983 """ ipsec 4o4 tunnel basic test """
984 self.verify_tun_44(self.params[socket.AF_INET], count=1)
Neale Ranns02950402019-12-20 00:54:57 +0000985 self.tun_if.admin_down()
986 self.tun_if.resolve_arp()
987 self.tun_if.admin_up()
988 self.verify_tun_44(self.params[socket.AF_INET], count=1)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000989
Neale Ranns14046982019-07-29 14:49:52 +0000990 def test_tun_reass_basic44(self):
991 """ ipsec 4o4 tunnel basic reassembly test """
992 self.verify_tun_reass_44(self.params[socket.AF_INET])
993
Klement Sekera611864f2018-09-26 11:19:00 +0200994 def test_tun_burst44(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200995 """ ipsec 4o4 tunnel burst test """
Neale Ranns02950402019-12-20 00:54:57 +0000996 self.verify_tun_44(self.params[socket.AF_INET], count=127)
997
998
999class IpsecTunEsp4Tests(IpsecTun4):
1000 def test_tun_bad_packet_sizes(self):
1001 """ ipsec v4 tunnel bad packet size """
1002 self.verify_tun_44_bad_packet_sizes(self.params[socket.AF_INET])
Klement Sekera611864f2018-09-26 11:19:00 +02001003
1004
Neale Ranns2ac885c2019-03-20 18:24:43 +00001005class IpsecTun6(object):
Neale Ranns4f33c802019-04-10 12:39:10 +00001006 """ verify methods for Tunnel v6 """
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001007 def verify_counters6(self, p_in, p_out, count, worker=None):
Neale Rannsc87b66c2019-02-07 07:26:12 -08001008 if (hasattr(p_in, "tun_sa_in")):
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001009 pkts = p_in.tun_sa_in.get_stats(worker)['packets']
Neale Ranns987aea82019-03-27 13:40:35 +00001010 self.assertEqual(pkts, count,
1011 "incorrect SA in counts: expected %d != %d" %
1012 (count, pkts))
Neale Rannsc87b66c2019-02-07 07:26:12 -08001013 if (hasattr(p_out, "tun_sa_out")):
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001014 pkts = p_out.tun_sa_out.get_stats(worker)['packets']
Neale Ranns987aea82019-03-27 13:40:35 +00001015 self.assertEqual(pkts, count,
1016 "incorrect SA out counts: expected %d != %d" %
1017 (count, pkts))
1018 self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count)
1019 self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count)
1020
Neale Rannsc87b66c2019-02-07 07:26:12 -08001021 def verify_decrypted6(self, p, rxs):
1022 for rx in rxs:
1023 self.assert_equal(rx[IPv6].src, p.remote_tun_if_host)
1024 self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6)
1025 self.assert_packet_checksums_valid(rx)
1026
1027 def verify_encrypted6(self, p, sa, rxs):
1028 for rx in rxs:
1029 self.assert_packet_checksums_valid(rx)
1030 self.assertEqual(len(rx) - len(Ether()) - len(IPv6()),
1031 rx[IPv6].plen)
1032 try:
1033 decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IPv6])
1034 if not decrypt_pkt.haslayer(IPv6):
1035 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
1036 self.assert_packet_checksums_valid(decrypt_pkt)
1037 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
1038 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
1039 except:
1040 self.logger.debug(ppp("Unexpected packet:", rx))
1041 try:
1042 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
1043 except:
1044 pass
1045 raise
1046
1047 def verify_drop_tun_66(self, p_in, count=1, payload_size=64):
Klement Sekera10d066e2018-11-13 11:12:57 +01001048 self.vapi.cli("clear errors")
Neale Rannsc87b66c2019-02-07 07:26:12 -08001049 self.vapi.cli("clear ipsec sa")
1050
Neale Rannsc87b66c2019-02-07 07:26:12 -08001051 send_pkts = self.gen_encrypt_pkts6(p_in.scapy_tun_sa, self.tun_if,
1052 src=p_in.remote_tun_if_host,
1053 dst=self.pg1.remote_ip6,
1054 count=count)
1055 self.send_and_assert_no_replies(self.tun_if, send_pkts)
1056 self.logger.info(self.vapi.cli("sh punt stats"))
1057
1058 def verify_tun_66(self, p_in, p_out=None, count=1, payload_size=64):
1059 self.vapi.cli("clear errors")
1060 self.vapi.cli("clear ipsec sa")
1061 if not p_out:
1062 p_out = p_in
Klement Sekera31da2e32018-06-24 22:49:55 +02001063 try:
Neale Rannsc87b66c2019-02-07 07:26:12 -08001064 send_pkts = self.gen_encrypt_pkts6(p_in.scapy_tun_sa, self.tun_if,
1065 src=p_in.remote_tun_if_host,
Klement Sekera611864f2018-09-26 11:19:00 +02001066 dst=self.pg1.remote_ip6,
1067 count=count)
1068 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Neale Rannsc87b66c2019-02-07 07:26:12 -08001069 self.verify_decrypted6(p_in, recv_pkts)
1070
Klement Sekera611864f2018-09-26 11:19:00 +02001071 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
Neale Rannsc87b66c2019-02-07 07:26:12 -08001072 dst=p_out.remote_tun_if_host,
1073 count=count,
1074 payload_size=payload_size)
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001075 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
Neale Rannsc87b66c2019-02-07 07:26:12 -08001076 self.verify_encrypted6(p_out, p_out.vpp_tun_sa, recv_pkts)
1077
Neale Rannsf3a66222020-01-02 05:04:00 +00001078 for rx in recv_pkts:
1079 self.assertEqual(rx[IPv6].src, p_out.tun_src)
1080 self.assertEqual(rx[IPv6].dst, p_out.tun_dst)
1081
Klement Sekera31da2e32018-06-24 22:49:55 +02001082 finally:
1083 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -04001084 self.logger.info(self.vapi.ppcli("show ipsec all"))
Neale Rannsc87b66c2019-02-07 07:26:12 -08001085 self.verify_counters6(p_in, p_out, count)
Klement Sekera31da2e32018-06-24 22:49:55 +02001086
Neale Ranns14046982019-07-29 14:49:52 +00001087 def verify_tun_reass_66(self, p):
1088 self.vapi.cli("clear errors")
1089 self.vapi.ip_reassembly_enable_disable(
1090 sw_if_index=self.tun_if.sw_if_index, enable_ip6=True)
1091
1092 try:
Neale Ranns14046982019-07-29 14:49:52 +00001093 send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if,
1094 src=p.remote_tun_if_host,
1095 dst=self.pg1.remote_ip6,
1096 count=1,
Neale Ranns02950402019-12-20 00:54:57 +00001097 payload_size=1850)
Neale Ranns14046982019-07-29 14:49:52 +00001098 send_pkts = fragment_rfc8200(send_pkts[0], 1, 1400, self.logger)
1099 recv_pkts = self.send_and_expect(self.tun_if, send_pkts,
1100 self.pg1, n_rx=1)
1101 self.verify_decrypted6(p, recv_pkts)
1102
1103 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
1104 dst=p.remote_tun_if_host,
1105 count=1,
1106 payload_size=64)
1107 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
1108 self.tun_if)
1109 self.verify_encrypted6(p, p.vpp_tun_sa, recv_pkts)
1110 finally:
1111 self.logger.info(self.vapi.ppcli("show error"))
1112 self.logger.info(self.vapi.ppcli("show ipsec all"))
1113 self.verify_counters6(p, p, 1)
1114 self.vapi.ip_reassembly_enable_disable(
1115 sw_if_index=self.tun_if.sw_if_index, enable_ip6=False)
1116
Neale Ranns987aea82019-03-27 13:40:35 +00001117 def verify_tun_46(self, p, count=1):
1118 """ ipsec 4o6 tunnel basic test """
1119 self.vapi.cli("clear errors")
1120 try:
Neale Ranns987aea82019-03-27 13:40:35 +00001121 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
1122 src=p.remote_tun_if_host4,
1123 dst=self.pg1.remote_ip4,
1124 count=count)
1125 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
1126 for recv_pkt in recv_pkts:
1127 self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host4)
1128 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
1129 self.assert_packet_checksums_valid(recv_pkt)
1130 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
1131 dst=p.remote_tun_if_host4,
1132 count=count)
1133 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
1134 for recv_pkt in recv_pkts:
1135 try:
1136 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6])
1137 if not decrypt_pkt.haslayer(IP):
1138 decrypt_pkt = IP(decrypt_pkt[Raw].load)
1139 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
1140 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host4)
1141 self.assert_packet_checksums_valid(decrypt_pkt)
1142 except:
1143 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
1144 try:
1145 self.logger.debug(ppp("Decrypted packet:",
1146 decrypt_pkt))
1147 except:
1148 pass
1149 raise
1150 finally:
1151 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -04001152 self.logger.info(self.vapi.ppcli("show ipsec all"))
Neale Rannsc87b66c2019-02-07 07:26:12 -08001153 self.verify_counters6(p, p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +01001154
Neale Ranns2ac885c2019-03-20 18:24:43 +00001155
1156class IpsecTun6Tests(IpsecTun6):
Neale Ranns4f33c802019-04-10 12:39:10 +00001157 """ UT test methods for Tunnel v6 """
Neale Ranns2ac885c2019-03-20 18:24:43 +00001158
1159 def test_tun_basic66(self):
1160 """ ipsec 6o6 tunnel basic test """
1161 self.verify_tun_66(self.params[socket.AF_INET6], count=1)
1162
Neale Ranns14046982019-07-29 14:49:52 +00001163 def test_tun_reass_basic66(self):
1164 """ ipsec 6o6 tunnel basic reassembly test """
1165 self.verify_tun_reass_66(self.params[socket.AF_INET6])
1166
Klement Sekera611864f2018-09-26 11:19:00 +02001167 def test_tun_burst66(self):
1168 """ ipsec 6o6 tunnel burst test """
Neale Ranns2ac885c2019-03-20 18:24:43 +00001169 self.verify_tun_66(self.params[socket.AF_INET6], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +02001170
1171
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001172class IpsecTun6HandoffTests(IpsecTun6):
1173 """ UT test methods for Tunnel v6 with multiple workers """
1174 worker_config = "workers 2"
1175
1176 def test_tun_handoff_66(self):
1177 """ ipsec 6o6 tunnel worker hand-off test """
1178 N_PKTS = 15
1179 p = self.params[socket.AF_INET6]
1180
1181 # inject alternately on worker 0 and 1. all counts on the SA
1182 # should be against worker 0
1183 for worker in [0, 1, 0, 1]:
1184 send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if,
1185 src=p.remote_tun_if_host,
1186 dst=self.pg1.remote_ip6,
1187 count=N_PKTS)
1188 recv_pkts = self.send_and_expect(self.tun_if, send_pkts,
1189 self.pg1, worker=worker)
1190 self.verify_decrypted6(p, recv_pkts)
1191
1192 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
1193 dst=p.remote_tun_if_host,
1194 count=N_PKTS)
1195 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
1196 self.tun_if, worker=worker)
1197 self.verify_encrypted6(p, p.vpp_tun_sa, recv_pkts)
1198
1199 # all counts against the first worker that was used
1200 self.verify_counters6(p, p, 4*N_PKTS, worker=0)
1201
1202
1203class IpsecTun4HandoffTests(IpsecTun4):
1204 """ UT test methods for Tunnel v4 with multiple workers """
1205 worker_config = "workers 2"
1206
1207 def test_tun_handooff_44(self):
1208 """ ipsec 4o4 tunnel worker hand-off test """
1209 N_PKTS = 15
1210 p = self.params[socket.AF_INET]
1211
1212 # inject alternately on worker 0 and 1. all counts on the SA
1213 # should be against worker 0
1214 for worker in [0, 1, 0, 1]:
1215 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
1216 src=p.remote_tun_if_host,
1217 dst=self.pg1.remote_ip4,
1218 count=N_PKTS)
1219 recv_pkts = self.send_and_expect(self.tun_if, send_pkts,
1220 self.pg1, worker=worker)
1221 self.verify_decrypted(p, recv_pkts)
1222
1223 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
1224 dst=p.remote_tun_if_host,
1225 count=N_PKTS)
1226 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
1227 self.tun_if, worker=worker)
1228 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
1229
1230 # all counts against the first worker that was used
1231 self.verify_counters4(p, 4*N_PKTS, worker=0)
1232
1233
Neale Ranns53f526b2019-02-25 14:32:02 +00001234class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests):
Neale Ranns4f33c802019-04-10 12:39:10 +00001235 """ UT test methods for Tunnel v6 & v4 """
Klement Sekera611864f2018-09-26 11:19:00 +02001236 pass
1237
Klement Sekera31da2e32018-06-24 22:49:55 +02001238
1239if __name__ == '__main__':
1240 unittest.main(testRunner=VppTestRunner)