blob: 483699c2eac334fee71ca148b9027e913c7bdcb5 [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
10from util import ppp
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'
Klement Sekera611864f2018-09-26 11:19:00 +020024
Neale Ranns17dcec02019-01-09 21:22:20 -080025 self.scapy_tun_sa_id = 10
26 self.scapy_tun_spi = 1001
27 self.vpp_tun_sa_id = 20
28 self.vpp_tun_spi = 1000
Klement Sekera611864f2018-09-26 11:19:00 +020029
Neale Ranns17dcec02019-01-09 21:22:20 -080030 self.scapy_tra_sa_id = 30
31 self.scapy_tra_spi = 2001
32 self.vpp_tra_sa_id = 40
33 self.vpp_tra_spi = 2000
Klement Sekera611864f2018-09-26 11:19:00 +020034
Neale Ranns17dcec02019-01-09 21:22:20 -080035 self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t.
36 IPSEC_API_INTEG_ALG_SHA1_96)
37 self.auth_algo = 'HMAC-SHA1-96' # scapy name
38 self.auth_key = 'C91KUR9GYMm5GfkEvNjX'
39
40 self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t.
41 IPSEC_API_CRYPTO_ALG_AES_CBC_128)
42 self.crypt_algo = 'AES-CBC' # scapy name
43 self.crypt_key = 'JPjyOWBeVEQiMe7h'
Neale Ranns53f526b2019-02-25 14:32:02 +000044 self.flags = 0
45 self.nat_header = None
Klement Sekera611864f2018-09-26 11:19:00 +020046
47
48class IPsecIPv6Params(object):
Neale Ranns17dcec02019-01-09 21:22:20 -080049
Klement Sekera611864f2018-09-26 11:19:00 +020050 addr_type = socket.AF_INET6
51 addr_any = "0::0"
52 addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
53 addr_len = 128
54 is_ipv6 = 1
Klement Sekera611864f2018-09-26 11:19:00 +020055
Neale Ranns17dcec02019-01-09 21:22:20 -080056 def __init__(self):
57 self.remote_tun_if_host = '1111:1111:1111:1111:1111:1111:1111:1111'
Klement Sekera611864f2018-09-26 11:19:00 +020058
Neale Ranns17dcec02019-01-09 21:22:20 -080059 self.scapy_tun_sa_id = 50
60 self.scapy_tun_spi = 3001
61 self.vpp_tun_sa_id = 60
62 self.vpp_tun_spi = 3000
Klement Sekera611864f2018-09-26 11:19:00 +020063
Neale Ranns17dcec02019-01-09 21:22:20 -080064 self.scapy_tra_sa_id = 70
65 self.scapy_tra_spi = 4001
66 self.vpp_tra_sa_id = 80
67 self.vpp_tra_spi = 4000
Klement Sekera611864f2018-09-26 11:19:00 +020068
Neale Ranns17dcec02019-01-09 21:22:20 -080069 self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t.
70 IPSEC_API_INTEG_ALG_SHA_256_128)
71 self.auth_algo = 'SHA2-256-128' # scapy name
72 self.auth_key = 'C91KUR9GYMm5GfkEvNjX'
73
74 self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t.
75 IPSEC_API_CRYPTO_ALG_AES_CBC_256)
76 self.crypt_algo = 'AES-CBC' # scapy name
77 self.crypt_key = 'JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h'
Neale Ranns53f526b2019-02-25 14:32:02 +000078 self.flags = 0
79 self.nat_header = None
Klement Sekera611864f2018-09-26 11:19:00 +020080
81
Klement Sekera31da2e32018-06-24 22:49:55 +020082class TemplateIpsec(VppTestCase):
83 """
84 TRANSPORT MODE:
85
86 ------ encrypt ---
87 |tra_if| <-------> |VPP|
88 ------ decrypt ---
89
90 TUNNEL MODE:
91
92 ------ encrypt --- plain ---
93 |tun_if| <------- |VPP| <------ |pg1|
94 ------ --- ---
95
96 ------ decrypt --- plain ---
97 |tun_if| -------> |VPP| ------> |pg1|
98 ------ --- ---
99 """
100
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800101 def ipsec_select_backend(self):
Klement Sekerab4d30532018-11-08 13:00:02 +0100102 """ empty method to be overloaded when necessary """
103 pass
104
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800105 def setUp(self):
106 super(TemplateIpsec, self).setUp()
107
108 self.ipv4_params = IPsecIPv4Params()
109 self.ipv6_params = IPsecIPv6Params()
110 self.params = {self.ipv4_params.addr_type: self.ipv4_params,
111 self.ipv6_params.addr_type: self.ipv6_params}
112
113 self.payload = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"\
114 "XXXXXXXXXXXXXXXXXXXXX"
115
116 self.tun_spd_id = 1
117 self.tra_spd_id = 2
118
Neale Ranns17dcec02019-01-09 21:22:20 -0800119 self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t.
120 IPSEC_API_PROTO_ESP)
121 self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t.
122 IPSEC_API_PROTO_AH)
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800123
124 self.create_pg_interfaces(range(3))
125 self.interfaces = list(self.pg_interfaces)
126 for i in self.interfaces:
Klement Sekera31da2e32018-06-24 22:49:55 +0200127 i.admin_up()
128 i.config_ip4()
129 i.resolve_arp()
Klement Sekera611864f2018-09-26 11:19:00 +0200130 i.config_ip6()
131 i.resolve_ndp()
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800132 self.ipsec_select_backend()
Klement Sekera31da2e32018-06-24 22:49:55 +0200133
134 def tearDown(self):
135 super(TemplateIpsec, self).tearDown()
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800136
137 for i in self.interfaces:
138 i.admin_down()
139 i.unconfig_ip4()
140 i.unconfig_ip6()
141
Klement Sekera31da2e32018-06-24 22:49:55 +0200142 if not self.vpp_dead:
143 self.vapi.cli("show hardware")
144
Klement Sekera31da2e32018-06-24 22:49:55 +0200145 def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1):
146 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
147 sa.encrypt(IP(src=src, dst=dst) / ICMP() / self.payload)
148 for i in range(count)]
149
Klement Sekera611864f2018-09-26 11:19:00 +0200150 def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1):
151 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
152 sa.encrypt(IPv6(src=src, dst=dst) /
153 ICMPv6EchoRequest(id=0, seq=1, data=self.payload))
154 for i in range(count)]
155
Klement Sekera31da2e32018-06-24 22:49:55 +0200156 def gen_pkts(self, sw_intf, src, dst, count=1):
157 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
158 IP(src=src, dst=dst) / ICMP() / self.payload
159 for i in range(count)]
160
Klement Sekera611864f2018-09-26 11:19:00 +0200161 def gen_pkts6(self, sw_intf, src, dst, count=1):
162 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
163 IPv6(src=src, dst=dst) /
164 ICMPv6EchoRequest(id=0, seq=1, data=self.payload)
165 for i in range(count)]
166
167 def configure_sa_tun(self, params):
168 ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
169 scapy_tun_sa = SecurityAssociation(
170 self.encryption_type, spi=params.vpp_tun_spi,
171 crypt_algo=params.crypt_algo, crypt_key=params.crypt_key,
172 auth_algo=params.auth_algo, auth_key=params.auth_key,
173 tunnel_header=ip_class_by_addr_type[params.addr_type](
174 src=self.tun_if.remote_addr[params.addr_type],
Neale Ranns53f526b2019-02-25 14:32:02 +0000175 dst=self.tun_if.local_addr[params.addr_type]),
176 nat_t_header=params.nat_header)
Klement Sekera611864f2018-09-26 11:19:00 +0200177 vpp_tun_sa = SecurityAssociation(
178 self.encryption_type, spi=params.scapy_tun_spi,
179 crypt_algo=params.crypt_algo, crypt_key=params.crypt_key,
180 auth_algo=params.auth_algo, auth_key=params.auth_key,
181 tunnel_header=ip_class_by_addr_type[params.addr_type](
182 dst=self.tun_if.remote_addr[params.addr_type],
Neale Ranns53f526b2019-02-25 14:32:02 +0000183 src=self.tun_if.local_addr[params.addr_type]),
184 nat_t_header=params.nat_header)
Klement Sekera31da2e32018-06-24 22:49:55 +0200185 return vpp_tun_sa, scapy_tun_sa
186
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800187 def configure_sa_tra(self, params):
Neale Ranns53f526b2019-02-25 14:32:02 +0000188 params.scapy_tra_sa = SecurityAssociation(
189 self.encryption_type,
190 spi=params.vpp_tra_spi,
191 crypt_algo=params.crypt_algo,
192 crypt_key=params.crypt_key,
193 auth_algo=params.auth_algo,
194 auth_key=params.auth_key,
195 nat_t_header=params.nat_header)
196 params.vpp_tra_sa = SecurityAssociation(
197 self.encryption_type,
198 spi=params.scapy_tra_spi,
199 crypt_algo=params.crypt_algo,
200 crypt_key=params.crypt_key,
201 auth_algo=params.auth_algo,
202 auth_key=params.auth_key,
203 nat_t_header=params.nat_header)
Klement Sekera31da2e32018-06-24 22:49:55 +0200204
205
206class IpsecTcpTests(object):
207 def test_tcp_checksum(self):
208 """ verify checksum correctness for vpp generated packets """
209 self.vapi.cli("test http server")
Klement Sekera611864f2018-09-26 11:19:00 +0200210 p = self.params[socket.AF_INET]
211 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p)
Klement Sekera31da2e32018-06-24 22:49:55 +0200212 send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
Klement Sekera611864f2018-09-26 11:19:00 +0200213 scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200214 dst=self.tun_if.local_ip4) /
215 TCP(flags='S', dport=80)))
216 self.logger.debug(ppp("Sending packet:", send))
Klement Sekera611864f2018-09-26 11:19:00 +0200217 recv = self.send_and_expect(self.tun_if, [send], self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200218 recv = recv[0]
219 decrypted = vpp_tun_sa.decrypt(recv[IP])
220 self.assert_packet_checksums_valid(decrypted)
221
222
Neale Ranns53f526b2019-02-25 14:32:02 +0000223class IpsecTra4Tests(object):
Neale Rannsde847272018-11-28 01:38:34 -0800224 def test_tra_anti_replay(self, count=1):
225 """ ipsec v4 transport anti-reply test """
226 p = self.params[socket.AF_INET]
227
228 # fire in a packet with seq number 1
229 pkt = (Ether(src=self.tra_if.remote_mac,
230 dst=self.tra_if.local_mac) /
231 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
232 dst=self.tra_if.local_ip4) /
233 ICMP(),
234 seq_num=1))
235 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
236
237 # now move the window over to 235
238 pkt = (Ether(src=self.tra_if.remote_mac,
239 dst=self.tra_if.local_mac) /
240 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
241 dst=self.tra_if.local_ip4) /
242 ICMP(),
243 seq_num=235))
244 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
245
246 # the window size is 64 packets
247 # in window are still accepted
248 pkt = (Ether(src=self.tra_if.remote_mac,
249 dst=self.tra_if.local_mac) /
250 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
251 dst=self.tra_if.local_ip4) /
252 ICMP(),
253 seq_num=172))
254 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
255
256 # out of window are dropped
257 pkt = (Ether(src=self.tra_if.remote_mac,
258 dst=self.tra_if.local_mac) /
259 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
260 dst=self.tra_if.local_ip4) /
261 ICMP(),
262 seq_num=17))
263 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
264
Klement Sekera14d7e902018-12-10 13:46:09 +0100265 self.assert_packet_counter_equal(
266 '/err/%s/SA replayed packet' % self.tra4_decrypt_node_name, 17)
Neale Rannsde847272018-11-28 01:38:34 -0800267
268 # a packet that does not decrypt does not move the window forward
269 bogus_sa = SecurityAssociation(self.encryption_type,
270 p.vpp_tra_spi)
271 pkt = (Ether(src=self.tra_if.remote_mac,
272 dst=self.tra_if.local_mac) /
273 bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4,
274 dst=self.tra_if.local_ip4) /
275 ICMP(),
276 seq_num=350))
277 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
278
Klement Sekera14d7e902018-12-10 13:46:09 +0100279 self.assert_packet_counter_equal(
280 '/err/%s/Integrity check failed' % self.tra4_decrypt_node_name, 17)
Neale Rannsde847272018-11-28 01:38:34 -0800281
282 # which we can determine since this packet is still in the window
283 pkt = (Ether(src=self.tra_if.remote_mac,
284 dst=self.tra_if.local_mac) /
285 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
286 dst=self.tra_if.local_ip4) /
287 ICMP(),
288 seq_num=234))
Klement Sekera14d7e902018-12-10 13:46:09 +0100289 self.send_and_expect(self.tra_if, [pkt], self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800290
291 # move the security-associations seq number on to the last we used
292 p.scapy_tra_sa.seq_num = 351
293 p.vpp_tra_sa.seq_num = 351
294
Klement Sekera31da2e32018-06-24 22:49:55 +0200295 def test_tra_basic(self, count=1):
296 """ ipsec v4 transport basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100297 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200298 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200299 p = self.params[socket.AF_INET]
Neale Rannsde847272018-11-28 01:38:34 -0800300 send_pkts = self.gen_encrypt_pkts(p.scapy_tra_sa, self.tra_if,
Klement Sekera31da2e32018-06-24 22:49:55 +0200301 src=self.tra_if.remote_ip4,
302 dst=self.tra_if.local_ip4,
303 count=count)
304 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
Klement Sekera611864f2018-09-26 11:19:00 +0200305 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800306 for rx in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200307 try:
Neale Rannsde847272018-11-28 01:38:34 -0800308 decrypted = p.vpp_tra_sa.decrypt(rx[IP])
Klement Sekera611864f2018-09-26 11:19:00 +0200309 self.assert_packet_checksums_valid(decrypted)
310 except:
Neale Rannsde847272018-11-28 01:38:34 -0800311 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200312 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200313 finally:
314 self.logger.info(self.vapi.ppcli("show error"))
315 self.logger.info(self.vapi.ppcli("show ipsec"))
316
Neale Rannseba31ec2019-02-17 18:04:27 +0000317 pkts = p.tra_sa_in.get_stats()['packets']
318 self.assertEqual(pkts, count,
319 "incorrect SA in counts: expected %d != %d" %
320 (count, pkts))
321 pkts = p.tra_sa_out.get_stats()['packets']
322 self.assertEqual(pkts, count,
323 "incorrect SA out counts: expected %d != %d" %
324 (count, pkts))
325
Klement Sekera10d066e2018-11-13 11:12:57 +0100326 self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count)
327 self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count)
328
Klement Sekera31da2e32018-06-24 22:49:55 +0200329 def test_tra_burst(self):
330 """ ipsec v4 transport burst test """
Klement Sekera611864f2018-09-26 11:19:00 +0200331 self.test_tra_basic(count=257)
332
Neale Ranns53f526b2019-02-25 14:32:02 +0000333
334class IpsecTra6Tests(object):
Klement Sekera611864f2018-09-26 11:19:00 +0200335 def test_tra_basic6(self, count=1):
336 """ ipsec v6 transport basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100337 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200338 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200339 p = self.params[socket.AF_INET6]
Neale Rannsde847272018-11-28 01:38:34 -0800340 send_pkts = self.gen_encrypt_pkts6(p.scapy_tra_sa, self.tra_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200341 src=self.tra_if.remote_ip6,
342 dst=self.tra_if.local_ip6,
343 count=count)
344 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
345 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800346 for rx in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200347 try:
Neale Rannsde847272018-11-28 01:38:34 -0800348 decrypted = p.vpp_tra_sa.decrypt(rx[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200349 self.assert_packet_checksums_valid(decrypted)
350 except:
Neale Rannsde847272018-11-28 01:38:34 -0800351 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200352 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200353 finally:
354 self.logger.info(self.vapi.ppcli("show error"))
355 self.logger.info(self.vapi.ppcli("show ipsec"))
356
Neale Rannseba31ec2019-02-17 18:04:27 +0000357 pkts = p.tra_sa_in.get_stats()['packets']
358 self.assertEqual(pkts, count,
359 "incorrect SA in counts: expected %d != %d" %
360 (count, pkts))
361 pkts = p.tra_sa_out.get_stats()['packets']
362 self.assertEqual(pkts, count,
363 "incorrect SA out counts: expected %d != %d" %
364 (count, pkts))
Klement Sekera10d066e2018-11-13 11:12:57 +0100365 self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count)
366 self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count)
367
Klement Sekera611864f2018-09-26 11:19:00 +0200368 def test_tra_burst6(self):
369 """ ipsec v6 transport burst test """
370 self.test_tra_basic6(count=257)
Klement Sekera31da2e32018-06-24 22:49:55 +0200371
Klement Sekera611864f2018-09-26 11:19:00 +0200372
Neale Ranns53f526b2019-02-25 14:32:02 +0000373class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests):
374 pass
375
376
Klement Sekera611864f2018-09-26 11:19:00 +0200377class IpsecTun4Tests(object):
378 def test_tun_basic44(self, count=1):
Klement Sekera31da2e32018-06-24 22:49:55 +0200379 """ ipsec 4o4 tunnel basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100380 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200381 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200382 p = self.params[socket.AF_INET]
383 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p)
Klement Sekera31da2e32018-06-24 22:49:55 +0200384 send_pkts = self.gen_encrypt_pkts(scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200385 src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200386 dst=self.pg1.remote_ip4,
387 count=count)
Klement Sekera611864f2018-09-26 11:19:00 +0200388 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Klement Sekera31da2e32018-06-24 22:49:55 +0200389 for recv_pkt in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200390 self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host)
Klement Sekera31da2e32018-06-24 22:49:55 +0200391 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
392 self.assert_packet_checksums_valid(recv_pkt)
393 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
Klement Sekera611864f2018-09-26 11:19:00 +0200394 dst=p.remote_tun_if_host, count=count)
395 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200396 for recv_pkt in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200397 try:
398 decrypt_pkt = vpp_tun_sa.decrypt(recv_pkt[IP])
399 if not decrypt_pkt.haslayer(IP):
400 decrypt_pkt = IP(decrypt_pkt[Raw].load)
401 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
402 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
403 self.assert_packet_checksums_valid(decrypt_pkt)
404 except:
405 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
406 try:
407 self.logger.debug(
408 ppp("Decrypted packet:", decrypt_pkt))
409 except:
410 pass
411 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200412 finally:
413 self.logger.info(self.vapi.ppcli("show error"))
414 self.logger.info(self.vapi.ppcli("show ipsec"))
415
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800416 if (hasattr(p, "spd_policy_in_any")):
417 pkts = p.spd_policy_in_any.get_stats()['packets']
418 self.assertEqual(pkts, count,
419 "incorrect SPD any policy: expected %d != %d" %
420 (count, pkts))
Neale Rannseba31ec2019-02-17 18:04:27 +0000421
422 if (hasattr(p, "tun_sa_in")):
423 pkts = p.tun_sa_in.get_stats()['packets']
424 self.assertEqual(pkts, count,
425 "incorrect SA in counts: expected %d != %d" %
426 (count, pkts))
427 pkts = p.tun_sa_out.get_stats()['packets']
428 self.assertEqual(pkts, count,
429 "incorrect SA out counts: expected %d != %d" %
430 (count, pkts))
431
Klement Sekera10d066e2018-11-13 11:12:57 +0100432 self.assert_packet_counter_equal(self.tun4_encrypt_node_name, count)
433 self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count)
434
Klement Sekera611864f2018-09-26 11:19:00 +0200435 def test_tun_burst44(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200436 """ ipsec 4o4 tunnel burst test """
Klement Sekera611864f2018-09-26 11:19:00 +0200437 self.test_tun_basic44(count=257)
438
439
440class IpsecTun6Tests(object):
441 def test_tun_basic66(self, count=1):
442 """ ipsec 6o6 tunnel basic test """
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]
446 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p)
447 send_pkts = self.gen_encrypt_pkts6(scapy_tun_sa, self.tun_if,
448 src=p.remote_tun_if_host,
449 dst=self.pg1.remote_ip6,
450 count=count)
451 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
452 for recv_pkt in recv_pkts:
453 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host)
454 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
455 self.assert_packet_checksums_valid(recv_pkt)
456 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
457 dst=p.remote_tun_if_host,
458 count=count)
459 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
460 for recv_pkt in recv_pkts:
461 try:
462 decrypt_pkt = vpp_tun_sa.decrypt(recv_pkt[IPv6])
463 if not decrypt_pkt.haslayer(IPv6):
464 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
465 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
466 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
467 self.assert_packet_checksums_valid(decrypt_pkt)
468 except:
469 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
470 try:
471 self.logger.debug(
472 ppp("Decrypted packet:", decrypt_pkt))
473 except:
474 pass
475 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200476 finally:
477 self.logger.info(self.vapi.ppcli("show error"))
478 self.logger.info(self.vapi.ppcli("show ipsec"))
479
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400480 if (hasattr(p, "tun_sa_in")):
481 pkts = p.tun_sa_in.get_stats()['packets']
482 self.assertEqual(pkts, count,
483 "incorrect SA in counts: expected %d != %d" %
484 (count, pkts))
485 pkts = p.tun_sa_out.get_stats()['packets']
486 self.assertEqual(pkts, count,
487 "incorrect SA out counts: expected %d != %d" %
488 (count, pkts))
Klement Sekera10d066e2018-11-13 11:12:57 +0100489 self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count)
490 self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count)
491
Klement Sekera611864f2018-09-26 11:19:00 +0200492 def test_tun_burst66(self):
493 """ ipsec 6o6 tunnel burst test """
494 self.test_tun_basic66(count=257)
495
496
Neale Ranns53f526b2019-02-25 14:32:02 +0000497class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests):
Klement Sekera611864f2018-09-26 11:19:00 +0200498 pass
499
Klement Sekera31da2e32018-06-24 22:49:55 +0200500
501if __name__ == '__main__':
502 unittest.main(testRunner=VppTestRunner)