Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1 | import unittest |
| 2 | import socket |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 3 | import copy |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 4 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 5 | from scapy.layers.ipsec import SecurityAssociation, ESP |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 6 | from scapy.layers.l2 import Ether, GRE, Dot1Q |
Arthur de Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 7 | from scapy.packet import Raw, bind_layers |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 8 | from scapy.layers.inet import IP, UDP |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 9 | from scapy.layers.inet6 import IPv6 |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 10 | from scapy.contrib.mpls import MPLS |
Dave Wallace | 76a1d05 | 2022-09-27 13:11:53 -0400 | [diff] [blame] | 11 | from framework import tag_fixme_vpp_workers |
juraj.linkes | 1105766 | 2019-07-08 10:22:55 +0200 | [diff] [blame] | 12 | from framework import VppTestRunner |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 13 | from 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 25 | from vpp_gre_interface import VppGreInterface |
| 26 | from vpp_ipip_tun_interface import VppIpIpTunInterface |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 27 | from vpp_ip_route import ( |
| 28 | VppIpRoute, |
| 29 | VppRoutePath, |
| 30 | DpoProto, |
| 31 | VppMplsLabel, |
| 32 | VppMplsTable, |
| 33 | VppMplsRoute, |
| 34 | FibPathProto, |
| 35 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 36 | from vpp_ipsec import VppIpsecSA, VppIpsecTunProtect, VppIpsecInterface |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 37 | from vpp_l2 import VppBridgeDomain, VppBridgeDomainPort |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 38 | from vpp_sub_interface import L2_VTR_OP, VppDot1QSubint |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 39 | from vpp_teib import VppTeib |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 40 | from util import ppp |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 41 | from vpp_papi import VppEnum |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 42 | from vpp_papi_provider import CliFailedCommandError |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 43 | from vpp_acl import AclRule, VppAcl, VppAclInterface |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 44 | from vpp_policer import PolicerAction, VppPolicer, Dir |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 45 | |
| 46 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 47 | def config_tun_params(p, encryption_type, tun_if, src=None, dst=None): |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 48 | ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6} |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 49 | esn_en = bool( |
| 50 | p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN) |
| 51 | ) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 52 | crypt_key = mk_scapy_crypt_key(p) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 53 | 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 Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 60 | if p.nat_header: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 61 | is_default_port = p.nat_header.dport == 4500 |
Arthur de Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 62 | else: |
| 63 | is_default_port = True |
| 64 | |
| 65 | if is_default_port: |
| 66 | outbound_nat_header = p.nat_header |
| 67 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 68 | outbound_nat_header = UDP(sport=p.nat_header.dport, dport=p.nat_header.sport) |
Arthur de Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 69 | bind_layers(UDP, ESP, dport=p.nat_header.dport) |
| 70 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 71 | p.scapy_tun_sa = SecurityAssociation( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 72 | encryption_type, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 73 | spi=p.scapy_tun_spi, |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 74 | crypt_algo=p.crypt_algo, |
| 75 | crypt_key=crypt_key, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 76 | 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 Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 79 | nat_t_header=outbound_nat_header, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 80 | esn_en=esn_en, |
| 81 | ) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 82 | p.vpp_tun_sa = SecurityAssociation( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 83 | encryption_type, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 84 | spi=p.vpp_tun_spi, |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 85 | crypt_algo=p.crypt_algo, |
| 86 | crypt_key=crypt_key, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 87 | 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 Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 90 | nat_t_header=p.nat_header, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 91 | esn_en=esn_en, |
| 92 | ) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 93 | |
| 94 | |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 95 | def config_tra_params(p, encryption_type, tun_if): |
| 96 | ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6} |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 97 | esn_en = bool( |
| 98 | p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN) |
| 99 | ) |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 100 | crypt_key = mk_scapy_crypt_key(p) |
Neale Ranns | f3a6622 | 2020-01-02 05:04:00 +0000 | [diff] [blame] | 101 | p.tun_dst = tun_if.remote_ip |
| 102 | p.tun_src = tun_if.local_ip |
Arthur de Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 103 | |
| 104 | if p.nat_header: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 105 | is_default_port = p.nat_header.dport == 4500 |
Arthur de Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 106 | else: |
| 107 | is_default_port = True |
| 108 | |
| 109 | if is_default_port: |
| 110 | outbound_nat_header = p.nat_header |
| 111 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 112 | outbound_nat_header = UDP(sport=p.nat_header.dport, dport=p.nat_header.sport) |
Arthur de Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 113 | bind_layers(UDP, ESP, dport=p.nat_header.dport) |
| 114 | |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 115 | p.scapy_tun_sa = SecurityAssociation( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 116 | encryption_type, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 117 | spi=p.scapy_tun_spi, |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 118 | crypt_algo=p.crypt_algo, |
| 119 | crypt_key=crypt_key, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 120 | auth_algo=p.auth_algo, |
| 121 | auth_key=p.auth_key, |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 122 | esn_en=esn_en, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 123 | nat_t_header=outbound_nat_header, |
| 124 | ) |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 125 | p.vpp_tun_sa = SecurityAssociation( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 126 | encryption_type, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 127 | spi=p.vpp_tun_spi, |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 128 | crypt_algo=p.crypt_algo, |
| 129 | crypt_key=crypt_key, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 130 | auth_algo=p.auth_algo, |
| 131 | auth_key=p.auth_key, |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 132 | esn_en=esn_en, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 133 | nat_t_header=p.nat_header, |
| 134 | ) |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 135 | |
| 136 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 137 | class TemplateIpsec4TunProtect(object): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 138 | """IPsec IPv4 Tunnel protect""" |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 139 | |
| 140 | encryption_type = ESP |
| 141 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 142 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 143 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 148 | p.tun_sa_out = VppIpsecSA( |
| 149 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 150 | p.vpp_tun_sa_id, |
| 151 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 152 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 159 | p.tun_sa_out.add_vpp_config() |
| 160 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 161 | p.tun_sa_in = VppIpsecSA( |
| 162 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 163 | p.scapy_tun_sa_id, |
| 164 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 165 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 172 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 177 | p.tun_sa_out = VppIpsecSA( |
| 178 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 179 | p.vpp_tun_sa_id, |
| 180 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 181 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 190 | p.tun_sa_out.add_vpp_config() |
| 191 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 192 | p.tun_sa_in = VppIpsecSA( |
| 193 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 194 | p.scapy_tun_sa_id, |
| 195 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 196 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 205 | p.tun_sa_in.add_vpp_config() |
| 206 | |
| 207 | def config_protect(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 208 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 209 | p.tun_protect.add_vpp_config() |
| 210 | |
| 211 | def config_network(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 212 | if hasattr(p, "tun_dst"): |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 213 | tun_dst = p.tun_dst |
| 214 | else: |
| 215 | tun_dst = self.pg0.remote_ip4 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 216 | p.tun_if = VppIpIpTunInterface(self, self.pg0, self.pg0.local_ip4, tun_dst) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 217 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 222 | p.route = VppIpRoute( |
| 223 | self, |
| 224 | p.remote_tun_if_host, |
| 225 | 32, |
| 226 | [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)], |
| 227 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 228 | p.route.add_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 229 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 239 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 253 | class TemplateIpsec4TunIfEsp(TemplateIpsec4TunProtect, TemplateIpsec): |
| 254 | """IPsec tunnel interface tests""" |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 255 | |
| 256 | encryption_type = ESP |
| 257 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 258 | @classmethod |
| 259 | def setUpClass(cls): |
| 260 | super(TemplateIpsec4TunIfEsp, cls).setUpClass() |
| 261 | |
| 262 | @classmethod |
| 263 | def tearDownClass(cls): |
| 264 | super(TemplateIpsec4TunIfEsp, cls).tearDownClass() |
| 265 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 266 | def setUp(self): |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 267 | super(TemplateIpsec4TunIfEsp, self).setUp() |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 268 | |
| 269 | self.tun_if = self.pg0 |
| 270 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 271 | p = self.ipv4_params |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 272 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 273 | self.config_network(p) |
| 274 | self.config_sa_tra(p) |
| 275 | self.config_protect(p) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 276 | |
| 277 | def tearDown(self): |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 278 | super(TemplateIpsec4TunIfEsp, self).tearDown() |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 279 | |
| 280 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 281 | class TemplateIpsec4TunIfEspUdp(TemplateIpsec4TunProtect, TemplateIpsec): |
| 282 | """IPsec UDP tunnel interface tests""" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 283 | |
| 284 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 285 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 286 | 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 Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 296 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 302 | self.assert_equal(rx[UDP].sport, p.nat_header.sport) |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 303 | self.assert_equal(rx[UDP].dport, p.nat_header.dport) |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 304 | |
| 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 320 | def config_sa_tra(self, p): |
| 321 | config_tun_params(p, self.encryption_type, p.tun_if) |
| 322 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 323 | p.tun_sa_out = VppIpsecSA( |
| 324 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 325 | p.vpp_tun_sa_id, |
| 326 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 327 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 336 | p.tun_sa_out.add_vpp_config() |
| 337 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 338 | p.tun_sa_in = VppIpsecSA( |
| 339 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 340 | p.scapy_tun_sa_id, |
| 341 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 342 | 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 Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 347 | flags=p.flags |
| 348 | | VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_IS_INBOUND, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 349 | udp_src=p.nat_header.sport, |
| 350 | udp_dst=p.nat_header.dport, |
| 351 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 352 | p.tun_sa_in.add_vpp_config() |
| 353 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 354 | def setUp(self): |
| 355 | super(TemplateIpsec4TunIfEspUdp, self).setUp() |
| 356 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 357 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 358 | p.flags = VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_UDP_ENCAP |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 359 | p.nat_header = UDP(sport=5454, dport=4500) |
| 360 | |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 361 | self.tun_if = self.pg0 |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 362 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 363 | self.config_network(p) |
| 364 | self.config_sa_tra(p) |
| 365 | self.config_protect(p) |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 366 | |
| 367 | def tearDown(self): |
| 368 | super(TemplateIpsec4TunIfEspUdp, self).tearDown() |
| 369 | |
| 370 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 371 | class TestIpsec4TunIfEsp1(TemplateIpsec4TunIfEsp, IpsecTun4Tests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 372 | """Ipsec ESP - TUN tests""" |
| 373 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 374 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 375 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 376 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 377 | def test_tun_basic64(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 378 | """ipsec 6o4 tunnel basic test""" |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 379 | self.tun4_encrypt_node_name = "esp4-encrypt-tun" |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 380 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 381 | self.verify_tun_64(self.params[socket.AF_INET], count=1) |
| 382 | |
| 383 | def test_tun_burst64(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 384 | """ipsec 6o4 tunnel basic test""" |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 385 | self.tun4_encrypt_node_name = "esp4-encrypt-tun" |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 386 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 387 | self.verify_tun_64(self.params[socket.AF_INET], count=257) |
| 388 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 389 | def test_tun_basic_frag44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 390 | """ipsec 4o4 tunnel frag basic test""" |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 391 | self.tun4_encrypt_node_name = "esp4-encrypt-tun" |
| 392 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 393 | p = self.ipv4_params |
| 394 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 395 | 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 Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 400 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 401 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 402 | class TestIpsec4TunIfEspUdp(TemplateIpsec4TunIfEspUdp, IpsecTun4Tests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 403 | """Ipsec ESP UDP tests""" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 404 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 405 | tun4_input_node = "ipsec4-tun-input" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 406 | |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 407 | def setUp(self): |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 408 | super(TestIpsec4TunIfEspUdp, self).setUp() |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 409 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 410 | def test_keepalive(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 411 | """IPSEC NAT Keepalive""" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 412 | self.verify_keepalive(self.ipv4_params) |
| 413 | |
| 414 | |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 415 | class TestIpsec4TunIfEspUdpGCM(TemplateIpsec4TunIfEspUdp, IpsecTun4Tests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 416 | """Ipsec ESP UDP GCM tests""" |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 417 | |
| 418 | tun4_input_node = "ipsec4-tun-input" |
| 419 | |
| 420 | def setUp(self): |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 421 | super(TestIpsec4TunIfEspUdpGCM, self).setUp() |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 422 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 423 | 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 Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 427 | p.crypt_algo = "AES-GCM" |
| 428 | p.auth_algo = "NULL" |
| 429 | p.crypt_key = b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h" |
| 430 | p.salt = 0 |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 431 | |
| 432 | |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 433 | class 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 Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 451 | class TestIpsec4TunIfEsp2(TemplateIpsec4TunIfEsp, IpsecTcpTests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 452 | """Ipsec ESP - TCP tests""" |
| 453 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 454 | pass |
| 455 | |
| 456 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 457 | class TemplateIpsec6TunProtect(object): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 458 | """IPsec IPv6 Tunnel protect""" |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 459 | |
| 460 | def config_sa_tra(self, p): |
| 461 | config_tun_params(p, self.encryption_type, p.tun_if) |
| 462 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 463 | p.tun_sa_out = VppIpsecSA( |
| 464 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 465 | p.vpp_tun_sa_id, |
| 466 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 467 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 473 | p.tun_sa_out.add_vpp_config() |
| 474 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 475 | p.tun_sa_in = VppIpsecSA( |
| 476 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 477 | p.scapy_tun_sa_id, |
| 478 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 479 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 485 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 490 | p.tun_sa_out = VppIpsecSA( |
| 491 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 492 | p.vpp_tun_sa_id, |
| 493 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 494 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 502 | p.tun_sa_out.add_vpp_config() |
| 503 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 504 | p.tun_sa_in = VppIpsecSA( |
| 505 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 506 | p.scapy_tun_sa_id, |
| 507 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 508 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 516 | p.tun_sa_in.add_vpp_config() |
| 517 | |
| 518 | def config_protect(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 519 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 520 | p.tun_protect.add_vpp_config() |
| 521 | |
| 522 | def config_network(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 523 | if hasattr(p, "tun_dst"): |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 524 | tun_dst = p.tun_dst |
| 525 | else: |
| 526 | tun_dst = self.pg0.remote_ip6 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 527 | p.tun_if = VppIpIpTunInterface(self, self.pg0, self.pg0.local_ip6, tun_dst) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 528 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 533 | 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 543 | p.route.add_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 544 | r = VppIpRoute( |
| 545 | self, |
| 546 | p.remote_tun_if_host4, |
| 547 | 32, |
| 548 | [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)], |
| 549 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 550 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 564 | class TemplateIpsec6TunIfEsp(TemplateIpsec6TunProtect, TemplateIpsec): |
| 565 | """IPsec tunnel interface tests""" |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 566 | |
| 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 575 | self.config_network(p) |
| 576 | self.config_sa_tra(p) |
| 577 | self.config_protect(p) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 578 | |
| 579 | def tearDown(self): |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 580 | super(TemplateIpsec6TunIfEsp, self).tearDown() |
| 581 | |
| 582 | |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 583 | class 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 Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 605 | self.assert_equal(rx[UDP].dport, p.nat_header.dport) |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 606 | |
| 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 Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 629 | p.vpp_tun_sa_id, |
| 630 | p.vpp_tun_spi, |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 631 | 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 Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 644 | p.scapy_tun_sa_id, |
| 645 | p.scapy_tun_spi, |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 646 | 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 Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 651 | flags=p.flags |
| 652 | | VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_IS_INBOUND, |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 653 | 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 | |
| 675 | class 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 | |
| 690 | class 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 710 | class TestIpsec6TunIfEsp1(TemplateIpsec6TunIfEsp, IpsecTun6Tests): |
| 711 | """Ipsec ESP - TUN tests""" |
| 712 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 713 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 714 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 715 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 716 | def test_tun_basic46(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 717 | """ipsec 4o6 tunnel basic test""" |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 718 | self.tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 719 | self.verify_tun_46(self.params[socket.AF_INET6], count=1) |
| 720 | |
| 721 | def test_tun_burst46(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 722 | """ipsec 4o6 tunnel burst test""" |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 723 | self.tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 724 | self.verify_tun_46(self.params[socket.AF_INET6], count=257) |
| 725 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 726 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 727 | class TestIpsec6TunIfEspHandoff(TemplateIpsec6TunIfEsp, IpsecTun6HandoffTests): |
| 728 | """Ipsec ESP 6 Handoff tests""" |
| 729 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 730 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 731 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 732 | |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 733 | def test_tun_handoff_66_police(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 734 | """ESP 6o6 tunnel with policer worker hand-off test""" |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 735 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 742 | 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 Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 755 | policer.add_vpp_config() |
| 756 | |
| 757 | # Start policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 758 | policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, True) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 759 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 765 | 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 Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 776 | 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 Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 783 | if pol_bind == 1: |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 784 | # 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 788 | self.assertEqual(stats0["conform_packets"], 0) |
| 789 | self.assertEqual(stats0["exceed_packets"], 0) |
| 790 | self.assertEqual(stats0["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 791 | else: |
| 792 | # Second pass: both workers should have policed equal amounts |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 793 | self.assertGreater(stats1["conform_packets"], 0) |
| 794 | self.assertEqual(stats1["exceed_packets"], 0) |
| 795 | self.assertGreater(stats1["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 796 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 797 | self.assertGreater(stats0["conform_packets"], 0) |
| 798 | self.assertEqual(stats0["exceed_packets"], 0) |
| 799 | self.assertGreater(stats0["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 800 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 801 | self.assertEqual( |
| 802 | stats0["conform_packets"] + stats0["violate_packets"], |
| 803 | stats1["conform_packets"] + stats1["violate_packets"], |
| 804 | ) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 805 | |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 806 | policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, False) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 807 | policer.remove_vpp_config() |
| 808 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 809 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 810 | class TestIpsec4TunIfEspHandoff(TemplateIpsec4TunIfEsp, IpsecTun4HandoffTests): |
| 811 | """Ipsec ESP 4 Handoff tests""" |
| 812 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 813 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 814 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 815 | |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 816 | def test_tun_handoff_44_police(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 817 | """ESP 4o4 tunnel with policer worker hand-off test""" |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 818 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 825 | 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 Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 838 | policer.add_vpp_config() |
| 839 | |
| 840 | # Start policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 841 | policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, True) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 842 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 848 | 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 Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 859 | 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 Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 866 | if pol_bind == 1: |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 867 | # 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 871 | self.assertEqual(stats0["conform_packets"], 0) |
| 872 | self.assertEqual(stats0["exceed_packets"], 0) |
| 873 | self.assertEqual(stats0["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 874 | else: |
| 875 | # Second pass: both workers should have policed equal amounts |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 876 | self.assertGreater(stats1["conform_packets"], 0) |
| 877 | self.assertEqual(stats1["exceed_packets"], 0) |
| 878 | self.assertGreater(stats1["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 879 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 880 | self.assertGreater(stats0["conform_packets"], 0) |
| 881 | self.assertEqual(stats0["exceed_packets"], 0) |
| 882 | self.assertGreater(stats0["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 883 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 884 | self.assertEqual( |
| 885 | stats0["conform_packets"] + stats0["violate_packets"], |
| 886 | stats1["conform_packets"] + stats1["violate_packets"], |
| 887 | ) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 888 | |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 889 | policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, False) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 890 | policer.remove_vpp_config() |
| 891 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 892 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 893 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 894 | class TestIpsec4MultiTunIfEsp(TemplateIpsec4TunProtect, TemplateIpsec, IpsecTun4): |
| 895 | """IPsec IPv4 Multi Tunnel interface""" |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 896 | |
| 897 | encryption_type = ESP |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 898 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 899 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 900 | |
| 901 | def setUp(self): |
| 902 | super(TestIpsec4MultiTunIfEsp, self).setUp() |
| 903 | |
| 904 | self.tun_if = self.pg0 |
| 905 | |
| 906 | self.multi_params = [] |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 907 | self.pg0.generate_remote_hosts(10) |
| 908 | self.pg0.configure_ipv4_neighbors() |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 909 | |
| 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 923 | p.tun_dst = self.pg0.remote_hosts[ii].ip4 |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 924 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 925 | self.multi_params.append(p) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 926 | self.config_network(p) |
| 927 | self.config_sa_tra(p) |
| 928 | self.config_protect(p) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 929 | |
| 930 | def tearDown(self): |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 931 | super(TestIpsec4MultiTunIfEsp, self).tearDown() |
| 932 | |
| 933 | def test_tun_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 934 | """Multiple IPSEC tunnel interfaces""" |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 935 | for p in self.multi_params: |
| 936 | self.verify_tun_44(p, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 937 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 938 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 939 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 940 | def test_tun_rr_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 941 | """Round-robin packets acrros multiple interface""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 942 | tx = [] |
| 943 | for p in self.multi_params: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 944 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 951 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 958 | tx = tx + self.gen_pkts( |
| 959 | self.pg1, src=self.pg1.remote_ip4, dst=p.remote_tun_if_host |
| 960 | ) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 961 | 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 Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 966 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 967 | class TestIpsec4TunIfEspAll(TemplateIpsec4TunProtect, TemplateIpsec, IpsecTun4): |
| 968 | """IPsec IPv4 Tunnel interface all Algos""" |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 969 | |
| 970 | encryption_type = ESP |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 971 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 972 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 973 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 974 | def setUp(self): |
| 975 | super(TestIpsec4TunIfEspAll, self).setUp() |
| 976 | |
| 977 | self.tun_if = self.pg0 |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 978 | p = self.ipv4_params |
| 979 | |
| 980 | self.config_network(p) |
| 981 | self.config_sa_tra(p) |
| 982 | self.config_protect(p) |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 983 | |
| 984 | def tearDown(self): |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 985 | p = self.ipv4_params |
| 986 | self.unconfig_protect(p) |
| 987 | self.unconfig_network(p) |
| 988 | self.unconfig_sa(p) |
| 989 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 990 | super(TestIpsec4TunIfEspAll, self).tearDown() |
| 991 | |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 992 | def rekey(self, p): |
| 993 | # |
| 994 | # change the key and the SPI |
| 995 | # |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 996 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 997 | p.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 998 | 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 Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1005 | config_tun_params(p, self.encryption_type, p.tun_if) |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 1006 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1007 | p.tun_sa_out = VppIpsecSA( |
| 1008 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1009 | p.vpp_tun_sa_id, |
| 1010 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1011 | 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 Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1021 | p.scapy_tun_sa_id, |
| 1022 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1023 | 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 Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 1031 | p.tun_sa_in.add_vpp_config() |
| 1032 | p.tun_sa_out.add_vpp_config() |
| 1033 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1034 | self.config_protect(p) |
| 1035 | np.tun_sa_out.remove_vpp_config() |
| 1036 | np.tun_sa_in.remove_vpp_config() |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 1037 | self.logger.info(self.vapi.cli("sh ipsec sa")) |
| 1038 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 1039 | def test_tun_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1040 | """IPSEC tunnel all algos""" |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 1041 | |
| 1042 | # foreach VPP crypto engine |
| 1043 | engines = ["ia32", "ipsecmb", "openssl"] |
| 1044 | |
| 1045 | # foreach crypto algorithm |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1046 | 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 Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 1132 | |
| 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1142 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1143 | 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 Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 1149 | |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 1150 | # |
| 1151 | # rekey the tunnel |
| 1152 | # |
| 1153 | self.rekey(p) |
| 1154 | self.verify_tun_44(p, count=127) |
| 1155 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 1156 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1157 | class TestIpsec4TunIfEspNoAlgo(TemplateIpsec4TunProtect, TemplateIpsec, IpsecTun4): |
| 1158 | """IPsec IPv4 Tunnel interface no Algos""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1159 | |
| 1160 | encryption_type = ESP |
| 1161 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1162 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1163 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1164 | def setUp(self): |
| 1165 | super(TestIpsec4TunIfEspNoAlgo, self).setUp() |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1166 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1167 | self.tun_if = self.pg0 |
| 1168 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1169 | p.auth_algo_vpp_id = VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE |
| 1170 | p.auth_algo = "NULL" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1171 | p.auth_key = [] |
| 1172 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1173 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1177 | p.crypt_key = [] |
| 1178 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1179 | def tearDown(self): |
| 1180 | super(TestIpsec4TunIfEspNoAlgo, self).tearDown() |
| 1181 | |
| 1182 | def test_tun_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1183 | """IPSec SA with NULL algos""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1184 | p = self.ipv4_params |
| 1185 | |
| 1186 | self.config_network(p) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1187 | self.config_sa_tra(p) |
| 1188 | self.config_protect(p) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1189 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1190 | tx = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, dst=p.remote_tun_if_host) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1191 | self.send_and_assert_no_replies(self.pg1, tx) |
| 1192 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1193 | self.unconfig_protect(p) |
| 1194 | self.unconfig_sa(p) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1195 | self.unconfig_network(p) |
| 1196 | |
| 1197 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 1198 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1199 | class TestIpsec6MultiTunIfEsp(TemplateIpsec6TunProtect, TemplateIpsec, IpsecTun6): |
| 1200 | """IPsec IPv6 Multi Tunnel interface""" |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1201 | |
| 1202 | encryption_type = ESP |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 1203 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1204 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1205 | |
| 1206 | def setUp(self): |
| 1207 | super(TestIpsec6MultiTunIfEsp, self).setUp() |
| 1208 | |
| 1209 | self.tun_if = self.pg0 |
| 1210 | |
| 1211 | self.multi_params = [] |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1212 | self.pg0.generate_remote_hosts(10) |
| 1213 | self.pg0.configure_ipv6_neighbors() |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1214 | |
| 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 Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1228 | p.tun_dst = self.pg0.remote_hosts[ii].ip6 |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1229 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1230 | self.multi_params.append(p) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1231 | self.config_network(p) |
| 1232 | self.config_sa_tra(p) |
| 1233 | self.config_protect(p) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1234 | |
| 1235 | def tearDown(self): |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1236 | super(TestIpsec6MultiTunIfEsp, self).tearDown() |
| 1237 | |
| 1238 | def test_tun_66(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1239 | """Multiple IPSEC tunnel interfaces""" |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1240 | for p in self.multi_params: |
| 1241 | self.verify_tun_66(p, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 1242 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 1243 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1244 | |
| 1245 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1246 | class TestIpsecGreTebIfEsp(TemplateIpsec, IpsecTun4Tests): |
| 1247 | """Ipsec GRE TEB ESP - TUN tests""" |
| 1248 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1249 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1250 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1251 | encryption_type = ESP |
| 1252 | omac = "00:11:22:33:44:55" |
| 1253 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1254 | 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 Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1267 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1268 | 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 Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1276 | |
| 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1304 | super(TestIpsecGreTebIfEsp, self).setUp() |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1305 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1313 | p.tun_sa_out = VppIpsecSA( |
| 1314 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1315 | p.vpp_tun_sa_id, |
| 1316 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1317 | 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 Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1325 | p.tun_sa_out.add_vpp_config() |
| 1326 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1327 | p.tun_sa_in = VppIpsecSA( |
| 1328 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1329 | p.scapy_tun_sa_id, |
| 1330 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1331 | 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 Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1339 | p.tun_sa_in.add_vpp_config() |
| 1340 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1341 | 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 Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1347 | p.tun_if.add_vpp_config() |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1348 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1349 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1350 | |
| 1351 | p.tun_protect.add_vpp_config() |
| 1352 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1353 | p.tun_if.admin_up() |
| 1354 | p.tun_if.config_ip4() |
| 1355 | config_tun_params(p, self.encryption_type, p.tun_if) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1356 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1357 | VppBridgeDomainPort(self, bd1, p.tun_if).add_vpp_config() |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1358 | VppBridgeDomainPort(self, bd1, self.pg1).add_vpp_config() |
| 1359 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1360 | self.vapi.cli("clear ipsec sa") |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1361 | self.vapi.cli("sh adj") |
| 1362 | self.vapi.cli("sh ipsec tun") |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1363 | |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1364 | def tearDown(self): |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1365 | p = self.ipv4_params |
| 1366 | p.tun_if.unconfig_ip4() |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1367 | super(TestIpsecGreTebIfEsp, self).tearDown() |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1368 | |
| 1369 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1370 | class TestIpsecGreTebVlanIfEsp(TemplateIpsec, IpsecTun4Tests): |
| 1371 | """Ipsec GRE TEB ESP - TUN tests""" |
| 1372 | |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1373 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1374 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1375 | encryption_type = ESP |
| 1376 | omac = "00:11:22:33:44:55" |
| 1377 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1378 | 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 Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1391 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1392 | 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 Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1401 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1442 | sw_if_index=self.pg1_11.sw_if_index, |
| 1443 | vtr_op=L2_VTR_OP.L2_POP_1, |
| 1444 | push_dot1q=11, |
| 1445 | ) |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1446 | self.pg1_11.admin_up() |
| 1447 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1448 | p.tun_sa_out = VppIpsecSA( |
| 1449 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1450 | p.vpp_tun_sa_id, |
| 1451 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1452 | 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 Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1460 | p.tun_sa_out.add_vpp_config() |
| 1461 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1462 | p.tun_sa_in = VppIpsecSA( |
| 1463 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1464 | p.scapy_tun_sa_id, |
| 1465 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1466 | 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 Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1474 | p.tun_sa_in.add_vpp_config() |
| 1475 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1476 | 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 Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1482 | p.tun_if.add_vpp_config() |
| 1483 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1484 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1485 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1505 | class TestIpsecGreTebIfEspTra(TemplateIpsec, IpsecTun4Tests): |
| 1506 | """Ipsec GRE TEB ESP - Tra tests""" |
| 1507 | |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1508 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1509 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1510 | encryption_type = ESP |
| 1511 | omac = "00:11:22:33:44:55" |
| 1512 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1513 | 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 Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1526 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1527 | 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 Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1535 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1572 | p.tun_sa_out = VppIpsecSA( |
| 1573 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1574 | p.vpp_tun_sa_id, |
| 1575 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1576 | 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 Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1582 | p.tun_sa_out.add_vpp_config() |
| 1583 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1584 | p.tun_sa_in = VppIpsecSA( |
| 1585 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1586 | p.scapy_tun_sa_id, |
| 1587 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1588 | 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 Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1594 | p.tun_sa_in.add_vpp_config() |
| 1595 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1596 | 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 Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1602 | p.tun_if.add_vpp_config() |
| 1603 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1604 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1605 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1623 | class TestIpsecGreTebUdpIfEspTra(TemplateIpsec, IpsecTun4Tests): |
| 1624 | """Ipsec GRE TEB UDP ESP - Tra tests""" |
| 1625 | |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1626 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1627 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1628 | encryption_type = ESP |
| 1629 | omac = "00:11:22:33:44:55" |
| 1630 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1631 | 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 Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1644 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1645 | 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 Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1653 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1690 | p.flags = VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_UDP_ENCAP |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1691 | p.nat_header = UDP(sport=5454, dport=4545) |
| 1692 | |
| 1693 | bd1 = VppBridgeDomain(self, 1) |
| 1694 | bd1.add_vpp_config() |
| 1695 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1696 | p.tun_sa_out = VppIpsecSA( |
| 1697 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1698 | p.vpp_tun_sa_id, |
| 1699 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1700 | 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 Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1709 | p.tun_sa_out.add_vpp_config() |
| 1710 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1711 | p.tun_sa_in = VppIpsecSA( |
| 1712 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1713 | p.scapy_tun_sa_id, |
| 1714 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1715 | 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 Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1726 | p.tun_sa_in.add_vpp_config() |
| 1727 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1728 | 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 Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1734 | p.tun_if.add_vpp_config() |
| 1735 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1736 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1737 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1756 | class TestIpsecGreIfEsp(TemplateIpsec, IpsecTun4Tests): |
| 1757 | """Ipsec GRE ESP - TUN tests""" |
| 1758 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1759 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1760 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1761 | encryption_type = ESP |
| 1762 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1763 | 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1775 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1776 | 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1784 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1820 | p.tun_sa_out = VppIpsecSA( |
| 1821 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1822 | p.vpp_tun_sa_id, |
| 1823 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1824 | 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1832 | p.tun_sa_out.add_vpp_config() |
| 1833 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1834 | p.tun_sa_in = VppIpsecSA( |
| 1835 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1836 | p.scapy_tun_sa_id, |
| 1837 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1838 | 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1846 | p.tun_sa_in.add_vpp_config() |
| 1847 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1848 | p.tun_if = VppGreInterface(self, self.pg0.local_ip4, self.pg0.remote_ip4) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1849 | p.tun_if.add_vpp_config() |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1850 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1851 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1852 | p.tun_protect.add_vpp_config() |
| 1853 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1854 | p.tun_if.admin_up() |
| 1855 | p.tun_if.config_ip4() |
| 1856 | config_tun_params(p, self.encryption_type, p.tun_if) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1857 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1858 | VppIpRoute( |
| 1859 | self, "1.1.1.2", 32, [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)] |
| 1860 | ).add_vpp_config() |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1861 | |
| 1862 | def tearDown(self): |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1863 | p = self.ipv4_params |
| 1864 | p.tun_if.unconfig_ip4() |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1865 | super(TestIpsecGreIfEsp, self).tearDown() |
| 1866 | |
| 1867 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1868 | class TestIpsecGreIfEspTra(TemplateIpsec, IpsecTun4Tests): |
| 1869 | """Ipsec GRE ESP - TRA tests""" |
| 1870 | |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1871 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1872 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1873 | encryption_type = ESP |
| 1874 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1875 | 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 Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1887 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1888 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1899 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1900 | 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 Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1908 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1939 | p.tun_sa_out = VppIpsecSA( |
| 1940 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1941 | p.vpp_tun_sa_id, |
| 1942 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1943 | 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 Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1949 | p.tun_sa_out.add_vpp_config() |
| 1950 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1951 | p.tun_sa_in = VppIpsecSA( |
| 1952 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1953 | p.scapy_tun_sa_id, |
| 1954 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1955 | 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 Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1961 | p.tun_sa_in.add_vpp_config() |
| 1962 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1963 | p.tun_if = VppGreInterface(self, self.pg0.local_ip4, self.pg0.remote_ip4) |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1964 | p.tun_if.add_vpp_config() |
| 1965 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1966 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1967 | p.tun_protect.add_vpp_config() |
| 1968 | |
| 1969 | p.tun_if.admin_up() |
| 1970 | p.tun_if.config_ip4() |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1971 | config_tra_params(p, self.encryption_type, p.tun_if) |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1972 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1973 | VppIpRoute( |
| 1974 | self, "1.1.1.2", 32, [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)] |
| 1975 | ).add_vpp_config() |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1976 | |
| 1977 | def tearDown(self): |
| 1978 | p = self.ipv4_params |
| 1979 | p.tun_if.unconfig_ip4() |
| 1980 | super(TestIpsecGreIfEspTra, self).tearDown() |
| 1981 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1982 | def test_gre_non_ip(self): |
| 1983 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1984 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1990 | self.send_and_assert_no_replies(self.tun_if, tx) |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1991 | node_name = "/err/%s/unsup_payload" % self.tun4_decrypt_node_name[0] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1992 | self.assertEqual(1, self.statistics.get_err_counter(node_name)) |
| 1993 | |
| 1994 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1995 | class TestIpsecGre6IfEspTra(TemplateIpsec, IpsecTun6Tests): |
| 1996 | """Ipsec GRE ESP - TRA tests""" |
| 1997 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1998 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1999 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2000 | encryption_type = ESP |
| 2001 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2002 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2014 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2015 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2023 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2057 | p.tun_sa_out = VppIpsecSA( |
| 2058 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2059 | p.vpp_tun_sa_id, |
| 2060 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2061 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2067 | p.tun_sa_out.add_vpp_config() |
| 2068 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2069 | p.tun_sa_in = VppIpsecSA( |
| 2070 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2071 | p.scapy_tun_sa_id, |
| 2072 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2073 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2079 | p.tun_sa_in.add_vpp_config() |
| 2080 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2081 | p.tun_if = VppGreInterface(self, self.pg0.local_ip6, self.pg0.remote_ip6) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2082 | p.tun_if.add_vpp_config() |
| 2083 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2084 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2085 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2091 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2101 | 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 Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 2108 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2109 | class TestIpsecMGreIfEspTra4(TemplateIpsec, IpsecTun4): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2110 | """Ipsec mGRE ESP v4 TRA tests""" |
| 2111 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2112 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2113 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2114 | encryption_type = ESP |
| 2115 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2116 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2128 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2129 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2137 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2167 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2173 | 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 Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 2183 | for ii in range(N_NHS): |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2184 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2196 | p.tun_sa_out = VppIpsecSA( |
| 2197 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2198 | p.vpp_tun_sa_id, |
| 2199 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2200 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2206 | p.tun_sa_out.add_vpp_config() |
| 2207 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2208 | p.tun_sa_in = VppIpsecSA( |
| 2209 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2210 | p.scapy_tun_sa_id, |
| 2211 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2212 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2218 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2225 | nh=p.tun_if.remote_hosts[ii].ip4, |
| 2226 | ) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2227 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2231 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2237 | |
| 2238 | # in this v4 variant add the teibs after the protect |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2239 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2245 | 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 | |
| 2264 | class TestIpsecMGreIfEspTra6(TemplateIpsec, IpsecTun6): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2265 | """Ipsec mGRE ESP v6 TRA tests""" |
| 2266 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2267 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2268 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2269 | encryption_type = ESP |
| 2270 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2271 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2283 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2284 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2292 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2324 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2330 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2353 | p.tun_sa_out = VppIpsecSA( |
| 2354 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2355 | p.vpp_tun_sa_id, |
| 2356 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2357 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2363 | p.tun_sa_out.add_vpp_config() |
| 2364 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2365 | p.tun_sa_in = VppIpsecSA( |
| 2366 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2367 | p.scapy_tun_sa_id, |
| 2368 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2369 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2375 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2379 | VppTeib( |
| 2380 | self, p.tun_if, p.tun_if.remote_hosts[ii].ip6, p.tun_dst |
| 2381 | ).add_vpp_config() |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2382 | |
| 2383 | p.tun_protect = VppIpsecTunProtect( |
| 2384 | self, |
| 2385 | p.tun_if, |
| 2386 | p.tun_sa_out, |
| 2387 | [p.tun_sa_in], |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2388 | nh=p.tun_if.remote_hosts[ii].ip6, |
| 2389 | ) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2390 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2394 | 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 Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2400 | 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 Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 2417 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2418 | class TestIpsec4TunProtect(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4): |
| 2419 | """IPsec IPv4 Tunnel protect - transport mode""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2420 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2421 | 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 Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2439 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2440 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2441 | |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 2442 | self.vapi.cli("clear ipsec sa") |
| 2443 | self.verify_tun_64(p, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2444 | self.assertEqual(p.tun_if.get_rx_stats(), 254) |
| 2445 | self.assertEqual(p.tun_if.get_tx_stats(), 254) |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 2446 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2447 | # rekey - create new SAs and update the tunnel protection |
| 2448 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2449 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2450 | 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 Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2462 | self.assertEqual(p.tun_if.get_rx_stats(), 381) |
| 2463 | self.assertEqual(p.tun_if.get_tx_stats(), 381) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2464 | |
| 2465 | # teardown |
| 2466 | self.unconfig_protect(np) |
| 2467 | self.unconfig_sa(np) |
| 2468 | self.unconfig_network(p) |
| 2469 | |
| 2470 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 2471 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2472 | class TestIpsec4TunProtectUdp(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4): |
| 2473 | """IPsec IPv4 Tunnel protect - transport mode""" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 2474 | |
| 2475 | def setUp(self): |
| 2476 | super(TestIpsec4TunProtectUdp, self).setUp() |
| 2477 | |
| 2478 | self.tun_if = self.pg0 |
| 2479 | |
| 2480 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2481 | p.flags = VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_UDP_ENCAP |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 2482 | p.nat_header = UDP(sport=4500, dport=4500) |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 2483 | 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 Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 2494 | 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 Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 2501 | 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 Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2507 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2508 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 2509 | |
| 2510 | def test_keepalive(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2511 | """IPSEC NAT Keepalive""" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 2512 | self.verify_keepalive(self.ipv4_params) |
| 2513 | |
| 2514 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 2515 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2516 | class TestIpsec4TunProtectTun(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4): |
| 2517 | """IPsec IPv4 Tunnel protect - tunnel mode""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2518 | |
| 2519 | encryption_type = ESP |
| 2520 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2521 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2522 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2531 | 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2542 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2543 | 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2551 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2579 | """IPSEC tunnel protect""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2580 | |
| 2581 | p = self.ipv4_params |
| 2582 | |
| 2583 | self.config_network(p) |
| 2584 | self.config_sa_tun(p) |
| 2585 | self.config_protect(p) |
| 2586 | |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 2587 | # also add an output features on the tunnel and physical interface |
| 2588 | # so we test they still work |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2589 | r_all = AclRule(True, src_prefix="0.0.0.0/0", dst_prefix="0.0.0.0/0", proto=0) |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 2590 | 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2595 | self.verify_tun_44(p, count=127) |
| 2596 | |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2597 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2598 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2599 | |
| 2600 | # rekey - create new SAs and update the tunnel protection |
| 2601 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2602 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2603 | 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 Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2615 | self.assertEqual(p.tun_if.get_rx_stats(), 254) |
| 2616 | self.assertEqual(p.tun_if.get_tx_stats(), 254) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2617 | |
| 2618 | # teardown |
| 2619 | self.unconfig_protect(np) |
| 2620 | self.unconfig_sa(np) |
| 2621 | self.unconfig_network(p) |
| 2622 | |
| 2623 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2624 | class TestIpsec4TunProtectTunDrop(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4): |
| 2625 | """IPsec IPv4 Tunnel protect - tunnel mode - drop""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2626 | |
| 2627 | encryption_type = ESP |
| 2628 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2629 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2630 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2639 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2650 | |
| 2651 | def test_tun_drop_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2652 | """IPSEC tunnel protect bogus tunnel header""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2653 | |
| 2654 | p = self.ipv4_params |
| 2655 | |
| 2656 | self.config_network(p) |
| 2657 | self.config_sa_tun(p) |
| 2658 | self.config_protect(p) |
| 2659 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2660 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2668 | 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 Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 2676 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2677 | class TestIpsec6TunProtect(TemplateIpsec, TemplateIpsec6TunProtect, IpsecTun6): |
| 2678 | """IPsec IPv6 Tunnel protect - transport mode""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2679 | |
| 2680 | encryption_type = ESP |
| 2681 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2682 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2683 | |
| 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2693 | """IPSEC tunnel protect 6o6""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2694 | |
| 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 Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2702 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2703 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2704 | |
| 2705 | # rekey - create new SAs and update the tunnel protection |
| 2706 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2707 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2708 | 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 Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2720 | self.assertEqual(p.tun_if.get_rx_stats(), 254) |
| 2721 | self.assertEqual(p.tun_if.get_tx_stats(), 254) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2722 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2723 | # bounce the interface state |
| 2724 | p.tun_if.admin_down() |
| 2725 | self.verify_drop_tun_66(np, count=127) |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 2726 | node = "/err/ipsec6-tun-input/disabled" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2727 | 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2731 | # 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2736 | np3.crypt_key = b"Z" + p.crypt_key[1:] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2737 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2747 | p.tun_protect.update_vpp_config(np.tun_sa_out, [np.tun_sa_in, np3.tun_sa_in]) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2748 | self.verify_tun_66(np, np, count=127) |
| 2749 | self.verify_tun_66(np3, np, count=127) |
| 2750 | |
| 2751 | # step 2; |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2752 | p.tun_protect.update_vpp_config(np3.tun_sa_out, [np.tun_sa_in, np3.tun_sa_in]) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2753 | self.verify_tun_66(np, np3, count=127) |
| 2754 | self.verify_tun_66(np3, np3, count=127) |
| 2755 | |
| 2756 | # step 1; |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2757 | p.tun_protect.update_vpp_config(np3.tun_sa_out, [np3.tun_sa_in]) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2758 | self.verify_tun_66(np3, np3, count=127) |
Neale Ranns | 49378f2 | 2022-01-10 10:38:43 +0000 | [diff] [blame] | 2759 | self.verify_drop_tun_rx_66(np, count=127) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2760 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2761 | self.assertEqual(p.tun_if.get_rx_stats(), 127 * 9) |
| 2762 | self.assertEqual(p.tun_if.get_tx_stats(), 127 * 8) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2763 | self.unconfig_sa(np) |
| 2764 | |
| 2765 | # teardown |
| 2766 | self.unconfig_protect(np3) |
| 2767 | self.unconfig_sa(np3) |
| 2768 | self.unconfig_network(p) |
| 2769 | |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 2770 | def test_tun_46(self): |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2771 | """IPSEC tunnel protect 4o6""" |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 2772 | |
| 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 Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2780 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2781 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 2782 | |
| 2783 | # teardown |
| 2784 | self.unconfig_protect(p) |
| 2785 | self.unconfig_sa(p) |
| 2786 | self.unconfig_network(p) |
| 2787 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2788 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 2789 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2790 | class TestIpsec6TunProtectTun(TemplateIpsec, TemplateIpsec6TunProtect, IpsecTun6): |
| 2791 | """IPsec IPv6 Tunnel protect - tunnel mode""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2792 | |
| 2793 | encryption_type = ESP |
| 2794 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2795 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2796 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2805 | 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2816 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2817 | 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 Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2825 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2853 | """IPSEC tunnel protect""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2854 | |
| 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 Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2863 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2864 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2865 | |
| 2866 | # rekey - create new SAs and update the tunnel protection |
| 2867 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2868 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2869 | 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 Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2881 | self.assertEqual(p.tun_if.get_rx_stats(), 254) |
| 2882 | self.assertEqual(p.tun_if.get_tx_stats(), 254) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2883 | |
| 2884 | # teardown |
| 2885 | self.unconfig_protect(np) |
| 2886 | self.unconfig_sa(np) |
| 2887 | self.unconfig_network(p) |
| 2888 | |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 2889 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2890 | class TestIpsec6TunProtectTunDrop(TemplateIpsec, TemplateIpsec6TunProtect, IpsecTun6): |
| 2891 | """IPsec IPv6 Tunnel protect - tunnel mode - drop""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2892 | |
| 2893 | encryption_type = ESP |
| 2894 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2895 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2896 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2905 | def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2906 | # the IP destination of the revelaed packet does not match |
| 2907 | # that assigned to the tunnel |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2908 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2918 | |
| 2919 | def test_tun_drop_66(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2920 | """IPSEC 6 tunnel protect bogus tunnel header""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2921 | |
| 2922 | p = self.ipv6_params |
| 2923 | |
| 2924 | self.config_network(p) |
| 2925 | self.config_sa_tun(p) |
| 2926 | self.config_protect(p) |
| 2927 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2928 | 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 Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2936 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2943 | class TemplateIpsecItf4(object): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2944 | """IPsec Interface IPv4""" |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2945 | |
| 2946 | encryption_type = ESP |
| 2947 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2948 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2949 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2954 | p.tun_sa_out = VppIpsecSA( |
| 2955 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2956 | p.vpp_tun_sa_id, |
| 2957 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2958 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2967 | p.tun_sa_out.add_vpp_config() |
| 2968 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2969 | p.tun_sa_in = VppIpsecSA( |
| 2970 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2971 | p.scapy_tun_sa_id, |
| 2972 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2973 | 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 Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 2980 | flags=p.flags |
| 2981 | | VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_IS_INBOUND, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2982 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2983 | p.tun_sa_in.add_vpp_config() |
| 2984 | |
| 2985 | def config_protect(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2986 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2987 | p.tun_protect.add_vpp_config() |
| 2988 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2989 | def config_network(self, p, instance=0xFFFFFFFF): |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 2990 | p.tun_if = VppIpsecInterface(self, instance=instance) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2991 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2997 | p.route = VppIpRoute( |
| 2998 | self, |
| 2999 | p.remote_tun_if_host, |
| 3000 | 32, |
| 3001 | [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)], |
| 3002 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3003 | p.route.add_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3004 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3014 | 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 Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 3028 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3029 | class TestIpsecItf4(TemplateIpsec, TemplateIpsecItf4, IpsecTun4): |
| 3030 | """IPsec Interface IPv4""" |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3031 | |
| 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 Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 3040 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3052 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3059 | config_tun_params( |
| 3060 | p, self.encryption_type, None, self.pg0.local_ip4, self.pg0.remote_ip4 |
| 3061 | ) |
Neale Ranns | 49378f2 | 2022-01-10 10:38:43 +0000 | [diff] [blame] | 3062 | self.verify_tun_dropped_44(p, count=n_pkts) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3063 | self.config_sa_tun(p, self.pg0.local_ip4, self.pg0.remote_ip4) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3064 | self.config_protect(p) |
| 3065 | |
| 3066 | self.verify_tun_44(p, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3067 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3068 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3069 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3075 | self.assertEqual(p.tun_if.get_rx_stats(), 3 * n_pkts) |
| 3076 | self.assertEqual(p.tun_if.get_tx_stats(), 2 * n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3077 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3082 | self.assertEqual(p.tun_if.get_rx_stats(), 4 * n_pkts) |
| 3083 | self.assertEqual(p.tun_if.get_tx_stats(), 3 * n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3084 | |
| 3085 | self.tun4_encrypt_node_name = "esp4-encrypt-tun" |
| 3086 | |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 3087 | # 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3101 | self.vapi.cli("clear interfaces") |
| 3102 | |
| 3103 | # rekey - create new SAs and update the tunnel protection |
| 3104 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3105 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3106 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3113 | self.config_sa_tun(np, self.pg0.local_ip4, self.pg0.remote_ip4) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3114 | self.config_protect(np) |
| 3115 | self.unconfig_sa(p) |
| 3116 | |
| 3117 | self.verify_tun_44(np, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3118 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3119 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3120 | |
| 3121 | # teardown |
| 3122 | self.unconfig_protect(np) |
| 3123 | self.unconfig_sa(np) |
| 3124 | self.unconfig_network(p) |
| 3125 | |
Neale Ranns | 970187b | 2020-10-07 13:58:56 +0000 | [diff] [blame] | 3126 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3132 | 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 Ranns | 970187b | 2020-10-07 13:58:56 +0000 | [diff] [blame] | 3136 | p.crypt_algo = "NULL" |
| 3137 | p.auth_algo = "NULL" |
| 3138 | |
| 3139 | self.config_network(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3140 | self.config_sa_tun(p, self.pg0.local_ip4, self.pg0.remote_ip4) |
Neale Ranns | 970187b | 2020-10-07 13:58:56 +0000 | [diff] [blame] | 3141 | self.config_protect(p) |
| 3142 | |
Neale Ranns | 49378f2 | 2022-01-10 10:38:43 +0000 | [diff] [blame] | 3143 | self.logger.info(self.vapi.cli("sh ipsec sa")) |
Neale Ranns | 970187b | 2020-10-07 13:58:56 +0000 | [diff] [blame] | 3144 | self.verify_tun_44(p, count=n_pkts) |
| 3145 | |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 3146 | # teardown |
| 3147 | self.unconfig_protect(p) |
| 3148 | self.unconfig_sa(p) |
| 3149 | self.unconfig_network(p) |
| 3150 | |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3151 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3157 | self.config_sa_tun(p, self.pg0.local_ip4, self.pg0.remote_ip4) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3158 | self.config_protect(p) |
| 3159 | |
| 3160 | action_tx = PolicerAction( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3161 | 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 Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3174 | policer.add_vpp_config() |
| 3175 | |
| 3176 | # Start policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 3177 | policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, True) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3178 | |
| 3179 | self.verify_tun_44(p, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3180 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3181 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3182 | |
| 3183 | stats = policer.get_stats() |
| 3184 | |
| 3185 | # Single rate, 2 colour policer - expect conform, violate but no exceed |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3186 | self.assertGreater(stats["conform_packets"], 0) |
| 3187 | self.assertEqual(stats["exceed_packets"], 0) |
| 3188 | self.assertGreater(stats["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3189 | |
| 3190 | # Stop policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 3191 | policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, False) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3192 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3204 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3205 | class TestIpsecItf4MPLS(TemplateIpsec, TemplateIpsecItf4, IpsecTun4): |
| 3206 | """IPsec Interface MPLSoIPv4""" |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3207 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3218 | 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 Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3229 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3259 | 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 Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3269 | p.tun_if.enable_mpls() |
| 3270 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3271 | self.config_sa_tun(p, self.pg0.local_ip4, self.pg0.remote_ip4) |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3272 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3283 | class TemplateIpsecItf6(object): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3284 | """IPsec Interface IPv6""" |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3285 | |
| 3286 | encryption_type = ESP |
| 3287 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 3288 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3289 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3294 | if not hasattr(p, "tun_flags"): |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3295 | p.tun_flags = None |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3296 | if not hasattr(p, "hop_limit"): |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3297 | p.hop_limit = 255 |
| 3298 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3299 | p.tun_sa_out = VppIpsecSA( |
| 3300 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 3301 | p.vpp_tun_sa_id, |
| 3302 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3303 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3314 | p.tun_sa_out.add_vpp_config() |
| 3315 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3316 | p.tun_sa_in = VppIpsecSA( |
| 3317 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 3318 | p.scapy_tun_sa_id, |
| 3319 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3320 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3329 | p.tun_sa_in.add_vpp_config() |
| 3330 | |
| 3331 | def config_protect(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3332 | p.tun_protect = VppIpsecTunProtect(self, p.tun_if, p.tun_sa_out, [p.tun_sa_in]) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3333 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3343 | r = VppIpRoute( |
| 3344 | self, |
| 3345 | p.remote_tun_if_host4, |
| 3346 | 32, |
| 3347 | [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)], |
| 3348 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3349 | r.add_vpp_config() |
| 3350 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3351 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3361 | 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 Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 3375 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3376 | class TestIpsecItf6(TemplateIpsec, TemplateIpsecItf6, IpsecTun6): |
| 3377 | """IPsec Interface IPv6""" |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3378 | |
| 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 Ranns | 49378f2 | 2022-01-10 10:38:43 +0000 | [diff] [blame] | 3387 | def test_tun_66(self): |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3388 | """IPSEC interface IPv6""" |
| 3389 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3390 | tf = VppEnum.vl_api_tunnel_encap_decap_flags_t |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3391 | n_pkts = 127 |
| 3392 | p = self.ipv6_params |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3393 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3397 | |
| 3398 | self.config_network(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3399 | config_tun_params( |
| 3400 | p, self.encryption_type, None, self.pg0.local_ip6, self.pg0.remote_ip6 |
| 3401 | ) |
Neale Ranns | 49378f2 | 2022-01-10 10:38:43 +0000 | [diff] [blame] | 3402 | self.verify_drop_tun_66(p, count=n_pkts) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3403 | self.config_sa_tun(p, self.pg0.local_ip6, self.pg0.remote_ip6) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3404 | self.config_protect(p) |
| 3405 | |
| 3406 | self.verify_tun_66(p, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3407 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3408 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3409 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3415 | self.assertEqual(p.tun_if.get_rx_stats(), 3 * n_pkts) |
| 3416 | self.assertEqual(p.tun_if.get_tx_stats(), 2 * n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3417 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3422 | self.assertEqual(p.tun_if.get_rx_stats(), 4 * n_pkts) |
| 3423 | self.assertEqual(p.tun_if.get_tx_stats(), 3 * n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3424 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3431 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3432 | 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 Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3438 | np.inner_hop_limit = 24 |
| 3439 | np.outer_hop_limit = 128 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3440 | np.inner_flow_label = 0xABCDE |
| 3441 | np.outer_flow_label = 0xABCDE |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3442 | np.hop_limit = 128 |
| 3443 | np.tun_flags = tf.TUNNEL_API_ENCAP_DECAP_FLAG_ENCAP_COPY_FLOW_LABEL |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3444 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3445 | self.config_sa_tun(np, self.pg0.local_ip6, self.pg0.remote_ip6) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3446 | self.config_protect(np) |
| 3447 | self.unconfig_sa(p) |
| 3448 | |
| 3449 | self.verify_tun_66(np, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3450 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3451 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3452 | |
| 3453 | # teardown |
| 3454 | self.unconfig_protect(np) |
| 3455 | self.unconfig_sa(np) |
| 3456 | self.unconfig_network(p) |
| 3457 | |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3458 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3469 | self.config_sa_tun(p, self.pg0.local_ip6, self.pg0.remote_ip6) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3470 | self.config_protect(p) |
| 3471 | |
| 3472 | action_tx = PolicerAction( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3473 | 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 Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3486 | policer.add_vpp_config() |
| 3487 | |
| 3488 | # Start policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 3489 | policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, True) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3490 | |
| 3491 | self.verify_tun_66(p, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3492 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3493 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3494 | |
| 3495 | stats = policer.get_stats() |
| 3496 | |
| 3497 | # Single rate, 2 colour policer - expect conform, violate but no exceed |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3498 | self.assertGreater(stats["conform_packets"], 0) |
| 3499 | self.assertEqual(stats["exceed_packets"], 0) |
| 3500 | self.assertGreater(stats["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3501 | |
| 3502 | # Stop policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 3503 | policer.apply_vpp_config(p.tun_if.sw_if_index, Dir.RX, False) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3504 | 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 Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3516 | |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3517 | class TestIpsecMIfEsp4(TemplateIpsec, IpsecTun4): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3518 | """Ipsec P2MP ESP v4 tests""" |
| 3519 | |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3520 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 3521 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3522 | encryption_type = ESP |
| 3523 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3524 | 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 Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3534 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3535 | 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 Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3543 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3552 | self.assertEqual( |
| 3553 | rx[IP].tos, VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_EF << 2 |
| 3554 | ) |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3555 | self.assertEqual(rx[IP].ttl, p.hop_limit) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3556 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3576 | p.tun_if = VppIpsecInterface( |
| 3577 | self, mode=(VppEnum.vl_api_tunnel_mode_t.TUNNEL_API_MODE_MP) |
| 3578 | ) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3579 | p.tun_if.add_vpp_config() |
| 3580 | p.tun_if.admin_up() |
| 3581 | p.tun_if.config_ip4() |
Neale Ranns | cfe949d | 2020-11-25 19:35:38 +0000 | [diff] [blame] | 3582 | p.tun_if.unconfig_ip4() |
| 3583 | p.tun_if.config_ip4() |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3584 | p.tun_if.generate_remote_hosts(N_NHS) |
| 3585 | self.pg0.generate_remote_hosts(N_NHS) |
| 3586 | self.pg0.configure_ipv4_neighbors() |
| 3587 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3588 | r_all = AclRule(True, src_prefix="0.0.0.0/0", dst_prefix="0.0.0.0/0", proto=0) |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 3589 | 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 Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3594 | # 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3610 | p.hop_limit = ii + 10 |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 3611 | p.tun_sa_out = VppIpsecSA( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3612 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 3613 | p.vpp_tun_sa_id, |
| 3614 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3615 | p.auth_algo_vpp_id, |
| 3616 | p.auth_key, |
| 3617 | p.crypt_algo_vpp_id, |
| 3618 | p.crypt_key, |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 3619 | self.vpp_esp_protocol, |
| 3620 | self.pg0.local_ip4, |
| 3621 | self.pg0.remote_hosts[ii].ip4, |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3622 | dscp=VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_EF, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3623 | hop_limit=p.hop_limit, |
| 3624 | ) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3625 | p.tun_sa_out.add_vpp_config() |
| 3626 | |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 3627 | p.tun_sa_in = VppIpsecSA( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3628 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 3629 | p.scapy_tun_sa_id, |
| 3630 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3631 | p.auth_algo_vpp_id, |
| 3632 | p.auth_key, |
| 3633 | p.crypt_algo_vpp_id, |
| 3634 | p.crypt_key, |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 3635 | self.vpp_esp_protocol, |
| 3636 | self.pg0.remote_hosts[ii].ip4, |
| 3637 | self.pg0.local_ip4, |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3638 | dscp=VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_EF, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3639 | hop_limit=p.hop_limit, |
| 3640 | ) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3641 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3648 | nh=p.tun_if.remote_hosts[ii].ip4, |
| 3649 | ) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3650 | p.tun_protect.add_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3651 | 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 Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3658 | self.multi_params.append(p) |
| 3659 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 3660 | p.via_tun_route = VppIpRoute( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3661 | 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 Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3666 | |
| 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 Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 3680 | # 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 Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3695 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3696 | class TestIpsecItf6MPLS(TemplateIpsec, TemplateIpsecItf6, IpsecTun6): |
| 3697 | """IPsec Interface MPLSoIPv6""" |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3698 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3709 | 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 Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3720 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3750 | 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 Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3764 | p.tun_if.enable_mpls() |
| 3765 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3766 | self.config_sa_tun(p, self.pg0.local_ip6, self.pg0.remote_ip6) |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3767 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3778 | if __name__ == "__main__": |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 3779 | unittest.main(testRunner=VppTestRunner) |