blob: 5d835104ec48169b2df976afe5381f7a4227f7ed [file] [log] [blame]
Klement Sekera31da2e32018-06-24 22:49:55 +02001import unittest
Klement Sekera611864f2018-09-26 11:19:00 +02002import socket
Neale Ranns80f6fd52019-04-16 02:41:34 +00003import struct
Klement Sekera31da2e32018-06-24 22:49:55 +02004
Neale Ranns53f526b2019-02-25 14:32:02 +00005from scapy.layers.inet import IP, ICMP, TCP, UDP
Damjan Mariona829b132019-04-24 23:39:16 +02006from scapy.layers.ipsec import SecurityAssociation, ESP
snaramre5d4b8912019-12-13 23:39:35 +00007from scapy.layers.l2 import Ether
Paul Vinciguerra582eac52020-04-03 12:18:40 -04008from scapy.packet import raw, Raw
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02009from scapy.layers.inet6 import (
10 IPv6,
11 ICMPv6EchoRequest,
12 IPv6ExtHdrHopByHop,
13 IPv6ExtHdrFragment,
14 IPv6ExtHdrDestOpt,
15)
Neale Ranns02950402019-12-20 00:54:57 +000016
Klement Sekera31da2e32018-06-24 22:49:55 +020017
18from framework import VppTestCase, VppTestRunner
Neale Ranns14046982019-07-29 14:49:52 +000019from util import ppp, reassemble4, fragment_rfc791, fragment_rfc8200
Neale Ranns17dcec02019-01-09 21:22:20 -080020from vpp_papi import VppEnum
Klement Sekera31da2e32018-06-24 22:49:55 +020021
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020022from vpp_ipsec import VppIpsecSpd, VppIpsecSpdEntry, VppIpsecSpdItfBinding
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +000023from ipaddress import ip_address
24from re import search
25from os import popen
26
Klement Sekera31da2e32018-06-24 22:49:55 +020027
Paul Vinciguerrae061dad2020-12-04 14:57:51 -050028class IPsecIPv4Params:
Klement Sekera611864f2018-09-26 11:19:00 +020029 addr_type = socket.AF_INET
30 addr_any = "0.0.0.0"
31 addr_bcast = "255.255.255.255"
32 addr_len = 32
33 is_ipv6 = 0
Klement Sekera611864f2018-09-26 11:19:00 +020034
Neale Ranns17dcec02019-01-09 21:22:20 -080035 def __init__(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020036 self.remote_tun_if_host = "1.1.1.1"
37 self.remote_tun_if_host6 = "1111::1"
Klement Sekera611864f2018-09-26 11:19:00 +020038
Neale Ranns28287212019-12-16 00:53:11 +000039 self.scapy_tun_sa_id = 100
Neale Rannsa9e27742020-12-23 16:22:28 +000040 self.scapy_tun_spi = 1000
Neale Ranns28287212019-12-16 00:53:11 +000041 self.vpp_tun_sa_id = 200
Neale Rannsa9e27742020-12-23 16:22:28 +000042 self.vpp_tun_spi = 2000
Klement Sekera611864f2018-09-26 11:19:00 +020043
Neale Ranns28287212019-12-16 00:53:11 +000044 self.scapy_tra_sa_id = 300
Neale Rannsa9e27742020-12-23 16:22:28 +000045 self.scapy_tra_spi = 3000
Neale Ranns28287212019-12-16 00:53:11 +000046 self.vpp_tra_sa_id = 400
Neale Rannsa9e27742020-12-23 16:22:28 +000047 self.vpp_tra_spi = 4000
Klement Sekera611864f2018-09-26 11:19:00 +020048
Neale Ranns9ec846c2021-02-09 14:04:02 +000049 self.outer_hop_limit = 64
50 self.inner_hop_limit = 255
51 self.outer_flow_label = 0
52 self.inner_flow_label = 0x12345
53
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020054 self.auth_algo_vpp_id = (
55 VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA1_96
56 )
57 self.auth_algo = "HMAC-SHA1-96" # scapy name
58 self.auth_key = b"C91KUR9GYMm5GfkEvNjX"
Neale Ranns17dcec02019-01-09 21:22:20 -080059
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020060 self.crypt_algo_vpp_id = (
61 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_CBC_128
62 )
63 self.crypt_algo = "AES-CBC" # scapy name
64 self.crypt_key = b"JPjyOWBeVEQiMe7h"
Neale Ranns80f6fd52019-04-16 02:41:34 +000065 self.salt = 0
Neale Ranns53f526b2019-02-25 14:32:02 +000066 self.flags = 0
67 self.nat_header = None
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020068 self.tun_flags = (
69 VppEnum.vl_api_tunnel_encap_decap_flags_t.TUNNEL_API_ENCAP_DECAP_FLAG_NONE
70 )
Neale Ranns041add72020-01-02 04:06:10 +000071 self.dscp = 0
Neale Ranns8c609af2021-02-25 10:05:32 +000072 self.async_mode = False
Klement Sekera611864f2018-09-26 11:19:00 +020073
74
Paul Vinciguerrae061dad2020-12-04 14:57:51 -050075class IPsecIPv6Params:
Klement Sekera611864f2018-09-26 11:19:00 +020076 addr_type = socket.AF_INET6
77 addr_any = "0::0"
78 addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
79 addr_len = 128
80 is_ipv6 = 1
Klement Sekera611864f2018-09-26 11:19:00 +020081
Neale Ranns17dcec02019-01-09 21:22:20 -080082 def __init__(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020083 self.remote_tun_if_host = "1111:1111:1111:1111:1111:1111:1111:1111"
84 self.remote_tun_if_host4 = "1.1.1.1"
Klement Sekera611864f2018-09-26 11:19:00 +020085
Neale Ranns28287212019-12-16 00:53:11 +000086 self.scapy_tun_sa_id = 500
Neale Ranns17dcec02019-01-09 21:22:20 -080087 self.scapy_tun_spi = 3001
Neale Ranns28287212019-12-16 00:53:11 +000088 self.vpp_tun_sa_id = 600
Neale Ranns17dcec02019-01-09 21:22:20 -080089 self.vpp_tun_spi = 3000
Klement Sekera611864f2018-09-26 11:19:00 +020090
Neale Ranns28287212019-12-16 00:53:11 +000091 self.scapy_tra_sa_id = 700
Neale Ranns17dcec02019-01-09 21:22:20 -080092 self.scapy_tra_spi = 4001
Neale Ranns28287212019-12-16 00:53:11 +000093 self.vpp_tra_sa_id = 800
Neale Ranns17dcec02019-01-09 21:22:20 -080094 self.vpp_tra_spi = 4000
Klement Sekera611864f2018-09-26 11:19:00 +020095
Neale Ranns9ec846c2021-02-09 14:04:02 +000096 self.outer_hop_limit = 64
97 self.inner_hop_limit = 255
98 self.outer_flow_label = 0
99 self.inner_flow_label = 0x12345
100
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200101 self.auth_algo_vpp_id = (
102 VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA1_96
103 )
104 self.auth_algo = "HMAC-SHA1-96" # scapy name
105 self.auth_key = b"C91KUR9GYMm5GfkEvNjX"
Neale Ranns17dcec02019-01-09 21:22:20 -0800106
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200107 self.crypt_algo_vpp_id = (
108 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_CBC_128
109 )
110 self.crypt_algo = "AES-CBC" # scapy name
111 self.crypt_key = b"JPjyOWBeVEQiMe7h"
Neale Ranns80f6fd52019-04-16 02:41:34 +0000112 self.salt = 0
Neale Ranns53f526b2019-02-25 14:32:02 +0000113 self.flags = 0
114 self.nat_header = None
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200115 self.tun_flags = (
116 VppEnum.vl_api_tunnel_encap_decap_flags_t.TUNNEL_API_ENCAP_DECAP_FLAG_NONE
117 )
Neale Ranns041add72020-01-02 04:06:10 +0000118 self.dscp = 0
Neale Ranns8c609af2021-02-25 10:05:32 +0000119 self.async_mode = False
Klement Sekera611864f2018-09-26 11:19:00 +0200120
121
Neale Ranns12989b52019-09-26 16:20:19 +0000122def mk_scapy_crypt_key(p):
Benoît Ganne84e66582023-03-10 17:33:03 +0100123 if p.crypt_algo in ("AES-GCM", "AES-CTR", "AES-NULL-GMAC"):
Neale Ranns6afaae12019-07-17 15:07:14 +0000124 return p.crypt_key + struct.pack("!I", p.salt)
125 else:
126 return p.crypt_key
127
128
Neale Ranns2ac885c2019-03-20 18:24:43 +0000129def config_tun_params(p, encryption_type, tun_if):
130 ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200131 esn_en = bool(
132 p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN)
133 )
Neale Rannsf3a66222020-01-02 05:04:00 +0000134 p.tun_dst = tun_if.remote_addr[p.addr_type]
135 p.tun_src = tun_if.local_addr[p.addr_type]
Neale Ranns12989b52019-09-26 16:20:19 +0000136 crypt_key = mk_scapy_crypt_key(p)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000137 p.scapy_tun_sa = SecurityAssociation(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200138 encryption_type,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100139 spi=p.scapy_tun_spi,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000140 crypt_algo=p.crypt_algo,
141 crypt_key=crypt_key,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200142 auth_algo=p.auth_algo,
143 auth_key=p.auth_key,
144 tunnel_header=ip_class_by_addr_type[p.addr_type](src=p.tun_dst, dst=p.tun_src),
Neale Ranns3833ffd2019-03-21 14:34:09 +0000145 nat_t_header=p.nat_header,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200146 esn_en=esn_en,
147 )
Neale Ranns2ac885c2019-03-20 18:24:43 +0000148 p.vpp_tun_sa = SecurityAssociation(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200149 encryption_type,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100150 spi=p.vpp_tun_spi,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000151 crypt_algo=p.crypt_algo,
152 crypt_key=crypt_key,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200153 auth_algo=p.auth_algo,
154 auth_key=p.auth_key,
155 tunnel_header=ip_class_by_addr_type[p.addr_type](dst=p.tun_dst, src=p.tun_src),
Neale Ranns3833ffd2019-03-21 14:34:09 +0000156 nat_t_header=p.nat_header,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200157 esn_en=esn_en,
158 )
Neale Ranns2ac885c2019-03-20 18:24:43 +0000159
160
161def config_tra_params(p, encryption_type):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200162 esn_en = bool(
163 p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN)
164 )
Neale Ranns12989b52019-09-26 16:20:19 +0000165 crypt_key = mk_scapy_crypt_key(p)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000166 p.scapy_tra_sa = SecurityAssociation(
167 encryption_type,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100168 spi=p.scapy_tra_spi,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000169 crypt_algo=p.crypt_algo,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000170 crypt_key=crypt_key,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000171 auth_algo=p.auth_algo,
172 auth_key=p.auth_key,
Neale Ranns3833ffd2019-03-21 14:34:09 +0000173 nat_t_header=p.nat_header,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200174 esn_en=esn_en,
175 )
Neale Ranns2ac885c2019-03-20 18:24:43 +0000176 p.vpp_tra_sa = SecurityAssociation(
177 encryption_type,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100178 spi=p.vpp_tra_spi,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000179 crypt_algo=p.crypt_algo,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000180 crypt_key=crypt_key,
Neale Ranns2ac885c2019-03-20 18:24:43 +0000181 auth_algo=p.auth_algo,
182 auth_key=p.auth_key,
Neale Ranns3833ffd2019-03-21 14:34:09 +0000183 nat_t_header=p.nat_header,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200184 esn_en=esn_en,
185 )
Neale Ranns2ac885c2019-03-20 18:24:43 +0000186
187
Klement Sekera31da2e32018-06-24 22:49:55 +0200188class TemplateIpsec(VppTestCase):
189 """
Dave Wallaced1706812021-08-12 18:36:02 -0400190 TRANSPORT MODE::
Klement Sekera31da2e32018-06-24 22:49:55 +0200191
Dave Wallaced1706812021-08-12 18:36:02 -0400192 ------ encrypt ---
193 |tra_if| <-------> |VPP|
194 ------ decrypt ---
Klement Sekera31da2e32018-06-24 22:49:55 +0200195
Dave Wallaced1706812021-08-12 18:36:02 -0400196 TUNNEL MODE::
Klement Sekera31da2e32018-06-24 22:49:55 +0200197
Dave Wallaced1706812021-08-12 18:36:02 -0400198 ------ encrypt --- plain ---
199 |tun_if| <------- |VPP| <------ |pg1|
200 ------ --- ---
Klement Sekera31da2e32018-06-24 22:49:55 +0200201
Dave Wallaced1706812021-08-12 18:36:02 -0400202 ------ decrypt --- plain ---
203 |tun_if| -------> |VPP| ------> |pg1|
204 ------ --- ---
Klement Sekera31da2e32018-06-24 22:49:55 +0200205 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200206
Neale Ranns4f33c802019-04-10 12:39:10 +0000207 tun_spd_id = 1
208 tra_spd_id = 2
Klement Sekera31da2e32018-06-24 22:49:55 +0200209
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800210 def ipsec_select_backend(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200211 """empty method to be overloaded when necessary"""
Klement Sekerab4d30532018-11-08 13:00:02 +0100212 pass
213
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700214 @classmethod
215 def setUpClass(cls):
216 super(TemplateIpsec, cls).setUpClass()
217
218 @classmethod
219 def tearDownClass(cls):
220 super(TemplateIpsec, cls).tearDownClass()
221
Neale Ranns3833ffd2019-03-21 14:34:09 +0000222 def setup_params(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200223 if not hasattr(self, "ipv4_params"):
Neale Ranns041add72020-01-02 04:06:10 +0000224 self.ipv4_params = IPsecIPv4Params()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200225 if not hasattr(self, "ipv6_params"):
Neale Ranns041add72020-01-02 04:06:10 +0000226 self.ipv6_params = IPsecIPv6Params()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200227 self.params = {
228 self.ipv4_params.addr_type: self.ipv4_params,
229 self.ipv6_params.addr_type: self.ipv6_params,
230 }
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800231
Neale Ranns4f33c802019-04-10 12:39:10 +0000232 def config_interfaces(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800233 self.create_pg_interfaces(range(3))
234 self.interfaces = list(self.pg_interfaces)
235 for i in self.interfaces:
Klement Sekera31da2e32018-06-24 22:49:55 +0200236 i.admin_up()
237 i.config_ip4()
238 i.resolve_arp()
Klement Sekera611864f2018-09-26 11:19:00 +0200239 i.config_ip6()
240 i.resolve_ndp()
Neale Ranns4f33c802019-04-10 12:39:10 +0000241
242 def setUp(self):
243 super(TemplateIpsec, self).setUp()
244
245 self.setup_params()
246
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200247 self.vpp_esp_protocol = VppEnum.vl_api_ipsec_proto_t.IPSEC_API_PROTO_ESP
248 self.vpp_ah_protocol = VppEnum.vl_api_ipsec_proto_t.IPSEC_API_PROTO_AH
Neale Ranns4f33c802019-04-10 12:39:10 +0000249
250 self.config_interfaces()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700251
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800252 self.ipsec_select_backend()
Klement Sekera31da2e32018-06-24 22:49:55 +0200253
Neale Ranns4f33c802019-04-10 12:39:10 +0000254 def unconfig_interfaces(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800255 for i in self.interfaces:
256 i.admin_down()
257 i.unconfig_ip4()
258 i.unconfig_ip6()
259
Neale Ranns4f33c802019-04-10 12:39:10 +0000260 def tearDown(self):
261 super(TemplateIpsec, self).tearDown()
262
263 self.unconfig_interfaces()
264
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700265 def show_commands_at_teardown(self):
266 self.logger.info(self.vapi.cli("show hardware"))
Klement Sekera31da2e32018-06-24 22:49:55 +0200267
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200268 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=54):
269 return [
270 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
271 / sa.encrypt(IP(src=src, dst=dst) / ICMP() / Raw(b"X" * payload_size))
272 for i in range(count)
273 ]
Klement Sekera31da2e32018-06-24 22:49:55 +0200274
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200275 def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=54):
276 return [
277 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
278 / sa.encrypt(
279 IPv6(src=src, dst=dst, hlim=p.inner_hop_limit, fl=p.inner_flow_label)
280 / ICMPv6EchoRequest(id=0, seq=1, data="X" * payload_size)
281 )
282 for i in range(count)
283 ]
Klement Sekera611864f2018-09-26 11:19:00 +0200284
Neale Rannsd7603d92019-03-28 08:56:10 +0000285 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=54):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200286 return [
287 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
288 / IP(src=src, dst=dst)
289 / ICMP()
290 / Raw(b"X" * payload_size)
291 for i in range(count)
292 ]
Klement Sekera31da2e32018-06-24 22:49:55 +0200293
Neale Ranns9ec846c2021-02-09 14:04:02 +0000294 def gen_pkts6(self, p, sw_intf, src, dst, count=1, payload_size=54):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200295 return [
296 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
297 / IPv6(src=src, dst=dst, hlim=p.inner_hop_limit, fl=p.inner_flow_label)
298 / ICMPv6EchoRequest(id=0, seq=1, data="X" * payload_size)
299 for i in range(count)
300 ]
Klement Sekera611864f2018-09-26 11:19:00 +0200301
Klement Sekera31da2e32018-06-24 22:49:55 +0200302
Neale Ranns4f33c802019-04-10 12:39:10 +0000303class IpsecTcp(object):
304 def verify_tcp_checksum(self):
Florin Corasa1175b72022-01-26 00:15:03 -0800305 # start http cli server listener on http://0.0.0.0:80
306 self.vapi.cli("http cli server")
Klement Sekera611864f2018-09-26 11:19:00 +0200307 p = self.params[socket.AF_INET]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200308 send = Ether(
309 src=self.tun_if.remote_mac, dst=self.tun_if.local_mac
310 ) / p.scapy_tun_sa.encrypt(
311 IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4)
312 / TCP(flags="S", dport=80)
313 )
Klement Sekera31da2e32018-06-24 22:49:55 +0200314 self.logger.debug(ppp("Sending packet:", send))
Klement Sekera611864f2018-09-26 11:19:00 +0200315 recv = self.send_and_expect(self.tun_if, [send], self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200316 recv = recv[0]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000317 decrypted = p.vpp_tun_sa.decrypt(recv[IP])
Klement Sekera31da2e32018-06-24 22:49:55 +0200318 self.assert_packet_checksums_valid(decrypted)
319
320
Neale Ranns4f33c802019-04-10 12:39:10 +0000321class IpsecTcpTests(IpsecTcp):
322 def test_tcp_checksum(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200323 """verify checksum correctness for vpp generated packets"""
Neale Ranns4f33c802019-04-10 12:39:10 +0000324 self.verify_tcp_checksum()
325
326
327class IpsecTra4(object):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200328 """verify methods for Transport v4"""
329
Neale Ranns8c609af2021-02-25 10:05:32 +0000330 def get_replay_counts(self, p):
Neale Ranns93688d72022-08-09 03:34:51 +0000331 replay_node_name = "/err/%s/replay" % self.tra4_decrypt_node_name[0]
Neale Ranns8c609af2021-02-25 10:05:32 +0000332 count = self.statistics.get_err_counter(replay_node_name)
333
334 if p.async_mode:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200335 replay_post_node_name = (
Neale Ranns93688d72022-08-09 03:34:51 +0000336 "/err/%s/replay" % self.tra4_decrypt_node_name[p.async_mode]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200337 )
Neale Ranns8c609af2021-02-25 10:05:32 +0000338 count += self.statistics.get_err_counter(replay_post_node_name)
339
340 return count
341
342 def get_hash_failed_counts(self, p):
Benoît Ganne84e66582023-03-10 17:33:03 +0100343 if ESP == self.encryption_type and p.crypt_algo in ("AES-GCM", "AES-NULL-GMAC"):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200344 hash_failed_node_name = (
Neale Ranns93688d72022-08-09 03:34:51 +0000345 "/err/%s/decryption_failed" % self.tra4_decrypt_node_name[p.async_mode]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200346 )
Neale Ranns8c609af2021-02-25 10:05:32 +0000347 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200348 hash_failed_node_name = (
Neale Ranns93688d72022-08-09 03:34:51 +0000349 "/err/%s/integ_error" % self.tra4_decrypt_node_name[p.async_mode]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200350 )
Neale Ranns8c609af2021-02-25 10:05:32 +0000351 count = self.statistics.get_err_counter(hash_failed_node_name)
352
353 if p.async_mode:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200354 count += self.statistics.get_err_counter("/err/crypto-dispatch/bad-hmac")
Neale Ranns8c609af2021-02-25 10:05:32 +0000355
356 return count
357
Neale Ranns5b891102021-06-28 13:31:28 +0000358 def verify_hi_seq_num(self):
359 p = self.params[socket.AF_INET]
360 saf = VppEnum.vl_api_ipsec_sad_flags_t
361 esn_on = p.vpp_tra_sa.esn_en
362 ar_on = p.flags & saf.IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY
363
Neale Ranns93688d72022-08-09 03:34:51 +0000364 seq_cycle_node_name = "/err/%s/seq_cycled" % self.tra4_encrypt_node_name
Neale Ranns5b891102021-06-28 13:31:28 +0000365 replay_count = self.get_replay_counts(p)
366 hash_failed_count = self.get_hash_failed_counts(p)
367 seq_cycle_count = self.statistics.get_err_counter(seq_cycle_node_name)
368
369 # a few packets so we get the rx seq number above the window size and
370 # thus can simulate a wrap with an out of window packet
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200371 pkts = [
372 (
373 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
374 / p.scapy_tra_sa.encrypt(
375 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
376 seq_num=seq,
377 )
378 )
379 for seq in range(63, 80)
380 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000381 recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if)
382
383 # these 4 packets will all choose seq-num 0 to decrpyt since none
384 # are out of window when first checked. however, once #200 has
385 # decrypted it will move the window to 200 and has #81 is out of
386 # window. this packet should be dropped.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200387 pkts = [
388 (
389 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
390 / p.scapy_tra_sa.encrypt(
391 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
392 seq_num=200,
393 )
394 ),
395 (
396 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
397 / p.scapy_tra_sa.encrypt(
398 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
399 seq_num=81,
400 )
401 ),
402 (
403 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
404 / p.scapy_tra_sa.encrypt(
405 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
406 seq_num=201,
407 )
408 ),
409 (
410 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
411 / p.scapy_tra_sa.encrypt(
412 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
413 seq_num=202,
414 )
415 ),
416 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000417
418 # if anti-replay is off then we won't drop #81
419 n_rx = 3 if ar_on else 4
420 self.send_and_expect(self.tra_if, pkts, self.tra_if, n_rx=n_rx)
421 # this packet is one before the wrap
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200422 pkts = [
423 (
424 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
425 / p.scapy_tra_sa.encrypt(
426 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
427 seq_num=203,
428 )
429 )
430 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000431 recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if)
432
Neale Rannsfe2d23f2022-11-18 04:24:09 +0000433 # a replayed packet, then an out of window, then a legit
434 # tests that a early failure on the batch doesn't affect subsequent packets.
435 pkts = [
436 (
437 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
438 / p.scapy_tra_sa.encrypt(
439 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
440 seq_num=203,
441 )
442 ),
443 (
444 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
445 / p.scapy_tra_sa.encrypt(
446 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
447 seq_num=81,
448 )
449 ),
450 (
451 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
452 / p.scapy_tra_sa.encrypt(
453 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
454 seq_num=204,
455 )
456 ),
457 ]
458 n_rx = 1 if ar_on else 3
459 recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if, n_rx=n_rx)
460
Neale Ranns5b891102021-06-28 13:31:28 +0000461 # move the window over half way to a wrap
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200462 pkts = [
463 (
464 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
465 / p.scapy_tra_sa.encrypt(
466 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
467 seq_num=0x80000001,
468 )
469 )
470 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000471 recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if)
472
473 # anti-replay will drop old packets, no anti-replay will not
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200474 pkts = [
475 (
476 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
477 / p.scapy_tra_sa.encrypt(
478 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
479 seq_num=0x44000001,
480 )
481 )
482 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000483
484 if ar_on:
485 self.send_and_assert_no_replies(self.tra_if, pkts)
486 else:
487 recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if)
488
489 if esn_on:
490 #
491 # validate wrapping the ESN
492 #
493
494 # wrap scapy's TX SA SN
495 p.scapy_tra_sa.seq_num = 0x100000005
496
497 # send a packet that wraps the window for both AR and no AR
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200498 pkts = [
499 (
500 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
501 / p.scapy_tra_sa.encrypt(
502 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4)
503 / ICMP(),
504 seq_num=0x100000005,
505 )
506 )
507 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000508
509 rxs = self.send_and_expect(self.tra_if, pkts, self.tra_if)
510 for rx in rxs:
511 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
512
513 # move the window forward to half way to the next wrap
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200514 pkts = [
515 (
516 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
517 / p.scapy_tra_sa.encrypt(
518 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4)
519 / ICMP(),
520 seq_num=0x180000005,
521 )
522 )
523 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000524
525 rxs = self.send_and_expect(self.tra_if, pkts, self.tra_if)
526
527 # a packet less than 2^30 from the current position is:
528 # - AR: out of window and dropped
529 # - non-AR: accepted
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200530 pkts = [
531 (
532 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
533 / p.scapy_tra_sa.encrypt(
534 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4)
535 / ICMP(),
536 seq_num=0x170000005,
537 )
538 )
539 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000540
541 if ar_on:
542 self.send_and_assert_no_replies(self.tra_if, pkts)
543 else:
544 self.send_and_expect(self.tra_if, pkts, self.tra_if)
545
546 # a packet more than 2^30 from the current position is:
547 # - AR: out of window and dropped
548 # - non-AR: considered a wrap, but since it's not a wrap
549 # it won't decrpyt and so will be dropped
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200550 pkts = [
551 (
552 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
553 / p.scapy_tra_sa.encrypt(
554 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4)
555 / ICMP(),
556 seq_num=0x130000005,
557 )
558 )
559 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000560
561 self.send_and_assert_no_replies(self.tra_if, pkts)
562
563 # a packet less than 2^30 from the current position and is a
564 # wrap; (the seq is currently at 0x180000005).
565 # - AR: out of window so considered a wrap, so accepted
566 # - non-AR: not considered a wrap, so won't decrypt
567 p.scapy_tra_sa.seq_num = 0x260000005
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200568 pkts = [
569 (
570 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
571 / p.scapy_tra_sa.encrypt(
572 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4)
573 / ICMP(),
574 seq_num=0x260000005,
575 )
576 )
577 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000578 if ar_on:
579 self.send_and_expect(self.tra_if, pkts, self.tra_if)
580 else:
581 self.send_and_assert_no_replies(self.tra_if, pkts)
582
583 #
584 # window positions are different now for AR/non-AR
585 # move non-AR forward
586 #
587 if not ar_on:
588 # a packet more than 2^30 from the current position and is a
589 # wrap; (the seq is currently at 0x180000005).
590 # - AR: accepted
591 # - non-AR: not considered a wrap, so won't decrypt
592
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200593 pkts = [
594 (
595 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
596 / p.scapy_tra_sa.encrypt(
597 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4)
598 / ICMP(),
599 seq_num=0x200000005,
600 )
601 ),
602 (
603 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
604 / p.scapy_tra_sa.encrypt(
605 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4)
606 / ICMP(),
607 seq_num=0x200000006,
608 )
609 ),
610 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000611 self.send_and_expect(self.tra_if, pkts, self.tra_if)
612
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200613 pkts = [
614 (
615 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
616 / p.scapy_tra_sa.encrypt(
617 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4)
618 / ICMP(),
619 seq_num=0x260000005,
620 )
621 )
622 ]
Neale Ranns5b891102021-06-28 13:31:28 +0000623 self.send_and_expect(self.tra_if, pkts, self.tra_if)
624
Neale Ranns6afaae12019-07-17 15:07:14 +0000625 def verify_tra_anti_replay(self):
Neale Rannsde847272018-11-28 01:38:34 -0800626 p = self.params[socket.AF_INET]
snaramre5d4b8912019-12-13 23:39:35 +0000627 esn_en = p.vpp_tra_sa.esn_en
Neale Rannsde847272018-11-28 01:38:34 -0800628
Neale Ranns93688d72022-08-09 03:34:51 +0000629 seq_cycle_node_name = "/err/%s/seq_cycled" % self.tra4_encrypt_node_name
Neale Ranns8c609af2021-02-25 10:05:32 +0000630 replay_count = self.get_replay_counts(p)
631 hash_failed_count = self.get_hash_failed_counts(p)
Neale Ranns6afaae12019-07-17 15:07:14 +0000632 seq_cycle_count = self.statistics.get_err_counter(seq_cycle_node_name)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100633 hash_err = "integ_error"
Neale Rannsde847272018-11-28 01:38:34 -0800634
Neale Ranns6afaae12019-07-17 15:07:14 +0000635 if ESP == self.encryption_type:
Neale Ranns93688d72022-08-09 03:34:51 +0000636 undersize_node_name = "/err/%s/runt" % self.tra4_decrypt_node_name[0]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200637 undersize_count = self.statistics.get_err_counter(undersize_node_name)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100638 # For AES-GCM an error in the hash is reported as a decryption failure
Benoît Ganne84e66582023-03-10 17:33:03 +0100639 if p.crypt_algo in ("AES-GCM", "AES-NULL-GMAC"):
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100640 hash_err = "decryption_failed"
641 # In async mode, we don't report errors in the hash.
642 if p.async_mode:
643 hash_err = ""
Neale Ranns6afaae12019-07-17 15:07:14 +0000644
645 #
646 # send packets with seq numbers 1->34
647 # this means the window size is still in Case B (see RFC4303
648 # Appendix A)
649 #
650 # for reasons i haven't investigated Scapy won't create a packet with
651 # seq_num=0
652 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200653 pkts = [
654 (
655 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
656 / p.scapy_tra_sa.encrypt(
657 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
658 seq_num=seq,
659 )
660 )
661 for seq in range(1, 34)
662 ]
Neale Ranns6afaae12019-07-17 15:07:14 +0000663 recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if)
664
665 # replayed packets are dropped
Neale Ranns5b891102021-06-28 13:31:28 +0000666 self.send_and_assert_no_replies(self.tra_if, pkts, timeout=0.2)
Neale Ranns6afaae12019-07-17 15:07:14 +0000667 replay_count += len(pkts)
Neale Ranns8c609af2021-02-25 10:05:32 +0000668 self.assertEqual(self.get_replay_counts(p), replay_count)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100669 err = p.tra_sa_in.get_err("replay")
670 self.assertEqual(err, replay_count)
Neale Ranns6afaae12019-07-17 15:07:14 +0000671
672 #
Neale Ranns3b9374f2019-08-01 04:45:15 -0700673 # now send a batch of packets all with the same sequence number
674 # the first packet in the batch is legitimate, the rest bogus
675 #
Neale Ranns8c609af2021-02-25 10:05:32 +0000676 self.vapi.cli("clear error")
677 self.vapi.cli("clear node counters")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200678 pkts = Ether(
679 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
680 ) / p.scapy_tra_sa.encrypt(
681 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
682 seq_num=35,
683 )
684 recv_pkts = self.send_and_expect(self.tra_if, pkts * 8, self.tra_if, n_rx=1)
Neale Ranns3b9374f2019-08-01 04:45:15 -0700685 replay_count += 7
Neale Ranns8c609af2021-02-25 10:05:32 +0000686 self.assertEqual(self.get_replay_counts(p), replay_count)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100687 err = p.tra_sa_in.get_err("replay")
688 self.assertEqual(err, replay_count)
Neale Ranns3b9374f2019-08-01 04:45:15 -0700689
690 #
Neale Ranns6afaae12019-07-17 15:07:14 +0000691 # now move the window over to 257 (more than one byte) and into Case A
692 #
Neale Ranns8c609af2021-02-25 10:05:32 +0000693 self.vapi.cli("clear error")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200694 pkt = Ether(
695 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
696 ) / p.scapy_tra_sa.encrypt(
697 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
698 seq_num=257,
699 )
Neale Rannsde847272018-11-28 01:38:34 -0800700 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
701
Neale Ranns3833ffd2019-03-21 14:34:09 +0000702 # replayed packets are dropped
Neale Ranns5b891102021-06-28 13:31:28 +0000703 self.send_and_assert_no_replies(self.tra_if, pkt * 3, timeout=0.2)
Neale Ranns6afaae12019-07-17 15:07:14 +0000704 replay_count += 3
Neale Ranns8c609af2021-02-25 10:05:32 +0000705 self.assertEqual(self.get_replay_counts(p), replay_count)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100706 err = p.tra_sa_in.get_err("replay")
707 self.assertEqual(err, replay_count)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000708
Neale Rannsde847272018-11-28 01:38:34 -0800709 # the window size is 64 packets
710 # in window are still accepted
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200711 pkt = Ether(
712 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
713 ) / p.scapy_tra_sa.encrypt(
714 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
715 seq_num=200,
716 )
Neale Rannsde847272018-11-28 01:38:34 -0800717 recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
718
Neale Rannsde847272018-11-28 01:38:34 -0800719 # a packet that does not decrypt does not move the window forward
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200720 bogus_sa = SecurityAssociation(
721 self.encryption_type,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100722 p.scapy_tra_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200723 crypt_algo=p.crypt_algo,
724 crypt_key=mk_scapy_crypt_key(p)[::-1],
725 auth_algo=p.auth_algo,
726 auth_key=p.auth_key[::-1],
727 )
728 pkt = Ether(
729 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
730 ) / bogus_sa.encrypt(
731 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
732 seq_num=350,
733 )
Neale Ranns5b891102021-06-28 13:31:28 +0000734 self.send_and_assert_no_replies(self.tra_if, pkt * 17, timeout=0.2)
Neale Rannsde847272018-11-28 01:38:34 -0800735
Neale Ranns6afaae12019-07-17 15:07:14 +0000736 hash_failed_count += 17
Neale Ranns8c609af2021-02-25 10:05:32 +0000737 self.assertEqual(self.get_hash_failed_counts(p), hash_failed_count)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100738 if hash_err != "":
739 err = p.tra_sa_in.get_err(hash_err)
740 self.assertEqual(err, hash_failed_count)
Neale Rannsde847272018-11-28 01:38:34 -0800741
Damjan Mariona829b132019-04-24 23:39:16 +0200742 # a malformed 'runt' packet
743 # created by a mis-constructed SA
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200744 if ESP == self.encryption_type and p.crypt_algo != "NULL":
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100745 bogus_sa = SecurityAssociation(self.encryption_type, p.scapy_tra_spi)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200746 pkt = Ether(
747 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
748 ) / bogus_sa.encrypt(
749 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
750 seq_num=350,
751 )
Neale Ranns5b891102021-06-28 13:31:28 +0000752 self.send_and_assert_no_replies(self.tra_if, pkt * 17, timeout=0.2)
Damjan Mariona829b132019-04-24 23:39:16 +0200753
Neale Ranns6afaae12019-07-17 15:07:14 +0000754 undersize_count += 17
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200755 self.assert_error_counter_equal(undersize_node_name, undersize_count)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100756 err = p.tra_sa_in.get_err("runt")
757 self.assertEqual(err, undersize_count)
Damjan Mariona829b132019-04-24 23:39:16 +0200758
Neale Rannsde847272018-11-28 01:38:34 -0800759 # which we can determine since this packet is still in the window
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200760 pkt = Ether(
761 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
762 ) / p.scapy_tra_sa.encrypt(
763 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
764 seq_num=234,
765 )
Klement Sekera14d7e902018-12-10 13:46:09 +0100766 self.send_and_expect(self.tra_if, [pkt], self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -0800767
Neale Ranns6afaae12019-07-17 15:07:14 +0000768 #
Neale Ranns3833ffd2019-03-21 14:34:09 +0000769 # out of window are dropped
Neale Ranns6afaae12019-07-17 15:07:14 +0000770 # this is Case B. So VPP will consider this to be a high seq num wrap
771 # and so the decrypt attempt will fail
772 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200773 pkt = Ether(
774 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
775 ) / p.scapy_tra_sa.encrypt(
776 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
777 seq_num=17,
778 )
Neale Ranns5b891102021-06-28 13:31:28 +0000779 self.send_and_assert_no_replies(self.tra_if, pkt * 17, timeout=0.2)
Neale Ranns00a44202019-03-21 16:36:28 +0000780
snaramre5d4b8912019-12-13 23:39:35 +0000781 if esn_en:
Neale Ranns3833ffd2019-03-21 14:34:09 +0000782 # an out of window error with ESN looks like a high sequence
783 # wrap. but since it isn't then the verify will fail.
Neale Ranns6afaae12019-07-17 15:07:14 +0000784 hash_failed_count += 17
Neale Ranns8c609af2021-02-25 10:05:32 +0000785 self.assertEqual(self.get_hash_failed_counts(p), hash_failed_count)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100786 if hash_err != "":
787 err = p.tra_sa_in.get_err(hash_err)
788 self.assertEqual(err, hash_failed_count)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000789
790 else:
Neale Ranns6afaae12019-07-17 15:07:14 +0000791 replay_count += 17
Neale Ranns8c609af2021-02-25 10:05:32 +0000792 self.assertEqual(self.get_replay_counts(p), replay_count)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100793 err = p.tra_sa_in.get_err("replay")
794 self.assertEqual(err, replay_count)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000795
Neale Ranns6afaae12019-07-17 15:07:14 +0000796 # valid packet moves the window over to 258
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200797 pkt = Ether(
798 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
799 ) / p.scapy_tra_sa.encrypt(
800 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
801 seq_num=258,
802 )
Neale Ranns3833ffd2019-03-21 14:34:09 +0000803 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
804 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
805
Neale Ranns6afaae12019-07-17 15:07:14 +0000806 #
807 # move VPP's SA TX seq-num to just before the seq-number wrap.
808 # then fire in a packet that VPP should drop on TX because it
809 # causes the TX seq number to wrap; unless we're using extened sequence
810 # numbers.
811 #
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100812 self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.vpp_tra_sa_id)
Neale Ranns6afaae12019-07-17 15:07:14 +0000813 self.logger.info(self.vapi.ppcli("show ipsec sa 0"))
814 self.logger.info(self.vapi.ppcli("show ipsec sa 1"))
Neale Ranns3833ffd2019-03-21 14:34:09 +0000815
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200816 pkts = [
817 (
818 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
819 / p.scapy_tra_sa.encrypt(
820 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
821 seq_num=seq,
822 )
823 )
824 for seq in range(259, 280)
825 ]
Neale Ranns3833ffd2019-03-21 14:34:09 +0000826
snaramre5d4b8912019-12-13 23:39:35 +0000827 if esn_en:
Neale Ranns6afaae12019-07-17 15:07:14 +0000828 rxs = self.send_and_expect(self.tra_if, pkts, self.tra_if)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000829
Neale Ranns6afaae12019-07-17 15:07:14 +0000830 #
831 # in order for scapy to decrypt its SA's high order number needs
832 # to wrap
833 #
834 p.vpp_tra_sa.seq_num = 0x100000000
835 for rx in rxs:
836 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
837
838 #
839 # wrap scapy's TX high sequence number. VPP is in case B, so it
840 # will consider this a high seq wrap also.
841 # The low seq num we set it to will place VPP's RX window in Case A
842 #
Neale Ranns3833ffd2019-03-21 14:34:09 +0000843 p.scapy_tra_sa.seq_num = 0x100000005
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200844 pkt = Ether(
845 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
846 ) / p.scapy_tra_sa.encrypt(
847 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
848 seq_num=0x100000005,
849 )
Neale Ranns3833ffd2019-03-21 14:34:09 +0000850 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
Neale Ranns5b891102021-06-28 13:31:28 +0000851
Neale Ranns3833ffd2019-03-21 14:34:09 +0000852 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
Neale Ranns6afaae12019-07-17 15:07:14 +0000853
854 #
855 # A packet that has seq num between (2^32-64) and 5 is within
856 # the window
857 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200858 p.scapy_tra_sa.seq_num = 0xFFFFFFFD
859 pkt = Ether(
860 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
861 ) / p.scapy_tra_sa.encrypt(
862 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
863 seq_num=0xFFFFFFFD,
864 )
Neale Ranns6afaae12019-07-17 15:07:14 +0000865 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
866 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
867
868 #
869 # While in case A we cannot wrap the high sequence number again
Neale Ranns5b891102021-06-28 13:31:28 +0000870 # because VPP will consider this packet to be one that moves the
Neale Ranns6afaae12019-07-17 15:07:14 +0000871 # window forward
872 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200873 pkt = Ether(
874 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
875 ) / p.scapy_tra_sa.encrypt(
876 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
877 seq_num=0x200000999,
878 )
879 self.send_and_assert_no_replies(
880 self.tra_if, [pkt], self.tra_if, timeout=0.2
881 )
Neale Ranns6afaae12019-07-17 15:07:14 +0000882
883 hash_failed_count += 1
Neale Ranns8c609af2021-02-25 10:05:32 +0000884 self.assertEqual(self.get_hash_failed_counts(p), hash_failed_count)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100885 if hash_err != "":
886 err = p.tra_sa_in.get_err(hash_err)
887 self.assertEqual(err, hash_failed_count)
Neale Ranns6afaae12019-07-17 15:07:14 +0000888
889 #
Neale Ranns5b891102021-06-28 13:31:28 +0000890 # but if we move the window forward to case B, then we can wrap
Neale Ranns6afaae12019-07-17 15:07:14 +0000891 # again
892 #
893 p.scapy_tra_sa.seq_num = 0x100000555
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200894 pkt = Ether(
895 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
896 ) / p.scapy_tra_sa.encrypt(
897 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
898 seq_num=0x100000555,
899 )
Neale Ranns6afaae12019-07-17 15:07:14 +0000900 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
901 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
902
903 p.scapy_tra_sa.seq_num = 0x200000444
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200904 pkt = Ether(
905 src=self.tra_if.remote_mac, dst=self.tra_if.local_mac
906 ) / p.scapy_tra_sa.encrypt(
907 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
908 seq_num=0x200000444,
909 )
Neale Ranns6afaae12019-07-17 15:07:14 +0000910 rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
911 decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
912
Neale Ranns3833ffd2019-03-21 14:34:09 +0000913 else:
Neale Ranns6afaae12019-07-17 15:07:14 +0000914 #
915 # without ESN TX sequence numbers can't wrap and packets are
916 # dropped from here on out.
917 #
Neale Ranns5b891102021-06-28 13:31:28 +0000918 self.send_and_assert_no_replies(self.tra_if, pkts, timeout=0.2)
Neale Ranns6afaae12019-07-17 15:07:14 +0000919 seq_cycle_count += len(pkts)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200920 self.assert_error_counter_equal(seq_cycle_node_name, seq_cycle_count)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100921 err = p.tra_sa_out.get_err("seq_cycled")
922 self.assertEqual(err, seq_cycle_count)
Neale Ranns00a44202019-03-21 16:36:28 +0000923
Neale Rannsde847272018-11-28 01:38:34 -0800924 # move the security-associations seq number on to the last we used
Neale Ranns00a44202019-03-21 16:36:28 +0000925 self.vapi.cli("test ipsec sa %d seq 0x15f" % p.scapy_tra_sa_id)
Neale Rannsde847272018-11-28 01:38:34 -0800926 p.scapy_tra_sa.seq_num = 351
927 p.vpp_tra_sa.seq_num = 351
928
Neale Rannse11203e2021-09-21 12:34:19 +0000929 def verify_tra_lost(self):
930 p = self.params[socket.AF_INET]
931 esn_en = p.vpp_tra_sa.esn_en
932
933 #
934 # send packets with seq numbers 1->34
935 # this means the window size is still in Case B (see RFC4303
936 # Appendix A)
937 #
938 # for reasons i haven't investigated Scapy won't create a packet with
939 # seq_num=0
940 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200941 pkts = [
942 (
943 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
944 / p.scapy_tra_sa.encrypt(
945 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
946 seq_num=seq,
947 )
948 )
949 for seq in range(1, 3)
950 ]
Neale Rannse11203e2021-09-21 12:34:19 +0000951 self.send_and_expect(self.tra_if, pkts, self.tra_if)
952
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100953 self.assertEqual(p.tra_sa_in.get_err("lost"), 0)
Neale Rannse11203e2021-09-21 12:34:19 +0000954
955 # skip a sequence number
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200956 pkts = [
957 (
958 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
959 / p.scapy_tra_sa.encrypt(
960 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
961 seq_num=seq,
962 )
963 )
964 for seq in range(4, 6)
965 ]
Neale Rannse11203e2021-09-21 12:34:19 +0000966 self.send_and_expect(self.tra_if, pkts, self.tra_if)
967
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100968 self.assertEqual(p.tra_sa_in.get_err("lost"), 0)
Neale Rannse11203e2021-09-21 12:34:19 +0000969
970 # the lost packet are counted untill we get up past the first
971 # sizeof(replay_window) packets
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200972 pkts = [
973 (
974 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
975 / p.scapy_tra_sa.encrypt(
976 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
977 seq_num=seq,
978 )
979 )
980 for seq in range(6, 100)
981 ]
Neale Rannse11203e2021-09-21 12:34:19 +0000982 self.send_and_expect(self.tra_if, pkts, self.tra_if)
983
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100984 self.assertEqual(p.tra_sa_in.get_err("lost"), 1)
Neale Rannse11203e2021-09-21 12:34:19 +0000985
986 # lost of holes in the sequence
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200987 pkts = [
988 (
989 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
990 / p.scapy_tra_sa.encrypt(
991 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
992 seq_num=seq,
993 )
994 )
995 for seq in range(100, 200, 2)
996 ]
Neale Rannse11203e2021-09-21 12:34:19 +0000997 self.send_and_expect(self.tra_if, pkts, self.tra_if, n_rx=50)
998
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200999 pkts = [
1000 (
1001 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
1002 / p.scapy_tra_sa.encrypt(
1003 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
1004 seq_num=seq,
1005 )
1006 )
1007 for seq in range(200, 300)
1008 ]
Neale Rannse11203e2021-09-21 12:34:19 +00001009 self.send_and_expect(self.tra_if, pkts, self.tra_if)
1010
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001011 self.assertEqual(p.tra_sa_in.get_err("lost"), 51)
Neale Rannse11203e2021-09-21 12:34:19 +00001012
1013 # a big hole in the seq number space
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001014 pkts = [
1015 (
1016 Ether(src=self.tra_if.remote_mac, dst=self.tra_if.local_mac)
1017 / p.scapy_tra_sa.encrypt(
1018 IP(src=self.tra_if.remote_ip4, dst=self.tra_if.local_ip4) / ICMP(),
1019 seq_num=seq,
1020 )
1021 )
1022 for seq in range(400, 500)
1023 ]
Neale Rannse11203e2021-09-21 12:34:19 +00001024 self.send_and_expect(self.tra_if, pkts, self.tra_if)
1025
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001026 self.assertEqual(p.tra_sa_in.get_err("lost"), 151)
Neale Rannse11203e2021-09-21 12:34:19 +00001027
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001028 def verify_tra_basic4(self, count=1, payload_size=54):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001029 """ipsec v4 transport basic test"""
Klement Sekera10d066e2018-11-13 11:12:57 +01001030 self.vapi.cli("clear errors")
Neale Ranns6afaae12019-07-17 15:07:14 +00001031 self.vapi.cli("clear ipsec sa")
Klement Sekera31da2e32018-06-24 22:49:55 +02001032 try:
Klement Sekera611864f2018-09-26 11:19:00 +02001033 p = self.params[socket.AF_INET]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001034 send_pkts = self.gen_encrypt_pkts(
1035 p,
1036 p.scapy_tra_sa,
1037 self.tra_if,
1038 src=self.tra_if.remote_ip4,
1039 dst=self.tra_if.local_ip4,
1040 count=count,
1041 payload_size=payload_size,
1042 )
1043 recv_pkts = self.send_and_expect(self.tra_if, send_pkts, self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -08001044 for rx in recv_pkts:
Neale Ranns1b582b82019-04-18 19:49:13 -07001045 self.assertEqual(len(rx) - len(Ether()), rx[IP].len)
1046 self.assert_packet_checksums_valid(rx)
Klement Sekera611864f2018-09-26 11:19:00 +02001047 try:
Neale Rannsde847272018-11-28 01:38:34 -08001048 decrypted = p.vpp_tra_sa.decrypt(rx[IP])
Klement Sekera611864f2018-09-26 11:19:00 +02001049 self.assert_packet_checksums_valid(decrypted)
1050 except:
Neale Rannsde847272018-11-28 01:38:34 -08001051 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +02001052 raise
Klement Sekera31da2e32018-06-24 22:49:55 +02001053 finally:
1054 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -04001055 self.logger.info(self.vapi.ppcli("show ipsec all"))
Klement Sekera31da2e32018-06-24 22:49:55 +02001056
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001057 pkts = p.tra_sa_in.get_stats()["packets"]
1058 self.assertEqual(
1059 pkts, count, "incorrect SA in counts: expected %d != %d" % (count, pkts)
1060 )
1061 pkts = p.tra_sa_out.get_stats()["packets"]
1062 self.assertEqual(
1063 pkts, count, "incorrect SA out counts: expected %d != %d" % (count, pkts)
1064 )
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001065 self.assertEqual(p.tra_sa_out.get_err("lost"), 0)
1066 self.assertEqual(p.tra_sa_in.get_err("lost"), 0)
Neale Rannseba31ec2019-02-17 18:04:27 +00001067
Klement Sekera10d066e2018-11-13 11:12:57 +01001068 self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count)
Neale Ranns8c609af2021-02-25 10:05:32 +00001069 self.assert_packet_counter_equal(self.tra4_decrypt_node_name[0], count)
Klement Sekera10d066e2018-11-13 11:12:57 +01001070
Neale Ranns4f33c802019-04-10 12:39:10 +00001071
1072class IpsecTra4Tests(IpsecTra4):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001073 """UT test methods for Transport v4"""
1074
Neale Ranns4f33c802019-04-10 12:39:10 +00001075 def test_tra_anti_replay(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001076 """ipsec v4 transport anti-replay test"""
Neale Ranns6afaae12019-07-17 15:07:14 +00001077 self.verify_tra_anti_replay()
Neale Ranns4f33c802019-04-10 12:39:10 +00001078
Neale Rannse11203e2021-09-21 12:34:19 +00001079 def test_tra_lost(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001080 """ipsec v4 transport lost packet test"""
Neale Rannse11203e2021-09-21 12:34:19 +00001081 self.verify_tra_lost()
1082
Neale Ranns4f33c802019-04-10 12:39:10 +00001083 def test_tra_basic(self, count=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001084 """ipsec v4 transport basic test"""
Neale Ranns4f33c802019-04-10 12:39:10 +00001085 self.verify_tra_basic4(count=1)
1086
Klement Sekera31da2e32018-06-24 22:49:55 +02001087 def test_tra_burst(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001088 """ipsec v4 transport burst test"""
Neale Ranns4f33c802019-04-10 12:39:10 +00001089 self.verify_tra_basic4(count=257)
Klement Sekera611864f2018-09-26 11:19:00 +02001090
Neale Ranns53f526b2019-02-25 14:32:02 +00001091
Neale Ranns4f33c802019-04-10 12:39:10 +00001092class IpsecTra6(object):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001093 """verify methods for Transport v6"""
1094
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001095 def verify_tra_basic6(self, count=1, payload_size=54):
Klement Sekera10d066e2018-11-13 11:12:57 +01001096 self.vapi.cli("clear errors")
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001097 self.vapi.cli("clear ipsec sa")
Klement Sekera31da2e32018-06-24 22:49:55 +02001098 try:
Klement Sekera611864f2018-09-26 11:19:00 +02001099 p = self.params[socket.AF_INET6]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001100 send_pkts = self.gen_encrypt_pkts6(
1101 p,
1102 p.scapy_tra_sa,
1103 self.tra_if,
1104 src=self.tra_if.remote_ip6,
1105 dst=self.tra_if.local_ip6,
1106 count=count,
1107 payload_size=payload_size,
1108 )
1109 recv_pkts = self.send_and_expect(self.tra_if, send_pkts, self.tra_if)
Neale Rannsde847272018-11-28 01:38:34 -08001110 for rx in recv_pkts:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001111 self.assertEqual(len(rx) - len(Ether()) - len(IPv6()), rx[IPv6].plen)
Klement Sekera611864f2018-09-26 11:19:00 +02001112 try:
Neale Rannsde847272018-11-28 01:38:34 -08001113 decrypted = p.vpp_tra_sa.decrypt(rx[IPv6])
Klement Sekera611864f2018-09-26 11:19:00 +02001114 self.assert_packet_checksums_valid(decrypted)
1115 except:
Neale Rannsde847272018-11-28 01:38:34 -08001116 self.logger.debug(ppp("Unexpected packet:", rx))
Klement Sekera611864f2018-09-26 11:19:00 +02001117 raise
Klement Sekera31da2e32018-06-24 22:49:55 +02001118 finally:
1119 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -04001120 self.logger.info(self.vapi.ppcli("show ipsec all"))
Klement Sekera31da2e32018-06-24 22:49:55 +02001121
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001122 pkts = p.tra_sa_in.get_stats()["packets"]
1123 self.assertEqual(
1124 pkts, count, "incorrect SA in counts: expected %d != %d" % (count, pkts)
1125 )
1126 pkts = p.tra_sa_out.get_stats()["packets"]
1127 self.assertEqual(
1128 pkts, count, "incorrect SA out counts: expected %d != %d" % (count, pkts)
1129 )
Klement Sekera10d066e2018-11-13 11:12:57 +01001130 self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count)
Neale Ranns8c609af2021-02-25 10:05:32 +00001131 self.assert_packet_counter_equal(self.tra6_decrypt_node_name[0], count)
Klement Sekera10d066e2018-11-13 11:12:57 +01001132
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001133 def gen_encrypt_pkts_ext_hdrs6(
1134 self, sa, sw_intf, src, dst, count=1, payload_size=54
1135 ):
1136 return [
1137 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1138 / sa.encrypt(
1139 IPv6(src=src, dst=dst)
1140 / ICMPv6EchoRequest(id=0, seq=1, data="X" * payload_size)
1141 )
1142 for i in range(count)
1143 ]
Neale Ranns02950402019-12-20 00:54:57 +00001144
1145 def gen_pkts_ext_hdrs6(self, sw_intf, src, dst, count=1, payload_size=54):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001146 return [
1147 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1148 / IPv6(src=src, dst=dst)
1149 / IPv6ExtHdrHopByHop()
1150 / IPv6ExtHdrFragment(id=2, offset=200)
1151 / Raw(b"\xff" * 200)
1152 for i in range(count)
1153 ]
Neale Ranns02950402019-12-20 00:54:57 +00001154
1155 def verify_tra_encrypted6(self, p, sa, rxs):
1156 decrypted = []
1157 for rx in rxs:
1158 self.assert_packet_checksums_valid(rx)
1159 try:
1160 decrypt_pkt = p.vpp_tra_sa.decrypt(rx[IPv6])
1161 decrypted.append(decrypt_pkt)
1162 self.assert_equal(decrypt_pkt.src, self.tra_if.local_ip6)
1163 self.assert_equal(decrypt_pkt.dst, self.tra_if.remote_ip6)
1164 except:
1165 self.logger.debug(ppp("Unexpected packet:", rx))
1166 try:
1167 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
1168 except:
1169 pass
1170 raise
1171 return decrypted
1172
1173 def verify_tra_66_ext_hdrs(self, p):
1174 count = 63
1175
1176 #
1177 # check we can decrypt with options
1178 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001179 tx = self.gen_encrypt_pkts_ext_hdrs6(
1180 p.scapy_tra_sa,
1181 self.tra_if,
1182 src=self.tra_if.remote_ip6,
1183 dst=self.tra_if.local_ip6,
1184 count=count,
1185 )
Neale Ranns02950402019-12-20 00:54:57 +00001186 self.send_and_expect(self.tra_if, tx, self.tra_if)
1187
1188 #
1189 # injecting a packet from ourselves to be routed of box is a hack
1190 # but it matches an outbout policy, alors je ne regrette rien
1191 #
1192
1193 # one extension before ESP
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001194 tx = (
1195 Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac)
1196 / IPv6(src=self.tra_if.local_ip6, dst=self.tra_if.remote_ip6)
1197 / IPv6ExtHdrFragment(id=2, offset=200)
1198 / Raw(b"\xff" * 200)
1199 )
Neale Ranns02950402019-12-20 00:54:57 +00001200
1201 rxs = self.send_and_expect(self.pg2, [tx], self.tra_if)
1202 dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs)
1203
1204 for dc in dcs:
1205 # for reasons i'm not going to investigate scapy does not
1206 # created the correct headers after decrypt. but reparsing
1207 # the ipv6 packet fixes it
1208 dc = IPv6(raw(dc[IPv6]))
1209 self.assert_equal(dc[IPv6ExtHdrFragment].id, 2)
1210
1211 # two extensions before ESP
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001212 tx = (
1213 Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac)
1214 / IPv6(src=self.tra_if.local_ip6, dst=self.tra_if.remote_ip6)
1215 / IPv6ExtHdrHopByHop()
1216 / IPv6ExtHdrFragment(id=2, offset=200)
1217 / Raw(b"\xff" * 200)
1218 )
Neale Ranns02950402019-12-20 00:54:57 +00001219
1220 rxs = self.send_and_expect(self.pg2, [tx], self.tra_if)
1221 dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs)
1222
1223 for dc in dcs:
1224 dc = IPv6(raw(dc[IPv6]))
1225 self.assertTrue(dc[IPv6ExtHdrHopByHop])
1226 self.assert_equal(dc[IPv6ExtHdrFragment].id, 2)
1227
1228 # two extensions before ESP, one after
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001229 tx = (
1230 Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac)
1231 / IPv6(src=self.tra_if.local_ip6, dst=self.tra_if.remote_ip6)
1232 / IPv6ExtHdrHopByHop()
1233 / IPv6ExtHdrFragment(id=2, offset=200)
1234 / IPv6ExtHdrDestOpt()
1235 / Raw(b"\xff" * 200)
1236 )
Neale Ranns02950402019-12-20 00:54:57 +00001237
1238 rxs = self.send_and_expect(self.pg2, [tx], self.tra_if)
1239 dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs)
1240
1241 for dc in dcs:
1242 dc = IPv6(raw(dc[IPv6]))
1243 self.assertTrue(dc[IPv6ExtHdrDestOpt])
1244 self.assertTrue(dc[IPv6ExtHdrHopByHop])
1245 self.assert_equal(dc[IPv6ExtHdrFragment].id, 2)
1246
Neale Ranns4f33c802019-04-10 12:39:10 +00001247
1248class IpsecTra6Tests(IpsecTra6):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001249 """UT test methods for Transport v6"""
1250
Neale Ranns4f33c802019-04-10 12:39:10 +00001251 def test_tra_basic6(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001252 """ipsec v6 transport basic test"""
Neale Ranns4f33c802019-04-10 12:39:10 +00001253 self.verify_tra_basic6(count=1)
1254
Klement Sekera611864f2018-09-26 11:19:00 +02001255 def test_tra_burst6(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001256 """ipsec v6 transport burst test"""
Neale Ranns4f33c802019-04-10 12:39:10 +00001257 self.verify_tra_basic6(count=257)
Klement Sekera31da2e32018-06-24 22:49:55 +02001258
Klement Sekera611864f2018-09-26 11:19:00 +02001259
Neale Ranns02950402019-12-20 00:54:57 +00001260class IpsecTra6ExtTests(IpsecTra6):
1261 def test_tra_ext_hdrs_66(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001262 """ipsec 6o6 tra extension headers test"""
Neale Ranns02950402019-12-20 00:54:57 +00001263 self.verify_tra_66_ext_hdrs(self.params[socket.AF_INET6])
1264
1265
Neale Ranns53f526b2019-02-25 14:32:02 +00001266class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001267 """UT test methods for Transport v6 and v4"""
1268
Neale Ranns53f526b2019-02-25 14:32:02 +00001269 pass
1270
1271
Neale Ranns2ac885c2019-03-20 18:24:43 +00001272class IpsecTun4(object):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001273 """verify methods for Tunnel v4"""
1274
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001275 def verify_counters4(self, p, count, n_frags=None, worker=None):
Klement Sekera6aa58b72019-05-16 14:34:55 +02001276 if not n_frags:
1277 n_frags = count
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001278 if hasattr(p, "spd_policy_in_any"):
1279 pkts = p.spd_policy_in_any.get_stats(worker)["packets"]
1280 self.assertEqual(
1281 pkts,
1282 count,
1283 "incorrect SPD any policy: expected %d != %d" % (count, pkts),
1284 )
Neale Ranns987aea82019-03-27 13:40:35 +00001285
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001286 if hasattr(p, "tun_sa_in"):
1287 pkts = p.tun_sa_in.get_stats(worker)["packets"]
1288 self.assertEqual(
1289 pkts, count, "incorrect SA in counts: expected %d != %d" % (count, pkts)
1290 )
1291 pkts = p.tun_sa_out.get_stats(worker)["packets"]
1292 self.assertEqual(
1293 pkts,
1294 n_frags,
1295 "incorrect SA out counts: expected %d != %d" % (count, pkts),
1296 )
Neale Ranns987aea82019-03-27 13:40:35 +00001297
Klement Sekera6aa58b72019-05-16 14:34:55 +02001298 self.assert_packet_counter_equal(self.tun4_encrypt_node_name, n_frags)
Neale Ranns8c609af2021-02-25 10:05:32 +00001299 self.assert_packet_counter_equal(self.tun4_decrypt_node_name[0], count)
Neale Ranns987aea82019-03-27 13:40:35 +00001300
Neale Rannsf05e7322019-03-29 20:23:58 +00001301 def verify_decrypted(self, p, rxs):
1302 for rx in rxs:
1303 self.assert_equal(rx[IP].src, p.remote_tun_if_host)
1304 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
1305 self.assert_packet_checksums_valid(rx)
1306
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -05001307 def verify_esp_padding(self, sa, esp_payload, decrypt_pkt):
1308 align = sa.crypt_algo.block_size
1309 if align < 4:
1310 align = 4
1311 exp_len = (len(decrypt_pkt) + 2 + (align - 1)) & ~(align - 1)
1312 exp_len += sa.crypt_algo.iv_size
1313 exp_len += sa.crypt_algo.icv_size or sa.auth_algo.icv_size
1314 self.assertEqual(exp_len, len(esp_payload))
1315
Neale Rannsf05e7322019-03-29 20:23:58 +00001316 def verify_encrypted(self, p, sa, rxs):
1317 decrypt_pkts = []
1318 for rx in rxs:
Neale Ranns41afb332019-07-16 06:19:35 -07001319 if p.nat_header:
Arthur de Kerhor4117b242022-08-31 19:13:03 +02001320 self.assertEqual(rx[UDP].dport, p.nat_header.dport)
Neale Ranns1b582b82019-04-18 19:49:13 -07001321 self.assert_packet_checksums_valid(rx)
1322 self.assertEqual(len(rx) - len(Ether()), rx[IP].len)
Neale Rannsf05e7322019-03-29 20:23:58 +00001323 try:
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -05001324 rx_ip = rx[IP]
1325 decrypt_pkt = p.vpp_tun_sa.decrypt(rx_ip)
Neale Rannsf05e7322019-03-29 20:23:58 +00001326 if not decrypt_pkt.haslayer(IP):
1327 decrypt_pkt = IP(decrypt_pkt[Raw].load)
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -05001328 if rx_ip.proto == socket.IPPROTO_ESP:
1329 self.verify_esp_padding(sa, rx_ip[ESP].data, decrypt_pkt)
Neale Rannsf05e7322019-03-29 20:23:58 +00001330 decrypt_pkts.append(decrypt_pkt)
1331 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
1332 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
1333 except:
1334 self.logger.debug(ppp("Unexpected packet:", rx))
1335 try:
1336 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
1337 except:
1338 pass
1339 raise
1340 pkts = reassemble4(decrypt_pkts)
1341 for pkt in pkts:
1342 self.assert_packet_checksums_valid(pkt)
1343
Neale Rannsd7603d92019-03-28 08:56:10 +00001344 def verify_tun_44(self, p, count=1, payload_size=64, n_rx=None):
Klement Sekera10d066e2018-11-13 11:12:57 +01001345 self.vapi.cli("clear errors")
Neale Ranns02950402019-12-20 00:54:57 +00001346 self.vapi.cli("clear ipsec counters")
Neale Ranns28287212019-12-16 00:53:11 +00001347 self.vapi.cli("clear ipsec sa")
Neale Rannsd7603d92019-03-28 08:56:10 +00001348 if not n_rx:
1349 n_rx = count
Klement Sekera31da2e32018-06-24 22:49:55 +02001350 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001351 send_pkts = self.gen_encrypt_pkts(
1352 p,
1353 p.scapy_tun_sa,
1354 self.tun_if,
1355 src=p.remote_tun_if_host,
1356 dst=self.pg1.remote_ip4,
1357 count=count,
1358 payload_size=payload_size,
1359 )
Klement Sekera611864f2018-09-26 11:19:00 +02001360 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Neale Rannsf05e7322019-03-29 20:23:58 +00001361 self.verify_decrypted(p, recv_pkts)
1362
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001363 send_pkts = self.gen_pkts(
1364 self.pg1,
1365 src=self.pg1.remote_ip4,
1366 dst=p.remote_tun_if_host,
1367 count=count,
1368 payload_size=payload_size,
1369 )
1370 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if, n_rx)
Neale Rannsf05e7322019-03-29 20:23:58 +00001371 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
1372
Neale Rannsf3a66222020-01-02 05:04:00 +00001373 for rx in recv_pkts:
1374 self.assertEqual(rx[IP].src, p.tun_src)
1375 self.assertEqual(rx[IP].dst, p.tun_dst)
1376
Klement Sekera31da2e32018-06-24 22:49:55 +02001377 finally:
1378 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -04001379 self.logger.info(self.vapi.ppcli("show ipsec all"))
Klement Sekera31da2e32018-06-24 22:49:55 +02001380
Neale Ranns02950402019-12-20 00:54:57 +00001381 self.logger.info(self.vapi.ppcli("show ipsec sa 0"))
1382 self.logger.info(self.vapi.ppcli("show ipsec sa 4"))
Klement Sekera6aa58b72019-05-16 14:34:55 +02001383 self.verify_counters4(p, count, n_rx)
Neale Rannseba31ec2019-02-17 18:04:27 +00001384
Neale Ranns28287212019-12-16 00:53:11 +00001385 def verify_tun_dropped_44(self, p, count=1, payload_size=64, n_rx=None):
1386 self.vapi.cli("clear errors")
1387 if not n_rx:
1388 n_rx = count
1389 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001390 send_pkts = self.gen_encrypt_pkts(
1391 p,
1392 p.scapy_tun_sa,
1393 self.tun_if,
1394 src=p.remote_tun_if_host,
1395 dst=self.pg1.remote_ip4,
1396 count=count,
1397 )
Neale Ranns28287212019-12-16 00:53:11 +00001398 self.send_and_assert_no_replies(self.tun_if, send_pkts)
1399
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001400 send_pkts = self.gen_pkts(
1401 self.pg1,
1402 src=self.pg1.remote_ip4,
1403 dst=p.remote_tun_if_host,
1404 count=count,
1405 payload_size=payload_size,
1406 )
Neale Ranns28287212019-12-16 00:53:11 +00001407 self.send_and_assert_no_replies(self.pg1, send_pkts)
1408
1409 finally:
1410 self.logger.info(self.vapi.ppcli("show error"))
1411 self.logger.info(self.vapi.ppcli("show ipsec all"))
1412
Neale Ranns14046982019-07-29 14:49:52 +00001413 def verify_tun_reass_44(self, p):
1414 self.vapi.cli("clear errors")
1415 self.vapi.ip_reassembly_enable_disable(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001416 sw_if_index=self.tun_if.sw_if_index, enable_ip4=True
1417 )
Neale Ranns14046982019-07-29 14:49:52 +00001418
1419 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001420 send_pkts = self.gen_encrypt_pkts(
1421 p,
1422 p.scapy_tun_sa,
1423 self.tun_if,
1424 src=p.remote_tun_if_host,
1425 dst=self.pg1.remote_ip4,
1426 payload_size=1900,
1427 count=1,
1428 )
Neale Ranns14046982019-07-29 14:49:52 +00001429 send_pkts = fragment_rfc791(send_pkts[0], 1400)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001430 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1, n_rx=1)
Neale Ranns14046982019-07-29 14:49:52 +00001431 self.verify_decrypted(p, recv_pkts)
1432
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001433 send_pkts = self.gen_pkts(
1434 self.pg1, src=self.pg1.remote_ip4, dst=p.remote_tun_if_host, count=1
1435 )
1436 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
Neale Ranns14046982019-07-29 14:49:52 +00001437 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
1438
1439 finally:
1440 self.logger.info(self.vapi.ppcli("show error"))
1441 self.logger.info(self.vapi.ppcli("show ipsec all"))
1442
1443 self.verify_counters4(p, 1, 1)
1444 self.vapi.ip_reassembly_enable_disable(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001445 sw_if_index=self.tun_if.sw_if_index, enable_ip4=False
1446 )
Neale Ranns14046982019-07-29 14:49:52 +00001447
Neale Ranns987aea82019-03-27 13:40:35 +00001448 def verify_tun_64(self, p, count=1):
1449 self.vapi.cli("clear errors")
Neale Rannsdd4ccf22020-06-30 07:47:14 +00001450 self.vapi.cli("clear ipsec sa")
Neale Ranns987aea82019-03-27 13:40:35 +00001451 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001452 send_pkts = self.gen_encrypt_pkts6(
1453 p,
1454 p.scapy_tun_sa,
1455 self.tun_if,
1456 src=p.remote_tun_if_host6,
1457 dst=self.pg1.remote_ip6,
1458 count=count,
1459 )
Neale Ranns987aea82019-03-27 13:40:35 +00001460 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
1461 for recv_pkt in recv_pkts:
1462 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host6)
1463 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
1464 self.assert_packet_checksums_valid(recv_pkt)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001465 send_pkts = self.gen_pkts6(
1466 p,
1467 self.pg1,
1468 src=self.pg1.remote_ip6,
1469 dst=p.remote_tun_if_host6,
1470 count=count,
1471 )
Neale Ranns987aea82019-03-27 13:40:35 +00001472 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
1473 for recv_pkt in recv_pkts:
1474 try:
1475 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP])
1476 if not decrypt_pkt.haslayer(IPv6):
1477 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
1478 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
1479 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host6)
1480 self.assert_packet_checksums_valid(decrypt_pkt)
1481 except:
1482 self.logger.error(ppp("Unexpected packet:", recv_pkt))
1483 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001484 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
Neale Ranns987aea82019-03-27 13:40:35 +00001485 except:
1486 pass
1487 raise
1488 finally:
1489 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -04001490 self.logger.info(self.vapi.ppcli("show ipsec all"))
Neale Rannseba31ec2019-02-17 18:04:27 +00001491
Klement Sekera6aa58b72019-05-16 14:34:55 +02001492 self.verify_counters4(p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +01001493
Neale Ranns41afb332019-07-16 06:19:35 -07001494 def verify_keepalive(self, p):
Neale Ranns992a4d02022-01-10 11:21:17 +00001495 # the sizeof Raw is calculated to pad to the minimum ehternet
1496 # frame size of 64 btyes
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001497 pkt = (
1498 Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac)
1499 / IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4)
1500 / UDP(sport=333, dport=4500)
1501 / Raw(b"\xff")
1502 / Padding(0 * 21)
1503 )
1504 self.send_and_assert_no_replies(self.tun_if, pkt * 31)
Neale Ranns41afb332019-07-16 06:19:35 -07001505 self.assert_error_counter_equal(
Neale Ranns93688d72022-08-09 03:34:51 +00001506 "/err/%s/nat_keepalive" % self.tun4_input_node, 31
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001507 )
Neale Ranns41afb332019-07-16 06:19:35 -07001508
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001509 pkt = (
1510 Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac)
1511 / IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4)
1512 / UDP(sport=333, dport=4500)
1513 / Raw(b"\xfe")
1514 )
1515 self.send_and_assert_no_replies(self.tun_if, pkt * 31)
Neale Ranns93688d72022-08-09 03:34:51 +00001516 self.assert_error_counter_equal("/err/%s/too_short" % self.tun4_input_node, 31)
Neale Ranns41afb332019-07-16 06:19:35 -07001517
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001518 pkt = (
1519 Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac)
1520 / IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4)
1521 / UDP(sport=333, dport=4500)
1522 / Raw(b"\xfe")
1523 / Padding(0 * 21)
1524 )
1525 self.send_and_assert_no_replies(self.tun_if, pkt * 31)
Neale Ranns93688d72022-08-09 03:34:51 +00001526 self.assert_error_counter_equal("/err/%s/too_short" % self.tun4_input_node, 62)
Neale Ranns992a4d02022-01-10 11:21:17 +00001527
Neale Ranns2ac885c2019-03-20 18:24:43 +00001528
1529class IpsecTun4Tests(IpsecTun4):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001530 """UT test methods for Tunnel v4"""
1531
Neale Ranns2ac885c2019-03-20 18:24:43 +00001532 def test_tun_basic44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001533 """ipsec 4o4 tunnel basic test"""
Neale Ranns2ac885c2019-03-20 18:24:43 +00001534 self.verify_tun_44(self.params[socket.AF_INET], count=1)
Neale Ranns02950402019-12-20 00:54:57 +00001535 self.tun_if.admin_down()
1536 self.tun_if.resolve_arp()
1537 self.tun_if.admin_up()
1538 self.verify_tun_44(self.params[socket.AF_INET], count=1)
Neale Ranns2ac885c2019-03-20 18:24:43 +00001539
Neale Ranns14046982019-07-29 14:49:52 +00001540 def test_tun_reass_basic44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001541 """ipsec 4o4 tunnel basic reassembly test"""
Neale Ranns14046982019-07-29 14:49:52 +00001542 self.verify_tun_reass_44(self.params[socket.AF_INET])
1543
Klement Sekera611864f2018-09-26 11:19:00 +02001544 def test_tun_burst44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001545 """ipsec 4o4 tunnel burst test"""
Neale Ranns02950402019-12-20 00:54:57 +00001546 self.verify_tun_44(self.params[socket.AF_INET], count=127)
1547
1548
Neale Ranns2ac885c2019-03-20 18:24:43 +00001549class IpsecTun6(object):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001550 """verify methods for Tunnel v6"""
1551
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001552 def verify_counters6(self, p_in, p_out, count, worker=None):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001553 if hasattr(p_in, "tun_sa_in"):
1554 pkts = p_in.tun_sa_in.get_stats(worker)["packets"]
1555 self.assertEqual(
1556 pkts, count, "incorrect SA in counts: expected %d != %d" % (count, pkts)
1557 )
1558 if hasattr(p_out, "tun_sa_out"):
1559 pkts = p_out.tun_sa_out.get_stats(worker)["packets"]
1560 self.assertEqual(
1561 pkts,
1562 count,
1563 "incorrect SA out counts: expected %d != %d" % (count, pkts),
1564 )
Neale Ranns987aea82019-03-27 13:40:35 +00001565 self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count)
Neale Ranns8c609af2021-02-25 10:05:32 +00001566 self.assert_packet_counter_equal(self.tun6_decrypt_node_name[0], count)
Neale Ranns987aea82019-03-27 13:40:35 +00001567
Neale Rannsc87b66c2019-02-07 07:26:12 -08001568 def verify_decrypted6(self, p, rxs):
1569 for rx in rxs:
1570 self.assert_equal(rx[IPv6].src, p.remote_tun_if_host)
1571 self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6)
1572 self.assert_packet_checksums_valid(rx)
1573
1574 def verify_encrypted6(self, p, sa, rxs):
1575 for rx in rxs:
1576 self.assert_packet_checksums_valid(rx)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001577 self.assertEqual(len(rx) - len(Ether()) - len(IPv6()), rx[IPv6].plen)
Neale Ranns9ec846c2021-02-09 14:04:02 +00001578 self.assert_equal(rx[IPv6].hlim, p.outer_hop_limit)
1579 if p.outer_flow_label:
1580 self.assert_equal(rx[IPv6].fl, p.outer_flow_label)
Neale Rannsc87b66c2019-02-07 07:26:12 -08001581 try:
1582 decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IPv6])
1583 if not decrypt_pkt.haslayer(IPv6):
1584 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
1585 self.assert_packet_checksums_valid(decrypt_pkt)
1586 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
1587 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
Neale Ranns9ec846c2021-02-09 14:04:02 +00001588 self.assert_equal(decrypt_pkt.hlim, p.inner_hop_limit - 1)
1589 self.assert_equal(decrypt_pkt.fl, p.inner_flow_label)
Neale Rannsc87b66c2019-02-07 07:26:12 -08001590 except:
1591 self.logger.debug(ppp("Unexpected packet:", rx))
1592 try:
1593 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
1594 except:
1595 pass
1596 raise
1597
Neale Ranns49378f22022-01-10 10:38:43 +00001598 def verify_drop_tun_tx_66(self, p_in, count=1, payload_size=64):
1599 self.vapi.cli("clear errors")
1600 self.vapi.cli("clear ipsec sa")
1601
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001602 send_pkts = self.gen_pkts6(
1603 p_in,
1604 self.pg1,
1605 src=self.pg1.remote_ip6,
1606 dst=p_in.remote_tun_if_host,
1607 count=count,
1608 payload_size=payload_size,
1609 )
Neale Ranns49378f22022-01-10 10:38:43 +00001610 self.send_and_assert_no_replies(self.tun_if, send_pkts)
1611 self.logger.info(self.vapi.cli("sh punt stats"))
1612
1613 def verify_drop_tun_rx_66(self, p_in, count=1, payload_size=64):
Klement Sekera10d066e2018-11-13 11:12:57 +01001614 self.vapi.cli("clear errors")
Neale Rannsc87b66c2019-02-07 07:26:12 -08001615 self.vapi.cli("clear ipsec sa")
1616
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001617 send_pkts = self.gen_encrypt_pkts6(
1618 p_in,
1619 p_in.scapy_tun_sa,
1620 self.tun_if,
1621 src=p_in.remote_tun_if_host,
1622 dst=self.pg1.remote_ip6,
1623 count=count,
1624 )
Neale Rannsc87b66c2019-02-07 07:26:12 -08001625 self.send_and_assert_no_replies(self.tun_if, send_pkts)
Neale Ranns49378f22022-01-10 10:38:43 +00001626
1627 def verify_drop_tun_66(self, p_in, count=1, payload_size=64):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001628 self.verify_drop_tun_tx_66(p_in, count=count, payload_size=payload_size)
1629 self.verify_drop_tun_rx_66(p_in, count=count, payload_size=payload_size)
Neale Rannsc87b66c2019-02-07 07:26:12 -08001630
1631 def verify_tun_66(self, p_in, p_out=None, count=1, payload_size=64):
1632 self.vapi.cli("clear errors")
1633 self.vapi.cli("clear ipsec sa")
1634 if not p_out:
1635 p_out = p_in
Klement Sekera31da2e32018-06-24 22:49:55 +02001636 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001637 send_pkts = self.gen_encrypt_pkts6(
1638 p_in,
1639 p_in.scapy_tun_sa,
1640 self.tun_if,
1641 src=p_in.remote_tun_if_host,
1642 dst=self.pg1.remote_ip6,
1643 count=count,
1644 payload_size=payload_size,
1645 )
Klement Sekera611864f2018-09-26 11:19:00 +02001646 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Neale Rannsc87b66c2019-02-07 07:26:12 -08001647 self.verify_decrypted6(p_in, recv_pkts)
1648
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001649 send_pkts = self.gen_pkts6(
1650 p_in,
1651 self.pg1,
1652 src=self.pg1.remote_ip6,
1653 dst=p_out.remote_tun_if_host,
1654 count=count,
1655 payload_size=payload_size,
1656 )
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001657 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
Neale Rannsc87b66c2019-02-07 07:26:12 -08001658 self.verify_encrypted6(p_out, p_out.vpp_tun_sa, recv_pkts)
1659
Neale Rannsf3a66222020-01-02 05:04:00 +00001660 for rx in recv_pkts:
1661 self.assertEqual(rx[IPv6].src, p_out.tun_src)
1662 self.assertEqual(rx[IPv6].dst, p_out.tun_dst)
1663
Klement Sekera31da2e32018-06-24 22:49:55 +02001664 finally:
1665 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -04001666 self.logger.info(self.vapi.ppcli("show ipsec all"))
Neale Rannsc87b66c2019-02-07 07:26:12 -08001667 self.verify_counters6(p_in, p_out, count)
Klement Sekera31da2e32018-06-24 22:49:55 +02001668
Neale Ranns14046982019-07-29 14:49:52 +00001669 def verify_tun_reass_66(self, p):
1670 self.vapi.cli("clear errors")
1671 self.vapi.ip_reassembly_enable_disable(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001672 sw_if_index=self.tun_if.sw_if_index, enable_ip6=True
1673 )
Neale Ranns14046982019-07-29 14:49:52 +00001674
1675 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001676 send_pkts = self.gen_encrypt_pkts6(
1677 p,
1678 p.scapy_tun_sa,
1679 self.tun_if,
1680 src=p.remote_tun_if_host,
1681 dst=self.pg1.remote_ip6,
1682 count=1,
1683 payload_size=1850,
1684 )
Neale Ranns14046982019-07-29 14:49:52 +00001685 send_pkts = fragment_rfc8200(send_pkts[0], 1, 1400, self.logger)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001686 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1, n_rx=1)
Neale Ranns14046982019-07-29 14:49:52 +00001687 self.verify_decrypted6(p, recv_pkts)
1688
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001689 send_pkts = self.gen_pkts6(
1690 p,
1691 self.pg1,
1692 src=self.pg1.remote_ip6,
1693 dst=p.remote_tun_if_host,
1694 count=1,
1695 payload_size=64,
1696 )
1697 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
Neale Ranns14046982019-07-29 14:49:52 +00001698 self.verify_encrypted6(p, p.vpp_tun_sa, recv_pkts)
1699 finally:
1700 self.logger.info(self.vapi.ppcli("show error"))
1701 self.logger.info(self.vapi.ppcli("show ipsec all"))
1702 self.verify_counters6(p, p, 1)
1703 self.vapi.ip_reassembly_enable_disable(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001704 sw_if_index=self.tun_if.sw_if_index, enable_ip6=False
1705 )
Neale Ranns14046982019-07-29 14:49:52 +00001706
Neale Ranns987aea82019-03-27 13:40:35 +00001707 def verify_tun_46(self, p, count=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001708 """ipsec 4o6 tunnel basic test"""
Neale Ranns987aea82019-03-27 13:40:35 +00001709 self.vapi.cli("clear errors")
Neale Rannsdd4ccf22020-06-30 07:47:14 +00001710 self.vapi.cli("clear ipsec sa")
Neale Ranns987aea82019-03-27 13:40:35 +00001711 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001712 send_pkts = self.gen_encrypt_pkts(
1713 p,
1714 p.scapy_tun_sa,
1715 self.tun_if,
1716 src=p.remote_tun_if_host4,
1717 dst=self.pg1.remote_ip4,
1718 count=count,
1719 )
Neale Ranns987aea82019-03-27 13:40:35 +00001720 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
1721 for recv_pkt in recv_pkts:
1722 self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host4)
1723 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
1724 self.assert_packet_checksums_valid(recv_pkt)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001725 send_pkts = self.gen_pkts(
1726 self.pg1,
1727 src=self.pg1.remote_ip4,
1728 dst=p.remote_tun_if_host4,
1729 count=count,
1730 )
Neale Ranns987aea82019-03-27 13:40:35 +00001731 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
1732 for recv_pkt in recv_pkts:
1733 try:
1734 decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6])
1735 if not decrypt_pkt.haslayer(IP):
1736 decrypt_pkt = IP(decrypt_pkt[Raw].load)
1737 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
1738 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host4)
1739 self.assert_packet_checksums_valid(decrypt_pkt)
1740 except:
1741 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
1742 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001743 self.logger.debug(ppp("Decrypted packet:", decrypt_pkt))
Neale Ranns987aea82019-03-27 13:40:35 +00001744 except:
1745 pass
1746 raise
1747 finally:
1748 self.logger.info(self.vapi.ppcli("show error"))
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -04001749 self.logger.info(self.vapi.ppcli("show ipsec all"))
Neale Rannsc87b66c2019-02-07 07:26:12 -08001750 self.verify_counters6(p, p, count)
Klement Sekera10d066e2018-11-13 11:12:57 +01001751
Matthew Smith6f1eb482022-08-09 22:19:38 +00001752 def verify_keepalive(self, p):
1753 # the sizeof Raw is calculated to pad to the minimum ehternet
1754 # frame size of 64 btyes
1755 pkt = (
1756 Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac)
1757 / IPv6(src=p.remote_tun_if_host, dst=self.tun_if.local_ip6)
1758 / UDP(sport=333, dport=4500)
1759 / Raw(b"\xff")
1760 / Padding(0 * 1)
1761 )
1762 self.send_and_assert_no_replies(self.tun_if, pkt * 31)
1763 self.assert_error_counter_equal(
1764 "/err/%s/nat_keepalive" % self.tun6_input_node, 31
1765 )
1766
1767 pkt = (
1768 Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac)
1769 / IPv6(src=p.remote_tun_if_host, dst=self.tun_if.local_ip6)
1770 / UDP(sport=333, dport=4500)
1771 / Raw(b"\xfe")
1772 )
1773 self.send_and_assert_no_replies(self.tun_if, pkt * 31)
1774 self.assert_error_counter_equal("/err/%s/too_short" % self.tun6_input_node, 31)
1775
1776 pkt = (
1777 Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac)
1778 / IPv6(src=p.remote_tun_if_host, dst=self.tun_if.local_ip6)
1779 / UDP(sport=333, dport=4500)
1780 / Raw(b"\xfe")
1781 / Padding(0 * 21)
1782 )
1783 self.send_and_assert_no_replies(self.tun_if, pkt * 31)
1784 self.assert_error_counter_equal("/err/%s/too_short" % self.tun6_input_node, 62)
1785
Neale Ranns2ac885c2019-03-20 18:24:43 +00001786
1787class IpsecTun6Tests(IpsecTun6):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001788 """UT test methods for Tunnel v6"""
Neale Ranns2ac885c2019-03-20 18:24:43 +00001789
1790 def test_tun_basic66(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001791 """ipsec 6o6 tunnel basic test"""
Neale Ranns2ac885c2019-03-20 18:24:43 +00001792 self.verify_tun_66(self.params[socket.AF_INET6], count=1)
1793
Neale Ranns14046982019-07-29 14:49:52 +00001794 def test_tun_reass_basic66(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001795 """ipsec 6o6 tunnel basic reassembly test"""
Neale Ranns14046982019-07-29 14:49:52 +00001796 self.verify_tun_reass_66(self.params[socket.AF_INET6])
1797
Klement Sekera611864f2018-09-26 11:19:00 +02001798 def test_tun_burst66(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001799 """ipsec 6o6 tunnel burst test"""
Neale Ranns2ac885c2019-03-20 18:24:43 +00001800 self.verify_tun_66(self.params[socket.AF_INET6], count=257)
Klement Sekera611864f2018-09-26 11:19:00 +02001801
1802
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001803class IpsecTun6HandoffTests(IpsecTun6):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001804 """UT test methods for Tunnel v6 with multiple workers"""
1805
Klement Sekera8d815022021-03-15 16:58:10 +01001806 vpp_worker_count = 2
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001807
1808 def test_tun_handoff_66(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001809 """ipsec 6o6 tunnel worker hand-off test"""
Brian Russell7a29a2d2021-02-22 18:42:24 +00001810 self.vapi.cli("clear errors")
1811 self.vapi.cli("clear ipsec sa")
1812
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001813 N_PKTS = 15
1814 p = self.params[socket.AF_INET6]
1815
1816 # inject alternately on worker 0 and 1. all counts on the SA
1817 # should be against worker 0
1818 for worker in [0, 1, 0, 1]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001819 send_pkts = self.gen_encrypt_pkts6(
1820 p,
1821 p.scapy_tun_sa,
1822 self.tun_if,
1823 src=p.remote_tun_if_host,
1824 dst=self.pg1.remote_ip6,
1825 count=N_PKTS,
1826 )
1827 recv_pkts = self.send_and_expect(
1828 self.tun_if, send_pkts, self.pg1, worker=worker
1829 )
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001830 self.verify_decrypted6(p, recv_pkts)
1831
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001832 send_pkts = self.gen_pkts6(
1833 p,
1834 self.pg1,
1835 src=self.pg1.remote_ip6,
1836 dst=p.remote_tun_if_host,
1837 count=N_PKTS,
1838 )
1839 recv_pkts = self.send_and_expect(
1840 self.pg1, send_pkts, self.tun_if, worker=worker
1841 )
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001842 self.verify_encrypted6(p, p.vpp_tun_sa, recv_pkts)
1843
1844 # all counts against the first worker that was used
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001845 self.verify_counters6(p, p, 4 * N_PKTS, worker=0)
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001846
1847
1848class IpsecTun4HandoffTests(IpsecTun4):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001849 """UT test methods for Tunnel v4 with multiple workers"""
1850
Klement Sekera8d815022021-03-15 16:58:10 +01001851 vpp_worker_count = 2
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001852
1853 def test_tun_handooff_44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001854 """ipsec 4o4 tunnel worker hand-off test"""
Brian Russell7a29a2d2021-02-22 18:42:24 +00001855 self.vapi.cli("clear errors")
1856 self.vapi.cli("clear ipsec sa")
1857
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001858 N_PKTS = 15
1859 p = self.params[socket.AF_INET]
1860
1861 # inject alternately on worker 0 and 1. all counts on the SA
1862 # should be against worker 0
1863 for worker in [0, 1, 0, 1]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001864 send_pkts = self.gen_encrypt_pkts(
1865 p,
1866 p.scapy_tun_sa,
1867 self.tun_if,
1868 src=p.remote_tun_if_host,
1869 dst=self.pg1.remote_ip4,
1870 count=N_PKTS,
1871 )
1872 recv_pkts = self.send_and_expect(
1873 self.tun_if, send_pkts, self.pg1, worker=worker
1874 )
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001875 self.verify_decrypted(p, recv_pkts)
1876
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001877 send_pkts = self.gen_pkts(
1878 self.pg1,
1879 src=self.pg1.remote_ip4,
1880 dst=p.remote_tun_if_host,
1881 count=N_PKTS,
1882 )
1883 recv_pkts = self.send_and_expect(
1884 self.pg1, send_pkts, self.tun_if, worker=worker
1885 )
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001886 self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts)
1887
1888 # all counts against the first worker that was used
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001889 self.verify_counters4(p, 4 * N_PKTS, worker=0)
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001890
1891
Neale Ranns53f526b2019-02-25 14:32:02 +00001892class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001893 """UT test methods for Tunnel v6 & v4"""
1894
Klement Sekera611864f2018-09-26 11:19:00 +02001895 pass
1896
Klement Sekera31da2e32018-06-24 22:49:55 +02001897
Zachary Leaf26fec712021-10-26 10:05:58 -05001898class IPSecIPv4Fwd(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001899 """Test IPSec by capturing and verifying IPv4 forwarded pkts"""
1900
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00001901 @classmethod
1902 def setUpConstants(cls):
Zachary Leaf26fec712021-10-26 10:05:58 -05001903 super(IPSecIPv4Fwd, cls).setUpConstants()
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00001904
1905 def setUp(self):
Zachary Leaf26fec712021-10-26 10:05:58 -05001906 super(IPSecIPv4Fwd, self).setUp()
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00001907 # store SPD objects so we can remove configs on tear down
1908 self.spd_objs = []
1909 self.spd_policies = []
1910
1911 def tearDown(self):
1912 # remove SPD policies
1913 for obj in self.spd_policies:
1914 obj.remove_vpp_config()
1915 self.spd_policies = []
1916 # remove SPD items (interface bindings first, then SPD)
1917 for obj in reversed(self.spd_objs):
1918 obj.remove_vpp_config()
1919 self.spd_objs = []
1920 # close down pg intfs
1921 for pg in self.pg_interfaces:
1922 pg.unconfig_ip4()
1923 pg.admin_down()
Zachary Leaf26fec712021-10-26 10:05:58 -05001924 super(IPSecIPv4Fwd, self).tearDown()
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00001925
1926 def create_interfaces(self, num_ifs=2):
1927 # create interfaces pg0 ... pg<num_ifs>
1928 self.create_pg_interfaces(range(num_ifs))
1929 for pg in self.pg_interfaces:
1930 # put the interface up
1931 pg.admin_up()
1932 # configure IPv4 address on the interface
1933 pg.config_ip4()
1934 # resolve ARP, so that we know VPP MAC
1935 pg.resolve_arp()
1936 self.logger.info(self.vapi.ppcli("show int addr"))
1937
1938 def spd_create_and_intf_add(self, spd_id, pg_list):
1939 spd = VppIpsecSpd(self, spd_id)
1940 spd.add_vpp_config()
1941 self.spd_objs.append(spd)
1942 for pg in pg_list:
1943 spdItf = VppIpsecSpdItfBinding(self, spd, pg)
1944 spdItf.add_vpp_config()
1945 self.spd_objs.append(spdItf)
1946
1947 def get_policy(self, policy_type):
1948 e = VppEnum.vl_api_ipsec_spd_action_t
1949 if policy_type == "protect":
1950 return e.IPSEC_API_SPD_ACTION_PROTECT
1951 elif policy_type == "bypass":
1952 return e.IPSEC_API_SPD_ACTION_BYPASS
1953 elif policy_type == "discard":
1954 return e.IPSEC_API_SPD_ACTION_DISCARD
1955 else:
1956 raise Exception("Invalid policy type: %s", policy_type)
1957
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001958 def spd_add_rem_policy(
1959 self,
1960 spd_id,
1961 src_if,
1962 dst_if,
1963 proto,
1964 is_out,
1965 priority,
1966 policy_type,
1967 remove=False,
1968 all_ips=False,
Fan Zhangab0bf0c2022-05-25 20:03:40 +00001969 ip_range=False,
1970 local_ip_start=ip_address("0.0.0.0"),
1971 local_ip_stop=ip_address("255.255.255.255"),
1972 remote_ip_start=ip_address("0.0.0.0"),
1973 remote_ip_stop=ip_address("255.255.255.255"),
1974 remote_port_start=0,
1975 remote_port_stop=65535,
1976 local_port_start=0,
1977 local_port_stop=65535,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001978 ):
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00001979 spd = VppIpsecSpd(self, spd_id)
1980
1981 if all_ips:
1982 src_range_low = ip_address("0.0.0.0")
1983 src_range_high = ip_address("255.255.255.255")
1984 dst_range_low = ip_address("0.0.0.0")
1985 dst_range_high = ip_address("255.255.255.255")
Fan Zhangab0bf0c2022-05-25 20:03:40 +00001986
1987 elif ip_range:
1988 src_range_low = local_ip_start
1989 src_range_high = local_ip_stop
1990 dst_range_low = remote_ip_start
1991 dst_range_high = remote_ip_stop
1992
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00001993 else:
1994 src_range_low = src_if.remote_ip4
1995 src_range_high = src_if.remote_ip4
1996 dst_range_low = dst_if.remote_ip4
1997 dst_range_high = dst_if.remote_ip4
1998
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001999 spdEntry = VppIpsecSpdEntry(
2000 self,
2001 spd,
2002 0,
2003 src_range_low,
2004 src_range_high,
2005 dst_range_low,
2006 dst_range_high,
2007 proto,
2008 priority=priority,
2009 policy=self.get_policy(policy_type),
2010 is_outbound=is_out,
Fan Zhangab0bf0c2022-05-25 20:03:40 +00002011 remote_port_start=remote_port_start,
2012 remote_port_stop=remote_port_stop,
2013 local_port_start=local_port_start,
2014 local_port_stop=local_port_stop,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002015 )
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002016
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002017 if remove is False:
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002018 spdEntry.add_vpp_config()
2019 self.spd_policies.append(spdEntry)
2020 else:
2021 spdEntry.remove_vpp_config()
2022 self.spd_policies.remove(spdEntry)
2023 self.logger.info(self.vapi.ppcli("show ipsec all"))
2024 return spdEntry
2025
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002026 def create_stream(self, src_if, dst_if, pkt_count, src_prt=1234, dst_prt=5678):
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002027 packets = []
2028 for i in range(pkt_count):
2029 # create packet info stored in the test case instance
2030 info = self.create_packet_info(src_if, dst_if)
2031 # convert the info into packet payload
2032 payload = self.info_to_payload(info)
2033 # create the packet itself
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002034 p = (
2035 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
2036 / IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4)
2037 / UDP(sport=src_prt, dport=dst_prt)
2038 / Raw(payload)
2039 )
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002040 # store a copy of the packet in the packet info
2041 info.data = p.copy()
2042 # append the packet to the list
2043 packets.append(p)
2044 # return the created packet list
2045 return packets
2046
2047 def verify_capture(self, src_if, dst_if, capture):
2048 packet_info = None
2049 for packet in capture:
2050 try:
2051 ip = packet[IP]
2052 udp = packet[UDP]
2053 # convert the payload to packet info object
2054 payload_info = self.payload_to_info(packet)
2055 # make sure the indexes match
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002056 self.assert_equal(
2057 payload_info.src, src_if.sw_if_index, "source sw_if_index"
2058 )
2059 self.assert_equal(
2060 payload_info.dst, dst_if.sw_if_index, "destination sw_if_index"
2061 )
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002062 packet_info = self.get_next_packet_info_for_interface2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002063 src_if.sw_if_index, dst_if.sw_if_index, packet_info
2064 )
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002065 # make sure we didn't run out of saved packets
2066 self.assertIsNotNone(packet_info)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002067 self.assert_equal(
2068 payload_info.index, packet_info.index, "packet info index"
2069 )
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002070 saved_packet = packet_info.data # fetch the saved packet
2071 # assert the values match
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002072 self.assert_equal(ip.src, saved_packet[IP].src, "IP source address")
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002073 # ... more assertions here
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002074 self.assert_equal(udp.sport, saved_packet[UDP].sport, "UDP source port")
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002075 except Exception as e:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002076 self.logger.error(ppp("Unexpected or invalid packet:", packet))
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002077 raise
2078 remaining_packet = self.get_next_packet_info_for_interface2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002079 src_if.sw_if_index, dst_if.sw_if_index, packet_info
2080 )
2081 self.assertIsNone(
2082 remaining_packet,
2083 "Interface %s: Packet expected from interface "
2084 "%s didn't arrive" % (dst_if.name, src_if.name),
2085 )
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002086
2087 def verify_policy_match(self, pkt_count, spdEntry):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002088 self.logger.info("XXXX %s %s", str(spdEntry), str(spdEntry.get_stats()))
2089 matched_pkts = spdEntry.get_stats().get("packets")
2090 self.logger.info("Policy %s matched: %d pkts", str(spdEntry), matched_pkts)
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002091 self.assert_equal(pkt_count, matched_pkts)
2092
Zachary Leaf26fec712021-10-26 10:05:58 -05002093
2094class SpdFlowCacheTemplate(IPSecIPv4Fwd):
2095 @classmethod
2096 def setUpConstants(cls):
2097 super(SpdFlowCacheTemplate, cls).setUpConstants()
2098 # Override this method with required cmdline parameters e.g.
2099 # cls.vpp_cmdline.extend(["ipsec", "{",
2100 # "ipv4-outbound-spd-flow-cache on",
2101 # "}"])
2102 # cls.logger.info("VPP modified cmdline is %s" % " "
2103 # .join(cls.vpp_cmdline))
2104
2105 def setUp(self):
2106 super(SpdFlowCacheTemplate, self).setUp()
2107
2108 def tearDown(self):
2109 super(SpdFlowCacheTemplate, self).tearDown()
2110
Zachary Leaf7cd35f52021-06-25 08:11:15 -05002111 def get_spd_flow_cache_entries(self, outbound):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002112 """'show ipsec spd' output:
Zachary Leaf7cd35f52021-06-25 08:11:15 -05002113 ipv4-inbound-spd-flow-cache-entries: 0
2114 ipv4-outbound-spd-flow-cache-entries: 0
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002115 """
2116 show_ipsec_reply = self.vapi.cli("show ipsec spd")
2117 # match the relevant section of 'show ipsec spd' output
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002118 if outbound:
Zachary Leaf7cd35f52021-06-25 08:11:15 -05002119 regex_match = re.search(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002120 "ipv4-outbound-spd-flow-cache-entries: (.*)",
2121 show_ipsec_reply,
2122 re.DOTALL,
2123 )
Zachary Leaf7cd35f52021-06-25 08:11:15 -05002124 else:
2125 regex_match = re.search(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002126 "ipv4-inbound-spd-flow-cache-entries: (.*)", show_ipsec_reply, re.DOTALL
2127 )
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002128 if regex_match is None:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002129 raise Exception(
2130 "Unable to find spd flow cache entries \
2131 in 'show ipsec spd' CLI output - regex failed to match"
2132 )
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002133 else:
2134 try:
2135 num_entries = int(regex_match.group(1))
2136 except ValueError:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002137 raise Exception(
2138 "Unable to get spd flow cache entries \
2139 from 'show ipsec spd' string: %s",
2140 regex_match.group(0),
2141 )
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002142 self.logger.info("%s", regex_match.group(0))
2143 return num_entries
2144
2145 def verify_num_outbound_flow_cache_entries(self, expected_elements):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002146 self.assertEqual(
2147 self.get_spd_flow_cache_entries(outbound=True), expected_elements
2148 )
Zachary Leaf7cd35f52021-06-25 08:11:15 -05002149
2150 def verify_num_inbound_flow_cache_entries(self, expected_elements):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002151 self.assertEqual(
2152 self.get_spd_flow_cache_entries(outbound=False), expected_elements
2153 )
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002154
2155 def crc32_supported(self):
2156 # lscpu is part of util-linux package, available on all Linux Distros
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002157 stream = os.popen("lscpu")
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +00002158 cpu_info = stream.read()
2159 # feature/flag "crc32" on Aarch64 and "sse4_2" on x86
2160 # see vppinfra/crc32.h
2161 if "crc32" or "sse4_2" in cpu_info:
2162 self.logger.info("\ncrc32 supported:\n" + cpu_info)
2163 return True
2164 else:
2165 self.logger.info("\ncrc32 NOT supported:\n" + cpu_info)
2166 return False
2167
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002168
Piotr Bronowski651cc012022-07-08 12:45:51 +00002169class IPSecIPv6Fwd(VppTestCase):
2170 """Test IPSec by capturing and verifying IPv6 forwarded pkts"""
2171
2172 @classmethod
2173 def setUpConstants(cls):
2174 super(IPSecIPv6Fwd, cls).setUpConstants()
2175
2176 def setUp(self):
2177 super(IPSecIPv6Fwd, self).setUp()
2178 # store SPD objects so we can remove configs on tear down
2179 self.spd_objs = []
2180 self.spd_policies = []
2181
2182 def tearDown(self):
2183 # remove SPD policies
2184 for obj in self.spd_policies:
2185 obj.remove_vpp_config()
2186 self.spd_policies = []
2187 # remove SPD items (interface bindings first, then SPD)
2188 for obj in reversed(self.spd_objs):
2189 obj.remove_vpp_config()
2190 self.spd_objs = []
2191 # close down pg intfs
2192 for pg in self.pg_interfaces:
2193 pg.unconfig_ip6()
2194 pg.admin_down()
2195 super(IPSecIPv6Fwd, self).tearDown()
2196
2197 def create_interfaces(self, num_ifs=2):
2198 # create interfaces pg0 ... pg<num_ifs>
2199 self.create_pg_interfaces(range(num_ifs))
2200 for pg in self.pg_interfaces:
2201 # put the interface up
2202 pg.admin_up()
2203 # configure IPv6 address on the interface
2204 pg.config_ip6()
2205 pg.resolve_ndp()
2206 self.logger.info(self.vapi.ppcli("show int addr"))
2207
2208 def spd_create_and_intf_add(self, spd_id, pg_list):
2209 spd = VppIpsecSpd(self, spd_id)
2210 spd.add_vpp_config()
2211 self.spd_objs.append(spd)
2212 for pg in pg_list:
2213 spdItf = VppIpsecSpdItfBinding(self, spd, pg)
2214 spdItf.add_vpp_config()
2215 self.spd_objs.append(spdItf)
2216
2217 def get_policy(self, policy_type):
2218 e = VppEnum.vl_api_ipsec_spd_action_t
2219 if policy_type == "protect":
2220 return e.IPSEC_API_SPD_ACTION_PROTECT
2221 elif policy_type == "bypass":
2222 return e.IPSEC_API_SPD_ACTION_BYPASS
2223 elif policy_type == "discard":
2224 return e.IPSEC_API_SPD_ACTION_DISCARD
2225 else:
2226 raise Exception("Invalid policy type: %s", policy_type)
2227
2228 def spd_add_rem_policy(
2229 self,
2230 spd_id,
2231 src_if,
2232 dst_if,
2233 proto,
2234 is_out,
2235 priority,
2236 policy_type,
2237 remove=False,
2238 all_ips=False,
2239 ip_range=False,
2240 local_ip_start=ip_address("0::0"),
2241 local_ip_stop=ip_address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
2242 remote_ip_start=ip_address("0::0"),
2243 remote_ip_stop=ip_address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
2244 remote_port_start=0,
2245 remote_port_stop=65535,
2246 local_port_start=0,
2247 local_port_stop=65535,
2248 ):
2249 spd = VppIpsecSpd(self, spd_id)
2250
2251 if all_ips:
2252 src_range_low = ip_address("0::0")
2253 src_range_high = ip_address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
2254 dst_range_low = ip_address("0::0")
2255 dst_range_high = ip_address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
2256
2257 elif ip_range:
2258 src_range_low = local_ip_start
2259 src_range_high = local_ip_stop
2260 dst_range_low = remote_ip_start
2261 dst_range_high = remote_ip_stop
2262
2263 else:
2264 src_range_low = src_if.remote_ip6
2265 src_range_high = src_if.remote_ip6
2266 dst_range_low = dst_if.remote_ip6
2267 dst_range_high = dst_if.remote_ip6
2268
2269 spdEntry = VppIpsecSpdEntry(
2270 self,
2271 spd,
2272 0,
2273 src_range_low,
2274 src_range_high,
2275 dst_range_low,
2276 dst_range_high,
2277 proto,
2278 priority=priority,
2279 policy=self.get_policy(policy_type),
2280 is_outbound=is_out,
2281 remote_port_start=remote_port_start,
2282 remote_port_stop=remote_port_stop,
2283 local_port_start=local_port_start,
2284 local_port_stop=local_port_stop,
2285 )
2286
2287 if remove is False:
2288 spdEntry.add_vpp_config()
2289 self.spd_policies.append(spdEntry)
2290 else:
2291 spdEntry.remove_vpp_config()
2292 self.spd_policies.remove(spdEntry)
2293 self.logger.info(self.vapi.ppcli("show ipsec all"))
2294 return spdEntry
2295
2296 def create_stream(self, src_if, dst_if, pkt_count, src_prt=1234, dst_prt=5678):
2297 packets = []
2298 for i in range(pkt_count):
2299 # create packet info stored in the test case instance
2300 info = self.create_packet_info(src_if, dst_if)
2301 # convert the info into packet payload
2302 payload = self.info_to_payload(info)
2303 # create the packet itself
2304 p = (
2305 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
2306 / IPv6(src=src_if.remote_ip6, dst=dst_if.remote_ip6)
2307 / UDP(sport=src_prt, dport=dst_prt)
2308 / Raw(payload)
2309 )
2310 # store a copy of the packet in the packet info
2311 info.data = p.copy()
2312 # append the packet to the list
2313 packets.append(p)
2314 # return the created packet list
2315 return packets
2316
2317 def verify_capture(self, src_if, dst_if, capture):
2318 packet_info = None
2319 for packet in capture:
2320 try:
2321 ip = packet[IPv6]
2322 udp = packet[UDP]
2323 # convert the payload to packet info object
2324 payload_info = self.payload_to_info(packet)
2325 # make sure the indexes match
2326 self.assert_equal(
2327 payload_info.src, src_if.sw_if_index, "source sw_if_index"
2328 )
2329 self.assert_equal(
2330 payload_info.dst, dst_if.sw_if_index, "destination sw_if_index"
2331 )
2332 packet_info = self.get_next_packet_info_for_interface2(
2333 src_if.sw_if_index, dst_if.sw_if_index, packet_info
2334 )
2335 # make sure we didn't run out of saved packets
2336 self.assertIsNotNone(packet_info)
2337 self.assert_equal(
2338 payload_info.index, packet_info.index, "packet info index"
2339 )
2340 saved_packet = packet_info.data # fetch the saved packet
2341 # assert the values match
2342 self.assert_equal(ip.src, saved_packet[IPv6].src, "IP source address")
2343 # ... more assertions here
2344 self.assert_equal(udp.sport, saved_packet[UDP].sport, "UDP source port")
2345 except Exception as e:
2346 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2347 raise
2348 remaining_packet = self.get_next_packet_info_for_interface2(
2349 src_if.sw_if_index, dst_if.sw_if_index, packet_info
2350 )
2351 self.assertIsNone(
2352 remaining_packet,
2353 "Interface %s: Packet expected from interface "
2354 "%s didn't arrive" % (dst_if.name, src_if.name),
2355 )
2356
2357 def verify_policy_match(self, pkt_count, spdEntry):
2358 self.logger.info("XXXX %s %s", str(spdEntry), str(spdEntry.get_stats()))
2359 matched_pkts = spdEntry.get_stats().get("packets")
2360 self.logger.info("Policy %s matched: %d pkts", str(spdEntry), matched_pkts)
2361 self.assert_equal(pkt_count, matched_pkts)
2362
2363
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002364if __name__ == "__main__":
Klement Sekera31da2e32018-06-24 22:49:55 +02002365 unittest.main(testRunner=VppTestRunner)