blob: 38d0dc3dde5df695cf948aa946bf3e8731f239d2 [file] [log] [blame]
Klement Sekera31da2e32018-06-24 22:49:55 +02001import unittest
2import socket
Neale Ranns2ac885c2019-03-20 18:24:43 +00003import copy
Neale Ranns47feb112019-04-11 15:14:07 +00004
Neale Ranns12989b52019-09-26 16:20:19 +00005from scapy.layers.ipsec import SecurityAssociation, ESP
John Lo90430b62020-01-31 23:48:30 -05006from scapy.layers.l2 import Ether, GRE, Dot1Q
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +01007from scapy.packet import Raw, bind_layers
Neale Rannsf05e7322019-03-29 20:23:58 +00008from scapy.layers.inet import IP, UDP
Neale Rannsc87b66c2019-02-07 07:26:12 -08009from scapy.layers.inet6 import IPv6
Neale Ranns4a58e492020-12-21 13:19:10 +000010from scapy.contrib.mpls import MPLS
Dave Wallace76a1d052022-09-27 13:11:53 -040011from framework import tag_fixme_vpp_workers
juraj.linkes11057662019-07-08 10:22:55 +020012from framework import VppTestRunner
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020013from template_ipsec import (
14 TemplateIpsec,
15 IpsecTun4Tests,
16 IpsecTun6Tests,
17 IpsecTun4,
18 IpsecTun6,
19 IpsecTcpTests,
20 mk_scapy_crypt_key,
21 IpsecTun6HandoffTests,
22 IpsecTun4HandoffTests,
23 config_tun_params,
24)
Neale Rannsc87b66c2019-02-07 07:26:12 -080025from vpp_gre_interface import VppGreInterface
26from vpp_ipip_tun_interface import VppIpIpTunInterface
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020027from vpp_ip_route import (
28 VppIpRoute,
29 VppRoutePath,
30 DpoProto,
31 VppMplsLabel,
32 VppMplsTable,
33 VppMplsRoute,
34 FibPathProto,
35)
Neale Rannsdd4ccf22020-06-30 07:47:14 +000036from vpp_ipsec import VppIpsecSA, VppIpsecTunProtect, VppIpsecInterface
Neale Rannsf05e7322019-03-29 20:23:58 +000037from vpp_l2 import VppBridgeDomain, VppBridgeDomainPort
John Lo90430b62020-01-31 23:48:30 -050038from vpp_sub_interface import L2_VTR_OP, VppDot1QSubint
Neale Ranns28287212019-12-16 00:53:11 +000039from vpp_teib import VppTeib
Neale Rannsf05e7322019-03-29 20:23:58 +000040from util import ppp
Neale Ranns47feb112019-04-11 15:14:07 +000041from vpp_papi import VppEnum
Eric Kinzie609d5792020-10-13 20:02:11 -040042from vpp_papi_provider import CliFailedCommandError
Neale Ranns5d0136f2020-05-12 08:51:02 +000043from vpp_acl import AclRule, VppAcl, VppAclInterface
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +020044from vpp_policer import PolicerAction, VppPolicer, Dir
Klement Sekera31da2e32018-06-24 22:49:55 +020045
46
Neale Rannsdd4ccf22020-06-30 07:47:14 +000047def config_tun_params(p, encryption_type, tun_if, src=None, dst=None):
Neale Ranns12989b52019-09-26 16:20:19 +000048 ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020049 esn_en = bool(
50 p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN)
51 )
Neale Ranns12989b52019-09-26 16:20:19 +000052 crypt_key = mk_scapy_crypt_key(p)
Neale Rannsdd4ccf22020-06-30 07:47:14 +000053 if tun_if:
54 p.tun_dst = tun_if.remote_ip
55 p.tun_src = tun_if.local_ip
56 else:
57 p.tun_dst = dst
58 p.tun_src = src
59
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010060 if p.nat_header:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020061 is_default_port = p.nat_header.dport == 4500
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010062 else:
63 is_default_port = True
64
65 if is_default_port:
66 outbound_nat_header = p.nat_header
67 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020068 outbound_nat_header = UDP(sport=p.nat_header.dport, dport=p.nat_header.sport)
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010069 bind_layers(UDP, ESP, dport=p.nat_header.dport)
70
Neale Ranns12989b52019-09-26 16:20:19 +000071 p.scapy_tun_sa = SecurityAssociation(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020072 encryption_type,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +010073 spi=p.scapy_tun_spi,
Neale Ranns12989b52019-09-26 16:20:19 +000074 crypt_algo=p.crypt_algo,
75 crypt_key=crypt_key,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 auth_algo=p.auth_algo,
77 auth_key=p.auth_key,
78 tunnel_header=ip_class_by_addr_type[p.addr_type](src=p.tun_dst, dst=p.tun_src),
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010079 nat_t_header=outbound_nat_header,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020080 esn_en=esn_en,
81 )
Neale Ranns12989b52019-09-26 16:20:19 +000082 p.vpp_tun_sa = SecurityAssociation(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020083 encryption_type,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +010084 spi=p.vpp_tun_spi,
Neale Ranns12989b52019-09-26 16:20:19 +000085 crypt_algo=p.crypt_algo,
86 crypt_key=crypt_key,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020087 auth_algo=p.auth_algo,
88 auth_key=p.auth_key,
89 tunnel_header=ip_class_by_addr_type[p.addr_type](dst=p.tun_dst, src=p.tun_src),
Neale Ranns12989b52019-09-26 16:20:19 +000090 nat_t_header=p.nat_header,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020091 esn_en=esn_en,
92 )
Neale Ranns12989b52019-09-26 16:20:19 +000093
94
Neale Ranns568acbb2019-12-18 05:54:40 +000095def config_tra_params(p, encryption_type, tun_if):
96 ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020097 esn_en = bool(
98 p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN)
99 )
Neale Ranns568acbb2019-12-18 05:54:40 +0000100 crypt_key = mk_scapy_crypt_key(p)
Neale Rannsf3a66222020-01-02 05:04:00 +0000101 p.tun_dst = tun_if.remote_ip
102 p.tun_src = tun_if.local_ip
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100103
104 if p.nat_header:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200105 is_default_port = p.nat_header.dport == 4500
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100106 else:
107 is_default_port = True
108
109 if is_default_port:
110 outbound_nat_header = p.nat_header
111 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200112 outbound_nat_header = UDP(sport=p.nat_header.dport, dport=p.nat_header.sport)
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100113 bind_layers(UDP, ESP, dport=p.nat_header.dport)
114
Neale Ranns568acbb2019-12-18 05:54:40 +0000115 p.scapy_tun_sa = SecurityAssociation(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200116 encryption_type,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100117 spi=p.scapy_tun_spi,
Neale Ranns568acbb2019-12-18 05:54:40 +0000118 crypt_algo=p.crypt_algo,
119 crypt_key=crypt_key,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200120 auth_algo=p.auth_algo,
121 auth_key=p.auth_key,
Neale Rannsabc56602020-04-01 09:45:23 +0000122 esn_en=esn_en,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200123 nat_t_header=outbound_nat_header,
124 )
Neale Ranns568acbb2019-12-18 05:54:40 +0000125 p.vpp_tun_sa = SecurityAssociation(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200126 encryption_type,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100127 spi=p.vpp_tun_spi,
Neale Ranns568acbb2019-12-18 05:54:40 +0000128 crypt_algo=p.crypt_algo,
129 crypt_key=crypt_key,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200130 auth_algo=p.auth_algo,
131 auth_key=p.auth_key,
Neale Rannsabc56602020-04-01 09:45:23 +0000132 esn_en=esn_en,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200133 nat_t_header=p.nat_header,
134 )
Neale Ranns568acbb2019-12-18 05:54:40 +0000135
136
Neale Rannsa9e27742020-12-23 16:22:28 +0000137class TemplateIpsec4TunProtect(object):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200138 """IPsec IPv4 Tunnel protect"""
Neale Rannsa9e27742020-12-23 16:22:28 +0000139
140 encryption_type = ESP
141 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +0000142 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Rannsa9e27742020-12-23 16:22:28 +0000143 tun4_input_node = "ipsec4-tun-input"
144
145 def config_sa_tra(self, p):
146 config_tun_params(p, self.encryption_type, p.tun_if)
147
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200148 p.tun_sa_out = VppIpsecSA(
149 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100150 p.vpp_tun_sa_id,
151 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200152 p.auth_algo_vpp_id,
153 p.auth_key,
154 p.crypt_algo_vpp_id,
155 p.crypt_key,
156 self.vpp_esp_protocol,
157 flags=p.flags,
158 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000159 p.tun_sa_out.add_vpp_config()
160
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200161 p.tun_sa_in = VppIpsecSA(
162 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100163 p.scapy_tun_sa_id,
164 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200165 p.auth_algo_vpp_id,
166 p.auth_key,
167 p.crypt_algo_vpp_id,
168 p.crypt_key,
169 self.vpp_esp_protocol,
170 flags=p.flags,
171 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000172 p.tun_sa_in.add_vpp_config()
173
174 def config_sa_tun(self, p):
175 config_tun_params(p, self.encryption_type, p.tun_if)
176
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200177 p.tun_sa_out = VppIpsecSA(
178 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100179 p.vpp_tun_sa_id,
180 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200181 p.auth_algo_vpp_id,
182 p.auth_key,
183 p.crypt_algo_vpp_id,
184 p.crypt_key,
185 self.vpp_esp_protocol,
186 self.tun_if.local_addr[p.addr_type],
187 self.tun_if.remote_addr[p.addr_type],
188 flags=p.flags,
189 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000190 p.tun_sa_out.add_vpp_config()
191
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200192 p.tun_sa_in = VppIpsecSA(
193 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100194 p.scapy_tun_sa_id,
195 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200196 p.auth_algo_vpp_id,
197 p.auth_key,
198 p.crypt_algo_vpp_id,
199 p.crypt_key,
200 self.vpp_esp_protocol,
201 self.tun_if.remote_addr[p.addr_type],
202 self.tun_if.local_addr[p.addr_type],
203 flags=p.flags,
204 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000205 p.tun_sa_in.add_vpp_config()
206
207 def config_protect(self, p):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200208 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
Neale Rannsa9e27742020-12-23 16:22:28 +0000209 p.tun_protect.add_vpp_config()
210
211 def config_network(self, p):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200212 if hasattr(p, "tun_dst"):
Neale Rannsa9e27742020-12-23 16:22:28 +0000213 tun_dst = p.tun_dst
214 else:
215 tun_dst = self.pg0.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200216 p.tun_if = VppIpIpTunInterface(self, self.pg0, self.pg0.local_ip4, tun_dst)
Neale Rannsa9e27742020-12-23 16:22:28 +0000217 p.tun_if.add_vpp_config()
218 p.tun_if.admin_up()
219 p.tun_if.config_ip4()
220 p.tun_if.config_ip6()
221
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200222 p.route = VppIpRoute(
223 self,
224 p.remote_tun_if_host,
225 32,
226 [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)],
227 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000228 p.route.add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200229 r = VppIpRoute(
230 self,
231 p.remote_tun_if_host6,
232 128,
233 [
234 VppRoutePath(
235 p.tun_if.remote_ip6, 0xFFFFFFFF, proto=DpoProto.DPO_PROTO_IP6
236 )
237 ],
238 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000239 r.add_vpp_config()
240
241 def unconfig_network(self, p):
242 p.route.remove_vpp_config()
243 p.tun_if.remove_vpp_config()
244
245 def unconfig_protect(self, p):
246 p.tun_protect.remove_vpp_config()
247
248 def unconfig_sa(self, p):
249 p.tun_sa_out.remove_vpp_config()
250 p.tun_sa_in.remove_vpp_config()
251
252
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200253class TemplateIpsec4TunIfEsp(TemplateIpsec4TunProtect, TemplateIpsec):
254 """IPsec tunnel interface tests"""
Klement Sekera31da2e32018-06-24 22:49:55 +0200255
256 encryption_type = ESP
257
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700258 @classmethod
259 def setUpClass(cls):
260 super(TemplateIpsec4TunIfEsp, cls).setUpClass()
261
262 @classmethod
263 def tearDownClass(cls):
264 super(TemplateIpsec4TunIfEsp, cls).tearDownClass()
265
Klement Sekera31da2e32018-06-24 22:49:55 +0200266 def setUp(self):
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400267 super(TemplateIpsec4TunIfEsp, self).setUp()
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800268
269 self.tun_if = self.pg0
270
Klement Sekera611864f2018-09-26 11:19:00 +0200271 p = self.ipv4_params
Neale Rannsd7603d92019-03-28 08:56:10 +0000272
Neale Rannsa9e27742020-12-23 16:22:28 +0000273 self.config_network(p)
274 self.config_sa_tra(p)
275 self.config_protect(p)
Klement Sekera31da2e32018-06-24 22:49:55 +0200276
277 def tearDown(self):
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400278 super(TemplateIpsec4TunIfEsp, self).tearDown()
Klement Sekera31da2e32018-06-24 22:49:55 +0200279
280
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200281class TemplateIpsec4TunIfEspUdp(TemplateIpsec4TunProtect, TemplateIpsec):
282 """IPsec UDP tunnel interface tests"""
Neale Ranns41afb332019-07-16 06:19:35 -0700283
284 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +0000285 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Ranns41afb332019-07-16 06:19:35 -0700286 encryption_type = ESP
287
288 @classmethod
289 def setUpClass(cls):
290 super(TemplateIpsec4TunIfEspUdp, cls).setUpClass()
291
292 @classmethod
293 def tearDownClass(cls):
294 super(TemplateIpsec4TunIfEspUdp, cls).tearDownClass()
295
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400296 def verify_encrypted(self, p, sa, rxs):
297 for rx in rxs:
298 try:
299 # ensure the UDP ports are correct before we decrypt
300 # which strips them
301 self.assertTrue(rx.haslayer(UDP))
Neale Rannsa9e27742020-12-23 16:22:28 +0000302 self.assert_equal(rx[UDP].sport, p.nat_header.sport)
Arthur de Kerhor4117b242022-08-31 19:13:03 +0200303 self.assert_equal(rx[UDP].dport, p.nat_header.dport)
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400304
305 pkt = sa.decrypt(rx[IP])
306 if not pkt.haslayer(IP):
307 pkt = IP(pkt[Raw].load)
308
309 self.assert_packet_checksums_valid(pkt)
310 self.assert_equal(pkt[IP].dst, "1.1.1.1")
311 self.assert_equal(pkt[IP].src, self.pg1.remote_ip4)
312 except (IndexError, AssertionError):
313 self.logger.debug(ppp("Unexpected packet:", rx))
314 try:
315 self.logger.debug(ppp("Decrypted packet:", pkt))
316 except:
317 pass
318 raise
319
Neale Rannsa9e27742020-12-23 16:22:28 +0000320 def config_sa_tra(self, p):
321 config_tun_params(p, self.encryption_type, p.tun_if)
322
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200323 p.tun_sa_out = VppIpsecSA(
324 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100325 p.vpp_tun_sa_id,
326 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200327 p.auth_algo_vpp_id,
328 p.auth_key,
329 p.crypt_algo_vpp_id,
330 p.crypt_key,
331 self.vpp_esp_protocol,
332 flags=p.flags,
333 udp_src=p.nat_header.sport,
334 udp_dst=p.nat_header.dport,
335 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000336 p.tun_sa_out.add_vpp_config()
337
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200338 p.tun_sa_in = VppIpsecSA(
339 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100340 p.scapy_tun_sa_id,
341 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200342 p.auth_algo_vpp_id,
343 p.auth_key,
344 p.crypt_algo_vpp_id,
345 p.crypt_key,
346 self.vpp_esp_protocol,
Arthur de Kerhor4117b242022-08-31 19:13:03 +0200347 flags=p.flags
348 | VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_IS_INBOUND,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200349 udp_src=p.nat_header.sport,
350 udp_dst=p.nat_header.dport,
351 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000352 p.tun_sa_in.add_vpp_config()
353
Neale Ranns41afb332019-07-16 06:19:35 -0700354 def setUp(self):
355 super(TemplateIpsec4TunIfEspUdp, self).setUp()
356
Neale Ranns41afb332019-07-16 06:19:35 -0700357 p = self.ipv4_params
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200358 p.flags = VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_UDP_ENCAP
Neale Ranns41afb332019-07-16 06:19:35 -0700359 p.nat_header = UDP(sport=5454, dport=4500)
360
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400361 self.tun_if = self.pg0
Neale Ranns41afb332019-07-16 06:19:35 -0700362
Neale Rannsa9e27742020-12-23 16:22:28 +0000363 self.config_network(p)
364 self.config_sa_tra(p)
365 self.config_protect(p)
Neale Ranns41afb332019-07-16 06:19:35 -0700366
367 def tearDown(self):
368 super(TemplateIpsec4TunIfEspUdp, self).tearDown()
369
370
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400371class TestIpsec4TunIfEsp1(TemplateIpsec4TunIfEsp, IpsecTun4Tests):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200372 """Ipsec ESP - TUN tests"""
373
Klement Sekera6aa58b72019-05-16 14:34:55 +0200374 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +0000375 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Klement Sekera31da2e32018-06-24 22:49:55 +0200376
Neale Ranns987aea82019-03-27 13:40:35 +0000377 def test_tun_basic64(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200378 """ipsec 6o4 tunnel basic test"""
Neale Ranns12989b52019-09-26 16:20:19 +0000379 self.tun4_encrypt_node_name = "esp4-encrypt-tun"
Klement Sekera6aa58b72019-05-16 14:34:55 +0200380
Neale Ranns987aea82019-03-27 13:40:35 +0000381 self.verify_tun_64(self.params[socket.AF_INET], count=1)
382
383 def test_tun_burst64(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200384 """ipsec 6o4 tunnel basic test"""
Neale Ranns12989b52019-09-26 16:20:19 +0000385 self.tun4_encrypt_node_name = "esp4-encrypt-tun"
Klement Sekera6aa58b72019-05-16 14:34:55 +0200386
Neale Ranns987aea82019-03-27 13:40:35 +0000387 self.verify_tun_64(self.params[socket.AF_INET], count=257)
388
Neale Rannsd7603d92019-03-28 08:56:10 +0000389 def test_tun_basic_frag44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200390 """ipsec 4o4 tunnel frag basic test"""
Klement Sekera6aa58b72019-05-16 14:34:55 +0200391 self.tun4_encrypt_node_name = "esp4-encrypt-tun"
392
Neale Rannsd7603d92019-03-28 08:56:10 +0000393 p = self.ipv4_params
394
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200395 self.vapi.sw_interface_set_mtu(p.tun_if.sw_if_index, [1500, 0, 0, 0])
396 self.verify_tun_44(
397 self.params[socket.AF_INET], count=1, payload_size=1800, n_rx=2
398 )
399 self.vapi.sw_interface_set_mtu(p.tun_if.sw_if_index, [9000, 0, 0, 0])
Neale Rannsd7603d92019-03-28 08:56:10 +0000400
Klement Sekera31da2e32018-06-24 22:49:55 +0200401
Neale Ranns41afb332019-07-16 06:19:35 -0700402class TestIpsec4TunIfEspUdp(TemplateIpsec4TunIfEspUdp, IpsecTun4Tests):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200403 """Ipsec ESP UDP tests"""
Neale Ranns41afb332019-07-16 06:19:35 -0700404
Neale Ranns12989b52019-09-26 16:20:19 +0000405 tun4_input_node = "ipsec4-tun-input"
Neale Ranns41afb332019-07-16 06:19:35 -0700406
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400407 def setUp(self):
Neale Rannsa9e27742020-12-23 16:22:28 +0000408 super(TestIpsec4TunIfEspUdp, self).setUp()
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400409
Neale Ranns41afb332019-07-16 06:19:35 -0700410 def test_keepalive(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200411 """IPSEC NAT Keepalive"""
Neale Ranns41afb332019-07-16 06:19:35 -0700412 self.verify_keepalive(self.ipv4_params)
413
414
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400415class TestIpsec4TunIfEspUdpGCM(TemplateIpsec4TunIfEspUdp, IpsecTun4Tests):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200416 """Ipsec ESP UDP GCM tests"""
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400417
418 tun4_input_node = "ipsec4-tun-input"
419
420 def setUp(self):
Neale Rannsa9e27742020-12-23 16:22:28 +0000421 super(TestIpsec4TunIfEspUdpGCM, self).setUp()
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400422 p = self.ipv4_params
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200423 p.auth_algo_vpp_id = VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE
424 p.crypt_algo_vpp_id = (
425 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_GCM_256
426 )
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400427 p.crypt_algo = "AES-GCM"
428 p.auth_algo = "NULL"
429 p.crypt_key = b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h"
430 p.salt = 0
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400431
432
Arthur de Kerhor4117b242022-08-31 19:13:03 +0200433class TestIpsec4TunIfEspUdpUpdate(TemplateIpsec4TunIfEspUdp, IpsecTun4Tests):
434 """Ipsec ESP UDP update tests"""
435
436 tun4_input_node = "ipsec4-tun-input"
437
438 def setUp(self):
439 super(TestIpsec4TunIfEspUdpUpdate, self).setUp()
440 p = self.ipv4_params
441 p.nat_header = UDP(sport=6565, dport=7676)
442 config_tun_params(p, self.encryption_type, p.tun_if)
443 p.tun_sa_in.update_vpp_config(
444 udp_src=p.nat_header.dport, udp_dst=p.nat_header.sport
445 )
446 p.tun_sa_out.update_vpp_config(
447 udp_src=p.nat_header.sport, udp_dst=p.nat_header.dport
448 )
449
450
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400451class TestIpsec4TunIfEsp2(TemplateIpsec4TunIfEsp, IpsecTcpTests):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200452 """Ipsec ESP - TCP tests"""
453
Klement Sekera31da2e32018-06-24 22:49:55 +0200454 pass
455
456
Neale Rannsa9e27742020-12-23 16:22:28 +0000457class TemplateIpsec6TunProtect(object):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200458 """IPsec IPv6 Tunnel protect"""
Neale Rannsa9e27742020-12-23 16:22:28 +0000459
460 def config_sa_tra(self, p):
461 config_tun_params(p, self.encryption_type, p.tun_if)
462
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200463 p.tun_sa_out = VppIpsecSA(
464 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100465 p.vpp_tun_sa_id,
466 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200467 p.auth_algo_vpp_id,
468 p.auth_key,
469 p.crypt_algo_vpp_id,
470 p.crypt_key,
471 self.vpp_esp_protocol,
472 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000473 p.tun_sa_out.add_vpp_config()
474
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200475 p.tun_sa_in = VppIpsecSA(
476 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100477 p.scapy_tun_sa_id,
478 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200479 p.auth_algo_vpp_id,
480 p.auth_key,
481 p.crypt_algo_vpp_id,
482 p.crypt_key,
483 self.vpp_esp_protocol,
484 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000485 p.tun_sa_in.add_vpp_config()
486
487 def config_sa_tun(self, p):
488 config_tun_params(p, self.encryption_type, p.tun_if)
489
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200490 p.tun_sa_out = VppIpsecSA(
491 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100492 p.vpp_tun_sa_id,
493 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200494 p.auth_algo_vpp_id,
495 p.auth_key,
496 p.crypt_algo_vpp_id,
497 p.crypt_key,
498 self.vpp_esp_protocol,
499 self.tun_if.local_addr[p.addr_type],
500 self.tun_if.remote_addr[p.addr_type],
501 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000502 p.tun_sa_out.add_vpp_config()
503
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200504 p.tun_sa_in = VppIpsecSA(
505 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100506 p.scapy_tun_sa_id,
507 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200508 p.auth_algo_vpp_id,
509 p.auth_key,
510 p.crypt_algo_vpp_id,
511 p.crypt_key,
512 self.vpp_esp_protocol,
513 self.tun_if.remote_addr[p.addr_type],
514 self.tun_if.local_addr[p.addr_type],
515 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000516 p.tun_sa_in.add_vpp_config()
517
518 def config_protect(self, p):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200519 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
Neale Rannsa9e27742020-12-23 16:22:28 +0000520 p.tun_protect.add_vpp_config()
521
522 def config_network(self, p):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200523 if hasattr(p, "tun_dst"):
Neale Rannsa9e27742020-12-23 16:22:28 +0000524 tun_dst = p.tun_dst
525 else:
526 tun_dst = self.pg0.remote_ip6
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200527 p.tun_if = VppIpIpTunInterface(self, self.pg0, self.pg0.local_ip6, tun_dst)
Neale Rannsa9e27742020-12-23 16:22:28 +0000528 p.tun_if.add_vpp_config()
529 p.tun_if.admin_up()
530 p.tun_if.config_ip6()
531 p.tun_if.config_ip4()
532
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200533 p.route = VppIpRoute(
534 self,
535 p.remote_tun_if_host,
536 128,
537 [
538 VppRoutePath(
539 p.tun_if.remote_ip6, 0xFFFFFFFF, proto=DpoProto.DPO_PROTO_IP6
540 )
541 ],
542 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000543 p.route.add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200544 r = VppIpRoute(
545 self,
546 p.remote_tun_if_host4,
547 32,
548 [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)],
549 )
Neale Rannsa9e27742020-12-23 16:22:28 +0000550 r.add_vpp_config()
551
552 def unconfig_network(self, p):
553 p.route.remove_vpp_config()
554 p.tun_if.remove_vpp_config()
555
556 def unconfig_protect(self, p):
557 p.tun_protect.remove_vpp_config()
558
559 def unconfig_sa(self, p):
560 p.tun_sa_out.remove_vpp_config()
561 p.tun_sa_in.remove_vpp_config()
562
563
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200564class TemplateIpsec6TunIfEsp(TemplateIpsec6TunProtect, TemplateIpsec):
565 """IPsec tunnel interface tests"""
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400566
567 encryption_type = ESP
568
569 def setUp(self):
570 super(TemplateIpsec6TunIfEsp, self).setUp()
571
572 self.tun_if = self.pg0
573
574 p = self.ipv6_params
Neale Rannsa9e27742020-12-23 16:22:28 +0000575 self.config_network(p)
576 self.config_sa_tra(p)
577 self.config_protect(p)
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400578
579 def tearDown(self):
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400580 super(TemplateIpsec6TunIfEsp, self).tearDown()
581
582
Matthew Smith6f1eb482022-08-09 22:19:38 +0000583class TemplateIpsec6TunIfEspUdp(TemplateIpsec6TunProtect, TemplateIpsec):
584 """IPsec6 UDP tunnel interface tests"""
585
586 tun4_encrypt_node_name = "esp6-encrypt-tun"
587 tun4_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
588 encryption_type = ESP
589
590 @classmethod
591 def setUpClass(cls):
592 super(TemplateIpsec6TunIfEspUdp, cls).setUpClass()
593
594 @classmethod
595 def tearDownClass(cls):
596 super(TemplateIpsec6TunIfEspUdp, cls).tearDownClass()
597
598 def verify_encrypted(self, p, sa, rxs):
599 for rx in rxs:
600 try:
601 # ensure the UDP ports are correct before we decrypt
602 # which strips them
603 self.assertTrue(rx.haslayer(UDP))
604 self.assert_equal(rx[UDP].sport, p.nat_header.sport)
Arthur de Kerhor4117b242022-08-31 19:13:03 +0200605 self.assert_equal(rx[UDP].dport, p.nat_header.dport)
Matthew Smith6f1eb482022-08-09 22:19:38 +0000606
607 pkt = sa.decrypt(rx[IP])
608 if not pkt.haslayer(IP):
609 pkt = IP(pkt[Raw].load)
610
611 self.assert_packet_checksums_valid(pkt)
612 self.assert_equal(
613 pkt[IP].dst, "1111:1111:1111:1111:1111:1111:1111:1111"
614 )
615 self.assert_equal(pkt[IP].src, self.pg1.remote_ip6)
616 except (IndexError, AssertionError):
617 self.logger.debug(ppp("Unexpected packet:", rx))
618 try:
619 self.logger.debug(ppp("Decrypted packet:", pkt))
620 except:
621 pass
622 raise
623
624 def config_sa_tra(self, p):
625 config_tun_params(p, self.encryption_type, p.tun_if)
626
627 p.tun_sa_out = VppIpsecSA(
628 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100629 p.vpp_tun_sa_id,
630 p.vpp_tun_spi,
Matthew Smith6f1eb482022-08-09 22:19:38 +0000631 p.auth_algo_vpp_id,
632 p.auth_key,
633 p.crypt_algo_vpp_id,
634 p.crypt_key,
635 self.vpp_esp_protocol,
636 flags=p.flags,
637 udp_src=p.nat_header.sport,
638 udp_dst=p.nat_header.dport,
639 )
640 p.tun_sa_out.add_vpp_config()
641
642 p.tun_sa_in = VppIpsecSA(
643 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +0100644 p.scapy_tun_sa_id,
645 p.scapy_tun_spi,
Matthew Smith6f1eb482022-08-09 22:19:38 +0000646 p.auth_algo_vpp_id,
647 p.auth_key,
648 p.crypt_algo_vpp_id,
649 p.crypt_key,
650 self.vpp_esp_protocol,
Arthur de Kerhor4117b242022-08-31 19:13:03 +0200651 flags=p.flags
652 | VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_IS_INBOUND,
Matthew Smith6f1eb482022-08-09 22:19:38 +0000653 udp_src=p.nat_header.sport,
654 udp_dst=p.nat_header.dport,
655 )
656 p.tun_sa_in.add_vpp_config()
657
658 def setUp(self):
659 super(TemplateIpsec6TunIfEspUdp, self).setUp()
660
661 p = self.ipv6_params
662 p.flags = VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_UDP_ENCAP
663 p.nat_header = UDP(sport=5454, dport=4500)
664
665 self.tun_if = self.pg0
666
667 self.config_network(p)
668 self.config_sa_tra(p)
669 self.config_protect(p)
670
671 def tearDown(self):
672 super(TemplateIpsec6TunIfEspUdp, self).tearDown()
673
674
675class TestIpsec6TunIfEspUdp(TemplateIpsec6TunIfEspUdp, IpsecTun6Tests):
676 """Ipsec ESP 6 UDP tests"""
677
678 tun6_input_node = "ipsec6-tun-input"
679 tun6_encrypt_node_name = "esp6-encrypt-tun"
680 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
681
682 def setUp(self):
683 super(TestIpsec6TunIfEspUdp, self).setUp()
684
685 def test_keepalive(self):
686 """IPSEC6 NAT Keepalive"""
687 self.verify_keepalive(self.ipv6_params)
688
689
690class TestIpsec6TunIfEspUdpGCM(TemplateIpsec6TunIfEspUdp, IpsecTun6Tests):
691 """Ipsec ESP 6 UDP GCM tests"""
692
693 tun6_input_node = "ipsec6-tun-input"
694 tun6_encrypt_node_name = "esp6-encrypt-tun"
695 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
696
697 def setUp(self):
698 super(TestIpsec6TunIfEspUdpGCM, self).setUp()
699 p = self.ipv6_params
700 p.auth_algo_vpp_id = VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE
701 p.crypt_algo_vpp_id = (
702 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_GCM_256
703 )
704 p.crypt_algo = "AES-GCM"
705 p.auth_algo = "NULL"
706 p.crypt_key = b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h"
707 p.salt = 0
708
709
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200710class TestIpsec6TunIfEsp1(TemplateIpsec6TunIfEsp, IpsecTun6Tests):
711 """Ipsec ESP - TUN tests"""
712
Klement Sekera6aa58b72019-05-16 14:34:55 +0200713 tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +0000714 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400715
Neale Ranns987aea82019-03-27 13:40:35 +0000716 def test_tun_basic46(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200717 """ipsec 4o6 tunnel basic test"""
Neale Ranns12989b52019-09-26 16:20:19 +0000718 self.tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns987aea82019-03-27 13:40:35 +0000719 self.verify_tun_46(self.params[socket.AF_INET6], count=1)
720
721 def test_tun_burst46(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200722 """ipsec 4o6 tunnel burst test"""
Neale Ranns12989b52019-09-26 16:20:19 +0000723 self.tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns987aea82019-03-27 13:40:35 +0000724 self.verify_tun_46(self.params[socket.AF_INET6], count=257)
725
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400726
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200727class TestIpsec6TunIfEspHandoff(TemplateIpsec6TunIfEsp, IpsecTun6HandoffTests):
728 """Ipsec ESP 6 Handoff tests"""
729
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000730 tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +0000731 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000732
Brian Russell7a29a2d2021-02-22 18:42:24 +0000733 def test_tun_handoff_66_police(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200734 """ESP 6o6 tunnel with policer worker hand-off test"""
Brian Russell7a29a2d2021-02-22 18:42:24 +0000735 self.vapi.cli("clear errors")
736 self.vapi.cli("clear ipsec sa")
737
738 N_PKTS = 15
739 p = self.params[socket.AF_INET6]
740
741 action_tx = PolicerAction(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200742 VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0
743 )
744 policer = VppPolicer(
745 self,
746 "pol1",
747 80,
748 0,
749 1000,
750 0,
751 conform_action=action_tx,
752 exceed_action=action_tx,
753 violate_action=action_tx,
754 )
Brian Russell7a29a2d2021-02-22 18:42:24 +0000755 policer.add_vpp_config()
756
757 # Start policing on tun
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +0200758 policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, True)
Brian Russell7a29a2d2021-02-22 18:42:24 +0000759
760 for pol_bind in [1, 0]:
761 policer.bind_vpp_config(pol_bind, True)
762
763 # inject alternately on worker 0 and 1.
764 for worker in [0, 1, 0, 1]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200765 send_pkts = self.gen_encrypt_pkts6(
766 p,
767 p.scapy_tun_sa,
768 self.tun_if,
769 src=p.remote_tun_if_host,
770 dst=self.pg1.remote_ip6,
771 count=N_PKTS,
772 )
773 recv_pkts = self.send_and_expect(
774 self.tun_if, send_pkts, self.pg1, worker=worker
775 )
Brian Russell7a29a2d2021-02-22 18:42:24 +0000776 self.verify_decrypted6(p, recv_pkts)
777 self.logger.debug(self.vapi.cli("show trace max 100"))
778
779 stats = policer.get_stats()
780 stats0 = policer.get_stats(worker=0)
781 stats1 = policer.get_stats(worker=1)
782
Ole Troan4376ab22021-03-03 10:40:05 +0100783 if pol_bind == 1:
Brian Russell7a29a2d2021-02-22 18:42:24 +0000784 # First pass: Worker 1, should have done all the policing
785 self.assertEqual(stats, stats1)
786
787 # Worker 0, should have handed everything off
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200788 self.assertEqual(stats0["conform_packets"], 0)
789 self.assertEqual(stats0["exceed_packets"], 0)
790 self.assertEqual(stats0["violate_packets"], 0)
Brian Russell7a29a2d2021-02-22 18:42:24 +0000791 else:
792 # Second pass: both workers should have policed equal amounts
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200793 self.assertGreater(stats1["conform_packets"], 0)
794 self.assertEqual(stats1["exceed_packets"], 0)
795 self.assertGreater(stats1["violate_packets"], 0)
Brian Russell7a29a2d2021-02-22 18:42:24 +0000796
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200797 self.assertGreater(stats0["conform_packets"], 0)
798 self.assertEqual(stats0["exceed_packets"], 0)
799 self.assertGreater(stats0["violate_packets"], 0)
Brian Russell7a29a2d2021-02-22 18:42:24 +0000800
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200801 self.assertEqual(
802 stats0["conform_packets"] + stats0["violate_packets"],
803 stats1["conform_packets"] + stats1["violate_packets"],
804 )
Brian Russell7a29a2d2021-02-22 18:42:24 +0000805
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +0200806 policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, False)
Brian Russell7a29a2d2021-02-22 18:42:24 +0000807 policer.remove_vpp_config()
808
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000809
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200810class TestIpsec4TunIfEspHandoff(TemplateIpsec4TunIfEsp, IpsecTun4HandoffTests):
811 """Ipsec ESP 4 Handoff tests"""
812
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000813 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +0000814 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000815
Brian Russell7a29a2d2021-02-22 18:42:24 +0000816 def test_tun_handoff_44_police(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200817 """ESP 4o4 tunnel with policer worker hand-off test"""
Brian Russell7a29a2d2021-02-22 18:42:24 +0000818 self.vapi.cli("clear errors")
819 self.vapi.cli("clear ipsec sa")
820
821 N_PKTS = 15
822 p = self.params[socket.AF_INET]
823
824 action_tx = PolicerAction(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200825 VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0
826 )
827 policer = VppPolicer(
828 self,
829 "pol1",
830 80,
831 0,
832 1000,
833 0,
834 conform_action=action_tx,
835 exceed_action=action_tx,
836 violate_action=action_tx,
837 )
Brian Russell7a29a2d2021-02-22 18:42:24 +0000838 policer.add_vpp_config()
839
840 # Start policing on tun
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +0200841 policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, True)
Brian Russell7a29a2d2021-02-22 18:42:24 +0000842
843 for pol_bind in [1, 0]:
844 policer.bind_vpp_config(pol_bind, True)
845
846 # inject alternately on worker 0 and 1.
847 for worker in [0, 1, 0, 1]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200848 send_pkts = self.gen_encrypt_pkts(
849 p,
850 p.scapy_tun_sa,
851 self.tun_if,
852 src=p.remote_tun_if_host,
853 dst=self.pg1.remote_ip4,
854 count=N_PKTS,
855 )
856 recv_pkts = self.send_and_expect(
857 self.tun_if, send_pkts, self.pg1, worker=worker
858 )
Brian Russell7a29a2d2021-02-22 18:42:24 +0000859 self.verify_decrypted(p, recv_pkts)
860 self.logger.debug(self.vapi.cli("show trace max 100"))
861
862 stats = policer.get_stats()
863 stats0 = policer.get_stats(worker=0)
864 stats1 = policer.get_stats(worker=1)
865
Ole Troan4376ab22021-03-03 10:40:05 +0100866 if pol_bind == 1:
Brian Russell7a29a2d2021-02-22 18:42:24 +0000867 # First pass: Worker 1, should have done all the policing
868 self.assertEqual(stats, stats1)
869
870 # Worker 0, should have handed everything off
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200871 self.assertEqual(stats0["conform_packets"], 0)
872 self.assertEqual(stats0["exceed_packets"], 0)
873 self.assertEqual(stats0["violate_packets"], 0)
Brian Russell7a29a2d2021-02-22 18:42:24 +0000874 else:
875 # Second pass: both workers should have policed equal amounts
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200876 self.assertGreater(stats1["conform_packets"], 0)
877 self.assertEqual(stats1["exceed_packets"], 0)
878 self.assertGreater(stats1["violate_packets"], 0)
Brian Russell7a29a2d2021-02-22 18:42:24 +0000879
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200880 self.assertGreater(stats0["conform_packets"], 0)
881 self.assertEqual(stats0["exceed_packets"], 0)
882 self.assertGreater(stats0["violate_packets"], 0)
Brian Russell7a29a2d2021-02-22 18:42:24 +0000883
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200884 self.assertEqual(
885 stats0["conform_packets"] + stats0["violate_packets"],
886 stats1["conform_packets"] + stats1["violate_packets"],
887 )
Brian Russell7a29a2d2021-02-22 18:42:24 +0000888
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +0200889 policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, False)
Brian Russell7a29a2d2021-02-22 18:42:24 +0000890 policer.remove_vpp_config()
891
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000892
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +0000893@tag_fixme_vpp_workers
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200894class TestIpsec4MultiTunIfEsp(TemplateIpsec4TunProtect, TemplateIpsec, IpsecTun4):
895 """IPsec IPv4 Multi Tunnel interface"""
Neale Ranns2ac885c2019-03-20 18:24:43 +0000896
897 encryption_type = ESP
Klement Sekera6aa58b72019-05-16 14:34:55 +0200898 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +0000899 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Ranns2ac885c2019-03-20 18:24:43 +0000900
901 def setUp(self):
902 super(TestIpsec4MultiTunIfEsp, self).setUp()
903
904 self.tun_if = self.pg0
905
906 self.multi_params = []
Neale Ranns12989b52019-09-26 16:20:19 +0000907 self.pg0.generate_remote_hosts(10)
908 self.pg0.configure_ipv4_neighbors()
Neale Ranns2ac885c2019-03-20 18:24:43 +0000909
910 for ii in range(10):
911 p = copy.copy(self.ipv4_params)
912
913 p.remote_tun_if_host = "1.1.1.%d" % (ii + 1)
914 p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii
915 p.scapy_tun_spi = p.scapy_tun_spi + ii
916 p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii
917 p.vpp_tun_spi = p.vpp_tun_spi + ii
918
919 p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii
920 p.scapy_tra_spi = p.scapy_tra_spi + ii
921 p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii
922 p.vpp_tra_spi = p.vpp_tra_spi + ii
Neale Ranns02950402019-12-20 00:54:57 +0000923 p.tun_dst = self.pg0.remote_hosts[ii].ip4
Neale Ranns2ac885c2019-03-20 18:24:43 +0000924
Neale Ranns12989b52019-09-26 16:20:19 +0000925 self.multi_params.append(p)
Neale Rannsa9e27742020-12-23 16:22:28 +0000926 self.config_network(p)
927 self.config_sa_tra(p)
928 self.config_protect(p)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000929
930 def tearDown(self):
Neale Ranns2ac885c2019-03-20 18:24:43 +0000931 super(TestIpsec4MultiTunIfEsp, self).tearDown()
932
933 def test_tun_44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200934 """Multiple IPSEC tunnel interfaces"""
Neale Ranns2ac885c2019-03-20 18:24:43 +0000935 for p in self.multi_params:
936 self.verify_tun_44(p, count=127)
Ole Troane66443c2021-03-18 11:12:01 +0100937 self.assertEqual(p.tun_if.get_rx_stats(), 127)
938 self.assertEqual(p.tun_if.get_tx_stats(), 127)
Neale Ranns2ac885c2019-03-20 18:24:43 +0000939
Neale Ranns02950402019-12-20 00:54:57 +0000940 def test_tun_rr_44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200941 """Round-robin packets acrros multiple interface"""
Neale Ranns02950402019-12-20 00:54:57 +0000942 tx = []
943 for p in self.multi_params:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200944 tx = tx + self.gen_encrypt_pkts(
945 p,
946 p.scapy_tun_sa,
947 self.tun_if,
948 src=p.remote_tun_if_host,
949 dst=self.pg1.remote_ip4,
950 )
Neale Ranns02950402019-12-20 00:54:57 +0000951 rxs = self.send_and_expect(self.tun_if, tx, self.pg1)
952
953 for rx, p in zip(rxs, self.multi_params):
954 self.verify_decrypted(p, [rx])
955
956 tx = []
957 for p in self.multi_params:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200958 tx = tx + self.gen_pkts(
959 self.pg1, src=self.pg1.remote_ip4, dst=p.remote_tun_if_host
960 )
Neale Ranns02950402019-12-20 00:54:57 +0000961 rxs = self.send_and_expect(self.pg1, tx, self.tun_if)
962
963 for rx, p in zip(rxs, self.multi_params):
964 self.verify_encrypted(p, p.vpp_tun_sa, [rx])
965
Neale Ranns2ac885c2019-03-20 18:24:43 +0000966
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200967class TestIpsec4TunIfEspAll(TemplateIpsec4TunProtect, TemplateIpsec, IpsecTun4):
968 """IPsec IPv4 Tunnel interface all Algos"""
Neale Ranns47feb112019-04-11 15:14:07 +0000969
970 encryption_type = ESP
Klement Sekera6aa58b72019-05-16 14:34:55 +0200971 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +0000972 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Ranns47feb112019-04-11 15:14:07 +0000973
Neale Ranns47feb112019-04-11 15:14:07 +0000974 def setUp(self):
975 super(TestIpsec4TunIfEspAll, self).setUp()
976
977 self.tun_if = self.pg0
Neale Rannsa9e27742020-12-23 16:22:28 +0000978 p = self.ipv4_params
979
980 self.config_network(p)
981 self.config_sa_tra(p)
982 self.config_protect(p)
Neale Ranns47feb112019-04-11 15:14:07 +0000983
984 def tearDown(self):
Neale Rannsa9e27742020-12-23 16:22:28 +0000985 p = self.ipv4_params
986 self.unconfig_protect(p)
987 self.unconfig_network(p)
988 self.unconfig_sa(p)
989
Neale Ranns47feb112019-04-11 15:14:07 +0000990 super(TestIpsec4TunIfEspAll, self).tearDown()
991
Neale Rannsd6c9e822019-04-17 16:29:00 -0700992 def rekey(self, p):
993 #
994 # change the key and the SPI
995 #
Neale Rannsa9e27742020-12-23 16:22:28 +0000996 np = copy.copy(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200997 p.crypt_key = b"X" + p.crypt_key[1:]
Neale Rannsd6c9e822019-04-17 16:29:00 -0700998 p.scapy_tun_spi += 1
999 p.scapy_tun_sa_id += 1
1000 p.vpp_tun_spi += 1
1001 p.vpp_tun_sa_id += 1
1002 p.tun_if.local_spi = p.vpp_tun_spi
1003 p.tun_if.remote_spi = p.scapy_tun_spi
1004
Neale Ranns12989b52019-09-26 16:20:19 +00001005 config_tun_params(p, self.encryption_type, p.tun_if)
Neale Rannsd6c9e822019-04-17 16:29:00 -07001006
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001007 p.tun_sa_out = VppIpsecSA(
1008 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001009 p.vpp_tun_sa_id,
1010 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001011 p.auth_algo_vpp_id,
1012 p.auth_key,
1013 p.crypt_algo_vpp_id,
1014 p.crypt_key,
1015 self.vpp_esp_protocol,
1016 flags=p.flags,
1017 salt=p.salt,
1018 )
1019 p.tun_sa_in = VppIpsecSA(
1020 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001021 p.scapy_tun_sa_id,
1022 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001023 p.auth_algo_vpp_id,
1024 p.auth_key,
1025 p.crypt_algo_vpp_id,
1026 p.crypt_key,
1027 self.vpp_esp_protocol,
1028 flags=p.flags,
1029 salt=p.salt,
1030 )
Neale Rannsd6c9e822019-04-17 16:29:00 -07001031 p.tun_sa_in.add_vpp_config()
1032 p.tun_sa_out.add_vpp_config()
1033
Neale Rannsa9e27742020-12-23 16:22:28 +00001034 self.config_protect(p)
1035 np.tun_sa_out.remove_vpp_config()
1036 np.tun_sa_in.remove_vpp_config()
Neale Rannsd6c9e822019-04-17 16:29:00 -07001037 self.logger.info(self.vapi.cli("sh ipsec sa"))
1038
Neale Ranns47feb112019-04-11 15:14:07 +00001039 def test_tun_44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001040 """IPSEC tunnel all algos"""
Neale Ranns47feb112019-04-11 15:14:07 +00001041
1042 # foreach VPP crypto engine
1043 engines = ["ia32", "ipsecmb", "openssl"]
1044
1045 # foreach crypto algorithm
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001046 algos = [
1047 {
1048 "vpp-crypto": (
1049 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_GCM_128
1050 ),
1051 "vpp-integ": (
1052 VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE
1053 ),
1054 "scapy-crypto": "AES-GCM",
1055 "scapy-integ": "NULL",
1056 "key": b"JPjyOWBeVEQiMe7h",
1057 "salt": 3333,
1058 },
1059 {
1060 "vpp-crypto": (
1061 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_GCM_192
1062 ),
1063 "vpp-integ": (
1064 VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE
1065 ),
1066 "scapy-crypto": "AES-GCM",
1067 "scapy-integ": "NULL",
1068 "key": b"JPjyOWBeVEQiMe7hJPjyOWBe",
1069 "salt": 0,
1070 },
1071 {
1072 "vpp-crypto": (
1073 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_GCM_256
1074 ),
1075 "vpp-integ": (
1076 VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE
1077 ),
1078 "scapy-crypto": "AES-GCM",
1079 "scapy-integ": "NULL",
1080 "key": b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h",
1081 "salt": 9999,
1082 },
1083 {
1084 "vpp-crypto": (
1085 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_CBC_128
1086 ),
1087 "vpp-integ": (
1088 VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA1_96
1089 ),
1090 "scapy-crypto": "AES-CBC",
1091 "scapy-integ": "HMAC-SHA1-96",
1092 "salt": 0,
1093 "key": b"JPjyOWBeVEQiMe7h",
1094 },
1095 {
1096 "vpp-crypto": (
1097 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_CBC_192
1098 ),
1099 "vpp-integ": (
1100 VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA_512_256
1101 ),
1102 "scapy-crypto": "AES-CBC",
1103 "scapy-integ": "SHA2-512-256",
1104 "salt": 0,
1105 "key": b"JPjyOWBeVEQiMe7hJPjyOWBe",
1106 },
1107 {
1108 "vpp-crypto": (
1109 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_CBC_256
1110 ),
1111 "vpp-integ": (
1112 VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA_256_128
1113 ),
1114 "scapy-crypto": "AES-CBC",
1115 "scapy-integ": "SHA2-256-128",
1116 "salt": 0,
1117 "key": b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h",
1118 },
1119 {
1120 "vpp-crypto": (
1121 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_NONE
1122 ),
1123 "vpp-integ": (
1124 VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA1_96
1125 ),
1126 "scapy-crypto": "NULL",
1127 "scapy-integ": "HMAC-SHA1-96",
1128 "salt": 0,
1129 "key": b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h",
1130 },
1131 ]
Neale Ranns47feb112019-04-11 15:14:07 +00001132
1133 for engine in engines:
1134 self.vapi.cli("set crypto handler all %s" % engine)
1135
1136 #
1137 # loop through each of the algorithms
1138 #
1139 for algo in algos:
1140 # with self.subTest(algo=algo['scapy']):
1141
Neale Rannsa9e27742020-12-23 16:22:28 +00001142 p = self.ipv4_params
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001143 p.auth_algo_vpp_id = algo["vpp-integ"]
1144 p.crypt_algo_vpp_id = algo["vpp-crypto"]
1145 p.crypt_algo = algo["scapy-crypto"]
1146 p.auth_algo = algo["scapy-integ"]
1147 p.crypt_key = algo["key"]
1148 p.salt = algo["salt"]
Neale Ranns47feb112019-04-11 15:14:07 +00001149
Neale Rannsd6c9e822019-04-17 16:29:00 -07001150 #
1151 # rekey the tunnel
1152 #
1153 self.rekey(p)
1154 self.verify_tun_44(p, count=127)
1155
Neale Ranns47feb112019-04-11 15:14:07 +00001156
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001157class TestIpsec4TunIfEspNoAlgo(TemplateIpsec4TunProtect, TemplateIpsec, IpsecTun4):
1158 """IPsec IPv4 Tunnel interface no Algos"""
Neale Ranns02950402019-12-20 00:54:57 +00001159
1160 encryption_type = ESP
1161 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00001162 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Ranns02950402019-12-20 00:54:57 +00001163
Neale Rannsa9e27742020-12-23 16:22:28 +00001164 def setUp(self):
1165 super(TestIpsec4TunIfEspNoAlgo, self).setUp()
Neale Ranns02950402019-12-20 00:54:57 +00001166
Neale Rannsa9e27742020-12-23 16:22:28 +00001167 self.tun_if = self.pg0
1168 p = self.ipv4_params
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001169 p.auth_algo_vpp_id = VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE
1170 p.auth_algo = "NULL"
Neale Ranns02950402019-12-20 00:54:57 +00001171 p.auth_key = []
1172
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001173 p.crypt_algo_vpp_id = (
1174 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_NONE
1175 )
1176 p.crypt_algo = "NULL"
Neale Ranns02950402019-12-20 00:54:57 +00001177 p.crypt_key = []
1178
Neale Ranns02950402019-12-20 00:54:57 +00001179 def tearDown(self):
1180 super(TestIpsec4TunIfEspNoAlgo, self).tearDown()
1181
1182 def test_tun_44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001183 """IPSec SA with NULL algos"""
Neale Ranns02950402019-12-20 00:54:57 +00001184 p = self.ipv4_params
1185
1186 self.config_network(p)
Neale Rannsa9e27742020-12-23 16:22:28 +00001187 self.config_sa_tra(p)
1188 self.config_protect(p)
Neale Ranns02950402019-12-20 00:54:57 +00001189
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001190 tx = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, dst=p.remote_tun_if_host)
Neale Ranns02950402019-12-20 00:54:57 +00001191 self.send_and_assert_no_replies(self.pg1, tx)
1192
Neale Rannsa9e27742020-12-23 16:22:28 +00001193 self.unconfig_protect(p)
1194 self.unconfig_sa(p)
Neale Ranns02950402019-12-20 00:54:57 +00001195 self.unconfig_network(p)
1196
1197
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00001198@tag_fixme_vpp_workers
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001199class TestIpsec6MultiTunIfEsp(TemplateIpsec6TunProtect, TemplateIpsec, IpsecTun6):
1200 """IPsec IPv6 Multi Tunnel interface"""
Neale Ranns2ac885c2019-03-20 18:24:43 +00001201
1202 encryption_type = ESP
Klement Sekera6aa58b72019-05-16 14:34:55 +02001203 tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00001204 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
Neale Ranns2ac885c2019-03-20 18:24:43 +00001205
1206 def setUp(self):
1207 super(TestIpsec6MultiTunIfEsp, self).setUp()
1208
1209 self.tun_if = self.pg0
1210
1211 self.multi_params = []
Neale Ranns12989b52019-09-26 16:20:19 +00001212 self.pg0.generate_remote_hosts(10)
1213 self.pg0.configure_ipv6_neighbors()
Neale Ranns2ac885c2019-03-20 18:24:43 +00001214
1215 for ii in range(10):
1216 p = copy.copy(self.ipv6_params)
1217
1218 p.remote_tun_if_host = "1111::%d" % (ii + 1)
1219 p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii
1220 p.scapy_tun_spi = p.scapy_tun_spi + ii
1221 p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii
1222 p.vpp_tun_spi = p.vpp_tun_spi + ii
1223
1224 p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii
1225 p.scapy_tra_spi = p.scapy_tra_spi + ii
1226 p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii
1227 p.vpp_tra_spi = p.vpp_tra_spi + ii
Neale Rannsa9e27742020-12-23 16:22:28 +00001228 p.tun_dst = self.pg0.remote_hosts[ii].ip6
Neale Ranns2ac885c2019-03-20 18:24:43 +00001229
Neale Ranns12989b52019-09-26 16:20:19 +00001230 self.multi_params.append(p)
Neale Rannsa9e27742020-12-23 16:22:28 +00001231 self.config_network(p)
1232 self.config_sa_tra(p)
1233 self.config_protect(p)
Neale Ranns2ac885c2019-03-20 18:24:43 +00001234
1235 def tearDown(self):
Neale Ranns2ac885c2019-03-20 18:24:43 +00001236 super(TestIpsec6MultiTunIfEsp, self).tearDown()
1237
1238 def test_tun_66(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001239 """Multiple IPSEC tunnel interfaces"""
Neale Ranns2ac885c2019-03-20 18:24:43 +00001240 for p in self.multi_params:
1241 self.verify_tun_66(p, count=127)
Ole Troane66443c2021-03-18 11:12:01 +01001242 self.assertEqual(p.tun_if.get_rx_stats(), 127)
1243 self.assertEqual(p.tun_if.get_tx_stats(), 127)
Neale Ranns2ac885c2019-03-20 18:24:43 +00001244
1245
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001246class TestIpsecGreTebIfEsp(TemplateIpsec, IpsecTun4Tests):
1247 """Ipsec GRE TEB ESP - TUN tests"""
1248
Neale Rannsc87b66c2019-02-07 07:26:12 -08001249 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00001250 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Rannsf05e7322019-03-29 20:23:58 +00001251 encryption_type = ESP
1252 omac = "00:11:22:33:44:55"
1253
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001254 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
1255 return [
1256 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1257 / sa.encrypt(
1258 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
1259 / GRE()
1260 / Ether(dst=self.omac)
1261 / IP(src="1.1.1.1", dst="1.1.1.2")
1262 / UDP(sport=1144, dport=2233)
1263 / Raw(b"X" * payload_size)
1264 )
1265 for i in range(count)
1266 ]
Neale Rannsf05e7322019-03-29 20:23:58 +00001267
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001268 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100):
1269 return [
1270 Ether(dst=self.omac)
1271 / IP(src="1.1.1.1", dst="1.1.1.2")
1272 / UDP(sport=1144, dport=2233)
1273 / Raw(b"X" * payload_size)
1274 for i in range(count)
1275 ]
Neale Rannsf05e7322019-03-29 20:23:58 +00001276
1277 def verify_decrypted(self, p, rxs):
1278 for rx in rxs:
1279 self.assert_equal(rx[Ether].dst, self.omac)
1280 self.assert_equal(rx[IP].dst, "1.1.1.2")
1281
1282 def verify_encrypted(self, p, sa, rxs):
1283 for rx in rxs:
1284 try:
1285 pkt = sa.decrypt(rx[IP])
1286 if not pkt.haslayer(IP):
1287 pkt = IP(pkt[Raw].load)
1288 self.assert_packet_checksums_valid(pkt)
1289 self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4)
1290 self.assert_equal(pkt[IP].src, self.pg0.local_ip4)
1291 self.assertTrue(pkt.haslayer(GRE))
1292 e = pkt[Ether]
1293 self.assertEqual(e[Ether].dst, self.omac)
1294 self.assertEqual(e[IP].dst, "1.1.1.2")
1295 except (IndexError, AssertionError):
1296 self.logger.debug(ppp("Unexpected packet:", rx))
1297 try:
1298 self.logger.debug(ppp("Decrypted packet:", pkt))
1299 except:
1300 pass
1301 raise
1302
1303 def setUp(self):
Neale Rannsc87b66c2019-02-07 07:26:12 -08001304 super(TestIpsecGreTebIfEsp, self).setUp()
Neale Rannsf05e7322019-03-29 20:23:58 +00001305
1306 self.tun_if = self.pg0
1307
1308 p = self.ipv4_params
1309
1310 bd1 = VppBridgeDomain(self, 1)
1311 bd1.add_vpp_config()
1312
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001313 p.tun_sa_out = VppIpsecSA(
1314 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001315 p.vpp_tun_sa_id,
1316 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001317 p.auth_algo_vpp_id,
1318 p.auth_key,
1319 p.crypt_algo_vpp_id,
1320 p.crypt_key,
1321 self.vpp_esp_protocol,
1322 self.pg0.local_ip4,
1323 self.pg0.remote_ip4,
1324 )
Neale Rannsf05e7322019-03-29 20:23:58 +00001325 p.tun_sa_out.add_vpp_config()
1326
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001327 p.tun_sa_in = VppIpsecSA(
1328 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001329 p.scapy_tun_sa_id,
1330 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001331 p.auth_algo_vpp_id,
1332 p.auth_key,
1333 p.crypt_algo_vpp_id,
1334 p.crypt_key,
1335 self.vpp_esp_protocol,
1336 self.pg0.remote_ip4,
1337 self.pg0.local_ip4,
1338 )
Neale Rannsf05e7322019-03-29 20:23:58 +00001339 p.tun_sa_in.add_vpp_config()
1340
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001341 p.tun_if = VppGreInterface(
1342 self,
1343 self.pg0.local_ip4,
1344 self.pg0.remote_ip4,
1345 type=(VppEnum.vl_api_gre_tunnel_type_t.GRE_API_TUNNEL_TYPE_TEB),
1346 )
Neale Ranns12989b52019-09-26 16:20:19 +00001347 p.tun_if.add_vpp_config()
Neale Rannsc87b66c2019-02-07 07:26:12 -08001348
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001349 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
Neale Rannsc87b66c2019-02-07 07:26:12 -08001350
1351 p.tun_protect.add_vpp_config()
1352
Neale Ranns12989b52019-09-26 16:20:19 +00001353 p.tun_if.admin_up()
1354 p.tun_if.config_ip4()
1355 config_tun_params(p, self.encryption_type, p.tun_if)
Neale Rannsf05e7322019-03-29 20:23:58 +00001356
Neale Ranns12989b52019-09-26 16:20:19 +00001357 VppBridgeDomainPort(self, bd1, p.tun_if).add_vpp_config()
Neale Rannsf05e7322019-03-29 20:23:58 +00001358 VppBridgeDomainPort(self, bd1, self.pg1).add_vpp_config()
1359
Neale Rannsc87b66c2019-02-07 07:26:12 -08001360 self.vapi.cli("clear ipsec sa")
Neale Ranns28287212019-12-16 00:53:11 +00001361 self.vapi.cli("sh adj")
1362 self.vapi.cli("sh ipsec tun")
Neale Rannsc87b66c2019-02-07 07:26:12 -08001363
Neale Rannsf05e7322019-03-29 20:23:58 +00001364 def tearDown(self):
Neale Ranns12989b52019-09-26 16:20:19 +00001365 p = self.ipv4_params
1366 p.tun_if.unconfig_ip4()
Neale Rannsc87b66c2019-02-07 07:26:12 -08001367 super(TestIpsecGreTebIfEsp, self).tearDown()
Neale Rannsf05e7322019-03-29 20:23:58 +00001368
1369
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001370class TestIpsecGreTebVlanIfEsp(TemplateIpsec, IpsecTun4Tests):
1371 """Ipsec GRE TEB ESP - TUN tests"""
1372
John Lo90430b62020-01-31 23:48:30 -05001373 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00001374 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
John Lo90430b62020-01-31 23:48:30 -05001375 encryption_type = ESP
1376 omac = "00:11:22:33:44:55"
1377
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001378 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
1379 return [
1380 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1381 / sa.encrypt(
1382 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
1383 / GRE()
1384 / Ether(dst=self.omac)
1385 / IP(src="1.1.1.1", dst="1.1.1.2")
1386 / UDP(sport=1144, dport=2233)
1387 / Raw(b"X" * payload_size)
1388 )
1389 for i in range(count)
1390 ]
John Lo90430b62020-01-31 23:48:30 -05001391
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001392 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100):
1393 return [
1394 Ether(dst=self.omac)
1395 / Dot1Q(vlan=11)
1396 / IP(src="1.1.1.1", dst="1.1.1.2")
1397 / UDP(sport=1144, dport=2233)
1398 / Raw(b"X" * payload_size)
1399 for i in range(count)
1400 ]
John Lo90430b62020-01-31 23:48:30 -05001401
1402 def verify_decrypted(self, p, rxs):
1403 for rx in rxs:
1404 self.assert_equal(rx[Ether].dst, self.omac)
1405 self.assert_equal(rx[Dot1Q].vlan, 11)
1406 self.assert_equal(rx[IP].dst, "1.1.1.2")
1407
1408 def verify_encrypted(self, p, sa, rxs):
1409 for rx in rxs:
1410 try:
1411 pkt = sa.decrypt(rx[IP])
1412 if not pkt.haslayer(IP):
1413 pkt = IP(pkt[Raw].load)
1414 self.assert_packet_checksums_valid(pkt)
1415 self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4)
1416 self.assert_equal(pkt[IP].src, self.pg0.local_ip4)
1417 self.assertTrue(pkt.haslayer(GRE))
1418 e = pkt[Ether]
1419 self.assertEqual(e[Ether].dst, self.omac)
1420 self.assertFalse(e.haslayer(Dot1Q))
1421 self.assertEqual(e[IP].dst, "1.1.1.2")
1422 except (IndexError, AssertionError):
1423 self.logger.debug(ppp("Unexpected packet:", rx))
1424 try:
1425 self.logger.debug(ppp("Decrypted packet:", pkt))
1426 except:
1427 pass
1428 raise
1429
1430 def setUp(self):
1431 super(TestIpsecGreTebVlanIfEsp, self).setUp()
1432
1433 self.tun_if = self.pg0
1434
1435 p = self.ipv4_params
1436
1437 bd1 = VppBridgeDomain(self, 1)
1438 bd1.add_vpp_config()
1439
1440 self.pg1_11 = VppDot1QSubint(self, self.pg1, 11)
1441 self.vapi.l2_interface_vlan_tag_rewrite(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001442 sw_if_index=self.pg1_11.sw_if_index,
1443 vtr_op=L2_VTR_OP.L2_POP_1,
1444 push_dot1q=11,
1445 )
John Lo90430b62020-01-31 23:48:30 -05001446 self.pg1_11.admin_up()
1447
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001448 p.tun_sa_out = VppIpsecSA(
1449 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001450 p.vpp_tun_sa_id,
1451 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001452 p.auth_algo_vpp_id,
1453 p.auth_key,
1454 p.crypt_algo_vpp_id,
1455 p.crypt_key,
1456 self.vpp_esp_protocol,
1457 self.pg0.local_ip4,
1458 self.pg0.remote_ip4,
1459 )
John Lo90430b62020-01-31 23:48:30 -05001460 p.tun_sa_out.add_vpp_config()
1461
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001462 p.tun_sa_in = VppIpsecSA(
1463 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001464 p.scapy_tun_sa_id,
1465 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001466 p.auth_algo_vpp_id,
1467 p.auth_key,
1468 p.crypt_algo_vpp_id,
1469 p.crypt_key,
1470 self.vpp_esp_protocol,
1471 self.pg0.remote_ip4,
1472 self.pg0.local_ip4,
1473 )
John Lo90430b62020-01-31 23:48:30 -05001474 p.tun_sa_in.add_vpp_config()
1475
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001476 p.tun_if = VppGreInterface(
1477 self,
1478 self.pg0.local_ip4,
1479 self.pg0.remote_ip4,
1480 type=(VppEnum.vl_api_gre_tunnel_type_t.GRE_API_TUNNEL_TYPE_TEB),
1481 )
John Lo90430b62020-01-31 23:48:30 -05001482 p.tun_if.add_vpp_config()
1483
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001484 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
John Lo90430b62020-01-31 23:48:30 -05001485
1486 p.tun_protect.add_vpp_config()
1487
1488 p.tun_if.admin_up()
1489 p.tun_if.config_ip4()
1490 config_tun_params(p, self.encryption_type, p.tun_if)
1491
1492 VppBridgeDomainPort(self, bd1, p.tun_if).add_vpp_config()
1493 VppBridgeDomainPort(self, bd1, self.pg1_11).add_vpp_config()
1494
1495 self.vapi.cli("clear ipsec sa")
1496
1497 def tearDown(self):
1498 p = self.ipv4_params
1499 p.tun_if.unconfig_ip4()
1500 super(TestIpsecGreTebVlanIfEsp, self).tearDown()
1501 self.pg1_11.admin_down()
1502 self.pg1_11.remove_vpp_config()
1503
1504
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001505class TestIpsecGreTebIfEspTra(TemplateIpsec, IpsecTun4Tests):
1506 """Ipsec GRE TEB ESP - Tra tests"""
1507
Neale Ranns568acbb2019-12-18 05:54:40 +00001508 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00001509 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Ranns568acbb2019-12-18 05:54:40 +00001510 encryption_type = ESP
1511 omac = "00:11:22:33:44:55"
1512
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001513 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
1514 return [
1515 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1516 / sa.encrypt(
1517 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
1518 / GRE()
1519 / Ether(dst=self.omac)
1520 / IP(src="1.1.1.1", dst="1.1.1.2")
1521 / UDP(sport=1144, dport=2233)
1522 / Raw(b"X" * payload_size)
1523 )
1524 for i in range(count)
1525 ]
Neale Ranns568acbb2019-12-18 05:54:40 +00001526
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001527 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100):
1528 return [
1529 Ether(dst=self.omac)
1530 / IP(src="1.1.1.1", dst="1.1.1.2")
1531 / UDP(sport=1144, dport=2233)
1532 / Raw(b"X" * payload_size)
1533 for i in range(count)
1534 ]
Neale Ranns568acbb2019-12-18 05:54:40 +00001535
1536 def verify_decrypted(self, p, rxs):
1537 for rx in rxs:
1538 self.assert_equal(rx[Ether].dst, self.omac)
1539 self.assert_equal(rx[IP].dst, "1.1.1.2")
1540
1541 def verify_encrypted(self, p, sa, rxs):
1542 for rx in rxs:
1543 try:
1544 pkt = sa.decrypt(rx[IP])
1545 if not pkt.haslayer(IP):
1546 pkt = IP(pkt[Raw].load)
1547 self.assert_packet_checksums_valid(pkt)
1548 self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4)
1549 self.assert_equal(pkt[IP].src, self.pg0.local_ip4)
1550 self.assertTrue(pkt.haslayer(GRE))
1551 e = pkt[Ether]
1552 self.assertEqual(e[Ether].dst, self.omac)
1553 self.assertEqual(e[IP].dst, "1.1.1.2")
1554 except (IndexError, AssertionError):
1555 self.logger.debug(ppp("Unexpected packet:", rx))
1556 try:
1557 self.logger.debug(ppp("Decrypted packet:", pkt))
1558 except:
1559 pass
1560 raise
1561
1562 def setUp(self):
1563 super(TestIpsecGreTebIfEspTra, self).setUp()
1564
1565 self.tun_if = self.pg0
1566
1567 p = self.ipv4_params
1568
1569 bd1 = VppBridgeDomain(self, 1)
1570 bd1.add_vpp_config()
1571
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001572 p.tun_sa_out = VppIpsecSA(
1573 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001574 p.vpp_tun_sa_id,
1575 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001576 p.auth_algo_vpp_id,
1577 p.auth_key,
1578 p.crypt_algo_vpp_id,
1579 p.crypt_key,
1580 self.vpp_esp_protocol,
1581 )
Neale Ranns568acbb2019-12-18 05:54:40 +00001582 p.tun_sa_out.add_vpp_config()
1583
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001584 p.tun_sa_in = VppIpsecSA(
1585 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001586 p.scapy_tun_sa_id,
1587 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001588 p.auth_algo_vpp_id,
1589 p.auth_key,
1590 p.crypt_algo_vpp_id,
1591 p.crypt_key,
1592 self.vpp_esp_protocol,
1593 )
Neale Ranns568acbb2019-12-18 05:54:40 +00001594 p.tun_sa_in.add_vpp_config()
1595
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001596 p.tun_if = VppGreInterface(
1597 self,
1598 self.pg0.local_ip4,
1599 self.pg0.remote_ip4,
1600 type=(VppEnum.vl_api_gre_tunnel_type_t.GRE_API_TUNNEL_TYPE_TEB),
1601 )
Neale Ranns568acbb2019-12-18 05:54:40 +00001602 p.tun_if.add_vpp_config()
1603
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001604 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
Neale Ranns568acbb2019-12-18 05:54:40 +00001605
1606 p.tun_protect.add_vpp_config()
1607
1608 p.tun_if.admin_up()
1609 p.tun_if.config_ip4()
1610 config_tra_params(p, self.encryption_type, p.tun_if)
1611
1612 VppBridgeDomainPort(self, bd1, p.tun_if).add_vpp_config()
1613 VppBridgeDomainPort(self, bd1, self.pg1).add_vpp_config()
1614
1615 self.vapi.cli("clear ipsec sa")
1616
1617 def tearDown(self):
1618 p = self.ipv4_params
1619 p.tun_if.unconfig_ip4()
1620 super(TestIpsecGreTebIfEspTra, self).tearDown()
1621
1622
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001623class TestIpsecGreTebUdpIfEspTra(TemplateIpsec, IpsecTun4Tests):
1624 """Ipsec GRE TEB UDP ESP - Tra tests"""
1625
Neale Rannsabc56602020-04-01 09:45:23 +00001626 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00001627 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Rannsabc56602020-04-01 09:45:23 +00001628 encryption_type = ESP
1629 omac = "00:11:22:33:44:55"
1630
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001631 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
1632 return [
1633 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1634 / sa.encrypt(
1635 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
1636 / GRE()
1637 / Ether(dst=self.omac)
1638 / IP(src="1.1.1.1", dst="1.1.1.2")
1639 / UDP(sport=1144, dport=2233)
1640 / Raw(b"X" * payload_size)
1641 )
1642 for i in range(count)
1643 ]
Neale Rannsabc56602020-04-01 09:45:23 +00001644
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001645 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100):
1646 return [
1647 Ether(dst=self.omac)
1648 / IP(src="1.1.1.1", dst="1.1.1.2")
1649 / UDP(sport=1144, dport=2233)
1650 / Raw(b"X" * payload_size)
1651 for i in range(count)
1652 ]
Neale Rannsabc56602020-04-01 09:45:23 +00001653
1654 def verify_decrypted(self, p, rxs):
1655 for rx in rxs:
1656 self.assert_equal(rx[Ether].dst, self.omac)
1657 self.assert_equal(rx[IP].dst, "1.1.1.2")
1658
1659 def verify_encrypted(self, p, sa, rxs):
1660 for rx in rxs:
1661 self.assertTrue(rx.haslayer(UDP))
1662 self.assertEqual(rx[UDP].dport, 4545)
1663 self.assertEqual(rx[UDP].sport, 5454)
1664 try:
1665 pkt = sa.decrypt(rx[IP])
1666 if not pkt.haslayer(IP):
1667 pkt = IP(pkt[Raw].load)
1668 self.assert_packet_checksums_valid(pkt)
1669 self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4)
1670 self.assert_equal(pkt[IP].src, self.pg0.local_ip4)
1671 self.assertTrue(pkt.haslayer(GRE))
1672 e = pkt[Ether]
1673 self.assertEqual(e[Ether].dst, self.omac)
1674 self.assertEqual(e[IP].dst, "1.1.1.2")
1675 except (IndexError, AssertionError):
1676 self.logger.debug(ppp("Unexpected packet:", rx))
1677 try:
1678 self.logger.debug(ppp("Decrypted packet:", pkt))
1679 except:
1680 pass
1681 raise
1682
1683 def setUp(self):
1684 super(TestIpsecGreTebUdpIfEspTra, self).setUp()
1685
1686 self.tun_if = self.pg0
1687
1688 p = self.ipv4_params
1689 p = self.ipv4_params
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001690 p.flags = VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_UDP_ENCAP
Neale Rannsabc56602020-04-01 09:45:23 +00001691 p.nat_header = UDP(sport=5454, dport=4545)
1692
1693 bd1 = VppBridgeDomain(self, 1)
1694 bd1.add_vpp_config()
1695
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001696 p.tun_sa_out = VppIpsecSA(
1697 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001698 p.vpp_tun_sa_id,
1699 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001700 p.auth_algo_vpp_id,
1701 p.auth_key,
1702 p.crypt_algo_vpp_id,
1703 p.crypt_key,
1704 self.vpp_esp_protocol,
1705 flags=p.flags,
1706 udp_src=5454,
1707 udp_dst=4545,
1708 )
Neale Rannsabc56602020-04-01 09:45:23 +00001709 p.tun_sa_out.add_vpp_config()
1710
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001711 p.tun_sa_in = VppIpsecSA(
1712 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001713 p.scapy_tun_sa_id,
1714 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001715 p.auth_algo_vpp_id,
1716 p.auth_key,
1717 p.crypt_algo_vpp_id,
1718 p.crypt_key,
1719 self.vpp_esp_protocol,
1720 flags=(
1721 p.flags | VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_IS_INBOUND
1722 ),
1723 udp_src=4545,
1724 udp_dst=5454,
1725 )
Neale Rannsabc56602020-04-01 09:45:23 +00001726 p.tun_sa_in.add_vpp_config()
1727
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001728 p.tun_if = VppGreInterface(
1729 self,
1730 self.pg0.local_ip4,
1731 self.pg0.remote_ip4,
1732 type=(VppEnum.vl_api_gre_tunnel_type_t.GRE_API_TUNNEL_TYPE_TEB),
1733 )
Neale Rannsabc56602020-04-01 09:45:23 +00001734 p.tun_if.add_vpp_config()
1735
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001736 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
Neale Rannsabc56602020-04-01 09:45:23 +00001737
1738 p.tun_protect.add_vpp_config()
1739
1740 p.tun_if.admin_up()
1741 p.tun_if.config_ip4()
1742 config_tra_params(p, self.encryption_type, p.tun_if)
1743
1744 VppBridgeDomainPort(self, bd1, p.tun_if).add_vpp_config()
1745 VppBridgeDomainPort(self, bd1, self.pg1).add_vpp_config()
1746
1747 self.vapi.cli("clear ipsec sa")
1748 self.logger.info(self.vapi.cli("sh ipsec sa 0"))
1749
1750 def tearDown(self):
1751 p = self.ipv4_params
1752 p.tun_if.unconfig_ip4()
1753 super(TestIpsecGreTebUdpIfEspTra, self).tearDown()
1754
1755
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001756class TestIpsecGreIfEsp(TemplateIpsec, IpsecTun4Tests):
1757 """Ipsec GRE ESP - TUN tests"""
1758
Neale Rannsc87b66c2019-02-07 07:26:12 -08001759 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00001760 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Rannsc87b66c2019-02-07 07:26:12 -08001761 encryption_type = ESP
1762
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001763 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
1764 return [
1765 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1766 / sa.encrypt(
1767 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
1768 / GRE()
1769 / IP(src=self.pg1.local_ip4, dst=self.pg1.remote_ip4)
1770 / UDP(sport=1144, dport=2233)
1771 / Raw(b"X" * payload_size)
1772 )
1773 for i in range(count)
1774 ]
Neale Rannsc87b66c2019-02-07 07:26:12 -08001775
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001776 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100):
1777 return [
1778 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1779 / IP(src="1.1.1.1", dst="1.1.1.2")
1780 / UDP(sport=1144, dport=2233)
1781 / Raw(b"X" * payload_size)
1782 for i in range(count)
1783 ]
Neale Rannsc87b66c2019-02-07 07:26:12 -08001784
1785 def verify_decrypted(self, p, rxs):
1786 for rx in rxs:
1787 self.assert_equal(rx[Ether].dst, self.pg1.remote_mac)
1788 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
1789
1790 def verify_encrypted(self, p, sa, rxs):
1791 for rx in rxs:
1792 try:
1793 pkt = sa.decrypt(rx[IP])
1794 if not pkt.haslayer(IP):
1795 pkt = IP(pkt[Raw].load)
1796 self.assert_packet_checksums_valid(pkt)
1797 self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4)
1798 self.assert_equal(pkt[IP].src, self.pg0.local_ip4)
1799 self.assertTrue(pkt.haslayer(GRE))
1800 e = pkt[GRE]
1801 self.assertEqual(e[IP].dst, "1.1.1.2")
1802 except (IndexError, AssertionError):
1803 self.logger.debug(ppp("Unexpected packet:", rx))
1804 try:
1805 self.logger.debug(ppp("Decrypted packet:", pkt))
1806 except:
1807 pass
1808 raise
1809
1810 def setUp(self):
1811 super(TestIpsecGreIfEsp, self).setUp()
1812
1813 self.tun_if = self.pg0
1814
1815 p = self.ipv4_params
1816
1817 bd1 = VppBridgeDomain(self, 1)
1818 bd1.add_vpp_config()
1819
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001820 p.tun_sa_out = VppIpsecSA(
1821 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001822 p.vpp_tun_sa_id,
1823 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001824 p.auth_algo_vpp_id,
1825 p.auth_key,
1826 p.crypt_algo_vpp_id,
1827 p.crypt_key,
1828 self.vpp_esp_protocol,
1829 self.pg0.local_ip4,
1830 self.pg0.remote_ip4,
1831 )
Neale Rannsc87b66c2019-02-07 07:26:12 -08001832 p.tun_sa_out.add_vpp_config()
1833
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001834 p.tun_sa_in = VppIpsecSA(
1835 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001836 p.scapy_tun_sa_id,
1837 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001838 p.auth_algo_vpp_id,
1839 p.auth_key,
1840 p.crypt_algo_vpp_id,
1841 p.crypt_key,
1842 self.vpp_esp_protocol,
1843 self.pg0.remote_ip4,
1844 self.pg0.local_ip4,
1845 )
Neale Rannsc87b66c2019-02-07 07:26:12 -08001846 p.tun_sa_in.add_vpp_config()
1847
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001848 p.tun_if = VppGreInterface(self, self.pg0.local_ip4, self.pg0.remote_ip4)
Neale Ranns12989b52019-09-26 16:20:19 +00001849 p.tun_if.add_vpp_config()
Neale Rannsc87b66c2019-02-07 07:26:12 -08001850
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001851 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
Neale Rannsc87b66c2019-02-07 07:26:12 -08001852 p.tun_protect.add_vpp_config()
1853
Neale Ranns12989b52019-09-26 16:20:19 +00001854 p.tun_if.admin_up()
1855 p.tun_if.config_ip4()
1856 config_tun_params(p, self.encryption_type, p.tun_if)
Neale Rannsc87b66c2019-02-07 07:26:12 -08001857
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001858 VppIpRoute(
1859 self, "1.1.1.2", 32, [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)]
1860 ).add_vpp_config()
Neale Rannsc87b66c2019-02-07 07:26:12 -08001861
1862 def tearDown(self):
Neale Ranns12989b52019-09-26 16:20:19 +00001863 p = self.ipv4_params
1864 p.tun_if.unconfig_ip4()
Neale Rannsc87b66c2019-02-07 07:26:12 -08001865 super(TestIpsecGreIfEsp, self).tearDown()
1866
1867
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001868class TestIpsecGreIfEspTra(TemplateIpsec, IpsecTun4Tests):
1869 """Ipsec GRE ESP - TRA tests"""
1870
Neale Rannsabde62f2019-12-02 22:32:05 +00001871 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00001872 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Rannsabde62f2019-12-02 22:32:05 +00001873 encryption_type = ESP
1874
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001875 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
1876 return [
1877 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1878 / sa.encrypt(
1879 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
1880 / GRE()
1881 / IP(src=self.pg1.local_ip4, dst=self.pg1.remote_ip4)
1882 / UDP(sport=1144, dport=2233)
1883 / Raw(b"X" * payload_size)
1884 )
1885 for i in range(count)
1886 ]
Neale Rannsabde62f2019-12-02 22:32:05 +00001887
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001888 def gen_encrypt_non_ip_pkts(self, sa, sw_intf, src, dst, count=1, payload_size=100):
1889 return [
1890 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1891 / sa.encrypt(
1892 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
1893 / GRE()
1894 / UDP(sport=1144, dport=2233)
1895 / Raw(b"X" * payload_size)
1896 )
1897 for i in range(count)
1898 ]
Neale Ranns02950402019-12-20 00:54:57 +00001899
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001900 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100):
1901 return [
1902 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
1903 / IP(src="1.1.1.1", dst="1.1.1.2")
1904 / UDP(sport=1144, dport=2233)
1905 / Raw(b"X" * payload_size)
1906 for i in range(count)
1907 ]
Neale Rannsabde62f2019-12-02 22:32:05 +00001908
1909 def verify_decrypted(self, p, rxs):
1910 for rx in rxs:
1911 self.assert_equal(rx[Ether].dst, self.pg1.remote_mac)
1912 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
1913
1914 def verify_encrypted(self, p, sa, rxs):
1915 for rx in rxs:
1916 try:
1917 pkt = sa.decrypt(rx[IP])
1918 if not pkt.haslayer(IP):
1919 pkt = IP(pkt[Raw].load)
1920 self.assert_packet_checksums_valid(pkt)
1921 self.assertTrue(pkt.haslayer(GRE))
1922 e = pkt[GRE]
1923 self.assertEqual(e[IP].dst, "1.1.1.2")
1924 except (IndexError, AssertionError):
1925 self.logger.debug(ppp("Unexpected packet:", rx))
1926 try:
1927 self.logger.debug(ppp("Decrypted packet:", pkt))
1928 except:
1929 pass
1930 raise
1931
1932 def setUp(self):
1933 super(TestIpsecGreIfEspTra, self).setUp()
1934
1935 self.tun_if = self.pg0
1936
1937 p = self.ipv4_params
1938
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001939 p.tun_sa_out = VppIpsecSA(
1940 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001941 p.vpp_tun_sa_id,
1942 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001943 p.auth_algo_vpp_id,
1944 p.auth_key,
1945 p.crypt_algo_vpp_id,
1946 p.crypt_key,
1947 self.vpp_esp_protocol,
1948 )
Neale Rannsabde62f2019-12-02 22:32:05 +00001949 p.tun_sa_out.add_vpp_config()
1950
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001951 p.tun_sa_in = VppIpsecSA(
1952 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01001953 p.scapy_tun_sa_id,
1954 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001955 p.auth_algo_vpp_id,
1956 p.auth_key,
1957 p.crypt_algo_vpp_id,
1958 p.crypt_key,
1959 self.vpp_esp_protocol,
1960 )
Neale Rannsabde62f2019-12-02 22:32:05 +00001961 p.tun_sa_in.add_vpp_config()
1962
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001963 p.tun_if = VppGreInterface(self, self.pg0.local_ip4, self.pg0.remote_ip4)
Neale Rannsabde62f2019-12-02 22:32:05 +00001964 p.tun_if.add_vpp_config()
1965
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001966 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
Neale Rannsabde62f2019-12-02 22:32:05 +00001967 p.tun_protect.add_vpp_config()
1968
1969 p.tun_if.admin_up()
1970 p.tun_if.config_ip4()
Neale Ranns568acbb2019-12-18 05:54:40 +00001971 config_tra_params(p, self.encryption_type, p.tun_if)
Neale Rannsabde62f2019-12-02 22:32:05 +00001972
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001973 VppIpRoute(
1974 self, "1.1.1.2", 32, [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)]
1975 ).add_vpp_config()
Neale Rannsabde62f2019-12-02 22:32:05 +00001976
1977 def tearDown(self):
1978 p = self.ipv4_params
1979 p.tun_if.unconfig_ip4()
1980 super(TestIpsecGreIfEspTra, self).tearDown()
1981
Neale Ranns02950402019-12-20 00:54:57 +00001982 def test_gre_non_ip(self):
1983 p = self.ipv4_params
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001984 tx = self.gen_encrypt_non_ip_pkts(
1985 p.scapy_tun_sa,
1986 self.tun_if,
1987 src=p.remote_tun_if_host,
1988 dst=self.pg1.remote_ip6,
1989 )
Neale Ranns02950402019-12-20 00:54:57 +00001990 self.send_and_assert_no_replies(self.tun_if, tx)
Neale Ranns93688d72022-08-09 03:34:51 +00001991 node_name = "/err/%s/unsup_payload" % self.tun4_decrypt_node_name[0]
Neale Ranns02950402019-12-20 00:54:57 +00001992 self.assertEqual(1, self.statistics.get_err_counter(node_name))
1993
1994
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001995class TestIpsecGre6IfEspTra(TemplateIpsec, IpsecTun6Tests):
1996 """Ipsec GRE ESP - TRA tests"""
1997
Neale Ranns02950402019-12-20 00:54:57 +00001998 tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00001999 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
Neale Ranns02950402019-12-20 00:54:57 +00002000 encryption_type = ESP
2001
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002002 def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
2003 return [
2004 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2005 / sa.encrypt(
2006 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6)
2007 / GRE()
2008 / IPv6(src=self.pg1.local_ip6, dst=self.pg1.remote_ip6)
2009 / UDP(sport=1144, dport=2233)
2010 / Raw(b"X" * payload_size)
2011 )
2012 for i in range(count)
2013 ]
Neale Ranns02950402019-12-20 00:54:57 +00002014
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002015 def gen_pkts6(self, p, sw_intf, src, dst, count=1, payload_size=100):
2016 return [
2017 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2018 / IPv6(src="1::1", dst="1::2")
2019 / UDP(sport=1144, dport=2233)
2020 / Raw(b"X" * payload_size)
2021 for i in range(count)
2022 ]
Neale Ranns02950402019-12-20 00:54:57 +00002023
2024 def verify_decrypted6(self, p, rxs):
2025 for rx in rxs:
2026 self.assert_equal(rx[Ether].dst, self.pg1.remote_mac)
2027 self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6)
2028
2029 def verify_encrypted6(self, p, sa, rxs):
2030 for rx in rxs:
2031 try:
2032 pkt = sa.decrypt(rx[IPv6])
2033 if not pkt.haslayer(IPv6):
2034 pkt = IPv6(pkt[Raw].load)
2035 self.assert_packet_checksums_valid(pkt)
2036 self.assertTrue(pkt.haslayer(GRE))
2037 e = pkt[GRE]
2038 self.assertEqual(e[IPv6].dst, "1::2")
2039 except (IndexError, AssertionError):
2040 self.logger.debug(ppp("Unexpected packet:", rx))
2041 try:
2042 self.logger.debug(ppp("Decrypted packet:", pkt))
2043 except:
2044 pass
2045 raise
2046
2047 def setUp(self):
2048 super(TestIpsecGre6IfEspTra, self).setUp()
2049
2050 self.tun_if = self.pg0
2051
2052 p = self.ipv6_params
2053
2054 bd1 = VppBridgeDomain(self, 1)
2055 bd1.add_vpp_config()
2056
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002057 p.tun_sa_out = VppIpsecSA(
2058 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01002059 p.vpp_tun_sa_id,
2060 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002061 p.auth_algo_vpp_id,
2062 p.auth_key,
2063 p.crypt_algo_vpp_id,
2064 p.crypt_key,
2065 self.vpp_esp_protocol,
2066 )
Neale Ranns02950402019-12-20 00:54:57 +00002067 p.tun_sa_out.add_vpp_config()
2068
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002069 p.tun_sa_in = VppIpsecSA(
2070 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01002071 p.scapy_tun_sa_id,
2072 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002073 p.auth_algo_vpp_id,
2074 p.auth_key,
2075 p.crypt_algo_vpp_id,
2076 p.crypt_key,
2077 self.vpp_esp_protocol,
2078 )
Neale Ranns02950402019-12-20 00:54:57 +00002079 p.tun_sa_in.add_vpp_config()
2080
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002081 p.tun_if = VppGreInterface(self, self.pg0.local_ip6, self.pg0.remote_ip6)
Neale Ranns02950402019-12-20 00:54:57 +00002082 p.tun_if.add_vpp_config()
2083
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002084 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
Neale Ranns02950402019-12-20 00:54:57 +00002085 p.tun_protect.add_vpp_config()
2086
2087 p.tun_if.admin_up()
2088 p.tun_if.config_ip6()
2089 config_tra_params(p, self.encryption_type, p.tun_if)
2090
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002091 r = VppIpRoute(
2092 self,
2093 "1::2",
2094 128,
2095 [
2096 VppRoutePath(
2097 p.tun_if.remote_ip6, 0xFFFFFFFF, proto=DpoProto.DPO_PROTO_IP6
2098 )
2099 ],
2100 )
Neale Ranns02950402019-12-20 00:54:57 +00002101 r.add_vpp_config()
2102
2103 def tearDown(self):
2104 p = self.ipv6_params
2105 p.tun_if.unconfig_ip6()
2106 super(TestIpsecGre6IfEspTra, self).tearDown()
2107
Neale Rannsabde62f2019-12-02 22:32:05 +00002108
Neale Ranns28287212019-12-16 00:53:11 +00002109class TestIpsecMGreIfEspTra4(TemplateIpsec, IpsecTun4):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002110 """Ipsec mGRE ESP v4 TRA tests"""
2111
Neale Ranns28287212019-12-16 00:53:11 +00002112 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00002113 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Ranns28287212019-12-16 00:53:11 +00002114 encryption_type = ESP
2115
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002116 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
2117 return [
2118 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2119 / sa.encrypt(
2120 IP(src=p.tun_dst, dst=self.pg0.local_ip4)
2121 / GRE()
2122 / IP(src=self.pg1.local_ip4, dst=self.pg1.remote_ip4)
2123 / UDP(sport=1144, dport=2233)
2124 / Raw(b"X" * payload_size)
2125 )
2126 for i in range(count)
2127 ]
Neale Ranns28287212019-12-16 00:53:11 +00002128
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002129 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100):
2130 return [
2131 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2132 / IP(src="1.1.1.1", dst=dst)
2133 / UDP(sport=1144, dport=2233)
2134 / Raw(b"X" * payload_size)
2135 for i in range(count)
2136 ]
Neale Ranns28287212019-12-16 00:53:11 +00002137
2138 def verify_decrypted(self, p, rxs):
2139 for rx in rxs:
2140 self.assert_equal(rx[Ether].dst, self.pg1.remote_mac)
2141 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
2142
2143 def verify_encrypted(self, p, sa, rxs):
2144 for rx in rxs:
2145 try:
2146 pkt = sa.decrypt(rx[IP])
2147 if not pkt.haslayer(IP):
2148 pkt = IP(pkt[Raw].load)
2149 self.assert_packet_checksums_valid(pkt)
2150 self.assertTrue(pkt.haslayer(GRE))
2151 e = pkt[GRE]
2152 self.assertEqual(e[IP].dst, p.remote_tun_if_host)
2153 except (IndexError, AssertionError):
2154 self.logger.debug(ppp("Unexpected packet:", rx))
2155 try:
2156 self.logger.debug(ppp("Decrypted packet:", pkt))
2157 except:
2158 pass
2159 raise
2160
2161 def setUp(self):
2162 super(TestIpsecMGreIfEspTra4, self).setUp()
2163
2164 N_NHS = 16
2165 self.tun_if = self.pg0
2166 p = self.ipv4_params
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002167 p.tun_if = VppGreInterface(
2168 self,
2169 self.pg0.local_ip4,
2170 "0.0.0.0",
2171 mode=(VppEnum.vl_api_tunnel_mode_t.TUNNEL_API_MODE_MP),
2172 )
Neale Ranns28287212019-12-16 00:53:11 +00002173 p.tun_if.add_vpp_config()
2174 p.tun_if.admin_up()
2175 p.tun_if.config_ip4()
2176 p.tun_if.generate_remote_hosts(N_NHS)
2177 self.pg0.generate_remote_hosts(N_NHS)
2178 self.pg0.configure_ipv4_neighbors()
2179
2180 # setup some SAs for several next-hops on the interface
2181 self.multi_params = []
2182
Neale Ranns6ba4e412020-10-19 09:59:41 +00002183 for ii in range(N_NHS):
Neale Ranns28287212019-12-16 00:53:11 +00002184 p = copy.copy(self.ipv4_params)
2185
2186 p.remote_tun_if_host = "1.1.1.%d" % (ii + 1)
2187 p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii
2188 p.scapy_tun_spi = p.scapy_tun_spi + ii
2189 p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii
2190 p.vpp_tun_spi = p.vpp_tun_spi + ii
2191
2192 p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii
2193 p.scapy_tra_spi = p.scapy_tra_spi + ii
2194 p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii
2195 p.vpp_tra_spi = p.vpp_tra_spi + ii
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002196 p.tun_sa_out = VppIpsecSA(
2197 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01002198 p.vpp_tun_sa_id,
2199 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002200 p.auth_algo_vpp_id,
2201 p.auth_key,
2202 p.crypt_algo_vpp_id,
2203 p.crypt_key,
2204 self.vpp_esp_protocol,
2205 )
Neale Ranns28287212019-12-16 00:53:11 +00002206 p.tun_sa_out.add_vpp_config()
2207
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002208 p.tun_sa_in = VppIpsecSA(
2209 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01002210 p.scapy_tun_sa_id,
2211 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002212 p.auth_algo_vpp_id,
2213 p.auth_key,
2214 p.crypt_algo_vpp_id,
2215 p.crypt_key,
2216 self.vpp_esp_protocol,
2217 )
Neale Ranns28287212019-12-16 00:53:11 +00002218 p.tun_sa_in.add_vpp_config()
2219
2220 p.tun_protect = VppIpsecTunProtect(
2221 self,
2222 p.tun_if,
2223 p.tun_sa_out,
2224 [p.tun_sa_in],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002225 nh=p.tun_if.remote_hosts[ii].ip4,
2226 )
Neale Ranns28287212019-12-16 00:53:11 +00002227 p.tun_protect.add_vpp_config()
2228 config_tra_params(p, self.encryption_type, p.tun_if)
2229 self.multi_params.append(p)
2230
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002231 VppIpRoute(
2232 self,
2233 p.remote_tun_if_host,
2234 32,
2235 [VppRoutePath(p.tun_if.remote_hosts[ii].ip4, p.tun_if.sw_if_index)],
2236 ).add_vpp_config()
Neale Ranns28287212019-12-16 00:53:11 +00002237
2238 # in this v4 variant add the teibs after the protect
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002239 p.teib = VppTeib(
2240 self,
2241 p.tun_if,
2242 p.tun_if.remote_hosts[ii].ip4,
2243 self.pg0.remote_hosts[ii].ip4,
2244 ).add_vpp_config()
Neale Ranns28287212019-12-16 00:53:11 +00002245 p.tun_dst = self.pg0.remote_hosts[ii].ip4
2246 self.logger.info(self.vapi.cli("sh ipsec protect-hash"))
2247
2248 def tearDown(self):
2249 p = self.ipv4_params
2250 p.tun_if.unconfig_ip4()
2251 super(TestIpsecMGreIfEspTra4, self).tearDown()
2252
2253 def test_tun_44(self):
2254 """mGRE IPSEC 44"""
2255 N_PKTS = 63
2256 for p in self.multi_params:
2257 self.verify_tun_44(p, count=N_PKTS)
2258 p.teib.remove_vpp_config()
2259 self.verify_tun_dropped_44(p, count=N_PKTS)
2260 p.teib.add_vpp_config()
2261 self.verify_tun_44(p, count=N_PKTS)
2262
2263
2264class TestIpsecMGreIfEspTra6(TemplateIpsec, IpsecTun6):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002265 """Ipsec mGRE ESP v6 TRA tests"""
2266
Neale Ranns28287212019-12-16 00:53:11 +00002267 tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00002268 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
Neale Ranns28287212019-12-16 00:53:11 +00002269 encryption_type = ESP
2270
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002271 def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
2272 return [
2273 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2274 / sa.encrypt(
2275 IPv6(src=p.tun_dst, dst=self.pg0.local_ip6)
2276 / GRE()
2277 / IPv6(src=self.pg1.local_ip6, dst=self.pg1.remote_ip6)
2278 / UDP(sport=1144, dport=2233)
2279 / Raw(b"X" * payload_size)
2280 )
2281 for i in range(count)
2282 ]
Neale Ranns28287212019-12-16 00:53:11 +00002283
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002284 def gen_pkts6(self, p, sw_intf, src, dst, count=1, payload_size=100):
2285 return [
2286 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2287 / IPv6(src="1::1", dst=dst)
2288 / UDP(sport=1144, dport=2233)
2289 / Raw(b"X" * payload_size)
2290 for i in range(count)
2291 ]
Neale Ranns28287212019-12-16 00:53:11 +00002292
2293 def verify_decrypted6(self, p, rxs):
2294 for rx in rxs:
2295 self.assert_equal(rx[Ether].dst, self.pg1.remote_mac)
2296 self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6)
2297
2298 def verify_encrypted6(self, p, sa, rxs):
2299 for rx in rxs:
2300 try:
2301 pkt = sa.decrypt(rx[IPv6])
2302 if not pkt.haslayer(IPv6):
2303 pkt = IPv6(pkt[Raw].load)
2304 self.assert_packet_checksums_valid(pkt)
2305 self.assertTrue(pkt.haslayer(GRE))
2306 e = pkt[GRE]
2307 self.assertEqual(e[IPv6].dst, p.remote_tun_if_host)
2308 except (IndexError, AssertionError):
2309 self.logger.debug(ppp("Unexpected packet:", rx))
2310 try:
2311 self.logger.debug(ppp("Decrypted packet:", pkt))
2312 except:
2313 pass
2314 raise
2315
2316 def setUp(self):
2317 super(TestIpsecMGreIfEspTra6, self).setUp()
2318
2319 self.vapi.cli("set logging class ipsec level debug")
2320
2321 N_NHS = 16
2322 self.tun_if = self.pg0
2323 p = self.ipv6_params
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002324 p.tun_if = VppGreInterface(
2325 self,
2326 self.pg0.local_ip6,
2327 "::",
2328 mode=(VppEnum.vl_api_tunnel_mode_t.TUNNEL_API_MODE_MP),
2329 )
Neale Ranns28287212019-12-16 00:53:11 +00002330 p.tun_if.add_vpp_config()
2331 p.tun_if.admin_up()
2332 p.tun_if.config_ip6()
2333 p.tun_if.generate_remote_hosts(N_NHS)
2334 self.pg0.generate_remote_hosts(N_NHS)
2335 self.pg0.configure_ipv6_neighbors()
2336
2337 # setup some SAs for several next-hops on the interface
2338 self.multi_params = []
2339
2340 for ii in range(N_NHS):
2341 p = copy.copy(self.ipv6_params)
2342
2343 p.remote_tun_if_host = "1::%d" % (ii + 1)
2344 p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii
2345 p.scapy_tun_spi = p.scapy_tun_spi + ii
2346 p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii
2347 p.vpp_tun_spi = p.vpp_tun_spi + ii
2348
2349 p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii
2350 p.scapy_tra_spi = p.scapy_tra_spi + ii
2351 p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii
2352 p.vpp_tra_spi = p.vpp_tra_spi + ii
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002353 p.tun_sa_out = VppIpsecSA(
2354 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01002355 p.vpp_tun_sa_id,
2356 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002357 p.auth_algo_vpp_id,
2358 p.auth_key,
2359 p.crypt_algo_vpp_id,
2360 p.crypt_key,
2361 self.vpp_esp_protocol,
2362 )
Neale Ranns28287212019-12-16 00:53:11 +00002363 p.tun_sa_out.add_vpp_config()
2364
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002365 p.tun_sa_in = VppIpsecSA(
2366 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01002367 p.scapy_tun_sa_id,
2368 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002369 p.auth_algo_vpp_id,
2370 p.auth_key,
2371 p.crypt_algo_vpp_id,
2372 p.crypt_key,
2373 self.vpp_esp_protocol,
2374 )
Neale Ranns28287212019-12-16 00:53:11 +00002375 p.tun_sa_in.add_vpp_config()
2376
2377 # in this v6 variant add the teibs first then the protection
2378 p.tun_dst = self.pg0.remote_hosts[ii].ip6
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002379 VppTeib(
2380 self, p.tun_if, p.tun_if.remote_hosts[ii].ip6, p.tun_dst
2381 ).add_vpp_config()
Neale Ranns28287212019-12-16 00:53:11 +00002382
2383 p.tun_protect = VppIpsecTunProtect(
2384 self,
2385 p.tun_if,
2386 p.tun_sa_out,
2387 [p.tun_sa_in],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002388 nh=p.tun_if.remote_hosts[ii].ip6,
2389 )
Neale Ranns28287212019-12-16 00:53:11 +00002390 p.tun_protect.add_vpp_config()
2391 config_tra_params(p, self.encryption_type, p.tun_if)
2392 self.multi_params.append(p)
2393
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002394 VppIpRoute(
2395 self,
2396 p.remote_tun_if_host,
2397 128,
2398 [VppRoutePath(p.tun_if.remote_hosts[ii].ip6, p.tun_if.sw_if_index)],
2399 ).add_vpp_config()
Neale Ranns28287212019-12-16 00:53:11 +00002400 p.tun_dst = self.pg0.remote_hosts[ii].ip6
2401
2402 self.logger.info(self.vapi.cli("sh log"))
2403 self.logger.info(self.vapi.cli("sh ipsec protect-hash"))
2404 self.logger.info(self.vapi.cli("sh adj 41"))
2405
2406 def tearDown(self):
2407 p = self.ipv6_params
2408 p.tun_if.unconfig_ip6()
2409 super(TestIpsecMGreIfEspTra6, self).tearDown()
2410
2411 def test_tun_66(self):
2412 """mGRE IPSec 66"""
2413 for p in self.multi_params:
2414 self.verify_tun_66(p, count=63)
2415
2416
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00002417@tag_fixme_vpp_workers
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002418class TestIpsec4TunProtect(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4):
2419 """IPsec IPv4 Tunnel protect - transport mode"""
Neale Rannsc87b66c2019-02-07 07:26:12 -08002420
Neale Rannsc87b66c2019-02-07 07:26:12 -08002421 def setUp(self):
2422 super(TestIpsec4TunProtect, self).setUp()
2423
2424 self.tun_if = self.pg0
2425
2426 def tearDown(self):
2427 super(TestIpsec4TunProtect, self).tearDown()
2428
2429 def test_tun_44(self):
2430 """IPSEC tunnel protect"""
2431
2432 p = self.ipv4_params
2433
2434 self.config_network(p)
2435 self.config_sa_tra(p)
2436 self.config_protect(p)
2437
2438 self.verify_tun_44(p, count=127)
Ole Troane66443c2021-03-18 11:12:01 +01002439 self.assertEqual(p.tun_if.get_rx_stats(), 127)
2440 self.assertEqual(p.tun_if.get_tx_stats(), 127)
Neale Rannsc87b66c2019-02-07 07:26:12 -08002441
Neale Rannsb3259832019-09-27 13:32:02 +00002442 self.vapi.cli("clear ipsec sa")
2443 self.verify_tun_64(p, count=127)
Ole Troane66443c2021-03-18 11:12:01 +01002444 self.assertEqual(p.tun_if.get_rx_stats(), 254)
2445 self.assertEqual(p.tun_if.get_tx_stats(), 254)
Neale Rannsb3259832019-09-27 13:32:02 +00002446
Neale Rannsc87b66c2019-02-07 07:26:12 -08002447 # rekey - create new SAs and update the tunnel protection
2448 np = copy.copy(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002449 np.crypt_key = b"X" + p.crypt_key[1:]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002450 np.scapy_tun_spi += 100
2451 np.scapy_tun_sa_id += 1
2452 np.vpp_tun_spi += 100
2453 np.vpp_tun_sa_id += 1
2454 np.tun_if.local_spi = p.vpp_tun_spi
2455 np.tun_if.remote_spi = p.scapy_tun_spi
2456
2457 self.config_sa_tra(np)
2458 self.config_protect(np)
2459 self.unconfig_sa(p)
2460
2461 self.verify_tun_44(np, count=127)
Ole Troane66443c2021-03-18 11:12:01 +01002462 self.assertEqual(p.tun_if.get_rx_stats(), 381)
2463 self.assertEqual(p.tun_if.get_tx_stats(), 381)
Neale Rannsc87b66c2019-02-07 07:26:12 -08002464
2465 # teardown
2466 self.unconfig_protect(np)
2467 self.unconfig_sa(np)
2468 self.unconfig_network(p)
2469
2470
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00002471@tag_fixme_vpp_workers
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002472class TestIpsec4TunProtectUdp(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4):
2473 """IPsec IPv4 Tunnel protect - transport mode"""
Neale Ranns41afb332019-07-16 06:19:35 -07002474
2475 def setUp(self):
2476 super(TestIpsec4TunProtectUdp, self).setUp()
2477
2478 self.tun_if = self.pg0
2479
2480 p = self.ipv4_params
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002481 p.flags = VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_UDP_ENCAP
Neale Rannsabc56602020-04-01 09:45:23 +00002482 p.nat_header = UDP(sport=4500, dport=4500)
Neale Ranns41afb332019-07-16 06:19:35 -07002483 self.config_network(p)
2484 self.config_sa_tra(p)
2485 self.config_protect(p)
2486
2487 def tearDown(self):
2488 p = self.ipv4_params
2489 self.unconfig_protect(p)
2490 self.unconfig_sa(p)
2491 self.unconfig_network(p)
2492 super(TestIpsec4TunProtectUdp, self).tearDown()
2493
Neale Rannsabc56602020-04-01 09:45:23 +00002494 def verify_encrypted(self, p, sa, rxs):
2495 # ensure encrypted packets are recieved with the default UDP ports
2496 for rx in rxs:
2497 self.assertEqual(rx[UDP].sport, 4500)
2498 self.assertEqual(rx[UDP].dport, 4500)
2499 super(TestIpsec4TunProtectUdp, self).verify_encrypted(p, sa, rxs)
2500
Neale Ranns41afb332019-07-16 06:19:35 -07002501 def test_tun_44(self):
2502 """IPSEC UDP tunnel protect"""
2503
2504 p = self.ipv4_params
2505
2506 self.verify_tun_44(p, count=127)
Ole Troane66443c2021-03-18 11:12:01 +01002507 self.assertEqual(p.tun_if.get_rx_stats(), 127)
2508 self.assertEqual(p.tun_if.get_tx_stats(), 127)
Neale Ranns41afb332019-07-16 06:19:35 -07002509
2510 def test_keepalive(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002511 """IPSEC NAT Keepalive"""
Neale Ranns41afb332019-07-16 06:19:35 -07002512 self.verify_keepalive(self.ipv4_params)
2513
2514
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00002515@tag_fixme_vpp_workers
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002516class TestIpsec4TunProtectTun(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4):
2517 """IPsec IPv4 Tunnel protect - tunnel mode"""
Neale Rannsc87b66c2019-02-07 07:26:12 -08002518
2519 encryption_type = ESP
2520 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00002521 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002522
2523 def setUp(self):
2524 super(TestIpsec4TunProtectTun, self).setUp()
2525
2526 self.tun_if = self.pg0
2527
2528 def tearDown(self):
2529 super(TestIpsec4TunProtectTun, self).tearDown()
2530
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002531 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
2532 return [
2533 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2534 / sa.encrypt(
2535 IP(src=sw_intf.remote_ip4, dst=sw_intf.local_ip4)
2536 / IP(src=src, dst=dst)
2537 / UDP(sport=1144, dport=2233)
2538 / Raw(b"X" * payload_size)
2539 )
2540 for i in range(count)
2541 ]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002542
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002543 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100):
2544 return [
2545 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2546 / IP(src=src, dst=dst)
2547 / UDP(sport=1144, dport=2233)
2548 / Raw(b"X" * payload_size)
2549 for i in range(count)
2550 ]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002551
2552 def verify_decrypted(self, p, rxs):
2553 for rx in rxs:
2554 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
2555 self.assert_equal(rx[IP].src, p.remote_tun_if_host)
2556 self.assert_packet_checksums_valid(rx)
2557
2558 def verify_encrypted(self, p, sa, rxs):
2559 for rx in rxs:
2560 try:
2561 pkt = sa.decrypt(rx[IP])
2562 if not pkt.haslayer(IP):
2563 pkt = IP(pkt[Raw].load)
2564 self.assert_packet_checksums_valid(pkt)
2565 self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4)
2566 self.assert_equal(pkt[IP].src, self.pg0.local_ip4)
2567 inner = pkt[IP].payload
2568 self.assertEqual(inner[IP][IP].dst, p.remote_tun_if_host)
2569
2570 except (IndexError, AssertionError):
2571 self.logger.debug(ppp("Unexpected packet:", rx))
2572 try:
2573 self.logger.debug(ppp("Decrypted packet:", pkt))
2574 except:
2575 pass
2576 raise
2577
2578 def test_tun_44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002579 """IPSEC tunnel protect"""
Neale Rannsc87b66c2019-02-07 07:26:12 -08002580
2581 p = self.ipv4_params
2582
2583 self.config_network(p)
2584 self.config_sa_tun(p)
2585 self.config_protect(p)
2586
Neale Ranns5d0136f2020-05-12 08:51:02 +00002587 # also add an output features on the tunnel and physical interface
2588 # so we test they still work
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002589 r_all = AclRule(True, src_prefix="0.0.0.0/0", dst_prefix="0.0.0.0/0", proto=0)
Neale Ranns5d0136f2020-05-12 08:51:02 +00002590 a = VppAcl(self, [r_all]).add_vpp_config()
2591
2592 VppAclInterface(self, self.pg0.sw_if_index, [a]).add_vpp_config()
2593 VppAclInterface(self, p.tun_if.sw_if_index, [a]).add_vpp_config()
2594
Neale Rannsc87b66c2019-02-07 07:26:12 -08002595 self.verify_tun_44(p, count=127)
2596
Ole Troane66443c2021-03-18 11:12:01 +01002597 self.assertEqual(p.tun_if.get_rx_stats(), 127)
2598 self.assertEqual(p.tun_if.get_tx_stats(), 127)
Neale Rannsc87b66c2019-02-07 07:26:12 -08002599
2600 # rekey - create new SAs and update the tunnel protection
2601 np = copy.copy(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002602 np.crypt_key = b"X" + p.crypt_key[1:]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002603 np.scapy_tun_spi += 100
2604 np.scapy_tun_sa_id += 1
2605 np.vpp_tun_spi += 100
2606 np.vpp_tun_sa_id += 1
2607 np.tun_if.local_spi = p.vpp_tun_spi
2608 np.tun_if.remote_spi = p.scapy_tun_spi
2609
2610 self.config_sa_tun(np)
2611 self.config_protect(np)
2612 self.unconfig_sa(p)
2613
2614 self.verify_tun_44(np, count=127)
Ole Troane66443c2021-03-18 11:12:01 +01002615 self.assertEqual(p.tun_if.get_rx_stats(), 254)
2616 self.assertEqual(p.tun_if.get_tx_stats(), 254)
Neale Rannsc87b66c2019-02-07 07:26:12 -08002617
2618 # teardown
2619 self.unconfig_protect(np)
2620 self.unconfig_sa(np)
2621 self.unconfig_network(p)
2622
2623
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002624class TestIpsec4TunProtectTunDrop(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4):
2625 """IPsec IPv4 Tunnel protect - tunnel mode - drop"""
Neale Ranns02950402019-12-20 00:54:57 +00002626
2627 encryption_type = ESP
2628 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00002629 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Ranns02950402019-12-20 00:54:57 +00002630
2631 def setUp(self):
2632 super(TestIpsec4TunProtectTunDrop, self).setUp()
2633
2634 self.tun_if = self.pg0
2635
2636 def tearDown(self):
2637 super(TestIpsec4TunProtectTunDrop, self).tearDown()
2638
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002639 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
2640 return [
2641 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2642 / sa.encrypt(
2643 IP(src=sw_intf.remote_ip4, dst="5.5.5.5")
2644 / IP(src=src, dst=dst)
2645 / UDP(sport=1144, dport=2233)
2646 / Raw(b"X" * payload_size)
2647 )
2648 for i in range(count)
2649 ]
Neale Ranns02950402019-12-20 00:54:57 +00002650
2651 def test_tun_drop_44(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002652 """IPSEC tunnel protect bogus tunnel header"""
Neale Ranns02950402019-12-20 00:54:57 +00002653
2654 p = self.ipv4_params
2655
2656 self.config_network(p)
2657 self.config_sa_tun(p)
2658 self.config_protect(p)
2659
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002660 tx = self.gen_encrypt_pkts(
2661 p,
2662 p.scapy_tun_sa,
2663 self.tun_if,
2664 src=p.remote_tun_if_host,
2665 dst=self.pg1.remote_ip4,
2666 count=63,
2667 )
Neale Ranns02950402019-12-20 00:54:57 +00002668 self.send_and_assert_no_replies(self.tun_if, tx)
2669
2670 # teardown
2671 self.unconfig_protect(p)
2672 self.unconfig_sa(p)
2673 self.unconfig_network(p)
2674
2675
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00002676@tag_fixme_vpp_workers
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002677class TestIpsec6TunProtect(TemplateIpsec, TemplateIpsec6TunProtect, IpsecTun6):
2678 """IPsec IPv6 Tunnel protect - transport mode"""
Neale Rannsc87b66c2019-02-07 07:26:12 -08002679
2680 encryption_type = ESP
2681 tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00002682 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002683
2684 def setUp(self):
2685 super(TestIpsec6TunProtect, self).setUp()
2686
2687 self.tun_if = self.pg0
2688
2689 def tearDown(self):
2690 super(TestIpsec6TunProtect, self).tearDown()
2691
2692 def test_tun_66(self):
Neale Ranns02950402019-12-20 00:54:57 +00002693 """IPSEC tunnel protect 6o6"""
Neale Rannsc87b66c2019-02-07 07:26:12 -08002694
2695 p = self.ipv6_params
2696
2697 self.config_network(p)
2698 self.config_sa_tra(p)
2699 self.config_protect(p)
2700
2701 self.verify_tun_66(p, count=127)
Ole Troane66443c2021-03-18 11:12:01 +01002702 self.assertEqual(p.tun_if.get_rx_stats(), 127)
2703 self.assertEqual(p.tun_if.get_tx_stats(), 127)
Neale Rannsc87b66c2019-02-07 07:26:12 -08002704
2705 # rekey - create new SAs and update the tunnel protection
2706 np = copy.copy(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002707 np.crypt_key = b"X" + p.crypt_key[1:]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002708 np.scapy_tun_spi += 100
2709 np.scapy_tun_sa_id += 1
2710 np.vpp_tun_spi += 100
2711 np.vpp_tun_sa_id += 1
2712 np.tun_if.local_spi = p.vpp_tun_spi
2713 np.tun_if.remote_spi = p.scapy_tun_spi
2714
2715 self.config_sa_tra(np)
2716 self.config_protect(np)
2717 self.unconfig_sa(p)
2718
2719 self.verify_tun_66(np, count=127)
Ole Troane66443c2021-03-18 11:12:01 +01002720 self.assertEqual(p.tun_if.get_rx_stats(), 254)
2721 self.assertEqual(p.tun_if.get_tx_stats(), 254)
Neale Rannsc87b66c2019-02-07 07:26:12 -08002722
Neale Ranns02950402019-12-20 00:54:57 +00002723 # bounce the interface state
2724 p.tun_if.admin_down()
2725 self.verify_drop_tun_66(np, count=127)
Neale Ranns93688d72022-08-09 03:34:51 +00002726 node = "/err/ipsec6-tun-input/disabled"
Neale Ranns02950402019-12-20 00:54:57 +00002727 self.assertEqual(127, self.statistics.get_err_counter(node))
2728 p.tun_if.admin_up()
2729 self.verify_tun_66(np, count=127)
2730
Neale Rannsc87b66c2019-02-07 07:26:12 -08002731 # 3 phase rekey
2732 # 1) add two input SAs [old, new]
2733 # 2) swap output SA to [new]
2734 # 3) use only [new] input SA
2735 np3 = copy.copy(np)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002736 np3.crypt_key = b"Z" + p.crypt_key[1:]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002737 np3.scapy_tun_spi += 100
2738 np3.scapy_tun_sa_id += 1
2739 np3.vpp_tun_spi += 100
2740 np3.vpp_tun_sa_id += 1
2741 np3.tun_if.local_spi = p.vpp_tun_spi
2742 np3.tun_if.remote_spi = p.scapy_tun_spi
2743
2744 self.config_sa_tra(np3)
2745
2746 # step 1;
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002747 p.tun_protect.update_vpp_config(np.tun_sa_out, [np.tun_sa_in, np3.tun_sa_in])
Neale Rannsc87b66c2019-02-07 07:26:12 -08002748 self.verify_tun_66(np, np, count=127)
2749 self.verify_tun_66(np3, np, count=127)
2750
2751 # step 2;
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002752 p.tun_protect.update_vpp_config(np3.tun_sa_out, [np.tun_sa_in, np3.tun_sa_in])
Neale Rannsc87b66c2019-02-07 07:26:12 -08002753 self.verify_tun_66(np, np3, count=127)
2754 self.verify_tun_66(np3, np3, count=127)
2755
2756 # step 1;
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002757 p.tun_protect.update_vpp_config(np3.tun_sa_out, [np3.tun_sa_in])
Neale Rannsc87b66c2019-02-07 07:26:12 -08002758 self.verify_tun_66(np3, np3, count=127)
Neale Ranns49378f22022-01-10 10:38:43 +00002759 self.verify_drop_tun_rx_66(np, count=127)
Neale Rannsc87b66c2019-02-07 07:26:12 -08002760
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002761 self.assertEqual(p.tun_if.get_rx_stats(), 127 * 9)
2762 self.assertEqual(p.tun_if.get_tx_stats(), 127 * 8)
Neale Rannsc87b66c2019-02-07 07:26:12 -08002763 self.unconfig_sa(np)
2764
2765 # teardown
2766 self.unconfig_protect(np3)
2767 self.unconfig_sa(np3)
2768 self.unconfig_network(p)
2769
Neale Rannsb3259832019-09-27 13:32:02 +00002770 def test_tun_46(self):
Neale Ranns02950402019-12-20 00:54:57 +00002771 """IPSEC tunnel protect 4o6"""
Neale Rannsb3259832019-09-27 13:32:02 +00002772
2773 p = self.ipv6_params
2774
2775 self.config_network(p)
2776 self.config_sa_tra(p)
2777 self.config_protect(p)
2778
2779 self.verify_tun_46(p, count=127)
Ole Troane66443c2021-03-18 11:12:01 +01002780 self.assertEqual(p.tun_if.get_rx_stats(), 127)
2781 self.assertEqual(p.tun_if.get_tx_stats(), 127)
Neale Rannsb3259832019-09-27 13:32:02 +00002782
2783 # teardown
2784 self.unconfig_protect(p)
2785 self.unconfig_sa(p)
2786 self.unconfig_network(p)
2787
Neale Rannsc87b66c2019-02-07 07:26:12 -08002788
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00002789@tag_fixme_vpp_workers
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002790class TestIpsec6TunProtectTun(TemplateIpsec, TemplateIpsec6TunProtect, IpsecTun6):
2791 """IPsec IPv6 Tunnel protect - tunnel mode"""
Neale Rannsc87b66c2019-02-07 07:26:12 -08002792
2793 encryption_type = ESP
2794 tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00002795 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002796
2797 def setUp(self):
2798 super(TestIpsec6TunProtectTun, self).setUp()
2799
2800 self.tun_if = self.pg0
2801
2802 def tearDown(self):
2803 super(TestIpsec6TunProtectTun, self).tearDown()
2804
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002805 def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
2806 return [
2807 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2808 / sa.encrypt(
2809 IPv6(src=sw_intf.remote_ip6, dst=sw_intf.local_ip6)
2810 / IPv6(src=src, dst=dst)
2811 / UDP(sport=1166, dport=2233)
2812 / Raw(b"X" * payload_size)
2813 )
2814 for i in range(count)
2815 ]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002816
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002817 def gen_pkts6(self, p, sw_intf, src, dst, count=1, payload_size=100):
2818 return [
2819 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2820 / IPv6(src=src, dst=dst)
2821 / UDP(sport=1166, dport=2233)
2822 / Raw(b"X" * payload_size)
2823 for i in range(count)
2824 ]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002825
2826 def verify_decrypted6(self, p, rxs):
2827 for rx in rxs:
2828 self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6)
2829 self.assert_equal(rx[IPv6].src, p.remote_tun_if_host)
2830 self.assert_packet_checksums_valid(rx)
2831
2832 def verify_encrypted6(self, p, sa, rxs):
2833 for rx in rxs:
2834 try:
2835 pkt = sa.decrypt(rx[IPv6])
2836 if not pkt.haslayer(IPv6):
2837 pkt = IPv6(pkt[Raw].load)
2838 self.assert_packet_checksums_valid(pkt)
2839 self.assert_equal(pkt[IPv6].dst, self.pg0.remote_ip6)
2840 self.assert_equal(pkt[IPv6].src, self.pg0.local_ip6)
2841 inner = pkt[IPv6].payload
2842 self.assertEqual(inner[IPv6][IPv6].dst, p.remote_tun_if_host)
2843
2844 except (IndexError, AssertionError):
2845 self.logger.debug(ppp("Unexpected packet:", rx))
2846 try:
2847 self.logger.debug(ppp("Decrypted packet:", pkt))
2848 except:
2849 pass
2850 raise
2851
2852 def test_tun_66(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002853 """IPSEC tunnel protect"""
Neale Rannsc87b66c2019-02-07 07:26:12 -08002854
2855 p = self.ipv6_params
2856
2857 self.config_network(p)
2858 self.config_sa_tun(p)
2859 self.config_protect(p)
2860
2861 self.verify_tun_66(p, count=127)
2862
Ole Troane66443c2021-03-18 11:12:01 +01002863 self.assertEqual(p.tun_if.get_rx_stats(), 127)
2864 self.assertEqual(p.tun_if.get_tx_stats(), 127)
Neale Rannsc87b66c2019-02-07 07:26:12 -08002865
2866 # rekey - create new SAs and update the tunnel protection
2867 np = copy.copy(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002868 np.crypt_key = b"X" + p.crypt_key[1:]
Neale Rannsc87b66c2019-02-07 07:26:12 -08002869 np.scapy_tun_spi += 100
2870 np.scapy_tun_sa_id += 1
2871 np.vpp_tun_spi += 100
2872 np.vpp_tun_sa_id += 1
2873 np.tun_if.local_spi = p.vpp_tun_spi
2874 np.tun_if.remote_spi = p.scapy_tun_spi
2875
2876 self.config_sa_tun(np)
2877 self.config_protect(np)
2878 self.unconfig_sa(p)
2879
2880 self.verify_tun_66(np, count=127)
Ole Troane66443c2021-03-18 11:12:01 +01002881 self.assertEqual(p.tun_if.get_rx_stats(), 254)
2882 self.assertEqual(p.tun_if.get_tx_stats(), 254)
Neale Rannsc87b66c2019-02-07 07:26:12 -08002883
2884 # teardown
2885 self.unconfig_protect(np)
2886 self.unconfig_sa(np)
2887 self.unconfig_network(p)
2888
Neale Rannsf05e7322019-03-29 20:23:58 +00002889
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002890class TestIpsec6TunProtectTunDrop(TemplateIpsec, TemplateIpsec6TunProtect, IpsecTun6):
2891 """IPsec IPv6 Tunnel protect - tunnel mode - drop"""
Neale Ranns02950402019-12-20 00:54:57 +00002892
2893 encryption_type = ESP
2894 tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00002895 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
Neale Ranns02950402019-12-20 00:54:57 +00002896
2897 def setUp(self):
2898 super(TestIpsec6TunProtectTunDrop, self).setUp()
2899
2900 self.tun_if = self.pg0
2901
2902 def tearDown(self):
2903 super(TestIpsec6TunProtectTunDrop, self).tearDown()
2904
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002905 def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
Neale Ranns02950402019-12-20 00:54:57 +00002906 # the IP destination of the revelaed packet does not match
2907 # that assigned to the tunnel
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002908 return [
2909 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
2910 / sa.encrypt(
2911 IPv6(src=sw_intf.remote_ip6, dst="5::5")
2912 / IPv6(src=src, dst=dst)
2913 / UDP(sport=1144, dport=2233)
2914 / Raw(b"X" * payload_size)
2915 )
2916 for i in range(count)
2917 ]
Neale Ranns02950402019-12-20 00:54:57 +00002918
2919 def test_tun_drop_66(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002920 """IPSEC 6 tunnel protect bogus tunnel header"""
Neale Ranns02950402019-12-20 00:54:57 +00002921
2922 p = self.ipv6_params
2923
2924 self.config_network(p)
2925 self.config_sa_tun(p)
2926 self.config_protect(p)
2927
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002928 tx = self.gen_encrypt_pkts6(
2929 p,
2930 p.scapy_tun_sa,
2931 self.tun_if,
2932 src=p.remote_tun_if_host,
2933 dst=self.pg1.remote_ip6,
2934 count=63,
2935 )
Neale Ranns02950402019-12-20 00:54:57 +00002936 self.send_and_assert_no_replies(self.tun_if, tx)
2937
2938 self.unconfig_protect(p)
2939 self.unconfig_sa(p)
2940 self.unconfig_network(p)
2941
2942
Neale Rannsdd4ccf22020-06-30 07:47:14 +00002943class TemplateIpsecItf4(object):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002944 """IPsec Interface IPv4"""
Neale Rannsdd4ccf22020-06-30 07:47:14 +00002945
2946 encryption_type = ESP
2947 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00002948 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Rannsdd4ccf22020-06-30 07:47:14 +00002949 tun4_input_node = "ipsec4-tun-input"
2950
2951 def config_sa_tun(self, p, src, dst):
2952 config_tun_params(p, self.encryption_type, None, src, dst)
2953
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002954 p.tun_sa_out = VppIpsecSA(
2955 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01002956 p.vpp_tun_sa_id,
2957 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002958 p.auth_algo_vpp_id,
2959 p.auth_key,
2960 p.crypt_algo_vpp_id,
2961 p.crypt_key,
2962 self.vpp_esp_protocol,
2963 src,
2964 dst,
2965 flags=p.flags,
2966 )
Neale Rannsdd4ccf22020-06-30 07:47:14 +00002967 p.tun_sa_out.add_vpp_config()
2968
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002969 p.tun_sa_in = VppIpsecSA(
2970 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01002971 p.scapy_tun_sa_id,
2972 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002973 p.auth_algo_vpp_id,
2974 p.auth_key,
2975 p.crypt_algo_vpp_id,
2976 p.crypt_key,
2977 self.vpp_esp_protocol,
2978 dst,
2979 src,
Arthur de Kerhor4117b242022-08-31 19:13:03 +02002980 flags=p.flags
2981 | VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_IS_INBOUND,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002982 )
Neale Rannsdd4ccf22020-06-30 07:47:14 +00002983 p.tun_sa_in.add_vpp_config()
2984
2985 def config_protect(self, p):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002986 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
Neale Rannsdd4ccf22020-06-30 07:47:14 +00002987 p.tun_protect.add_vpp_config()
2988
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002989 def config_network(self, p, instance=0xFFFFFFFF):
Eric Kinzie609d5792020-10-13 20:02:11 -04002990 p.tun_if = VppIpsecInterface(self, instance=instance)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00002991
2992 p.tun_if.add_vpp_config()
2993 p.tun_if.admin_up()
2994 p.tun_if.config_ip4()
2995 p.tun_if.config_ip6()
2996
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002997 p.route = VppIpRoute(
2998 self,
2999 p.remote_tun_if_host,
3000 32,
3001 [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)],
3002 )
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003003 p.route.add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003004 r = VppIpRoute(
3005 self,
3006 p.remote_tun_if_host6,
3007 128,
3008 [
3009 VppRoutePath(
3010 p.tun_if.remote_ip6, 0xFFFFFFFF, proto=DpoProto.DPO_PROTO_IP6
3011 )
3012 ],
3013 )
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003014 r.add_vpp_config()
3015
3016 def unconfig_network(self, p):
3017 p.route.remove_vpp_config()
3018 p.tun_if.remove_vpp_config()
3019
3020 def unconfig_protect(self, p):
3021 p.tun_protect.remove_vpp_config()
3022
3023 def unconfig_sa(self, p):
3024 p.tun_sa_out.remove_vpp_config()
3025 p.tun_sa_in.remove_vpp_config()
3026
3027
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00003028@tag_fixme_vpp_workers
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003029class TestIpsecItf4(TemplateIpsec, TemplateIpsecItf4, IpsecTun4):
3030 """IPsec Interface IPv4"""
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003031
3032 def setUp(self):
3033 super(TestIpsecItf4, self).setUp()
3034
3035 self.tun_if = self.pg0
3036
3037 def tearDown(self):
3038 super(TestIpsecItf4, self).tearDown()
3039
Eric Kinzie609d5792020-10-13 20:02:11 -04003040 def test_tun_instance_44(self):
3041 p = self.ipv4_params
3042 self.config_network(p, instance=3)
3043
3044 with self.assertRaises(CliFailedCommandError):
3045 self.vapi.cli("show interface ipsec0")
3046
3047 output = self.vapi.cli("show interface ipsec3")
3048 self.assertTrue("unknown" not in output)
3049
3050 self.unconfig_network(p)
3051
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003052 def test_tun_44(self):
3053 """IPSEC interface IPv4"""
3054
3055 n_pkts = 127
3056 p = self.ipv4_params
3057
3058 self.config_network(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003059 config_tun_params(
3060 p, self.encryption_type, None, self.pg0.local_ip4, self.pg0.remote_ip4
3061 )
Neale Ranns49378f22022-01-10 10:38:43 +00003062 self.verify_tun_dropped_44(p, count=n_pkts)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003063 self.config_sa_tun(p, self.pg0.local_ip4, self.pg0.remote_ip4)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003064 self.config_protect(p)
3065
3066 self.verify_tun_44(p, count=n_pkts)
Ole Troane66443c2021-03-18 11:12:01 +01003067 self.assertEqual(p.tun_if.get_rx_stats(), n_pkts)
3068 self.assertEqual(p.tun_if.get_tx_stats(), n_pkts)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003069
3070 p.tun_if.admin_down()
3071 self.verify_tun_dropped_44(p, count=n_pkts)
3072 p.tun_if.admin_up()
3073 self.verify_tun_44(p, count=n_pkts)
3074
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003075 self.assertEqual(p.tun_if.get_rx_stats(), 3 * n_pkts)
3076 self.assertEqual(p.tun_if.get_tx_stats(), 2 * n_pkts)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003077
3078 # it's a v6 packet when its encrypted
3079 self.tun4_encrypt_node_name = "esp6-encrypt-tun"
3080
3081 self.verify_tun_64(p, count=n_pkts)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003082 self.assertEqual(p.tun_if.get_rx_stats(), 4 * n_pkts)
3083 self.assertEqual(p.tun_if.get_tx_stats(), 3 * n_pkts)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003084
3085 self.tun4_encrypt_node_name = "esp4-encrypt-tun"
3086
Arthur de Kerhor4117b242022-08-31 19:13:03 +02003087 # update the SA tunnel
3088 config_tun_params(
3089 p, self.encryption_type, None, self.pg2.local_ip4, self.pg2.remote_ip4
3090 )
3091 p.tun_sa_in.update_vpp_config(
3092 is_tun=True, tun_src=self.pg2.remote_ip4, tun_dst=self.pg2.local_ip4
3093 )
3094 p.tun_sa_out.update_vpp_config(
3095 is_tun=True, tun_src=self.pg2.local_ip4, tun_dst=self.pg2.remote_ip4
3096 )
3097 self.verify_tun_44(p, count=n_pkts)
3098 self.assertEqual(p.tun_if.get_rx_stats(), 5 * n_pkts)
3099 self.assertEqual(p.tun_if.get_tx_stats(), 4 * n_pkts)
3100
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003101 self.vapi.cli("clear interfaces")
3102
3103 # rekey - create new SAs and update the tunnel protection
3104 np = copy.copy(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003105 np.crypt_key = b"X" + p.crypt_key[1:]
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003106 np.scapy_tun_spi += 100
3107 np.scapy_tun_sa_id += 1
3108 np.vpp_tun_spi += 100
3109 np.vpp_tun_sa_id += 1
3110 np.tun_if.local_spi = p.vpp_tun_spi
3111 np.tun_if.remote_spi = p.scapy_tun_spi
3112
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003113 self.config_sa_tun(np, self.pg0.local_ip4, self.pg0.remote_ip4)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003114 self.config_protect(np)
3115 self.unconfig_sa(p)
3116
3117 self.verify_tun_44(np, count=n_pkts)
Ole Troane66443c2021-03-18 11:12:01 +01003118 self.assertEqual(p.tun_if.get_rx_stats(), n_pkts)
3119 self.assertEqual(p.tun_if.get_tx_stats(), n_pkts)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003120
3121 # teardown
3122 self.unconfig_protect(np)
3123 self.unconfig_sa(np)
3124 self.unconfig_network(p)
3125
Neale Ranns970187b2020-10-07 13:58:56 +00003126 def test_tun_44_null(self):
3127 """IPSEC interface IPv4 NULL auth/crypto"""
3128
3129 n_pkts = 127
3130 p = copy.copy(self.ipv4_params)
3131
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003132 p.auth_algo_vpp_id = VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE
3133 p.crypt_algo_vpp_id = (
3134 VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_NONE
3135 )
Neale Ranns970187b2020-10-07 13:58:56 +00003136 p.crypt_algo = "NULL"
3137 p.auth_algo = "NULL"
3138
3139 self.config_network(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003140 self.config_sa_tun(p, self.pg0.local_ip4, self.pg0.remote_ip4)
Neale Ranns970187b2020-10-07 13:58:56 +00003141 self.config_protect(p)
3142
Neale Ranns49378f22022-01-10 10:38:43 +00003143 self.logger.info(self.vapi.cli("sh ipsec sa"))
Neale Ranns970187b2020-10-07 13:58:56 +00003144 self.verify_tun_44(p, count=n_pkts)
3145
Eric Kinzie609d5792020-10-13 20:02:11 -04003146 # teardown
3147 self.unconfig_protect(p)
3148 self.unconfig_sa(p)
3149 self.unconfig_network(p)
3150
Brian Russell7a29a2d2021-02-22 18:42:24 +00003151 def test_tun_44_police(self):
3152 """IPSEC interface IPv4 with input policer"""
3153 n_pkts = 127
3154 p = self.ipv4_params
3155
3156 self.config_network(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003157 self.config_sa_tun(p, self.pg0.local_ip4, self.pg0.remote_ip4)
Brian Russell7a29a2d2021-02-22 18:42:24 +00003158 self.config_protect(p)
3159
3160 action_tx = PolicerAction(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003161 VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0
3162 )
3163 policer = VppPolicer(
3164 self,
3165 "pol1",
3166 80,
3167 0,
3168 1000,
3169 0,
3170 conform_action=action_tx,
3171 exceed_action=action_tx,
3172 violate_action=action_tx,
3173 )
Brian Russell7a29a2d2021-02-22 18:42:24 +00003174 policer.add_vpp_config()
3175
3176 # Start policing on tun
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +02003177 policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, True)
Brian Russell7a29a2d2021-02-22 18:42:24 +00003178
3179 self.verify_tun_44(p, count=n_pkts)
Ole Troane66443c2021-03-18 11:12:01 +01003180 self.assertEqual(p.tun_if.get_rx_stats(), n_pkts)
3181 self.assertEqual(p.tun_if.get_tx_stats(), n_pkts)
Brian Russell7a29a2d2021-02-22 18:42:24 +00003182
3183 stats = policer.get_stats()
3184
3185 # Single rate, 2 colour policer - expect conform, violate but no exceed
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003186 self.assertGreater(stats["conform_packets"], 0)
3187 self.assertEqual(stats["exceed_packets"], 0)
3188 self.assertGreater(stats["violate_packets"], 0)
Brian Russell7a29a2d2021-02-22 18:42:24 +00003189
3190 # Stop policing on tun
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +02003191 policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, False)
Brian Russell7a29a2d2021-02-22 18:42:24 +00003192 self.verify_tun_44(p, count=n_pkts)
3193
3194 # No new policer stats
3195 statsnew = policer.get_stats()
3196 self.assertEqual(stats, statsnew)
3197
3198 # teardown
3199 policer.remove_vpp_config()
3200 self.unconfig_protect(p)
3201 self.unconfig_sa(p)
3202 self.unconfig_network(p)
3203
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003204
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003205class TestIpsecItf4MPLS(TemplateIpsec, TemplateIpsecItf4, IpsecTun4):
3206 """IPsec Interface MPLSoIPv4"""
Neale Ranns4a58e492020-12-21 13:19:10 +00003207
3208 tun4_encrypt_node_name = "esp-mpls-encrypt-tun"
3209
3210 def setUp(self):
3211 super(TestIpsecItf4MPLS, self).setUp()
3212
3213 self.tun_if = self.pg0
3214
3215 def tearDown(self):
3216 super(TestIpsecItf4MPLS, self).tearDown()
3217
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003218 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
3219 return [
3220 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
3221 / sa.encrypt(
3222 MPLS(label=44, ttl=3)
3223 / IP(src=src, dst=dst)
3224 / UDP(sport=1166, dport=2233)
3225 / Raw(b"X" * payload_size)
3226 )
3227 for i in range(count)
3228 ]
Neale Ranns4a58e492020-12-21 13:19:10 +00003229
3230 def verify_encrypted(self, p, sa, rxs):
3231 for rx in rxs:
3232 try:
3233 pkt = sa.decrypt(rx[IP])
3234 if not pkt.haslayer(IP):
3235 pkt = IP(pkt[Raw].load)
3236 self.assert_packet_checksums_valid(pkt)
3237 self.assert_equal(pkt[MPLS].label, 44)
3238 self.assert_equal(pkt[IP].dst, p.remote_tun_if_host)
3239 except (IndexError, AssertionError):
3240 self.logger.debug(ppp("Unexpected packet:", rx))
3241 try:
3242 self.logger.debug(ppp("Decrypted packet:", pkt))
3243 except:
3244 pass
3245 raise
3246
3247 def test_tun_mpls_o_ip4(self):
3248 """IPSEC interface MPLS over IPv4"""
3249
3250 n_pkts = 127
3251 p = self.ipv4_params
3252 f = FibPathProto
3253
3254 tbl = VppMplsTable(self, 0)
3255 tbl.add_vpp_config()
3256
3257 self.config_network(p)
3258 # deag MPLS routes from the tunnel
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003259 r4 = VppMplsRoute(
3260 self, 44, 1, [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)]
3261 ).add_vpp_config()
3262 p.route.modify(
3263 [
3264 VppRoutePath(
3265 p.tun_if.remote_ip4, p.tun_if.sw_if_index, labels=[VppMplsLabel(44)]
3266 )
3267 ]
3268 )
Neale Ranns4a58e492020-12-21 13:19:10 +00003269 p.tun_if.enable_mpls()
3270
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003271 self.config_sa_tun(p, self.pg0.local_ip4, self.pg0.remote_ip4)
Neale Ranns4a58e492020-12-21 13:19:10 +00003272 self.config_protect(p)
3273
3274 self.verify_tun_44(p, count=n_pkts)
3275
3276 # cleanup
3277 p.tun_if.disable_mpls()
3278 self.unconfig_protect(p)
3279 self.unconfig_sa(p)
3280 self.unconfig_network(p)
3281
3282
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003283class TemplateIpsecItf6(object):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003284 """IPsec Interface IPv6"""
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003285
3286 encryption_type = ESP
3287 tun6_encrypt_node_name = "esp6-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00003288 tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"]
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003289 tun6_input_node = "ipsec6-tun-input"
3290
3291 def config_sa_tun(self, p, src, dst):
3292 config_tun_params(p, self.encryption_type, None, src, dst)
3293
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003294 if not hasattr(p, "tun_flags"):
Neale Ranns9ec846c2021-02-09 14:04:02 +00003295 p.tun_flags = None
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003296 if not hasattr(p, "hop_limit"):
Neale Ranns9ec846c2021-02-09 14:04:02 +00003297 p.hop_limit = 255
3298
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003299 p.tun_sa_out = VppIpsecSA(
3300 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01003301 p.vpp_tun_sa_id,
3302 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003303 p.auth_algo_vpp_id,
3304 p.auth_key,
3305 p.crypt_algo_vpp_id,
3306 p.crypt_key,
3307 self.vpp_esp_protocol,
3308 src,
3309 dst,
3310 flags=p.flags,
3311 tun_flags=p.tun_flags,
3312 hop_limit=p.hop_limit,
3313 )
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003314 p.tun_sa_out.add_vpp_config()
3315
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003316 p.tun_sa_in = VppIpsecSA(
3317 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01003318 p.scapy_tun_sa_id,
3319 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003320 p.auth_algo_vpp_id,
3321 p.auth_key,
3322 p.crypt_algo_vpp_id,
3323 p.crypt_key,
3324 self.vpp_esp_protocol,
3325 dst,
3326 src,
3327 flags=p.flags,
3328 )
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003329 p.tun_sa_in.add_vpp_config()
3330
3331 def config_protect(self, p):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003332 p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in])
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003333 p.tun_protect.add_vpp_config()
3334
3335 def config_network(self, p):
3336 p.tun_if = VppIpsecInterface(self)
3337
3338 p.tun_if.add_vpp_config()
3339 p.tun_if.admin_up()
3340 p.tun_if.config_ip4()
3341 p.tun_if.config_ip6()
3342
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003343 r = VppIpRoute(
3344 self,
3345 p.remote_tun_if_host4,
3346 32,
3347 [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)],
3348 )
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003349 r.add_vpp_config()
3350
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003351 p.route = VppIpRoute(
3352 self,
3353 p.remote_tun_if_host,
3354 128,
3355 [
3356 VppRoutePath(
3357 p.tun_if.remote_ip6, 0xFFFFFFFF, proto=DpoProto.DPO_PROTO_IP6
3358 )
3359 ],
3360 )
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003361 p.route.add_vpp_config()
3362
3363 def unconfig_network(self, p):
3364 p.route.remove_vpp_config()
3365 p.tun_if.remove_vpp_config()
3366
3367 def unconfig_protect(self, p):
3368 p.tun_protect.remove_vpp_config()
3369
3370 def unconfig_sa(self, p):
3371 p.tun_sa_out.remove_vpp_config()
3372 p.tun_sa_in.remove_vpp_config()
3373
3374
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00003375@tag_fixme_vpp_workers
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003376class TestIpsecItf6(TemplateIpsec, TemplateIpsecItf6, IpsecTun6):
3377 """IPsec Interface IPv6"""
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003378
3379 def setUp(self):
3380 super(TestIpsecItf6, self).setUp()
3381
3382 self.tun_if = self.pg0
3383
3384 def tearDown(self):
3385 super(TestIpsecItf6, self).tearDown()
3386
Neale Ranns49378f22022-01-10 10:38:43 +00003387 def test_tun_66(self):
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003388 """IPSEC interface IPv6"""
3389
Neale Ranns9ec846c2021-02-09 14:04:02 +00003390 tf = VppEnum.vl_api_tunnel_encap_decap_flags_t
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003391 n_pkts = 127
3392 p = self.ipv6_params
Neale Ranns9ec846c2021-02-09 14:04:02 +00003393 p.inner_hop_limit = 24
3394 p.outer_hop_limit = 23
3395 p.outer_flow_label = 243224
3396 p.tun_flags = tf.TUNNEL_API_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003397
3398 self.config_network(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003399 config_tun_params(
3400 p, self.encryption_type, None, self.pg0.local_ip6, self.pg0.remote_ip6
3401 )
Neale Ranns49378f22022-01-10 10:38:43 +00003402 self.verify_drop_tun_66(p, count=n_pkts)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003403 self.config_sa_tun(p, self.pg0.local_ip6, self.pg0.remote_ip6)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003404 self.config_protect(p)
3405
3406 self.verify_tun_66(p, count=n_pkts)
Ole Troane66443c2021-03-18 11:12:01 +01003407 self.assertEqual(p.tun_if.get_rx_stats(), n_pkts)
3408 self.assertEqual(p.tun_if.get_tx_stats(), n_pkts)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003409
3410 p.tun_if.admin_down()
3411 self.verify_drop_tun_66(p, count=n_pkts)
3412 p.tun_if.admin_up()
3413 self.verify_tun_66(p, count=n_pkts)
3414
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003415 self.assertEqual(p.tun_if.get_rx_stats(), 3 * n_pkts)
3416 self.assertEqual(p.tun_if.get_tx_stats(), 2 * n_pkts)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003417
3418 # it's a v4 packet when its encrypted
3419 self.tun6_encrypt_node_name = "esp4-encrypt-tun"
3420
3421 self.verify_tun_46(p, count=n_pkts)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003422 self.assertEqual(p.tun_if.get_rx_stats(), 4 * n_pkts)
3423 self.assertEqual(p.tun_if.get_tx_stats(), 3 * n_pkts)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003424
3425 self.tun6_encrypt_node_name = "esp6-encrypt-tun"
3426
3427 self.vapi.cli("clear interfaces")
3428
3429 # rekey - create new SAs and update the tunnel protection
3430 np = copy.copy(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003431 np.crypt_key = b"X" + p.crypt_key[1:]
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003432 np.scapy_tun_spi += 100
3433 np.scapy_tun_sa_id += 1
3434 np.vpp_tun_spi += 100
3435 np.vpp_tun_sa_id += 1
3436 np.tun_if.local_spi = p.vpp_tun_spi
3437 np.tun_if.remote_spi = p.scapy_tun_spi
Neale Ranns9ec846c2021-02-09 14:04:02 +00003438 np.inner_hop_limit = 24
3439 np.outer_hop_limit = 128
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003440 np.inner_flow_label = 0xABCDE
3441 np.outer_flow_label = 0xABCDE
Neale Ranns9ec846c2021-02-09 14:04:02 +00003442 np.hop_limit = 128
3443 np.tun_flags = tf.TUNNEL_API_ENCAP_DECAP_FLAG_ENCAP_COPY_FLOW_LABEL
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003444
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003445 self.config_sa_tun(np, self.pg0.local_ip6, self.pg0.remote_ip6)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003446 self.config_protect(np)
3447 self.unconfig_sa(p)
3448
3449 self.verify_tun_66(np, count=n_pkts)
Ole Troane66443c2021-03-18 11:12:01 +01003450 self.assertEqual(p.tun_if.get_rx_stats(), n_pkts)
3451 self.assertEqual(p.tun_if.get_tx_stats(), n_pkts)
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003452
3453 # teardown
3454 self.unconfig_protect(np)
3455 self.unconfig_sa(np)
3456 self.unconfig_network(p)
3457
Brian Russell7a29a2d2021-02-22 18:42:24 +00003458 def test_tun_66_police(self):
3459 """IPSEC interface IPv6 with input policer"""
3460 tf = VppEnum.vl_api_tunnel_encap_decap_flags_t
3461 n_pkts = 127
3462 p = self.ipv6_params
3463 p.inner_hop_limit = 24
3464 p.outer_hop_limit = 23
3465 p.outer_flow_label = 243224
3466 p.tun_flags = tf.TUNNEL_API_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT
3467
3468 self.config_network(p)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003469 self.config_sa_tun(p, self.pg0.local_ip6, self.pg0.remote_ip6)
Brian Russell7a29a2d2021-02-22 18:42:24 +00003470 self.config_protect(p)
3471
3472 action_tx = PolicerAction(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003473 VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0
3474 )
3475 policer = VppPolicer(
3476 self,
3477 "pol1",
3478 80,
3479 0,
3480 1000,
3481 0,
3482 conform_action=action_tx,
3483 exceed_action=action_tx,
3484 violate_action=action_tx,
3485 )
Brian Russell7a29a2d2021-02-22 18:42:24 +00003486 policer.add_vpp_config()
3487
3488 # Start policing on tun
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +02003489 policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, True)
Brian Russell7a29a2d2021-02-22 18:42:24 +00003490
3491 self.verify_tun_66(p, count=n_pkts)
Ole Troane66443c2021-03-18 11:12:01 +01003492 self.assertEqual(p.tun_if.get_rx_stats(), n_pkts)
3493 self.assertEqual(p.tun_if.get_tx_stats(), n_pkts)
Brian Russell7a29a2d2021-02-22 18:42:24 +00003494
3495 stats = policer.get_stats()
3496
3497 # Single rate, 2 colour policer - expect conform, violate but no exceed
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003498 self.assertGreater(stats["conform_packets"], 0)
3499 self.assertEqual(stats["exceed_packets"], 0)
3500 self.assertGreater(stats["violate_packets"], 0)
Brian Russell7a29a2d2021-02-22 18:42:24 +00003501
3502 # Stop policing on tun
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +02003503 policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, False)
Brian Russell7a29a2d2021-02-22 18:42:24 +00003504 self.verify_tun_66(p, count=n_pkts)
3505
3506 # No new policer stats
3507 statsnew = policer.get_stats()
3508 self.assertEqual(stats, statsnew)
3509
3510 # teardown
3511 policer.remove_vpp_config()
3512 self.unconfig_protect(p)
3513 self.unconfig_sa(p)
3514 self.unconfig_network(p)
3515
Neale Rannsdd4ccf22020-06-30 07:47:14 +00003516
Neale Ranns6ba4e412020-10-19 09:59:41 +00003517class TestIpsecMIfEsp4(TemplateIpsec, IpsecTun4):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003518 """Ipsec P2MP ESP v4 tests"""
3519
Neale Ranns6ba4e412020-10-19 09:59:41 +00003520 tun4_encrypt_node_name = "esp4-encrypt-tun"
Neale Ranns8c609af2021-02-25 10:05:32 +00003521 tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"]
Neale Ranns6ba4e412020-10-19 09:59:41 +00003522 encryption_type = ESP
3523
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003524 def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
3525 return [
3526 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
3527 / sa.encrypt(
3528 IP(src=self.pg1.local_ip4, dst=self.pg1.remote_ip4)
3529 / UDP(sport=1144, dport=2233)
3530 / Raw(b"X" * payload_size)
3531 )
3532 for i in range(count)
3533 ]
Neale Ranns6ba4e412020-10-19 09:59:41 +00003534
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003535 def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100):
3536 return [
3537 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
3538 / IP(src="1.1.1.1", dst=dst)
3539 / UDP(sport=1144, dport=2233)
3540 / Raw(b"X" * payload_size)
3541 for i in range(count)
3542 ]
Neale Ranns6ba4e412020-10-19 09:59:41 +00003543
3544 def verify_decrypted(self, p, rxs):
3545 for rx in rxs:
3546 self.assert_equal(rx[Ether].dst, self.pg1.remote_mac)
3547 self.assert_equal(rx[IP].dst, self.pg1.remote_ip4)
3548
3549 def verify_encrypted(self, p, sa, rxs):
3550 for rx in rxs:
3551 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003552 self.assertEqual(
3553 rx[IP].tos, VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_EF << 2
3554 )
Neale Ranns9ec846c2021-02-09 14:04:02 +00003555 self.assertEqual(rx[IP].ttl, p.hop_limit)
Neale Ranns6ba4e412020-10-19 09:59:41 +00003556 pkt = sa.decrypt(rx[IP])
3557 if not pkt.haslayer(IP):
3558 pkt = IP(pkt[Raw].load)
3559 self.assert_packet_checksums_valid(pkt)
3560 e = pkt[IP]
3561 self.assertEqual(e[IP].dst, p.remote_tun_if_host)
3562 except (IndexError, AssertionError):
3563 self.logger.debug(ppp("Unexpected packet:", rx))
3564 try:
3565 self.logger.debug(ppp("Decrypted packet:", pkt))
3566 except:
3567 pass
3568 raise
3569
3570 def setUp(self):
3571 super(TestIpsecMIfEsp4, self).setUp()
3572
3573 N_NHS = 16
3574 self.tun_if = self.pg0
3575 p = self.ipv4_params
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003576 p.tun_if = VppIpsecInterface(
3577 self, mode=(VppEnum.vl_api_tunnel_mode_t.TUNNEL_API_MODE_MP)
3578 )
Neale Ranns6ba4e412020-10-19 09:59:41 +00003579 p.tun_if.add_vpp_config()
3580 p.tun_if.admin_up()
3581 p.tun_if.config_ip4()
Neale Rannscfe949d2020-11-25 19:35:38 +00003582 p.tun_if.unconfig_ip4()
3583 p.tun_if.config_ip4()
Neale Ranns6ba4e412020-10-19 09:59:41 +00003584 p.tun_if.generate_remote_hosts(N_NHS)
3585 self.pg0.generate_remote_hosts(N_NHS)
3586 self.pg0.configure_ipv4_neighbors()
3587
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003588 r_all = AclRule(True, src_prefix="0.0.0.0/0", dst_prefix="0.0.0.0/0", proto=0)
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00003589 a = VppAcl(self, [r_all]).add_vpp_config()
3590
3591 VppAclInterface(self, self.pg0.sw_if_index, [a]).add_vpp_config()
3592 VppAclInterface(self, p.tun_if.sw_if_index, [a]).add_vpp_config()
3593
Neale Ranns6ba4e412020-10-19 09:59:41 +00003594 # setup some SAs for several next-hops on the interface
3595 self.multi_params = []
3596
3597 for ii in range(N_NHS):
3598 p = copy.copy(self.ipv4_params)
3599
3600 p.remote_tun_if_host = "1.1.1.%d" % (ii + 1)
3601 p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii
3602 p.scapy_tun_spi = p.scapy_tun_spi + ii
3603 p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii
3604 p.vpp_tun_spi = p.vpp_tun_spi + ii
3605
3606 p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii
3607 p.scapy_tra_spi = p.scapy_tra_spi + ii
3608 p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii
3609 p.vpp_tra_spi = p.vpp_tra_spi + ii
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003610 p.hop_limit = ii + 10
Neale Ranns041add72020-01-02 04:06:10 +00003611 p.tun_sa_out = VppIpsecSA(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003612 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01003613 p.vpp_tun_sa_id,
3614 p.vpp_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003615 p.auth_algo_vpp_id,
3616 p.auth_key,
3617 p.crypt_algo_vpp_id,
3618 p.crypt_key,
Neale Ranns041add72020-01-02 04:06:10 +00003619 self.vpp_esp_protocol,
3620 self.pg0.local_ip4,
3621 self.pg0.remote_hosts[ii].ip4,
Neale Ranns9ec846c2021-02-09 14:04:02 +00003622 dscp=VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_EF,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003623 hop_limit=p.hop_limit,
3624 )
Neale Ranns6ba4e412020-10-19 09:59:41 +00003625 p.tun_sa_out.add_vpp_config()
3626
Neale Ranns041add72020-01-02 04:06:10 +00003627 p.tun_sa_in = VppIpsecSA(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003628 self,
Arthur de Kerhor0df06b62022-11-16 18:45:24 +01003629 p.scapy_tun_sa_id,
3630 p.scapy_tun_spi,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003631 p.auth_algo_vpp_id,
3632 p.auth_key,
3633 p.crypt_algo_vpp_id,
3634 p.crypt_key,
Neale Ranns041add72020-01-02 04:06:10 +00003635 self.vpp_esp_protocol,
3636 self.pg0.remote_hosts[ii].ip4,
3637 self.pg0.local_ip4,
Neale Ranns9ec846c2021-02-09 14:04:02 +00003638 dscp=VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_EF,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003639 hop_limit=p.hop_limit,
3640 )
Neale Ranns6ba4e412020-10-19 09:59:41 +00003641 p.tun_sa_in.add_vpp_config()
3642
3643 p.tun_protect = VppIpsecTunProtect(
3644 self,
3645 p.tun_if,
3646 p.tun_sa_out,
3647 [p.tun_sa_in],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003648 nh=p.tun_if.remote_hosts[ii].ip4,
3649 )
Neale Ranns6ba4e412020-10-19 09:59:41 +00003650 p.tun_protect.add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003651 config_tun_params(
3652 p,
3653 self.encryption_type,
3654 None,
3655 self.pg0.local_ip4,
3656 self.pg0.remote_hosts[ii].ip4,
3657 )
Neale Ranns6ba4e412020-10-19 09:59:41 +00003658 self.multi_params.append(p)
3659
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00003660 p.via_tun_route = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003661 self,
3662 p.remote_tun_if_host,
3663 32,
3664 [VppRoutePath(p.tun_if.remote_hosts[ii].ip4, p.tun_if.sw_if_index)],
3665 ).add_vpp_config()
Neale Ranns6ba4e412020-10-19 09:59:41 +00003666
3667 p.tun_dst = self.pg0.remote_hosts[ii].ip4
3668
3669 def tearDown(self):
3670 p = self.ipv4_params
3671 p.tun_if.unconfig_ip4()
3672 super(TestIpsecMIfEsp4, self).tearDown()
3673
3674 def test_tun_44(self):
3675 """P2MP IPSEC 44"""
3676 N_PKTS = 63
3677 for p in self.multi_params:
3678 self.verify_tun_44(p, count=N_PKTS)
3679
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00003680 # remove one tunnel protect, the rest should still work
3681 self.multi_params[0].tun_protect.remove_vpp_config()
3682 self.verify_tun_dropped_44(self.multi_params[0], count=N_PKTS)
3683 self.multi_params[0].via_tun_route.remove_vpp_config()
3684 self.verify_tun_dropped_44(self.multi_params[0], count=N_PKTS)
3685
3686 for p in self.multi_params[1:]:
3687 self.verify_tun_44(p, count=N_PKTS)
3688
3689 self.multi_params[0].tun_protect.add_vpp_config()
3690 self.multi_params[0].via_tun_route.add_vpp_config()
3691
3692 for p in self.multi_params:
3693 self.verify_tun_44(p, count=N_PKTS)
3694
Neale Ranns6ba4e412020-10-19 09:59:41 +00003695
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003696class TestIpsecItf6MPLS(TemplateIpsec, TemplateIpsecItf6, IpsecTun6):
3697 """IPsec Interface MPLSoIPv6"""
Neale Ranns4a58e492020-12-21 13:19:10 +00003698
3699 tun6_encrypt_node_name = "esp-mpls-encrypt-tun"
3700
3701 def setUp(self):
3702 super(TestIpsecItf6MPLS, self).setUp()
3703
3704 self.tun_if = self.pg0
3705
3706 def tearDown(self):
3707 super(TestIpsecItf6MPLS, self).tearDown()
3708
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003709 def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=100):
3710 return [
3711 Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac)
3712 / sa.encrypt(
3713 MPLS(label=66, ttl=3)
3714 / IPv6(src=src, dst=dst)
3715 / UDP(sport=1166, dport=2233)
3716 / Raw(b"X" * payload_size)
3717 )
3718 for i in range(count)
3719 ]
Neale Ranns4a58e492020-12-21 13:19:10 +00003720
3721 def verify_encrypted6(self, p, sa, rxs):
3722 for rx in rxs:
3723 try:
3724 pkt = sa.decrypt(rx[IPv6])
3725 if not pkt.haslayer(IPv6):
3726 pkt = IP(pkt[Raw].load)
3727 self.assert_packet_checksums_valid(pkt)
3728 self.assert_equal(pkt[MPLS].label, 66)
3729 self.assert_equal(pkt[IPv6].dst, p.remote_tun_if_host)
3730 except (IndexError, AssertionError):
3731 self.logger.debug(ppp("Unexpected packet:", rx))
3732 try:
3733 self.logger.debug(ppp("Decrypted packet:", pkt))
3734 except:
3735 pass
3736 raise
3737
3738 def test_tun_mpls_o_ip6(self):
3739 """IPSEC interface MPLS over IPv6"""
3740
3741 n_pkts = 127
3742 p = self.ipv6_params
3743 f = FibPathProto
3744
3745 tbl = VppMplsTable(self, 0)
3746 tbl.add_vpp_config()
3747
3748 self.config_network(p)
3749 # deag MPLS routes from the tunnel
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003750 r6 = VppMplsRoute(
3751 self,
3752 66,
3753 1,
3754 [VppRoutePath(self.pg1.remote_ip6, self.pg1.sw_if_index)],
3755 eos_proto=f.FIB_PATH_NH_PROTO_IP6,
3756 ).add_vpp_config()
3757 p.route.modify(
3758 [
3759 VppRoutePath(
3760 p.tun_if.remote_ip6, p.tun_if.sw_if_index, labels=[VppMplsLabel(66)]
3761 )
3762 ]
3763 )
Neale Ranns4a58e492020-12-21 13:19:10 +00003764 p.tun_if.enable_mpls()
3765
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003766 self.config_sa_tun(p, self.pg0.local_ip6, self.pg0.remote_ip6)
Neale Ranns4a58e492020-12-21 13:19:10 +00003767 self.config_protect(p)
3768
3769 self.verify_tun_66(p, count=n_pkts)
3770
3771 # cleanup
3772 p.tun_if.disable_mpls()
3773 self.unconfig_protect(p)
3774 self.unconfig_sa(p)
3775 self.unconfig_network(p)
3776
3777
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003778if __name__ == "__main__":
Klement Sekera31da2e32018-06-24 22:49:55 +02003779 unittest.main(testRunner=VppTestRunner)