blob: 6e6e37ba174824915ecd105c2cc23886073d5ae4 [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.
72 IPSEC_API_INTEG_ALG_SHA_256_128)
73 self.auth_algo = 'SHA2-256-128' # scapy name
74 self.auth_key = 'C91KUR9GYMm5GfkEvNjX'
75
76 self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t.
77 IPSEC_API_CRYPTO_ALG_AES_CBC_256)
78 self.crypt_algo = 'AES-CBC' # scapy name
79 self.crypt_key = 'JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h'
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 Ranns3833ffd2019-03-21 14:34:09 +0000109 use_esn = p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100110 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 """
149
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800150 def ipsec_select_backend(self):
Klement Sekerab4d30532018-11-08 13:00:02 +0100151 """ empty method to be overloaded when necessary """
152 pass
153
Neale Ranns3833ffd2019-03-21 14:34:09 +0000154 def setup_params(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800155 self.ipv4_params = IPsecIPv4Params()
156 self.ipv6_params = IPsecIPv6Params()
157 self.params = {self.ipv4_params.addr_type: self.ipv4_params,
158 self.ipv6_params.addr_type: self.ipv6_params}
159
Neale Ranns3833ffd2019-03-21 14:34:09 +0000160 def setUp(self):
161 super(TemplateIpsec, self).setUp()
162
163 self.setup_params()
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800164
165 self.tun_spd_id = 1
166 self.tra_spd_id = 2
167
Neale Ranns17dcec02019-01-09 21:22:20 -0800168 self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t.
169 IPSEC_API_PROTO_ESP)
170 self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t.
171 IPSEC_API_PROTO_AH)
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800172
173 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 Ranns8e4a89b2019-01-23 08:16:17 -0800181 self.ipsec_select_backend()
Klement Sekera31da2e32018-06-24 22:49:55 +0200182
183 def tearDown(self):
184 super(TemplateIpsec, self).tearDown()
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800185
186 for i in self.interfaces:
187 i.admin_down()
188 i.unconfig_ip4()
189 i.unconfig_ip6()
190
Klement Sekera31da2e32018-06-24 22:49:55 +0200191 if not self.vpp_dead:
192 self.vapi.cli("show hardware")
193
Neale Rannsd7603d92019-03-28 08:56:10 +0000194 def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1,
195 payload_size=54):
Klement Sekera31da2e32018-06-24 22:49:55 +0200196 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000197 sa.encrypt(IP(src=src, dst=dst) /
198 ICMP() / Raw('X' * payload_size))
Klement Sekera31da2e32018-06-24 22:49:55 +0200199 for i in range(count)]
200
Neale Rannsd7603d92019-03-28 08:56:10 +0000201 def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1,
202 payload_size=54):
Klement Sekera611864f2018-09-26 11:19:00 +0200203 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
204 sa.encrypt(IPv6(src=src, dst=dst) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000205 ICMPv6EchoRequest(id=0, seq=1,
206 data='X' * payload_size))
Klement Sekera611864f2018-09-26 11:19:00 +0200207 for i in range(count)]
208
Neale Rannsd7603d92019-03-28 08:56:10 +0000209 def gen_pkts(self, sw_intf, src, dst, count=1, 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 IP(src=src, dst=dst) / ICMP() / Raw('X' * payload_size)
Klement Sekera31da2e32018-06-24 22:49:55 +0200212 for i in range(count)]
213
Neale Rannsd7603d92019-03-28 08:56:10 +0000214 def gen_pkts6(self, sw_intf, src, dst, count=1, payload_size=54):
Klement Sekera611864f2018-09-26 11:19:00 +0200215 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
216 IPv6(src=src, dst=dst) /
Neale Rannsd7603d92019-03-28 08:56:10 +0000217 ICMPv6EchoRequest(id=0, seq=1, data='X' * payload_size)
Klement Sekera611864f2018-09-26 11:19:00 +0200218 for i in range(count)]
219
Klement Sekera31da2e32018-06-24 22:49:55 +0200220
221class IpsecTcpTests(object):
222 def test_tcp_checksum(self):
223 """ verify checksum correctness for vpp generated packets """
224 self.vapi.cli("test http server")
Klement Sekera611864f2018-09-26 11:19:00 +0200225 p = self.params[socket.AF_INET]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000226 config_tun_params(p, self.encryption_type, self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200227 send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
Neale Ranns2ac885c2019-03-20 18:24:43 +0000228 p.scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host,
229 dst=self.tun_if.local_ip4) /
230 TCP(flags='S', dport=80)))
Klement Sekera31da2e32018-06-24 22:49:55 +0200231 self.logger.debug(ppp("Sending packet:", send))
Klement Sekera611864f2018-09-26 11:19:00 +0200232 recv = self.send_and_expect(self.tun_if, [send], self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200233 recv = recv[0]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000234 decrypted = p.vpp_tun_sa.decrypt(recv[IP])
Klement Sekera31da2e32018-06-24 22:49:55 +0200235 self.assert_packet_checksums_valid(decrypted)
236
237
Neale Ranns53f526b2019-02-25 14:32:02 +0000238class IpsecTra4Tests(object):
Neale Rannsde847272018-11-28 01:38:34 -0800239 def test_tra_anti_replay(self, count=1):
240 """ ipsec v4 transport anti-reply test """
241 p = self.params[socket.AF_INET]
Neale Ranns3833ffd2019-03-21 14:34:09 +0000242 use_esn = p.vpp_tra_sa.use_esn
Neale Rannsde847272018-11-28 01:38:34 -0800243
244 # fire in a packet with seq number 1
245 pkt = (Ether(src=self.tra_if.remote_mac,
246 dst=self.tra_if.local_mac) /
247 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
248 dst=self.tra_if.local_ip4) /
249 ICMP(),
250 seq_num=1))
251 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
252
253 # now move the window over to 235
254 pkt = (Ether(src=self.tra_if.remote_mac,
255 dst=self.tra_if.local_mac) /
256 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
257 dst=self.tra_if.local_ip4) /
258 ICMP(),
259 seq_num=235))
260 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
261
Neale Ranns3833ffd2019-03-21 14:34:09 +0000262 # replayed packets are dropped
263 self.send_and_assert_no_replies(self.tra_if, pkt * 3)
264 self.assert_packet_counter_equal(
265 '/err/%s/SA replayed packet' % self.tra4_decrypt_node_name, 3)
266
Neale Rannsde847272018-11-28 01:38:34 -0800267 # the window size is 64 packets
268 # in window are still accepted
269 pkt = (Ether(src=self.tra_if.remote_mac,
270 dst=self.tra_if.local_mac) /
271 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
272 dst=self.tra_if.local_ip4) /
273 ICMP(),
274 seq_num=172))
275 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
276
Neale Rannsde847272018-11-28 01:38:34 -0800277 # a packet that does not decrypt does not move the window forward
278 bogus_sa = SecurityAssociation(self.encryption_type,
279 p.vpp_tra_spi)
280 pkt = (Ether(src=self.tra_if.remote_mac,
281 dst=self.tra_if.local_mac) /
282 bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4,
283 dst=self.tra_if.local_ip4) /
284 ICMP(),
285 seq_num=350))
286 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
287
Klement Sekera14d7e902018-12-10 13:46:09 +0100288 self.assert_packet_counter_equal(
289 '/err/%s/Integrity check failed' % self.tra4_decrypt_node_name, 17)
Neale Rannsde847272018-11-28 01:38:34 -0800290
291 # which we can determine since this packet is still in the window
292 pkt = (Ether(src=self.tra_if.remote_mac,
293 dst=self.tra_if.local_mac) /
294 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
295 dst=self.tra_if.local_ip4) /
296 ICMP(),
297 seq_num=234))
Klement Sekera14d7e902018-12-10 13:46:09 +0100298 self.send_and_expect(self.tra_if, [pkt], self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800299
Neale Ranns3833ffd2019-03-21 14:34:09 +0000300 # out of window are dropped
301 pkt = (Ether(src=self.tra_if.remote_mac,
302 dst=self.tra_if.local_mac) /
303 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
304 dst=self.tra_if.local_ip4) /
305 ICMP(),
306 seq_num=17))
307 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
Neale Ranns00a44202019-03-21 16:36:28 +0000308
Neale Ranns3833ffd2019-03-21 14:34:09 +0000309 if use_esn:
310 # an out of window error with ESN looks like a high sequence
311 # wrap. but since it isn't then the verify will fail.
312 self.assert_packet_counter_equal(
313 '/err/%s/Integrity check failed' %
314 self.tra4_decrypt_node_name, 34)
315
316 else:
317 self.assert_packet_counter_equal(
318 '/err/%s/SA replayed packet' %
319 self.tra4_decrypt_node_name, 20)
320
321 # valid packet moves the window over to 236
Neale Ranns00a44202019-03-21 16:36:28 +0000322 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=236))
Neale Ranns3833ffd2019-03-21 14:34:09 +0000328 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
329 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
330
331 # move VPP's SA to just before the seq-number wrap
332 self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.scapy_tra_sa_id)
333
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700334 # then fire in a packet that VPP should drop because it causes the
335 # seq number to wrap unless we're using extended.
Neale Ranns3833ffd2019-03-21 14:34:09 +0000336 pkt = (Ether(src=self.tra_if.remote_mac,
337 dst=self.tra_if.local_mac) /
338 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
339 dst=self.tra_if.local_ip4) /
340 ICMP(),
341 seq_num=237))
342
343 if use_esn:
344 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
345 # in order to decrpyt the high order number needs to wrap
346 p.vpp_tra_sa.seq_num = 0x100000000
347 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
348
349 # send packets with high bits set
350 p.scapy_tra_sa.seq_num = 0x100000005
351 pkt = (Ether(src=self.tra_if.remote_mac,
352 dst=self.tra_if.local_mac) /
353 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
354 dst=self.tra_if.local_ip4) /
355 ICMP(),
356 seq_num=0x100000005))
357 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
358 # in order to decrpyt the high order number needs to wrap
359 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
360 else:
361 self.send_and_assert_no_replies(self.tra_if, [pkt])
362 self.assert_packet_counter_equal(
363 '/err/%s/sequence number cycled' %
364 self.tra4_encrypt_node_name, 1)
Neale Ranns00a44202019-03-21 16:36:28 +0000365
Neale Rannsde847272018-11-28 01:38:34 -0800366 # move the security-associations seq number on to the last we used
Neale Ranns00a44202019-03-21 16:36:28 +0000367 self.vapi.cli("test ipsec sa %d seq 0x15f" % p.scapy_tra_sa_id)
Neale Rannsde847272018-11-28 01:38:34 -0800368 p.scapy_tra_sa.seq_num = 351
369 p.vpp_tra_sa.seq_num = 351
370
Klement Sekera31da2e32018-06-24 22:49:55 +0200371 def test_tra_basic(self, count=1):
372 """ ipsec v4 transport basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100373 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200374 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200375 p = self.params[socket.AF_INET]
Neale Rannsde847272018-11-28 01:38:34 -0800376 send_pkts = self.gen_encrypt_pkts(p.scapy_tra_sa, self.tra_if,
Klement Sekera31da2e32018-06-24 22:49:55 +0200377 src=self.tra_if.remote_ip4,
378 dst=self.tra_if.local_ip4,
379 count=count)
380 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
Klement Sekera611864f2018-09-26 11:19:00 +0200381 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800382 for rx in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200383 try:
Neale Rannsde847272018-11-28 01:38:34 -0800384 decrypted = p.vpp_tra_sa.decrypt(rx[IP])
Klement Sekera611864f2018-09-26 11:19:00 +0200385 self.assert_packet_checksums_valid(decrypted)
386 except:
Neale Rannsde847272018-11-28 01:38:34 -0800387 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200388 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200389 finally:
390 self.logger.info(self.vapi.ppcli("show error"))
391 self.logger.info(self.vapi.ppcli("show ipsec"))
392
Neale Rannseba31ec2019-02-17 18:04:27 +0000393 pkts = p.tra_sa_in.get_stats()['packets']
394 self.assertEqual(pkts, count,
395 "incorrect SA in counts: expected %d != %d" %
396 (count, pkts))
397 pkts = p.tra_sa_out.get_stats()['packets']
398 self.assertEqual(pkts, count,
399 "incorrect SA out counts: expected %d != %d" %
400 (count, pkts))
401
Klement Sekera10d066e2018-11-13 11:12:57 +0100402 self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count)
403 self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count)
404
Klement Sekera31da2e32018-06-24 22:49:55 +0200405 def test_tra_burst(self):
406 """ ipsec v4 transport burst test """
Klement Sekera611864f2018-09-26 11:19:00 +0200407 self.test_tra_basic(count=257)
408
Neale Ranns53f526b2019-02-25 14:32:02 +0000409
410class IpsecTra6Tests(object):
Klement Sekera611864f2018-09-26 11:19:00 +0200411 def test_tra_basic6(self, count=1):
412 """ ipsec v6 transport basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100413 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200414 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200415 p = self.params[socket.AF_INET6]
Neale Rannsde847272018-11-28 01:38:34 -0800416 send_pkts = self.gen_encrypt_pkts6(p.scapy_tra_sa, self.tra_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200417 src=self.tra_if.remote_ip6,
418 dst=self.tra_if.local_ip6,
419 count=count)
420 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
421 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800422 for rx in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200423 try:
Neale Rannsde847272018-11-28 01:38:34 -0800424 decrypted = p.vpp_tra_sa.decrypt(rx[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200425 self.assert_packet_checksums_valid(decrypted)
426 except:
Neale Rannsde847272018-11-28 01:38:34 -0800427 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200428 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200429 finally:
430 self.logger.info(self.vapi.ppcli("show error"))
431 self.logger.info(self.vapi.ppcli("show ipsec"))
432
Neale Rannseba31ec2019-02-17 18:04:27 +0000433 pkts = p.tra_sa_in.get_stats()['packets']
434 self.assertEqual(pkts, count,
435 "incorrect SA in counts: expected %d != %d" %
436 (count, pkts))
437 pkts = p.tra_sa_out.get_stats()['packets']
438 self.assertEqual(pkts, count,
439 "incorrect SA out counts: expected %d != %d" %
440 (count, pkts))
Klement Sekera10d066e2018-11-13 11:12:57 +0100441 self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count)
442 self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count)
443
Klement Sekera611864f2018-09-26 11:19:00 +0200444 def test_tra_burst6(self):
445 """ ipsec v6 transport burst test """
446 self.test_tra_basic6(count=257)
Klement Sekera31da2e32018-06-24 22:49:55 +0200447
Klement Sekera611864f2018-09-26 11:19:00 +0200448
Neale Ranns53f526b2019-02-25 14:32:02 +0000449class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests):
450 pass
451
452
Neale Ranns2ac885c2019-03-20 18:24:43 +0000453class IpsecTun4(object):
454
Neale Ranns987aea82019-03-27 13:40:35 +0000455 def verify_counters(self, p, count):
456 if (hasattr(p, "spd_policy_in_any")):
457 pkts = p.spd_policy_in_any.get_stats()['packets']
458 self.assertEqual(pkts, count,
459 "incorrect SPD any policy: expected %d != %d" %
460 (count, pkts))
461
462 if (hasattr(p, "tun_sa_in")):
463 pkts = p.tun_sa_in.get_stats()['packets']
464 self.assertEqual(pkts, count,
465 "incorrect SA in counts: expected %d != %d" %
466 (count, pkts))
467 pkts = p.tun_sa_out.get_stats()['packets']
468 self.assertEqual(pkts, count,
469 "incorrect SA out counts: expected %d != %d" %
470 (count, pkts))
471
472 self.assert_packet_counter_equal(self.tun4_encrypt_node_name, count)
473 self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count)
474
Neale Rannsf05e7322019-03-29 20:23:58 +0000475 def verify_decrypted(self, p, rxs):
476 for rx in rxs:
477 self.assert_equal(rx[IP].src, p.remote_tun_if_host)
478 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
479 self.assert_packet_checksums_valid(rx)
480
481 def verify_encrypted(self, p, sa, rxs):
482 decrypt_pkts = []
483 for rx in rxs:
484 try:
485 decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IP])
486 if not decrypt_pkt.haslayer(IP):
487 decrypt_pkt = IP(decrypt_pkt[Raw].load)
488 decrypt_pkts.append(decrypt_pkt)
489 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
490 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
491 except:
492 self.logger.debug(ppp("Unexpected packet:", rx))
493 try:
494 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
495 except:
496 pass
497 raise
498 pkts = reassemble4(decrypt_pkts)
499 for pkt in pkts:
500 self.assert_packet_checksums_valid(pkt)
501
Neale Rannsd7603d92019-03-28 08:56:10 +0000502 def verify_tun_44(self, p, count=1, payload_size=64, n_rx=None):
Klement Sekera10d066e2018-11-13 11:12:57 +0100503 self.vapi.cli("clear errors")
Neale Rannsd7603d92019-03-28 08:56:10 +0000504 if not n_rx:
505 n_rx = count
Klement Sekera31da2e32018-06-24 22:49:55 +0200506 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000507 config_tun_params(p, self.encryption_type, self.tun_if)
508 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200509 src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200510 dst=self.pg1.remote_ip4,
511 count=count)
Klement Sekera611864f2018-09-26 11:19:00 +0200512 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Neale Rannsf05e7322019-03-29 20:23:58 +0000513 self.verify_decrypted(p, recv_pkts)
514
Klement Sekera31da2e32018-06-24 22:49:55 +0200515 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
Neale Rannsd7603d92019-03-28 08:56:10 +0000516 dst=p.remote_tun_if_host, count=count,
517 payload_size=payload_size)
518 recv_pkts = self.send_and_expect(self.pg1, send_pkts,
519 self.tun_if, n_rx)
Neale Rannsf05e7322019-03-29 20:23:58 +0000520 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
521
Klement Sekera31da2e32018-06-24 22:49:55 +0200522 finally:
523 self.logger.info(self.vapi.ppcli("show error"))
524 self.logger.info(self.vapi.ppcli("show ipsec"))
525
Neale Ranns987aea82019-03-27 13:40:35 +0000526 self.verify_counters(p, count)
Neale Rannseba31ec2019-02-17 18:04:27 +0000527
Neale Ranns987aea82019-03-27 13:40:35 +0000528 def verify_tun_64(self, p, count=1):
529 self.vapi.cli("clear errors")
530 try:
531 config_tun_params(p, self.encryption_type, self.tun_if)
532 send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if,
533 src=p.remote_tun_if_host6,
534 dst=self.pg1.remote_ip6,
535 count=count)
536 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
537 for recv_pkt in recv_pkts:
538 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host6)
539 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
540 self.assert_packet_checksums_valid(recv_pkt)
541 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
542 dst=p.remote_tun_if_host6, count=count)
543 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
544 for recv_pkt in recv_pkts:
545 try:
546 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP])
547 if not decrypt_pkt.haslayer(IPv6):
548 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
549 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
550 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host6)
551 self.assert_packet_checksums_valid(decrypt_pkt)
552 except:
553 self.logger.error(ppp("Unexpected packet:", recv_pkt))
554 try:
555 self.logger.debug(
556 ppp("Decrypted packet:", decrypt_pkt))
557 except:
558 pass
559 raise
560 finally:
561 self.logger.info(self.vapi.ppcli("show error"))
562 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Rannseba31ec2019-02-17 18:04:27 +0000563
Neale Ranns987aea82019-03-27 13:40:35 +0000564 self.verify_counters(p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +0100565
Neale Ranns2ac885c2019-03-20 18:24:43 +0000566
567class IpsecTun4Tests(IpsecTun4):
568
569 def test_tun_basic44(self):
570 """ ipsec 4o4 tunnel basic test """
571 self.verify_tun_44(self.params[socket.AF_INET], count=1)
572
Klement Sekera611864f2018-09-26 11:19:00 +0200573 def test_tun_burst44(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200574 """ ipsec 4o4 tunnel burst test """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000575 self.verify_tun_44(self.params[socket.AF_INET], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200576
577
Neale Ranns2ac885c2019-03-20 18:24:43 +0000578class IpsecTun6(object):
579
Neale Ranns987aea82019-03-27 13:40:35 +0000580 def verify_counters(self, p, count):
581 if (hasattr(p, "tun_sa_in")):
582 pkts = p.tun_sa_in.get_stats()['packets']
583 self.assertEqual(pkts, count,
584 "incorrect SA in counts: expected %d != %d" %
585 (count, pkts))
586 pkts = p.tun_sa_out.get_stats()['packets']
587 self.assertEqual(pkts, count,
588 "incorrect SA out counts: expected %d != %d" %
589 (count, pkts))
590 self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count)
591 self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count)
592
Neale Ranns2ac885c2019-03-20 18:24:43 +0000593 def verify_tun_66(self, p, count=1):
Klement Sekera611864f2018-09-26 11:19:00 +0200594 """ ipsec 6o6 tunnel basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100595 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200596 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000597 config_tun_params(p, self.encryption_type, self.tun_if)
598 send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200599 src=p.remote_tun_if_host,
600 dst=self.pg1.remote_ip6,
601 count=count)
602 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
603 for recv_pkt in recv_pkts:
604 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host)
605 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
606 self.assert_packet_checksums_valid(recv_pkt)
607 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
608 dst=p.remote_tun_if_host,
609 count=count)
610 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
611 for recv_pkt in recv_pkts:
612 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000613 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200614 if not decrypt_pkt.haslayer(IPv6):
615 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
616 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
617 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
618 self.assert_packet_checksums_valid(decrypt_pkt)
619 except:
620 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
621 try:
622 self.logger.debug(
623 ppp("Decrypted packet:", decrypt_pkt))
624 except:
625 pass
626 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200627 finally:
628 self.logger.info(self.vapi.ppcli("show error"))
629 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Ranns987aea82019-03-27 13:40:35 +0000630 self.verify_counters(p, count)
Klement Sekera31da2e32018-06-24 22:49:55 +0200631
Neale Ranns987aea82019-03-27 13:40:35 +0000632 def verify_tun_46(self, p, count=1):
633 """ ipsec 4o6 tunnel basic test """
634 self.vapi.cli("clear errors")
635 try:
636 config_tun_params(p, self.encryption_type, self.tun_if)
637 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
638 src=p.remote_tun_if_host4,
639 dst=self.pg1.remote_ip4,
640 count=count)
641 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
642 for recv_pkt in recv_pkts:
643 self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host4)
644 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
645 self.assert_packet_checksums_valid(recv_pkt)
646 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
647 dst=p.remote_tun_if_host4,
648 count=count)
649 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
650 for recv_pkt in recv_pkts:
651 try:
652 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6])
653 if not decrypt_pkt.haslayer(IP):
654 decrypt_pkt = IP(decrypt_pkt[Raw].load)
655 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
656 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host4)
657 self.assert_packet_checksums_valid(decrypt_pkt)
658 except:
659 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
660 try:
661 self.logger.debug(ppp("Decrypted packet:",
662 decrypt_pkt))
663 except:
664 pass
665 raise
666 finally:
667 self.logger.info(self.vapi.ppcli("show error"))
668 self.logger.info(self.vapi.ppcli("show ipsec"))
669 self.verify_counters(p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +0100670
Neale Ranns2ac885c2019-03-20 18:24:43 +0000671
672class IpsecTun6Tests(IpsecTun6):
673
674 def test_tun_basic66(self):
675 """ ipsec 6o6 tunnel basic test """
676 self.verify_tun_66(self.params[socket.AF_INET6], count=1)
677
Klement Sekera611864f2018-09-26 11:19:00 +0200678 def test_tun_burst66(self):
679 """ ipsec 6o6 tunnel burst test """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000680 self.verify_tun_66(self.params[socket.AF_INET6], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200681
682
Neale Ranns53f526b2019-02-25 14:32:02 +0000683class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests):
Klement Sekera611864f2018-09-26 11:19:00 +0200684 pass
685
Klement Sekera31da2e32018-06-24 22:49:55 +0200686
687if __name__ == '__main__':
688 unittest.main(testRunner=VppTestRunner)