blob: 78d75844d5d752285a5252c2f98e79191d7f4c6f [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
Neale Ranns2ac885c2019-03-20 18:24:43 +000082def config_tun_params(p, encryption_type, tun_if):
83 ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
84 p.scapy_tun_sa = SecurityAssociation(
85 encryption_type, spi=p.vpp_tun_spi,
86 crypt_algo=p.crypt_algo, crypt_key=p.crypt_key,
87 auth_algo=p.auth_algo, auth_key=p.auth_key,
88 tunnel_header=ip_class_by_addr_type[p.addr_type](
89 src=tun_if.remote_addr[p.addr_type],
90 dst=tun_if.local_addr[p.addr_type]),
91 nat_t_header=p.nat_header)
92 p.vpp_tun_sa = SecurityAssociation(
93 encryption_type, spi=p.scapy_tun_spi,
94 crypt_algo=p.crypt_algo, crypt_key=p.crypt_key,
95 auth_algo=p.auth_algo, auth_key=p.auth_key,
96 tunnel_header=ip_class_by_addr_type[p.addr_type](
97 dst=tun_if.remote_addr[p.addr_type],
98 src=tun_if.local_addr[p.addr_type]),
99 nat_t_header=p.nat_header)
100
101
102def config_tra_params(p, encryption_type):
103 p.scapy_tra_sa = SecurityAssociation(
104 encryption_type,
105 spi=p.vpp_tra_spi,
106 crypt_algo=p.crypt_algo,
107 crypt_key=p.crypt_key,
108 auth_algo=p.auth_algo,
109 auth_key=p.auth_key,
110 nat_t_header=p.nat_header)
111 p.vpp_tra_sa = SecurityAssociation(
112 encryption_type,
113 spi=p.scapy_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,
118 nat_t_header=p.nat_header)
119
120
Klement Sekera31da2e32018-06-24 22:49:55 +0200121class TemplateIpsec(VppTestCase):
122 """
123 TRANSPORT MODE:
124
125 ------ encrypt ---
126 |tra_if| <-------> |VPP|
127 ------ decrypt ---
128
129 TUNNEL MODE:
130
131 ------ encrypt --- plain ---
132 |tun_if| <------- |VPP| <------ |pg1|
133 ------ --- ---
134
135 ------ decrypt --- plain ---
136 |tun_if| -------> |VPP| ------> |pg1|
137 ------ --- ---
138 """
139
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800140 def ipsec_select_backend(self):
Klement Sekerab4d30532018-11-08 13:00:02 +0100141 """ empty method to be overloaded when necessary """
142 pass
143
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800144 def setUp(self):
145 super(TemplateIpsec, self).setUp()
146
147 self.ipv4_params = IPsecIPv4Params()
148 self.ipv6_params = IPsecIPv6Params()
149 self.params = {self.ipv4_params.addr_type: self.ipv4_params,
150 self.ipv6_params.addr_type: self.ipv6_params}
151
152 self.payload = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"\
153 "XXXXXXXXXXXXXXXXXXXXX"
154
155 self.tun_spd_id = 1
156 self.tra_spd_id = 2
157
Neale Ranns17dcec02019-01-09 21:22:20 -0800158 self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t.
159 IPSEC_API_PROTO_ESP)
160 self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t.
161 IPSEC_API_PROTO_AH)
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800162
163 self.create_pg_interfaces(range(3))
164 self.interfaces = list(self.pg_interfaces)
165 for i in self.interfaces:
Klement Sekera31da2e32018-06-24 22:49:55 +0200166 i.admin_up()
167 i.config_ip4()
168 i.resolve_arp()
Klement Sekera611864f2018-09-26 11:19:00 +0200169 i.config_ip6()
170 i.resolve_ndp()
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800171 self.ipsec_select_backend()
Klement Sekera31da2e32018-06-24 22:49:55 +0200172
173 def tearDown(self):
174 super(TemplateIpsec, self).tearDown()
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800175
176 for i in self.interfaces:
177 i.admin_down()
178 i.unconfig_ip4()
179 i.unconfig_ip6()
180
Klement Sekera31da2e32018-06-24 22:49:55 +0200181 if not self.vpp_dead:
182 self.vapi.cli("show hardware")
183
Klement Sekera31da2e32018-06-24 22:49:55 +0200184 def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1):
185 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
186 sa.encrypt(IP(src=src, dst=dst) / ICMP() / self.payload)
187 for i in range(count)]
188
Klement Sekera611864f2018-09-26 11:19:00 +0200189 def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1):
190 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
191 sa.encrypt(IPv6(src=src, dst=dst) /
192 ICMPv6EchoRequest(id=0, seq=1, data=self.payload))
193 for i in range(count)]
194
Klement Sekera31da2e32018-06-24 22:49:55 +0200195 def gen_pkts(self, sw_intf, src, dst, count=1):
196 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
197 IP(src=src, dst=dst) / ICMP() / self.payload
198 for i in range(count)]
199
Klement Sekera611864f2018-09-26 11:19:00 +0200200 def gen_pkts6(self, sw_intf, src, dst, count=1):
201 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
202 IPv6(src=src, dst=dst) /
203 ICMPv6EchoRequest(id=0, seq=1, data=self.payload)
204 for i in range(count)]
205
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800206 def configure_sa_tra(self, params):
Neale Ranns53f526b2019-02-25 14:32:02 +0000207 params.scapy_tra_sa = SecurityAssociation(
208 self.encryption_type,
209 spi=params.vpp_tra_spi,
210 crypt_algo=params.crypt_algo,
211 crypt_key=params.crypt_key,
212 auth_algo=params.auth_algo,
213 auth_key=params.auth_key,
214 nat_t_header=params.nat_header)
215 params.vpp_tra_sa = SecurityAssociation(
216 self.encryption_type,
217 spi=params.scapy_tra_spi,
218 crypt_algo=params.crypt_algo,
219 crypt_key=params.crypt_key,
220 auth_algo=params.auth_algo,
221 auth_key=params.auth_key,
222 nat_t_header=params.nat_header)
Klement Sekera31da2e32018-06-24 22:49:55 +0200223
224
225class IpsecTcpTests(object):
226 def test_tcp_checksum(self):
227 """ verify checksum correctness for vpp generated packets """
228 self.vapi.cli("test http server")
Klement Sekera611864f2018-09-26 11:19:00 +0200229 p = self.params[socket.AF_INET]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000230 config_tun_params(p, self.encryption_type, self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200231 send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
Neale Ranns2ac885c2019-03-20 18:24:43 +0000232 p.scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host,
233 dst=self.tun_if.local_ip4) /
234 TCP(flags='S', dport=80)))
Klement Sekera31da2e32018-06-24 22:49:55 +0200235 self.logger.debug(ppp("Sending packet:", send))
Klement Sekera611864f2018-09-26 11:19:00 +0200236 recv = self.send_and_expect(self.tun_if, [send], self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200237 recv = recv[0]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000238 decrypted = p.vpp_tun_sa.decrypt(recv[IP])
Klement Sekera31da2e32018-06-24 22:49:55 +0200239 self.assert_packet_checksums_valid(decrypted)
240
241
Neale Ranns53f526b2019-02-25 14:32:02 +0000242class IpsecTra4Tests(object):
Neale Rannsde847272018-11-28 01:38:34 -0800243 def test_tra_anti_replay(self, count=1):
244 """ ipsec v4 transport anti-reply test """
245 p = self.params[socket.AF_INET]
246
247 # fire in a packet with seq number 1
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=1))
254 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
255
256 # now move the window over to 235
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=235))
263 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
264
265 # the window size is 64 packets
266 # in window are still accepted
267 pkt = (Ether(src=self.tra_if.remote_mac,
268 dst=self.tra_if.local_mac) /
269 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
270 dst=self.tra_if.local_ip4) /
271 ICMP(),
272 seq_num=172))
273 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
274
275 # out of window are dropped
276 pkt = (Ether(src=self.tra_if.remote_mac,
277 dst=self.tra_if.local_mac) /
278 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
279 dst=self.tra_if.local_ip4) /
280 ICMP(),
281 seq_num=17))
282 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
283
Klement Sekera14d7e902018-12-10 13:46:09 +0100284 self.assert_packet_counter_equal(
285 '/err/%s/SA replayed packet' % self.tra4_decrypt_node_name, 17)
Neale Rannsde847272018-11-28 01:38:34 -0800286
287 # a packet that does not decrypt does not move the window forward
288 bogus_sa = SecurityAssociation(self.encryption_type,
289 p.vpp_tra_spi)
290 pkt = (Ether(src=self.tra_if.remote_mac,
291 dst=self.tra_if.local_mac) /
292 bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4,
293 dst=self.tra_if.local_ip4) /
294 ICMP(),
295 seq_num=350))
296 self.send_and_assert_no_replies(self.tra_if, pkt * 17)
297
Klement Sekera14d7e902018-12-10 13:46:09 +0100298 self.assert_packet_counter_equal(
299 '/err/%s/Integrity check failed' % self.tra4_decrypt_node_name, 17)
Neale Rannsde847272018-11-28 01:38:34 -0800300
301 # which we can determine since this packet is still in the window
302 pkt = (Ether(src=self.tra_if.remote_mac,
303 dst=self.tra_if.local_mac) /
304 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
305 dst=self.tra_if.local_ip4) /
306 ICMP(),
307 seq_num=234))
Klement Sekera14d7e902018-12-10 13:46:09 +0100308 self.send_and_expect(self.tra_if, [pkt], self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800309
Neale Ranns00a44202019-03-21 16:36:28 +0000310 # move VPP's SA to just before the seq-number wrap
311 self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.scapy_tra_sa_id)
312
313 # then fire in a packet that VPP should drop becuase it causes the
314 # seq number to wrap
315 pkt = (Ether(src=self.tra_if.remote_mac,
316 dst=self.tra_if.local_mac) /
317 p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
318 dst=self.tra_if.local_ip4) /
319 ICMP(),
320 seq_num=236))
321 self.send_and_assert_no_replies(self.tra_if, [pkt])
322 self.assert_packet_counter_equal(
323 '/err/%s/sequence number cycled' % self.tra4_encrypt_node_name, 1)
324
Neale Rannsde847272018-11-28 01:38:34 -0800325 # move the security-associations seq number on to the last we used
Neale Ranns00a44202019-03-21 16:36:28 +0000326 self.vapi.cli("test ipsec sa %d seq 0x15f" % p.scapy_tra_sa_id)
Neale Rannsde847272018-11-28 01:38:34 -0800327 p.scapy_tra_sa.seq_num = 351
328 p.vpp_tra_sa.seq_num = 351
329
Klement Sekera31da2e32018-06-24 22:49:55 +0200330 def test_tra_basic(self, count=1):
331 """ ipsec v4 transport basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100332 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200333 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200334 p = self.params[socket.AF_INET]
Neale Rannsde847272018-11-28 01:38:34 -0800335 send_pkts = self.gen_encrypt_pkts(p.scapy_tra_sa, self.tra_if,
Klement Sekera31da2e32018-06-24 22:49:55 +0200336 src=self.tra_if.remote_ip4,
337 dst=self.tra_if.local_ip4,
338 count=count)
339 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
Klement Sekera611864f2018-09-26 11:19:00 +0200340 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800341 for rx in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200342 try:
Neale Rannsde847272018-11-28 01:38:34 -0800343 decrypted = p.vpp_tra_sa.decrypt(rx[IP])
Klement Sekera611864f2018-09-26 11:19:00 +0200344 self.assert_packet_checksums_valid(decrypted)
345 except:
Neale Rannsde847272018-11-28 01:38:34 -0800346 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200347 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200348 finally:
349 self.logger.info(self.vapi.ppcli("show error"))
350 self.logger.info(self.vapi.ppcli("show ipsec"))
351
Neale Rannseba31ec2019-02-17 18:04:27 +0000352 pkts = p.tra_sa_in.get_stats()['packets']
353 self.assertEqual(pkts, count,
354 "incorrect SA in counts: expected %d != %d" %
355 (count, pkts))
356 pkts = p.tra_sa_out.get_stats()['packets']
357 self.assertEqual(pkts, count,
358 "incorrect SA out counts: expected %d != %d" %
359 (count, pkts))
360
Klement Sekera10d066e2018-11-13 11:12:57 +0100361 self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count)
362 self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count)
363
Klement Sekera31da2e32018-06-24 22:49:55 +0200364 def test_tra_burst(self):
365 """ ipsec v4 transport burst test """
Klement Sekera611864f2018-09-26 11:19:00 +0200366 self.test_tra_basic(count=257)
367
Neale Ranns53f526b2019-02-25 14:32:02 +0000368
369class IpsecTra6Tests(object):
Klement Sekera611864f2018-09-26 11:19:00 +0200370 def test_tra_basic6(self, count=1):
371 """ ipsec v6 transport basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100372 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200373 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200374 p = self.params[socket.AF_INET6]
Neale Rannsde847272018-11-28 01:38:34 -0800375 send_pkts = self.gen_encrypt_pkts6(p.scapy_tra_sa, self.tra_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200376 src=self.tra_if.remote_ip6,
377 dst=self.tra_if.local_ip6,
378 count=count)
379 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
380 self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800381 for rx in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200382 try:
Neale Rannsde847272018-11-28 01:38:34 -0800383 decrypted = p.vpp_tra_sa.decrypt(rx[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200384 self.assert_packet_checksums_valid(decrypted)
385 except:
Neale Rannsde847272018-11-28 01:38:34 -0800386 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +0200387 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200388 finally:
389 self.logger.info(self.vapi.ppcli("show error"))
390 self.logger.info(self.vapi.ppcli("show ipsec"))
391
Neale Rannseba31ec2019-02-17 18:04:27 +0000392 pkts = p.tra_sa_in.get_stats()['packets']
393 self.assertEqual(pkts, count,
394 "incorrect SA in counts: expected %d != %d" %
395 (count, pkts))
396 pkts = p.tra_sa_out.get_stats()['packets']
397 self.assertEqual(pkts, count,
398 "incorrect SA out counts: expected %d != %d" %
399 (count, pkts))
Klement Sekera10d066e2018-11-13 11:12:57 +0100400 self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count)
401 self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count)
402
Klement Sekera611864f2018-09-26 11:19:00 +0200403 def test_tra_burst6(self):
404 """ ipsec v6 transport burst test """
405 self.test_tra_basic6(count=257)
Klement Sekera31da2e32018-06-24 22:49:55 +0200406
Klement Sekera611864f2018-09-26 11:19:00 +0200407
Neale Ranns53f526b2019-02-25 14:32:02 +0000408class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests):
409 pass
410
411
Neale Ranns2ac885c2019-03-20 18:24:43 +0000412class IpsecTun4(object):
413
414 def verify_tun_44(self, p, count=1):
Klement Sekera10d066e2018-11-13 11:12:57 +0100415 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200416 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000417 config_tun_params(p, self.encryption_type, self.tun_if)
418 send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200419 src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200420 dst=self.pg1.remote_ip4,
421 count=count)
Klement Sekera611864f2018-09-26 11:19:00 +0200422 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Klement Sekera31da2e32018-06-24 22:49:55 +0200423 for recv_pkt in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200424 self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host)
Klement Sekera31da2e32018-06-24 22:49:55 +0200425 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
426 self.assert_packet_checksums_valid(recv_pkt)
427 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
Klement Sekera611864f2018-09-26 11:19:00 +0200428 dst=p.remote_tun_if_host, count=count)
429 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200430 for recv_pkt in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200431 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000432 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP])
Klement Sekera611864f2018-09-26 11:19:00 +0200433 if not decrypt_pkt.haslayer(IP):
434 decrypt_pkt = IP(decrypt_pkt[Raw].load)
435 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
436 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
437 self.assert_packet_checksums_valid(decrypt_pkt)
438 except:
439 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
440 try:
441 self.logger.debug(
442 ppp("Decrypted packet:", decrypt_pkt))
443 except:
444 pass
445 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200446 finally:
447 self.logger.info(self.vapi.ppcli("show error"))
448 self.logger.info(self.vapi.ppcli("show ipsec"))
449
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800450 if (hasattr(p, "spd_policy_in_any")):
451 pkts = p.spd_policy_in_any.get_stats()['packets']
452 self.assertEqual(pkts, count,
453 "incorrect SPD any policy: expected %d != %d" %
454 (count, pkts))
Neale Rannseba31ec2019-02-17 18:04:27 +0000455
456 if (hasattr(p, "tun_sa_in")):
457 pkts = p.tun_sa_in.get_stats()['packets']
458 self.assertEqual(pkts, count,
459 "incorrect SA in counts: expected %d != %d" %
460 (count, pkts))
461 pkts = p.tun_sa_out.get_stats()['packets']
462 self.assertEqual(pkts, count,
463 "incorrect SA out counts: expected %d != %d" %
464 (count, pkts))
465
Klement Sekera10d066e2018-11-13 11:12:57 +0100466 self.assert_packet_counter_equal(self.tun4_encrypt_node_name, count)
467 self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count)
468
Neale Ranns2ac885c2019-03-20 18:24:43 +0000469
470class IpsecTun4Tests(IpsecTun4):
471
472 def test_tun_basic44(self):
473 """ ipsec 4o4 tunnel basic test """
474 self.verify_tun_44(self.params[socket.AF_INET], count=1)
475
Klement Sekera611864f2018-09-26 11:19:00 +0200476 def test_tun_burst44(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200477 """ ipsec 4o4 tunnel burst test """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000478 self.verify_tun_44(self.params[socket.AF_INET], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200479
480
Neale Ranns2ac885c2019-03-20 18:24:43 +0000481class IpsecTun6(object):
482
483 def verify_tun_66(self, p, count=1):
Klement Sekera611864f2018-09-26 11:19:00 +0200484 """ ipsec 6o6 tunnel basic test """
Klement Sekera10d066e2018-11-13 11:12:57 +0100485 self.vapi.cli("clear errors")
Klement Sekera31da2e32018-06-24 22:49:55 +0200486 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000487 config_tun_params(p, self.encryption_type, self.tun_if)
488 send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200489 src=p.remote_tun_if_host,
490 dst=self.pg1.remote_ip6,
491 count=count)
492 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
493 for recv_pkt in recv_pkts:
494 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host)
495 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
496 self.assert_packet_checksums_valid(recv_pkt)
497 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
498 dst=p.remote_tun_if_host,
499 count=count)
500 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
501 for recv_pkt in recv_pkts:
502 try:
Neale Ranns2ac885c2019-03-20 18:24:43 +0000503 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +0200504 if not decrypt_pkt.haslayer(IPv6):
505 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
506 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
507 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
508 self.assert_packet_checksums_valid(decrypt_pkt)
509 except:
510 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
511 try:
512 self.logger.debug(
513 ppp("Decrypted packet:", decrypt_pkt))
514 except:
515 pass
516 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200517 finally:
518 self.logger.info(self.vapi.ppcli("show error"))
519 self.logger.info(self.vapi.ppcli("show ipsec"))
520
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400521 if (hasattr(p, "tun_sa_in")):
522 pkts = p.tun_sa_in.get_stats()['packets']
523 self.assertEqual(pkts, count,
524 "incorrect SA in counts: expected %d != %d" %
525 (count, pkts))
526 pkts = p.tun_sa_out.get_stats()['packets']
527 self.assertEqual(pkts, count,
528 "incorrect SA out counts: expected %d != %d" %
529 (count, pkts))
Klement Sekera10d066e2018-11-13 11:12:57 +0100530 self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count)
531 self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count)
532
Neale Ranns2ac885c2019-03-20 18:24:43 +0000533
534class IpsecTun6Tests(IpsecTun6):
535
536 def test_tun_basic66(self):
537 """ ipsec 6o6 tunnel basic test """
538 self.verify_tun_66(self.params[socket.AF_INET6], count=1)
539
Klement Sekera611864f2018-09-26 11:19:00 +0200540 def test_tun_burst66(self):
541 """ ipsec 6o6 tunnel burst test """
Neale Ranns2ac885c2019-03-20 18:24:43 +0000542 self.verify_tun_66(self.params[socket.AF_INET6], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +0200543
544
Neale Ranns53f526b2019-02-25 14:32:02 +0000545class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests):
Klement Sekera611864f2018-09-26 11:19:00 +0200546 pass
547
Klement Sekera31da2e32018-06-24 22:49:55 +0200548
549if __name__ == '__main__':
550 unittest.main(testRunner=VppTestRunner)