blob: 6e42ac7f9f43474a9556df27a626a345f9974d86 [file] [log] [blame]
Klement Sekera31da2e32018-06-24 22:49:55 +02001import unittest
Klement Sekera611864f2018-09-26 11:19:00 +02002import socket
Klement Sekera31da2e32018-06-24 22:49:55 +02003
Neale Ranns53f526b2019-02-25 14:32:02 +00004from scapy.layers.inet import IP, ICMP, TCP, UDP
Klement Sekera31da2e32018-06-24 22:49:55 +02005from scapy.layers.ipsec import SecurityAssociation
6from scapy.layers.l2 import Ether, Raw
Klement Sekera611864f2018-09-26 11:19:00 +02007from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest
Klement Sekera31da2e32018-06-24 22:49:55 +02008
9from framework import VppTestCase, VppTestRunner
Neale Rannsd7603d92019-03-28 08:56:10 +000010from util import ppp, reassemble4
Neale Ranns17dcec02019-01-09 21:22:20 -080011from vpp_papi import VppEnum
Klement Sekera31da2e32018-06-24 22:49:55 +020012
13
Klement Sekera611864f2018-09-26 11:19:00 +020014class IPsecIPv4Params(object):
Neale Ranns17dcec02019-01-09 21:22:20 -080015
Klement Sekera611864f2018-09-26 11:19:00 +020016 addr_type = socket.AF_INET
17 addr_any = "0.0.0.0"
18 addr_bcast = "255.255.255.255"
19 addr_len = 32
20 is_ipv6 = 0
Klement Sekera611864f2018-09-26 11:19:00 +020021
Neale Ranns17dcec02019-01-09 21:22:20 -080022 def __init__(self):
23 self.remote_tun_if_host = '1.1.1.1'
Neale Ranns987aea82019-03-27 13:40:35 +000024 self.remote_tun_if_host6 = '1111::1'
Klement Sekera611864f2018-09-26 11:19:00 +020025
Neale Ranns17dcec02019-01-09 21:22:20 -080026 self.scapy_tun_sa_id = 10
27 self.scapy_tun_spi = 1001
28 self.vpp_tun_sa_id = 20
29 self.vpp_tun_spi = 1000
Klement Sekera611864f2018-09-26 11:19:00 +020030
Neale Ranns17dcec02019-01-09 21:22:20 -080031 self.scapy_tra_sa_id = 30
32 self.scapy_tra_spi = 2001
33 self.vpp_tra_sa_id = 40
34 self.vpp_tra_spi = 2000
Klement Sekera611864f2018-09-26 11:19:00 +020035
Neale Ranns17dcec02019-01-09 21:22:20 -080036 self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t.
37 IPSEC_API_INTEG_ALG_SHA1_96)
38 self.auth_algo = 'HMAC-SHA1-96' # scapy name
39 self.auth_key = 'C91KUR9GYMm5GfkEvNjX'
40
41 self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t.
42 IPSEC_API_CRYPTO_ALG_AES_CBC_128)
43 self.crypt_algo = 'AES-CBC' # scapy name
44 self.crypt_key = 'JPjyOWBeVEQiMe7h'
Neale Ranns47feb112019-04-11 15:14:07 +000045 self.crypt_salt = ''
Neale Ranns53f526b2019-02-25 14:32:02 +000046 self.flags = 0
47 self.nat_header = None
Klement Sekera611864f2018-09-26 11:19:00 +020048
49
50class IPsecIPv6Params(object):
Neale Ranns17dcec02019-01-09 21:22:20 -080051
Klement Sekera611864f2018-09-26 11:19:00 +020052 addr_type = socket.AF_INET6
53 addr_any = "0::0"
54 addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
55 addr_len = 128
56 is_ipv6 = 1
Klement Sekera611864f2018-09-26 11:19:00 +020057
Neale Ranns17dcec02019-01-09 21:22:20 -080058 def __init__(self):
59 self.remote_tun_if_host = '1111:1111:1111:1111:1111:1111:1111:1111'
Neale Ranns987aea82019-03-27 13:40:35 +000060 self.remote_tun_if_host4 = '1.1.1.1'
Klement Sekera611864f2018-09-26 11:19:00 +020061
Neale Ranns17dcec02019-01-09 21:22:20 -080062 self.scapy_tun_sa_id = 50
63 self.scapy_tun_spi = 3001
64 self.vpp_tun_sa_id = 60
65 self.vpp_tun_spi = 3000
Klement Sekera611864f2018-09-26 11:19:00 +020066
Neale Ranns17dcec02019-01-09 21:22:20 -080067 self.scapy_tra_sa_id = 70
68 self.scapy_tra_spi = 4001
69 self.vpp_tra_sa_id = 80
70 self.vpp_tra_spi = 4000
Klement Sekera611864f2018-09-26 11:19:00 +020071
Neale Ranns17dcec02019-01-09 21:22:20 -080072 self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t.
Neale Ranns1091c4a2019-04-08 14:48:23 +000073 IPSEC_API_INTEG_ALG_SHA1_96)
74 self.auth_algo = 'HMAC-SHA1-96' # scapy name
Neale Ranns17dcec02019-01-09 21:22:20 -080075 self.auth_key = 'C91KUR9GYMm5GfkEvNjX'
76
77 self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t.
Neale Ranns4f33c802019-04-10 12:39:10 +000078 IPSEC_API_CRYPTO_ALG_AES_CBC_128)
Neale Ranns17dcec02019-01-09 21:22:20 -080079 self.crypt_algo = 'AES-CBC' # scapy name
Neale Ranns4f33c802019-04-10 12:39:10 +000080 self.crypt_key = 'JPjyOWBeVEQiMe7h'
Neale Ranns47feb112019-04-11 15:14:07 +000081 self.crypt_salt = ''
Neale Ranns53f526b2019-02-25 14:32:02 +000082 self.flags = 0
83 self.nat_header = None
Klement Sekera611864f2018-09-26 11:19:00 +020084
85
Neale Ranns2ac885c2019-03-20 18:24:43 +000086def config_tun_params(p, encryption_type, tun_if):
87 ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
Neale Ranns3833ffd2019-03-21 14:34:09 +000088 use_esn = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.
Damjan Marion1e3aa5e2019-03-28 10:58:59 +010089 IPSEC_API_SAD_FLAG_USE_ESN))
Neale Ranns2ac885c2019-03-20 18:24:43 +000090 p.scapy_tun_sa = SecurityAssociation(
91 encryption_type, spi=p.vpp_tun_spi,
Neale Ranns47feb112019-04-11 15:14:07 +000092 crypt_algo=p.crypt_algo, crypt_key=p.crypt_key + p.crypt_salt,
Neale Ranns2ac885c2019-03-20 18:24:43 +000093 auth_algo=p.auth_algo, auth_key=p.auth_key,
94 tunnel_header=ip_class_by_addr_type[p.addr_type](
95 src=tun_if.remote_addr[p.addr_type],
96 dst=tun_if.local_addr[p.addr_type]),
Neale Ranns3833ffd2019-03-21 14:34:09 +000097 nat_t_header=p.nat_header,
98 use_esn=use_esn)
Neale Ranns2ac885c2019-03-20 18:24:43 +000099 p.vpp_tun_sa = SecurityAssociation(
100 encryption_type, spi=p.scapy_tun_spi,
Neale Ranns47feb112019-04-11 15:14:07 +0000101 crypt_algo=p.crypt_algo, crypt_key=p.crypt_key + p.crypt_salt,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000102 auth_algo=p.auth_algo, auth_key=p.auth_key,
103 tunnel_header=ip_class_by_addr_type[p.addr_type](
104 dst=tun_if.remote_addr[p.addr_type],
105 src=tun_if.local_addr[p.addr_type]),
Neale Ranns3833ffd2019-03-21 14:34:09 +0000106 nat_t_header=p.nat_header,
107 use_esn=use_esn)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000108
109
110def config_tra_params(p, encryption_type):
Neale Ranns4f33c802019-04-10 12:39:10 +0000111 use_esn = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.
112 IPSEC_API_SAD_FLAG_USE_ESN))
Neale Ranns2ac885c2019-03-20 18:24:43 +0000113 p.scapy_tra_sa = SecurityAssociation(
114 encryption_type,
115 spi=p.vpp_tra_spi,
116 crypt_algo=p.crypt_algo,
Neale Ranns47feb112019-04-11 15:14:07 +0000117 crypt_key=p.crypt_key + p.crypt_salt,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000118 auth_algo=p.auth_algo,
119 auth_key=p.auth_key,
Neale Ranns3833ffd2019-03-21 14:34:09 +0000120 nat_t_header=p.nat_header,
121 use_esn=use_esn)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000122 p.vpp_tra_sa = SecurityAssociation(
123 encryption_type,
124 spi=p.scapy_tra_spi,
125 crypt_algo=p.crypt_algo,
Neale Ranns47feb112019-04-11 15:14:07 +0000126 crypt_key=p.crypt_key + p.crypt_salt,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000127 auth_algo=p.auth_algo,
128 auth_key=p.auth_key,
Neale Ranns3833ffd2019-03-21 14:34:09 +0000129 nat_t_header=p.nat_header,
130 use_esn=use_esn)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000131
132
Klement Sekera31da2e32018-06-24 22:49:55 +0200133class TemplateIpsec(VppTestCase):
134 """
135 TRANSPORT MODE:
136
137 ------ encrypt ---
138 |tra_if| <-------> |VPP|
139 ------ decrypt ---
140
141 TUNNEL MODE:
142
143 ------ encrypt --- plain ---
144 |tun_if| <------- |VPP| <------ |pg1|
145 ------ --- ---
146
147 ------ decrypt --- plain ---
148 |tun_if| -------> |VPP| ------> |pg1|
149 ------ --- ---
150 """
Neale Ranns4f33c802019-04-10 12:39:10 +0000151 tun_spd_id = 1
152 tra_spd_id = 2
Klement Sekera31da2e32018-06-24 22:49:55 +0200153
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800154 def ipsec_select_backend(self):
Klement Sekerab4d30532018-11-08 13:00:02 +0100155 """ empty method to be overloaded when necessary """
156 pass
157
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700158 @classmethod
159 def setUpClass(cls):
160 super(TemplateIpsec, cls).setUpClass()
161
162 @classmethod
163 def tearDownClass(cls):
164 super(TemplateIpsec, cls).tearDownClass()
165
Neale Ranns3833ffd2019-03-21 14:34:09 +0000166 def setup_params(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800167 self.ipv4_params = IPsecIPv4Params()
168 self.ipv6_params = IPsecIPv6Params()
169 self.params = {self.ipv4_params.addr_type: self.ipv4_params,
170 self.ipv6_params.addr_type: self.ipv6_params}
171
Neale Ranns4f33c802019-04-10 12:39:10 +0000172 def config_interfaces(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800173 self.create_pg_interfaces(range(3))
174 self.interfaces = list(self.pg_interfaces)
175 for i in self.interfaces:
Klement Sekera31da2e32018-06-24 22:49:55 +0200176 i.admin_up()
177 i.config_ip4()
178 i.resolve_arp()
Klement Sekera611864f2018-09-26 11:19:00 +0200179 i.config_ip6()
180 i.resolve_ndp()
Neale Ranns4f33c802019-04-10 12:39:10 +0000181
182 def setUp(self):
183 super(TemplateIpsec, self).setUp()
184
185 self.setup_params()
186
187 self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t.
188 IPSEC_API_PROTO_ESP)
189 self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t.
190 IPSEC_API_PROTO_AH)
191
192 self.config_interfaces()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700193
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800194 self.ipsec_select_backend()
Klement Sekera31da2e32018-06-24 22:49:55 +0200195
Neale Ranns4f33c802019-04-10 12:39:10 +0000196 def unconfig_interfaces(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800197 for i in self.interfaces:
198 i.admin_down()
199 i.unconfig_ip4()
200 i.unconfig_ip6()
201
Neale Ranns4f33c802019-04-10 12:39:10 +0000202 def tearDown(self):
203 super(TemplateIpsec, self).tearDown()
204
205 self.unconfig_interfaces()
206
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700207 def show_commands_at_teardown(self):
208 self.logger.info(self.vapi.cli("show hardware"))
Klement Sekera31da2e32018-06-24 22:49:55 +0200209
Neale Rannsd7603d92019-03-28 08:56:10 +0000210 def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1,
211 payload_size=54):
Klement Sekera31da2e32018-06-24 22:49:55 +0200212 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000213 sa.encrypt(IP(src=src, dst=dst) /
214 ICMP() / Raw('X' * payload_size))
Klement Sekera31da2e32018-06-24 22:49:55 +0200215 for i in range(count)]
216
Neale Rannsd7603d92019-03-28 08:56:10 +0000217 def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1,
218 payload_size=54):
Klement Sekera611864f2018-09-26 11:19:00 +0200219 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
220 sa.encrypt(IPv6(src=src, dst=dst) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000221 ICMPv6EchoRequest(id=0, seq=1,
222 data='X' * payload_size))
Klement Sekera611864f2018-09-26 11:19:00 +0200223 for i in range(count)]
224
Neale Rannsd7603d92019-03-28 08:56:10 +0000225 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=54):
Klement Sekera31da2e32018-06-24 22:49:55 +0200226 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000227 IP(src=src, dst=dst) / ICMP() / Raw('X' * payload_size)
Klement Sekera31da2e32018-06-24 22:49:55 +0200228 for i in range(count)]
229
Neale Rannsd7603d92019-03-28 08:56:10 +0000230 def gen_pkts6(self, sw_intf, src, dst, count=1, payload_size=54):
Klement Sekera611864f2018-09-26 11:19:00 +0200231 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
232 IPv6(src=src, dst=dst) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000233 ICMPv6EchoRequest(id=0, seq=1, data='X' * payload_size)
Klement Sekera611864f2018-09-26 11:19:00 +0200234 for i in range(count)]
235
Klement Sekera31da2e32018-06-24 22:49:55 +0200236
Neale Ranns4f33c802019-04-10 12:39:10 +0000237class IpsecTcp(object):
238 def verify_tcp_checksum(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200239 self.vapi.cli("test http server")
Klement Sekera611864f2018-09-26 11:19:00 +0200240 p = self.params[socket.AF_INET]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000241 config_tun_params(p, self.encryption_type, self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200242 send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
Neale Ranns2ac885c2019-03-20 18:24:43 +0000243 p.scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host,
244 dst=self.tun_if.local_ip4) /
245 TCP(flags='S', dport=80)))
Klement Sekera31da2e32018-06-24 22:49:55 +0200246 self.logger.debug(ppp("Sending packet:", send))
Klement Sekera611864f2018-09-26 11:19:00 +0200247 recv = self.send_and_expect(self.tun_if, [send], self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200248 recv = recv[0]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000249 decrypted = p.vpp_tun_sa.decrypt(recv[IP])
Klement Sekera31da2e32018-06-24 22:49:55 +0200250 self.assert_packet_checksums_valid(decrypted)
251
252
Neale Ranns4f33c802019-04-10 12:39:10 +0000253class IpsecTcpTests(IpsecTcp):
254 def test_tcp_checksum(self):
255 """ verify checksum correctness for vpp generated packets """
256 self.verify_tcp_checksum()
257
258
259class IpsecTra4(object):
260 """ verify methods for Transport v4 """
261 def verify_tra_anti_replay(self, count=1):
Neale Rannsde847272018-11-28 01:38:34 -0800262 p = self.params[socket.AF_INET]
Neale Ranns3833ffd2019-03-21 14:34:09 +0000263 use_esn = p.vpp_tra_sa.use_esn
Neale Rannsde847272018-11-28 01:38:34 -0800264
265 # fire in a packet with seq number 1
266 pkt = (Ether(src=self.tra_if.remote_mac,
267 dst=self.tra_if.local_mac) /
268 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
269 dst=self.tra_if.local_ip4) /
270 ICMP(),
271 seq_num=1))
272 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
273
274 # now move the window over to 235
275 pkt = (Ether(src=self.tra_if.remote_mac,
276 dst=self.tra_if.local_mac) /
277 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
278 dst=self.tra_if.local_ip4) /
279 ICMP(),
280 seq_num=235))
281 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
282
Neale Ranns3833ffd2019-03-21 14:34:09 +0000283 # replayed packets are dropped
284 self.send_and_assert_no_replies(self.tra_if, pkt * 3)
285 self.assert_packet_counter_equal(
286 '/err/%s/SA replayed packet' % self.tra4_decrypt_node_name, 3)
287
Neale Rannsde847272018-11-28 01:38:34 -0800288 # the window size is 64 packets
289 # in window are still accepted
290 pkt = (Ether(src=self.tra_if.remote_mac,
291 dst=self.tra_if.local_mac) /
292 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
293 dst=self.tra_if.local_ip4) /
294 ICMP(),
295 seq_num=172))
296 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
297
Neale Rannsde847272018-11-28 01:38:34 -0800298 # a packet that does not decrypt does not move the window forward
299 bogus_sa = SecurityAssociation(self.encryption_type,
300 p.vpp_tra_spi)
301 pkt = (Ether(src=self.tra_if.remote_mac,
302 dst=self.tra_if.local_mac) /
303 bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4,
304 dst=self.tra_if.local_ip4) /
305 ICMP(),
306 seq_num=350))
307 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
308
Klement Sekera14d7e902018-12-10 13:46:09 +0100309 self.assert_packet_counter_equal(
310 '/err/%s/Integrity check failed' % self.tra4_decrypt_node_name, 17)
Neale Rannsde847272018-11-28 01:38:34 -0800311
312 # which we can determine since this packet is still in the window
313 pkt = (Ether(src=self.tra_if.remote_mac,
314 dst=self.tra_if.local_mac) /
315 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
316 dst=self.tra_if.local_ip4) /
317 ICMP(),
318 seq_num=234))
Klement Sekera14d7e902018-12-10 13:46:09 +0100319 self.send_and_expect(self.tra_if, [pkt], self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800320
Neale Ranns3833ffd2019-03-21 14:34:09 +0000321 # out of window are dropped
322 pkt = (Ether(src=self.tra_if.remote_mac,
323 dst=self.tra_if.local_mac) /
324 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
325 dst=self.tra_if.local_ip4) /
326 ICMP(),
327 seq_num=17))
328 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
Neale Ranns00a44202019-03-21 16:36:28 +0000329
Neale Ranns3833ffd2019-03-21 14:34:09 +0000330 if use_esn:
331 # an out of window error with ESN looks like a high sequence
332 # wrap. but since it isn't then the verify will fail.
333 self.assert_packet_counter_equal(
334 '/err/%s/Integrity check failed' %
335 self.tra4_decrypt_node_name, 34)
336
337 else:
338 self.assert_packet_counter_equal(
339 '/err/%s/SA replayed packet' %
340 self.tra4_decrypt_node_name, 20)
341
342 # valid packet moves the window over to 236
Neale Ranns00a44202019-03-21 16:36:28 +0000343 pkt = (Ether(src=self.tra_if.remote_mac,
344 dst=self.tra_if.local_mac) /
345 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
346 dst=self.tra_if.local_ip4) /
347 ICMP(),
348 seq_num=236))
Neale Ranns3833ffd2019-03-21 14:34:09 +0000349 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
350 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
351
352 # move VPP's SA to just before the seq-number wrap
353 self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.scapy_tra_sa_id)
354
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700355 # then fire in a packet that VPP should drop because it causes the
356 # seq number to wrap unless we're using extended.
Neale Ranns3833ffd2019-03-21 14:34:09 +0000357 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(),
362 seq_num=237))
363
364 if use_esn:
365 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
366 # in order to decrpyt the high order number needs to wrap
367 p.vpp_tra_sa.seq_num = 0x100000000
368 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
369
370 # send packets with high bits set
371 p.scapy_tra_sa.seq_num = 0x100000005
372 pkt = (Ether(src=self.tra_if.remote_mac,
373 dst=self.tra_if.local_mac) /
374 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
375 dst=self.tra_if.local_ip4) /
376 ICMP(),
377 seq_num=0x100000005))
378 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
379 # in order to decrpyt the high order number needs to wrap
380 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
381 else:
382 self.send_and_assert_no_replies(self.tra_if, [pkt])
383 self.assert_packet_counter_equal(
384 '/err/%s/sequence number cycled' %
385 self.tra4_encrypt_node_name, 1)
Neale Ranns00a44202019-03-21 16:36:28 +0000386
Neale Rannsde847272018-11-28 01:38:34 -0800387 # move the security-associations seq number on to the last we used
Neale Ranns00a44202019-03-21 16:36:28 +0000388 self.vapi.cli("test ipsec sa %d seq 0x15f" % p.scapy_tra_sa_id)
Neale Rannsde847272018-11-28 01:38:34 -0800389 p.scapy_tra_sa.seq_num = 351
390 p.vpp_tra_sa.seq_num = 351
391
Neale Ranns4f33c802019-04-10 12:39:10 +0000392 def verify_tra_basic4(self, count=1):
Klement Sekera31da2e32018-06-24 22:49:55 +0200393 """ ipsec v4 transport basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100394 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200395 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200396 p = self.params[socket.AF_INET]
Neale Rannsde847272018-11-28 01:38:34 -0800397 send_pkts = self.gen_encrypt_pkts(p.scapy_tra_sa, self.tra_if,
Klement Sekera31da2e32018-06-24 22:49:55 +0200398 src=self.tra_if.remote_ip4,
399 dst=self.tra_if.local_ip4,
400 count=count)
401 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
Klement Sekera611864f2018-09-26 11:19:00 +0200402 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800403 for rx in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200404 try:
Neale Rannsde847272018-11-28 01:38:34 -0800405 decrypted = p.vpp_tra_sa.decrypt(rx[IP])
Klement Sekera611864f2018-09-26 11:19:00 +0200406 self.assert_packet_checksums_valid(decrypted)
407 except:
Neale Rannsde847272018-11-28 01:38:34 -0800408 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200409 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200410 finally:
411 self.logger.info(self.vapi.ppcli("show error"))
412 self.logger.info(self.vapi.ppcli("show ipsec"))
413
Neale Rannseba31ec2019-02-17 18:04:27 +0000414 pkts = p.tra_sa_in.get_stats()['packets']
415 self.assertEqual(pkts, count,
416 "incorrect SA in counts: expected %d != %d" %
417 (count, pkts))
418 pkts = p.tra_sa_out.get_stats()['packets']
419 self.assertEqual(pkts, count,
420 "incorrect SA out counts: expected %d != %d" %
421 (count, pkts))
422
Klement Sekera10d066e2018-11-13 11:12:57 +0100423 self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count)
424 self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count)
425
Neale Ranns4f33c802019-04-10 12:39:10 +0000426
427class IpsecTra4Tests(IpsecTra4):
428 """ UT test methods for Transport v4 """
429 def test_tra_anti_replay(self):
430 """ ipsec v4 transport anti-reply test """
431 self.verify_tra_anti_replay(count=1)
432
433 def test_tra_basic(self, count=1):
434 """ ipsec v4 transport basic test """
435 self.verify_tra_basic4(count=1)
436
Klement Sekera31da2e32018-06-24 22:49:55 +0200437 def test_tra_burst(self):
438 """ ipsec v4 transport burst test """
Neale Ranns4f33c802019-04-10 12:39:10 +0000439 self.verify_tra_basic4(count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200440
Neale Ranns53f526b2019-02-25 14:32:02 +0000441
Neale Ranns4f33c802019-04-10 12:39:10 +0000442class IpsecTra6(object):
443 """ verify methods for Transport v6 """
444 def verify_tra_basic6(self, count=1):
Klement Sekera10d066e2018-11-13 11:12:57 +0100445 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200446 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200447 p = self.params[socket.AF_INET6]
Neale Rannsde847272018-11-28 01:38:34 -0800448 send_pkts = self.gen_encrypt_pkts6(p.scapy_tra_sa, self.tra_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200449 src=self.tra_if.remote_ip6,
450 dst=self.tra_if.local_ip6,
451 count=count)
452 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
453 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800454 for rx in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200455 try:
Neale Rannsde847272018-11-28 01:38:34 -0800456 decrypted = p.vpp_tra_sa.decrypt(rx[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200457 self.assert_packet_checksums_valid(decrypted)
458 except:
Neale Rannsde847272018-11-28 01:38:34 -0800459 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200460 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200461 finally:
462 self.logger.info(self.vapi.ppcli("show error"))
463 self.logger.info(self.vapi.ppcli("show ipsec"))
464
Neale Rannseba31ec2019-02-17 18:04:27 +0000465 pkts = p.tra_sa_in.get_stats()['packets']
466 self.assertEqual(pkts, count,
467 "incorrect SA in counts: expected %d != %d" %
468 (count, pkts))
469 pkts = p.tra_sa_out.get_stats()['packets']
470 self.assertEqual(pkts, count,
471 "incorrect SA out counts: expected %d != %d" %
472 (count, pkts))
Klement Sekera10d066e2018-11-13 11:12:57 +0100473 self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count)
474 self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count)
475
Neale Ranns4f33c802019-04-10 12:39:10 +0000476
477class IpsecTra6Tests(IpsecTra6):
478 """ UT test methods for Transport v6 """
479 def test_tra_basic6(self):
480 """ ipsec v6 transport basic test """
481 self.verify_tra_basic6(count=1)
482
Klement Sekera611864f2018-09-26 11:19:00 +0200483 def test_tra_burst6(self):
484 """ ipsec v6 transport burst test """
Neale Ranns4f33c802019-04-10 12:39:10 +0000485 self.verify_tra_basic6(count=257)
Klement Sekera31da2e32018-06-24 22:49:55 +0200486
Klement Sekera611864f2018-09-26 11:19:00 +0200487
Neale Ranns53f526b2019-02-25 14:32:02 +0000488class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests):
Neale Ranns4f33c802019-04-10 12:39:10 +0000489 """ UT test methods for Transport v6 and v4"""
Neale Ranns53f526b2019-02-25 14:32:02 +0000490 pass
491
492
Neale Ranns2ac885c2019-03-20 18:24:43 +0000493class IpsecTun4(object):
Neale Ranns4f33c802019-04-10 12:39:10 +0000494 """ verify methods for Tunnel v4 """
Neale Ranns987aea82019-03-27 13:40:35 +0000495 def verify_counters(self, p, count):
496 if (hasattr(p, "spd_policy_in_any")):
497 pkts = p.spd_policy_in_any.get_stats()['packets']
498 self.assertEqual(pkts, count,
499 "incorrect SPD any policy: expected %d != %d" %
500 (count, pkts))
501
502 if (hasattr(p, "tun_sa_in")):
503 pkts = p.tun_sa_in.get_stats()['packets']
504 self.assertEqual(pkts, count,
505 "incorrect SA in counts: expected %d != %d" %
506 (count, pkts))
507 pkts = p.tun_sa_out.get_stats()['packets']
508 self.assertEqual(pkts, count,
509 "incorrect SA out counts: expected %d != %d" %
510 (count, pkts))
511
512 self.assert_packet_counter_equal(self.tun4_encrypt_node_name, count)
513 self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count)
514
Neale Rannsf05e7322019-03-29 20:23:58 +0000515 def verify_decrypted(self, p, rxs):
516 for rx in rxs:
517 self.assert_equal(rx[IP].src, p.remote_tun_if_host)
518 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
519 self.assert_packet_checksums_valid(rx)
520
521 def verify_encrypted(self, p, sa, rxs):
522 decrypt_pkts = []
523 for rx in rxs:
524 try:
525 decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IP])
526 if not decrypt_pkt.haslayer(IP):
527 decrypt_pkt = IP(decrypt_pkt[Raw].load)
528 decrypt_pkts.append(decrypt_pkt)
529 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
530 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
531 except:
532 self.logger.debug(ppp("Unexpected packet:", rx))
533 try:
534 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
535 except:
536 pass
537 raise
538 pkts = reassemble4(decrypt_pkts)
539 for pkt in pkts:
540 self.assert_packet_checksums_valid(pkt)
541
Neale Rannsd7603d92019-03-28 08:56:10 +0000542 def verify_tun_44(self, p, count=1, payload_size=64, n_rx=None):
Klement Sekera10d066e2018-11-13 11:12:57 +0100543 self.vapi.cli("clear errors")
Neale Rannsd7603d92019-03-28 08:56:10 +0000544 if not n_rx:
545 n_rx = count
Klement Sekera31da2e32018-06-24 22:49:55 +0200546 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000547 config_tun_params(p, self.encryption_type, self.tun_if)
548 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200549 src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200550 dst=self.pg1.remote_ip4,
551 count=count)
Klement Sekera611864f2018-09-26 11:19:00 +0200552 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Neale Rannsf05e7322019-03-29 20:23:58 +0000553 self.verify_decrypted(p, recv_pkts)
554
Klement Sekera31da2e32018-06-24 22:49:55 +0200555 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
Neale Rannsd7603d92019-03-28 08:56:10 +0000556 dst=p.remote_tun_if_host, count=count,
557 payload_size=payload_size)
558 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
559 self.tun_if, n_rx)
Neale Rannsf05e7322019-03-29 20:23:58 +0000560 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
561
Klement Sekera31da2e32018-06-24 22:49:55 +0200562 finally:
563 self.logger.info(self.vapi.ppcli("show error"))
564 self.logger.info(self.vapi.ppcli("show ipsec"))
565
Neale Ranns987aea82019-03-27 13:40:35 +0000566 self.verify_counters(p, count)
Neale Rannseba31ec2019-02-17 18:04:27 +0000567
Neale Ranns987aea82019-03-27 13:40:35 +0000568 def verify_tun_64(self, p, count=1):
569 self.vapi.cli("clear errors")
570 try:
571 config_tun_params(p, self.encryption_type, self.tun_if)
572 send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if,
573 src=p.remote_tun_if_host6,
574 dst=self.pg1.remote_ip6,
575 count=count)
576 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
577 for recv_pkt in recv_pkts:
578 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host6)
579 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
580 self.assert_packet_checksums_valid(recv_pkt)
581 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
582 dst=p.remote_tun_if_host6, count=count)
583 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
584 for recv_pkt in recv_pkts:
585 try:
586 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP])
587 if not decrypt_pkt.haslayer(IPv6):
588 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
589 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
590 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host6)
591 self.assert_packet_checksums_valid(decrypt_pkt)
592 except:
593 self.logger.error(ppp("Unexpected packet:", recv_pkt))
594 try:
595 self.logger.debug(
596 ppp("Decrypted packet:", decrypt_pkt))
597 except:
598 pass
599 raise
600 finally:
601 self.logger.info(self.vapi.ppcli("show error"))
602 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Rannseba31ec2019-02-17 18:04:27 +0000603
Neale Ranns987aea82019-03-27 13:40:35 +0000604 self.verify_counters(p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +0100605
Neale Ranns2ac885c2019-03-20 18:24:43 +0000606
607class IpsecTun4Tests(IpsecTun4):
Neale Ranns4f33c802019-04-10 12:39:10 +0000608 """ UT test methods for Tunnel v4 """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000609 def test_tun_basic44(self):
610 """ ipsec 4o4 tunnel basic test """
611 self.verify_tun_44(self.params[socket.AF_INET], count=1)
612
Klement Sekera611864f2018-09-26 11:19:00 +0200613 def test_tun_burst44(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200614 """ ipsec 4o4 tunnel burst test """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000615 self.verify_tun_44(self.params[socket.AF_INET], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200616
617
Neale Ranns2ac885c2019-03-20 18:24:43 +0000618class IpsecTun6(object):
Neale Ranns4f33c802019-04-10 12:39:10 +0000619 """ verify methods for Tunnel v6 """
Neale Ranns987aea82019-03-27 13:40:35 +0000620 def verify_counters(self, p, count):
621 if (hasattr(p, "tun_sa_in")):
622 pkts = p.tun_sa_in.get_stats()['packets']
623 self.assertEqual(pkts, count,
624 "incorrect SA in counts: expected %d != %d" %
625 (count, pkts))
626 pkts = p.tun_sa_out.get_stats()['packets']
627 self.assertEqual(pkts, count,
628 "incorrect SA out counts: expected %d != %d" %
629 (count, pkts))
630 self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count)
631 self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count)
632
Neale Ranns2ac885c2019-03-20 18:24:43 +0000633 def verify_tun_66(self, p, count=1):
Klement Sekera611864f2018-09-26 11:19:00 +0200634 """ ipsec 6o6 tunnel basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100635 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200636 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000637 config_tun_params(p, self.encryption_type, self.tun_if)
638 send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200639 src=p.remote_tun_if_host,
640 dst=self.pg1.remote_ip6,
641 count=count)
642 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
643 for recv_pkt in recv_pkts:
644 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host)
645 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
646 self.assert_packet_checksums_valid(recv_pkt)
647 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
648 dst=p.remote_tun_if_host,
649 count=count)
650 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
651 for recv_pkt in recv_pkts:
652 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000653 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200654 if not decrypt_pkt.haslayer(IPv6):
655 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
656 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
657 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
658 self.assert_packet_checksums_valid(decrypt_pkt)
659 except:
660 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
661 try:
662 self.logger.debug(
663 ppp("Decrypted packet:", decrypt_pkt))
664 except:
665 pass
666 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200667 finally:
668 self.logger.info(self.vapi.ppcli("show error"))
669 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Ranns987aea82019-03-27 13:40:35 +0000670 self.verify_counters(p, count)
Klement Sekera31da2e32018-06-24 22:49:55 +0200671
Neale Ranns987aea82019-03-27 13:40:35 +0000672 def verify_tun_46(self, p, count=1):
673 """ ipsec 4o6 tunnel basic test """
674 self.vapi.cli("clear errors")
675 try:
676 config_tun_params(p, self.encryption_type, self.tun_if)
677 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
678 src=p.remote_tun_if_host4,
679 dst=self.pg1.remote_ip4,
680 count=count)
681 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
682 for recv_pkt in recv_pkts:
683 self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host4)
684 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
685 self.assert_packet_checksums_valid(recv_pkt)
686 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
687 dst=p.remote_tun_if_host4,
688 count=count)
689 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
690 for recv_pkt in recv_pkts:
691 try:
692 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6])
693 if not decrypt_pkt.haslayer(IP):
694 decrypt_pkt = IP(decrypt_pkt[Raw].load)
695 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
696 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host4)
697 self.assert_packet_checksums_valid(decrypt_pkt)
698 except:
699 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
700 try:
701 self.logger.debug(ppp("Decrypted packet:",
702 decrypt_pkt))
703 except:
704 pass
705 raise
706 finally:
707 self.logger.info(self.vapi.ppcli("show error"))
708 self.logger.info(self.vapi.ppcli("show ipsec"))
709 self.verify_counters(p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +0100710
Neale Ranns2ac885c2019-03-20 18:24:43 +0000711
712class IpsecTun6Tests(IpsecTun6):
Neale Ranns4f33c802019-04-10 12:39:10 +0000713 """ UT test methods for Tunnel v6 """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000714
715 def test_tun_basic66(self):
716 """ ipsec 6o6 tunnel basic test """
717 self.verify_tun_66(self.params[socket.AF_INET6], count=1)
718
Klement Sekera611864f2018-09-26 11:19:00 +0200719 def test_tun_burst66(self):
720 """ ipsec 6o6 tunnel burst test """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000721 self.verify_tun_66(self.params[socket.AF_INET6], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200722
723
Neale Ranns53f526b2019-02-25 14:32:02 +0000724class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests):
Neale Ranns4f33c802019-04-10 12:39:10 +0000725 """ UT test methods for Tunnel v6 & v4 """
Klement Sekera611864f2018-09-26 11:19:00 +0200726 pass
727
Klement Sekera31da2e32018-06-24 22:49:55 +0200728
729if __name__ == '__main__':
730 unittest.main(testRunner=VppTestRunner)