blob: 6854a35210818cf66f9d4bd589d0836c504c49c5 [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 Ranns53f526b2019-02-25 14:32:02 +000045 self.flags = 0
46 self.nat_header = None
Klement Sekera611864f2018-09-26 11:19:00 +020047
48
49class IPsecIPv6Params(object):
Neale Ranns17dcec02019-01-09 21:22:20 -080050
Klement Sekera611864f2018-09-26 11:19:00 +020051 addr_type = socket.AF_INET6
52 addr_any = "0::0"
53 addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
54 addr_len = 128
55 is_ipv6 = 1
Klement Sekera611864f2018-09-26 11:19:00 +020056
Neale Ranns17dcec02019-01-09 21:22:20 -080057 def __init__(self):
58 self.remote_tun_if_host = '1111:1111:1111:1111:1111:1111:1111:1111'
Neale Ranns987aea82019-03-27 13:40:35 +000059 self.remote_tun_if_host4 = '1.1.1.1'
Klement Sekera611864f2018-09-26 11:19:00 +020060
Neale Ranns17dcec02019-01-09 21:22:20 -080061 self.scapy_tun_sa_id = 50
62 self.scapy_tun_spi = 3001
63 self.vpp_tun_sa_id = 60
64 self.vpp_tun_spi = 3000
Klement Sekera611864f2018-09-26 11:19:00 +020065
Neale Ranns17dcec02019-01-09 21:22:20 -080066 self.scapy_tra_sa_id = 70
67 self.scapy_tra_spi = 4001
68 self.vpp_tra_sa_id = 80
69 self.vpp_tra_spi = 4000
Klement Sekera611864f2018-09-26 11:19:00 +020070
Neale Ranns17dcec02019-01-09 21:22:20 -080071 self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t.
Neale Ranns1091c4a2019-04-08 14:48:23 +000072 IPSEC_API_INTEG_ALG_SHA1_96)
73 self.auth_algo = 'HMAC-SHA1-96' # scapy name
Neale Ranns17dcec02019-01-09 21:22:20 -080074 self.auth_key = 'C91KUR9GYMm5GfkEvNjX'
75
76 self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t.
Neale Ranns4f33c802019-04-10 12:39:10 +000077 IPSEC_API_CRYPTO_ALG_AES_CBC_128)
Neale Ranns17dcec02019-01-09 21:22:20 -080078 self.crypt_algo = 'AES-CBC' # scapy name
Neale Ranns4f33c802019-04-10 12:39:10 +000079 self.crypt_key = 'JPjyOWBeVEQiMe7h'
Neale Ranns53f526b2019-02-25 14:32:02 +000080 self.flags = 0
81 self.nat_header = None
Klement Sekera611864f2018-09-26 11:19:00 +020082
83
Neale Ranns2ac885c2019-03-20 18:24:43 +000084def config_tun_params(p, encryption_type, tun_if):
85 ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
Neale Ranns3833ffd2019-03-21 14:34:09 +000086 use_esn = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.
Damjan Marion1e3aa5e2019-03-28 10:58:59 +010087 IPSEC_API_SAD_FLAG_USE_ESN))
Neale Ranns2ac885c2019-03-20 18:24:43 +000088 p.scapy_tun_sa = SecurityAssociation(
89 encryption_type, spi=p.vpp_tun_spi,
90 crypt_algo=p.crypt_algo, crypt_key=p.crypt_key,
91 auth_algo=p.auth_algo, auth_key=p.auth_key,
92 tunnel_header=ip_class_by_addr_type[p.addr_type](
93 src=tun_if.remote_addr[p.addr_type],
94 dst=tun_if.local_addr[p.addr_type]),
Neale Ranns3833ffd2019-03-21 14:34:09 +000095 nat_t_header=p.nat_header,
96 use_esn=use_esn)
Neale Ranns2ac885c2019-03-20 18:24:43 +000097 p.vpp_tun_sa = SecurityAssociation(
98 encryption_type, spi=p.scapy_tun_spi,
99 crypt_algo=p.crypt_algo, crypt_key=p.crypt_key,
100 auth_algo=p.auth_algo, auth_key=p.auth_key,
101 tunnel_header=ip_class_by_addr_type[p.addr_type](
102 dst=tun_if.remote_addr[p.addr_type],
103 src=tun_if.local_addr[p.addr_type]),
Neale Ranns3833ffd2019-03-21 14:34:09 +0000104 nat_t_header=p.nat_header,
105 use_esn=use_esn)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000106
107
108def config_tra_params(p, encryption_type):
Neale Ranns4f33c802019-04-10 12:39:10 +0000109 use_esn = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.
110 IPSEC_API_SAD_FLAG_USE_ESN))
Neale Ranns2ac885c2019-03-20 18:24:43 +0000111 p.scapy_tra_sa = SecurityAssociation(
112 encryption_type,
113 spi=p.vpp_tra_spi,
114 crypt_algo=p.crypt_algo,
115 crypt_key=p.crypt_key,
116 auth_algo=p.auth_algo,
117 auth_key=p.auth_key,
Neale Ranns3833ffd2019-03-21 14:34:09 +0000118 nat_t_header=p.nat_header,
119 use_esn=use_esn)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000120 p.vpp_tra_sa = SecurityAssociation(
121 encryption_type,
122 spi=p.scapy_tra_spi,
123 crypt_algo=p.crypt_algo,
124 crypt_key=p.crypt_key,
125 auth_algo=p.auth_algo,
126 auth_key=p.auth_key,
Neale Ranns3833ffd2019-03-21 14:34:09 +0000127 nat_t_header=p.nat_header,
128 use_esn=use_esn)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000129
130
Klement Sekera31da2e32018-06-24 22:49:55 +0200131class TemplateIpsec(VppTestCase):
132 """
133 TRANSPORT MODE:
134
135 ------ encrypt ---
136 |tra_if| <-------> |VPP|
137 ------ decrypt ---
138
139 TUNNEL MODE:
140
141 ------ encrypt --- plain ---
142 |tun_if| <------- |VPP| <------ |pg1|
143 ------ --- ---
144
145 ------ decrypt --- plain ---
146 |tun_if| -------> |VPP| ------> |pg1|
147 ------ --- ---
148 """
Neale Ranns4f33c802019-04-10 12:39:10 +0000149 tun_spd_id = 1
150 tra_spd_id = 2
Klement Sekera31da2e32018-06-24 22:49:55 +0200151
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800152 def ipsec_select_backend(self):
Klement Sekerab4d30532018-11-08 13:00:02 +0100153 """ empty method to be overloaded when necessary """
154 pass
155
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700156 @classmethod
157 def setUpClass(cls):
158 super(TemplateIpsec, cls).setUpClass()
159
160 @classmethod
161 def tearDownClass(cls):
162 super(TemplateIpsec, cls).tearDownClass()
163
Neale Ranns3833ffd2019-03-21 14:34:09 +0000164 def setup_params(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800165 self.ipv4_params = IPsecIPv4Params()
166 self.ipv6_params = IPsecIPv6Params()
167 self.params = {self.ipv4_params.addr_type: self.ipv4_params,
168 self.ipv6_params.addr_type: self.ipv6_params}
169
Neale Ranns4f33c802019-04-10 12:39:10 +0000170 def config_interfaces(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800171 self.create_pg_interfaces(range(3))
172 self.interfaces = list(self.pg_interfaces)
173 for i in self.interfaces:
Klement Sekera31da2e32018-06-24 22:49:55 +0200174 i.admin_up()
175 i.config_ip4()
176 i.resolve_arp()
Klement Sekera611864f2018-09-26 11:19:00 +0200177 i.config_ip6()
178 i.resolve_ndp()
Neale Ranns4f33c802019-04-10 12:39:10 +0000179
180 def setUp(self):
181 super(TemplateIpsec, self).setUp()
182
183 self.setup_params()
184
185 self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t.
186 IPSEC_API_PROTO_ESP)
187 self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t.
188 IPSEC_API_PROTO_AH)
189
190 self.config_interfaces()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700191
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800192 self.ipsec_select_backend()
Klement Sekera31da2e32018-06-24 22:49:55 +0200193
Neale Ranns4f33c802019-04-10 12:39:10 +0000194 def unconfig_interfaces(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800195 for i in self.interfaces:
196 i.admin_down()
197 i.unconfig_ip4()
198 i.unconfig_ip6()
199
Neale Ranns4f33c802019-04-10 12:39:10 +0000200 def tearDown(self):
201 super(TemplateIpsec, self).tearDown()
202
203 self.unconfig_interfaces()
204
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700205 def show_commands_at_teardown(self):
206 self.logger.info(self.vapi.cli("show hardware"))
Klement Sekera31da2e32018-06-24 22:49:55 +0200207
Neale Rannsd7603d92019-03-28 08:56:10 +0000208 def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1,
209 payload_size=54):
Klement Sekera31da2e32018-06-24 22:49:55 +0200210 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000211 sa.encrypt(IP(src=src, dst=dst) /
212 ICMP() / Raw('X' * payload_size))
Klement Sekera31da2e32018-06-24 22:49:55 +0200213 for i in range(count)]
214
Neale Rannsd7603d92019-03-28 08:56:10 +0000215 def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1,
216 payload_size=54):
Klement Sekera611864f2018-09-26 11:19:00 +0200217 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
218 sa.encrypt(IPv6(src=src, dst=dst) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000219 ICMPv6EchoRequest(id=0, seq=1,
220 data='X' * payload_size))
Klement Sekera611864f2018-09-26 11:19:00 +0200221 for i in range(count)]
222
Neale Rannsd7603d92019-03-28 08:56:10 +0000223 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=54):
Klement Sekera31da2e32018-06-24 22:49:55 +0200224 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000225 IP(src=src, dst=dst) / ICMP() / Raw('X' * payload_size)
Klement Sekera31da2e32018-06-24 22:49:55 +0200226 for i in range(count)]
227
Neale Rannsd7603d92019-03-28 08:56:10 +0000228 def gen_pkts6(self, sw_intf, src, dst, count=1, payload_size=54):
Klement Sekera611864f2018-09-26 11:19:00 +0200229 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
230 IPv6(src=src, dst=dst) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000231 ICMPv6EchoRequest(id=0, seq=1, data='X' * payload_size)
Klement Sekera611864f2018-09-26 11:19:00 +0200232 for i in range(count)]
233
Klement Sekera31da2e32018-06-24 22:49:55 +0200234
Neale Ranns4f33c802019-04-10 12:39:10 +0000235class IpsecTcp(object):
236 def verify_tcp_checksum(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200237 self.vapi.cli("test http server")
Klement Sekera611864f2018-09-26 11:19:00 +0200238 p = self.params[socket.AF_INET]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000239 config_tun_params(p, self.encryption_type, self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200240 send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
Neale Ranns2ac885c2019-03-20 18:24:43 +0000241 p.scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host,
242 dst=self.tun_if.local_ip4) /
243 TCP(flags='S', dport=80)))
Klement Sekera31da2e32018-06-24 22:49:55 +0200244 self.logger.debug(ppp("Sending packet:", send))
Klement Sekera611864f2018-09-26 11:19:00 +0200245 recv = self.send_and_expect(self.tun_if, [send], self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200246 recv = recv[0]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000247 decrypted = p.vpp_tun_sa.decrypt(recv[IP])
Klement Sekera31da2e32018-06-24 22:49:55 +0200248 self.assert_packet_checksums_valid(decrypted)
249
250
Neale Ranns4f33c802019-04-10 12:39:10 +0000251class IpsecTcpTests(IpsecTcp):
252 def test_tcp_checksum(self):
253 """ verify checksum correctness for vpp generated packets """
254 self.verify_tcp_checksum()
255
256
257class IpsecTra4(object):
258 """ verify methods for Transport v4 """
259 def verify_tra_anti_replay(self, count=1):
Neale Rannsde847272018-11-28 01:38:34 -0800260 p = self.params[socket.AF_INET]
Neale Ranns3833ffd2019-03-21 14:34:09 +0000261 use_esn = p.vpp_tra_sa.use_esn
Neale Rannsde847272018-11-28 01:38:34 -0800262
263 # fire in a packet with seq number 1
264 pkt = (Ether(src=self.tra_if.remote_mac,
265 dst=self.tra_if.local_mac) /
266 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
267 dst=self.tra_if.local_ip4) /
268 ICMP(),
269 seq_num=1))
270 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
271
272 # now move the window over to 235
273 pkt = (Ether(src=self.tra_if.remote_mac,
274 dst=self.tra_if.local_mac) /
275 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
276 dst=self.tra_if.local_ip4) /
277 ICMP(),
278 seq_num=235))
279 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
280
Neale Ranns3833ffd2019-03-21 14:34:09 +0000281 # replayed packets are dropped
282 self.send_and_assert_no_replies(self.tra_if, pkt * 3)
283 self.assert_packet_counter_equal(
284 '/err/%s/SA replayed packet' % self.tra4_decrypt_node_name, 3)
285
Neale Rannsde847272018-11-28 01:38:34 -0800286 # the window size is 64 packets
287 # in window are still accepted
288 pkt = (Ether(src=self.tra_if.remote_mac,
289 dst=self.tra_if.local_mac) /
290 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
291 dst=self.tra_if.local_ip4) /
292 ICMP(),
293 seq_num=172))
294 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
295
Neale Rannsde847272018-11-28 01:38:34 -0800296 # a packet that does not decrypt does not move the window forward
297 bogus_sa = SecurityAssociation(self.encryption_type,
298 p.vpp_tra_spi)
299 pkt = (Ether(src=self.tra_if.remote_mac,
300 dst=self.tra_if.local_mac) /
301 bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4,
302 dst=self.tra_if.local_ip4) /
303 ICMP(),
304 seq_num=350))
305 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
306
Klement Sekera14d7e902018-12-10 13:46:09 +0100307 self.assert_packet_counter_equal(
308 '/err/%s/Integrity check failed' % self.tra4_decrypt_node_name, 17)
Neale Rannsde847272018-11-28 01:38:34 -0800309
310 # which we can determine since this packet is still in the window
311 pkt = (Ether(src=self.tra_if.remote_mac,
312 dst=self.tra_if.local_mac) /
313 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
314 dst=self.tra_if.local_ip4) /
315 ICMP(),
316 seq_num=234))
Klement Sekera14d7e902018-12-10 13:46:09 +0100317 self.send_and_expect(self.tra_if, [pkt], self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800318
Neale Ranns3833ffd2019-03-21 14:34:09 +0000319 # out of window are dropped
320 pkt = (Ether(src=self.tra_if.remote_mac,
321 dst=self.tra_if.local_mac) /
322 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
323 dst=self.tra_if.local_ip4) /
324 ICMP(),
325 seq_num=17))
326 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
Neale Ranns00a44202019-03-21 16:36:28 +0000327
Neale Ranns3833ffd2019-03-21 14:34:09 +0000328 if use_esn:
329 # an out of window error with ESN looks like a high sequence
330 # wrap. but since it isn't then the verify will fail.
331 self.assert_packet_counter_equal(
332 '/err/%s/Integrity check failed' %
333 self.tra4_decrypt_node_name, 34)
334
335 else:
336 self.assert_packet_counter_equal(
337 '/err/%s/SA replayed packet' %
338 self.tra4_decrypt_node_name, 20)
339
340 # valid packet moves the window over to 236
Neale Ranns00a44202019-03-21 16:36:28 +0000341 pkt = (Ether(src=self.tra_if.remote_mac,
342 dst=self.tra_if.local_mac) /
343 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
344 dst=self.tra_if.local_ip4) /
345 ICMP(),
346 seq_num=236))
Neale Ranns3833ffd2019-03-21 14:34:09 +0000347 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
348 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
349
350 # move VPP's SA to just before the seq-number wrap
351 self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.scapy_tra_sa_id)
352
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700353 # then fire in a packet that VPP should drop because it causes the
354 # seq number to wrap unless we're using extended.
Neale Ranns3833ffd2019-03-21 14:34:09 +0000355 pkt = (Ether(src=self.tra_if.remote_mac,
356 dst=self.tra_if.local_mac) /
357 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
358 dst=self.tra_if.local_ip4) /
359 ICMP(),
360 seq_num=237))
361
362 if use_esn:
363 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
364 # in order to decrpyt the high order number needs to wrap
365 p.vpp_tra_sa.seq_num = 0x100000000
366 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
367
368 # send packets with high bits set
369 p.scapy_tra_sa.seq_num = 0x100000005
370 pkt = (Ether(src=self.tra_if.remote_mac,
371 dst=self.tra_if.local_mac) /
372 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
373 dst=self.tra_if.local_ip4) /
374 ICMP(),
375 seq_num=0x100000005))
376 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
377 # in order to decrpyt the high order number needs to wrap
378 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
379 else:
380 self.send_and_assert_no_replies(self.tra_if, [pkt])
381 self.assert_packet_counter_equal(
382 '/err/%s/sequence number cycled' %
383 self.tra4_encrypt_node_name, 1)
Neale Ranns00a44202019-03-21 16:36:28 +0000384
Neale Rannsde847272018-11-28 01:38:34 -0800385 # move the security-associations seq number on to the last we used
Neale Ranns00a44202019-03-21 16:36:28 +0000386 self.vapi.cli("test ipsec sa %d seq 0x15f" % p.scapy_tra_sa_id)
Neale Rannsde847272018-11-28 01:38:34 -0800387 p.scapy_tra_sa.seq_num = 351
388 p.vpp_tra_sa.seq_num = 351
389
Neale Ranns4f33c802019-04-10 12:39:10 +0000390 def verify_tra_basic4(self, count=1):
Klement Sekera31da2e32018-06-24 22:49:55 +0200391 """ ipsec v4 transport basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100392 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200393 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200394 p = self.params[socket.AF_INET]
Neale Rannsde847272018-11-28 01:38:34 -0800395 send_pkts = self.gen_encrypt_pkts(p.scapy_tra_sa, self.tra_if,
Klement Sekera31da2e32018-06-24 22:49:55 +0200396 src=self.tra_if.remote_ip4,
397 dst=self.tra_if.local_ip4,
398 count=count)
399 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
Klement Sekera611864f2018-09-26 11:19:00 +0200400 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800401 for rx in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200402 try:
Neale Rannsde847272018-11-28 01:38:34 -0800403 decrypted = p.vpp_tra_sa.decrypt(rx[IP])
Klement Sekera611864f2018-09-26 11:19:00 +0200404 self.assert_packet_checksums_valid(decrypted)
405 except:
Neale Rannsde847272018-11-28 01:38:34 -0800406 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200407 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200408 finally:
409 self.logger.info(self.vapi.ppcli("show error"))
410 self.logger.info(self.vapi.ppcli("show ipsec"))
411
Neale Rannseba31ec2019-02-17 18:04:27 +0000412 pkts = p.tra_sa_in.get_stats()['packets']
413 self.assertEqual(pkts, count,
414 "incorrect SA in counts: expected %d != %d" %
415 (count, pkts))
416 pkts = p.tra_sa_out.get_stats()['packets']
417 self.assertEqual(pkts, count,
418 "incorrect SA out counts: expected %d != %d" %
419 (count, pkts))
420
Klement Sekera10d066e2018-11-13 11:12:57 +0100421 self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count)
422 self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count)
423
Neale Ranns4f33c802019-04-10 12:39:10 +0000424
425class IpsecTra4Tests(IpsecTra4):
426 """ UT test methods for Transport v4 """
427 def test_tra_anti_replay(self):
428 """ ipsec v4 transport anti-reply test """
429 self.verify_tra_anti_replay(count=1)
430
431 def test_tra_basic(self, count=1):
432 """ ipsec v4 transport basic test """
433 self.verify_tra_basic4(count=1)
434
Klement Sekera31da2e32018-06-24 22:49:55 +0200435 def test_tra_burst(self):
436 """ ipsec v4 transport burst test """
Neale Ranns4f33c802019-04-10 12:39:10 +0000437 self.verify_tra_basic4(count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200438
Neale Ranns53f526b2019-02-25 14:32:02 +0000439
Neale Ranns4f33c802019-04-10 12:39:10 +0000440class IpsecTra6(object):
441 """ verify methods for Transport v6 """
442 def verify_tra_basic6(self, count=1):
Klement Sekera10d066e2018-11-13 11:12:57 +0100443 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200444 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200445 p = self.params[socket.AF_INET6]
Neale Rannsde847272018-11-28 01:38:34 -0800446 send_pkts = self.gen_encrypt_pkts6(p.scapy_tra_sa, self.tra_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200447 src=self.tra_if.remote_ip6,
448 dst=self.tra_if.local_ip6,
449 count=count)
450 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
451 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800452 for rx in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200453 try:
Neale Rannsde847272018-11-28 01:38:34 -0800454 decrypted = p.vpp_tra_sa.decrypt(rx[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200455 self.assert_packet_checksums_valid(decrypted)
456 except:
Neale Rannsde847272018-11-28 01:38:34 -0800457 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200458 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200459 finally:
460 self.logger.info(self.vapi.ppcli("show error"))
461 self.logger.info(self.vapi.ppcli("show ipsec"))
462
Neale Rannseba31ec2019-02-17 18:04:27 +0000463 pkts = p.tra_sa_in.get_stats()['packets']
464 self.assertEqual(pkts, count,
465 "incorrect SA in counts: expected %d != %d" %
466 (count, pkts))
467 pkts = p.tra_sa_out.get_stats()['packets']
468 self.assertEqual(pkts, count,
469 "incorrect SA out counts: expected %d != %d" %
470 (count, pkts))
Klement Sekera10d066e2018-11-13 11:12:57 +0100471 self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count)
472 self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count)
473
Neale Ranns4f33c802019-04-10 12:39:10 +0000474
475class IpsecTra6Tests(IpsecTra6):
476 """ UT test methods for Transport v6 """
477 def test_tra_basic6(self):
478 """ ipsec v6 transport basic test """
479 self.verify_tra_basic6(count=1)
480
Klement Sekera611864f2018-09-26 11:19:00 +0200481 def test_tra_burst6(self):
482 """ ipsec v6 transport burst test """
Neale Ranns4f33c802019-04-10 12:39:10 +0000483 self.verify_tra_basic6(count=257)
Klement Sekera31da2e32018-06-24 22:49:55 +0200484
Klement Sekera611864f2018-09-26 11:19:00 +0200485
Neale Ranns53f526b2019-02-25 14:32:02 +0000486class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests):
Neale Ranns4f33c802019-04-10 12:39:10 +0000487 """ UT test methods for Transport v6 and v4"""
Neale Ranns53f526b2019-02-25 14:32:02 +0000488 pass
489
490
Neale Ranns2ac885c2019-03-20 18:24:43 +0000491class IpsecTun4(object):
Neale Ranns4f33c802019-04-10 12:39:10 +0000492 """ verify methods for Tunnel v4 """
Neale Ranns987aea82019-03-27 13:40:35 +0000493 def verify_counters(self, p, count):
494 if (hasattr(p, "spd_policy_in_any")):
495 pkts = p.spd_policy_in_any.get_stats()['packets']
496 self.assertEqual(pkts, count,
497 "incorrect SPD any policy: expected %d != %d" %
498 (count, pkts))
499
500 if (hasattr(p, "tun_sa_in")):
501 pkts = p.tun_sa_in.get_stats()['packets']
502 self.assertEqual(pkts, count,
503 "incorrect SA in counts: expected %d != %d" %
504 (count, pkts))
505 pkts = p.tun_sa_out.get_stats()['packets']
506 self.assertEqual(pkts, count,
507 "incorrect SA out counts: expected %d != %d" %
508 (count, pkts))
509
510 self.assert_packet_counter_equal(self.tun4_encrypt_node_name, count)
511 self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count)
512
Neale Rannsf05e7322019-03-29 20:23:58 +0000513 def verify_decrypted(self, p, rxs):
514 for rx in rxs:
515 self.assert_equal(rx[IP].src, p.remote_tun_if_host)
516 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
517 self.assert_packet_checksums_valid(rx)
518
519 def verify_encrypted(self, p, sa, rxs):
520 decrypt_pkts = []
521 for rx in rxs:
522 try:
523 decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IP])
524 if not decrypt_pkt.haslayer(IP):
525 decrypt_pkt = IP(decrypt_pkt[Raw].load)
526 decrypt_pkts.append(decrypt_pkt)
527 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
528 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
529 except:
530 self.logger.debug(ppp("Unexpected packet:", rx))
531 try:
532 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
533 except:
534 pass
535 raise
536 pkts = reassemble4(decrypt_pkts)
537 for pkt in pkts:
538 self.assert_packet_checksums_valid(pkt)
539
Neale Rannsd7603d92019-03-28 08:56:10 +0000540 def verify_tun_44(self, p, count=1, payload_size=64, n_rx=None):
Klement Sekera10d066e2018-11-13 11:12:57 +0100541 self.vapi.cli("clear errors")
Neale Rannsd7603d92019-03-28 08:56:10 +0000542 if not n_rx:
543 n_rx = count
Klement Sekera31da2e32018-06-24 22:49:55 +0200544 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000545 config_tun_params(p, self.encryption_type, self.tun_if)
546 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200547 src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200548 dst=self.pg1.remote_ip4,
549 count=count)
Klement Sekera611864f2018-09-26 11:19:00 +0200550 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Neale Rannsf05e7322019-03-29 20:23:58 +0000551 self.verify_decrypted(p, recv_pkts)
552
Klement Sekera31da2e32018-06-24 22:49:55 +0200553 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
Neale Rannsd7603d92019-03-28 08:56:10 +0000554 dst=p.remote_tun_if_host, count=count,
555 payload_size=payload_size)
556 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
557 self.tun_if, n_rx)
Neale Rannsf05e7322019-03-29 20:23:58 +0000558 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
559
Klement Sekera31da2e32018-06-24 22:49:55 +0200560 finally:
561 self.logger.info(self.vapi.ppcli("show error"))
562 self.logger.info(self.vapi.ppcli("show ipsec"))
563
Neale Ranns987aea82019-03-27 13:40:35 +0000564 self.verify_counters(p, count)
Neale Rannseba31ec2019-02-17 18:04:27 +0000565
Neale Ranns987aea82019-03-27 13:40:35 +0000566 def verify_tun_64(self, p, count=1):
567 self.vapi.cli("clear errors")
568 try:
569 config_tun_params(p, self.encryption_type, self.tun_if)
570 send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if,
571 src=p.remote_tun_if_host6,
572 dst=self.pg1.remote_ip6,
573 count=count)
574 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
575 for recv_pkt in recv_pkts:
576 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host6)
577 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
578 self.assert_packet_checksums_valid(recv_pkt)
579 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
580 dst=p.remote_tun_if_host6, count=count)
581 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
582 for recv_pkt in recv_pkts:
583 try:
584 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP])
585 if not decrypt_pkt.haslayer(IPv6):
586 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
587 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
588 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host6)
589 self.assert_packet_checksums_valid(decrypt_pkt)
590 except:
591 self.logger.error(ppp("Unexpected packet:", recv_pkt))
592 try:
593 self.logger.debug(
594 ppp("Decrypted packet:", decrypt_pkt))
595 except:
596 pass
597 raise
598 finally:
599 self.logger.info(self.vapi.ppcli("show error"))
600 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Rannseba31ec2019-02-17 18:04:27 +0000601
Neale Ranns987aea82019-03-27 13:40:35 +0000602 self.verify_counters(p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +0100603
Neale Ranns2ac885c2019-03-20 18:24:43 +0000604
605class IpsecTun4Tests(IpsecTun4):
Neale Ranns4f33c802019-04-10 12:39:10 +0000606 """ UT test methods for Tunnel v4 """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000607 def test_tun_basic44(self):
608 """ ipsec 4o4 tunnel basic test """
609 self.verify_tun_44(self.params[socket.AF_INET], count=1)
610
Klement Sekera611864f2018-09-26 11:19:00 +0200611 def test_tun_burst44(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200612 """ ipsec 4o4 tunnel burst test """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000613 self.verify_tun_44(self.params[socket.AF_INET], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200614
615
Neale Ranns2ac885c2019-03-20 18:24:43 +0000616class IpsecTun6(object):
Neale Ranns4f33c802019-04-10 12:39:10 +0000617 """ verify methods for Tunnel v6 """
Neale Ranns987aea82019-03-27 13:40:35 +0000618 def verify_counters(self, p, count):
619 if (hasattr(p, "tun_sa_in")):
620 pkts = p.tun_sa_in.get_stats()['packets']
621 self.assertEqual(pkts, count,
622 "incorrect SA in counts: expected %d != %d" %
623 (count, pkts))
624 pkts = p.tun_sa_out.get_stats()['packets']
625 self.assertEqual(pkts, count,
626 "incorrect SA out counts: expected %d != %d" %
627 (count, pkts))
628 self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count)
629 self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count)
630
Neale Ranns2ac885c2019-03-20 18:24:43 +0000631 def verify_tun_66(self, p, count=1):
Klement Sekera611864f2018-09-26 11:19:00 +0200632 """ ipsec 6o6 tunnel basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100633 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200634 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000635 config_tun_params(p, self.encryption_type, self.tun_if)
636 send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200637 src=p.remote_tun_if_host,
638 dst=self.pg1.remote_ip6,
639 count=count)
640 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
641 for recv_pkt in recv_pkts:
642 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host)
643 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
644 self.assert_packet_checksums_valid(recv_pkt)
645 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
646 dst=p.remote_tun_if_host,
647 count=count)
648 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
649 for recv_pkt in recv_pkts:
650 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000651 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200652 if not decrypt_pkt.haslayer(IPv6):
653 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
654 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
655 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
656 self.assert_packet_checksums_valid(decrypt_pkt)
657 except:
658 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
659 try:
660 self.logger.debug(
661 ppp("Decrypted packet:", decrypt_pkt))
662 except:
663 pass
664 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200665 finally:
666 self.logger.info(self.vapi.ppcli("show error"))
667 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Ranns987aea82019-03-27 13:40:35 +0000668 self.verify_counters(p, count)
Klement Sekera31da2e32018-06-24 22:49:55 +0200669
Neale Ranns987aea82019-03-27 13:40:35 +0000670 def verify_tun_46(self, p, count=1):
671 """ ipsec 4o6 tunnel basic test """
672 self.vapi.cli("clear errors")
673 try:
674 config_tun_params(p, self.encryption_type, self.tun_if)
675 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
676 src=p.remote_tun_if_host4,
677 dst=self.pg1.remote_ip4,
678 count=count)
679 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
680 for recv_pkt in recv_pkts:
681 self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host4)
682 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
683 self.assert_packet_checksums_valid(recv_pkt)
684 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
685 dst=p.remote_tun_if_host4,
686 count=count)
687 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
688 for recv_pkt in recv_pkts:
689 try:
690 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6])
691 if not decrypt_pkt.haslayer(IP):
692 decrypt_pkt = IP(decrypt_pkt[Raw].load)
693 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
694 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host4)
695 self.assert_packet_checksums_valid(decrypt_pkt)
696 except:
697 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
698 try:
699 self.logger.debug(ppp("Decrypted packet:",
700 decrypt_pkt))
701 except:
702 pass
703 raise
704 finally:
705 self.logger.info(self.vapi.ppcli("show error"))
706 self.logger.info(self.vapi.ppcli("show ipsec"))
707 self.verify_counters(p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +0100708
Neale Ranns2ac885c2019-03-20 18:24:43 +0000709
710class IpsecTun6Tests(IpsecTun6):
Neale Ranns4f33c802019-04-10 12:39:10 +0000711 """ UT test methods for Tunnel v6 """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000712
713 def test_tun_basic66(self):
714 """ ipsec 6o6 tunnel basic test """
715 self.verify_tun_66(self.params[socket.AF_INET6], count=1)
716
Klement Sekera611864f2018-09-26 11:19:00 +0200717 def test_tun_burst66(self):
718 """ ipsec 6o6 tunnel burst test """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000719 self.verify_tun_66(self.params[socket.AF_INET6], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200720
721
Neale Ranns53f526b2019-02-25 14:32:02 +0000722class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests):
Neale Ranns4f33c802019-04-10 12:39:10 +0000723 """ UT test methods for Tunnel v6 & v4 """
Klement Sekera611864f2018-09-26 11:19:00 +0200724 pass
725
Klement Sekera31da2e32018-06-24 22:49:55 +0200726
727if __name__ == '__main__':
728 unittest.main(testRunner=VppTestRunner)