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 | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 11 | from asfframework import VppTestRunner, tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 12 | from template_ipsec import ( |
| 13 | TemplateIpsec, |
| 14 | IpsecTun4Tests, |
| 15 | IpsecTun6Tests, |
| 16 | IpsecTun4, |
| 17 | IpsecTun6, |
| 18 | IpsecTcpTests, |
| 19 | mk_scapy_crypt_key, |
| 20 | IpsecTun6HandoffTests, |
| 21 | IpsecTun4HandoffTests, |
| 22 | config_tun_params, |
| 23 | ) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 24 | from vpp_gre_interface import VppGreInterface |
| 25 | from vpp_ipip_tun_interface import VppIpIpTunInterface |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 26 | from vpp_ip_route import ( |
| 27 | VppIpRoute, |
| 28 | VppRoutePath, |
| 29 | DpoProto, |
| 30 | VppMplsLabel, |
| 31 | VppMplsTable, |
| 32 | VppMplsRoute, |
| 33 | FibPathProto, |
| 34 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 35 | from vpp_ipsec import VppIpsecSA, VppIpsecTunProtect, VppIpsecInterface |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 36 | from vpp_l2 import VppBridgeDomain, VppBridgeDomainPort |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 37 | from vpp_sub_interface import L2_VTR_OP, VppDot1QSubint |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 38 | from vpp_teib import VppTeib |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 39 | from util import ppp |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 40 | from vpp_papi import VppEnum |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 41 | from vpp_papi_provider import CliFailedCommandError |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 42 | from vpp_acl import AclRule, VppAcl, VppAclInterface |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 43 | from vpp_policer import PolicerAction, VppPolicer, Dir |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 44 | |
| 45 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 46 | 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] | 47 | 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] | 48 | esn_en = bool( |
| 49 | p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN) |
| 50 | ) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 51 | crypt_key = mk_scapy_crypt_key(p) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 52 | if tun_if: |
| 53 | p.tun_dst = tun_if.remote_ip |
| 54 | p.tun_src = tun_if.local_ip |
| 55 | else: |
| 56 | p.tun_dst = dst |
| 57 | p.tun_src = src |
| 58 | |
Arthur de Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 59 | if p.nat_header: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 60 | is_default_port = p.nat_header.dport == 4500 |
Arthur de Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 61 | else: |
| 62 | is_default_port = True |
| 63 | |
| 64 | if is_default_port: |
| 65 | outbound_nat_header = p.nat_header |
| 66 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 67 | 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] | 68 | bind_layers(UDP, ESP, dport=p.nat_header.dport) |
| 69 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 70 | p.scapy_tun_sa = SecurityAssociation( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 71 | encryption_type, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 72 | spi=p.scapy_tun_spi, |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 73 | crypt_algo=p.crypt_algo, |
| 74 | crypt_key=crypt_key, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 75 | auth_algo=p.auth_algo, |
| 76 | auth_key=p.auth_key, |
| 77 | 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] | 78 | nat_t_header=outbound_nat_header, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 79 | esn_en=esn_en, |
| 80 | ) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 81 | p.vpp_tun_sa = SecurityAssociation( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 82 | encryption_type, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 83 | spi=p.vpp_tun_spi, |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 84 | crypt_algo=p.crypt_algo, |
| 85 | crypt_key=crypt_key, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 86 | auth_algo=p.auth_algo, |
| 87 | auth_key=p.auth_key, |
| 88 | 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] | 89 | nat_t_header=p.nat_header, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 90 | esn_en=esn_en, |
| 91 | ) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 92 | |
| 93 | |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 94 | def config_tra_params(p, encryption_type, tun_if): |
| 95 | 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] | 96 | esn_en = bool( |
| 97 | p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN) |
| 98 | ) |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 99 | crypt_key = mk_scapy_crypt_key(p) |
Neale Ranns | f3a6622 | 2020-01-02 05:04:00 +0000 | [diff] [blame] | 100 | p.tun_dst = tun_if.remote_ip |
| 101 | p.tun_src = tun_if.local_ip |
Arthur de Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 102 | |
| 103 | if p.nat_header: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 104 | is_default_port = p.nat_header.dport == 4500 |
Arthur de Kerhor | ce04e3b | 2022-01-04 15:53:43 +0100 | [diff] [blame] | 105 | else: |
| 106 | is_default_port = True |
| 107 | |
| 108 | if is_default_port: |
| 109 | outbound_nat_header = p.nat_header |
| 110 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 111 | 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] | 112 | bind_layers(UDP, ESP, dport=p.nat_header.dport) |
| 113 | |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 114 | p.scapy_tun_sa = SecurityAssociation( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 115 | encryption_type, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 116 | spi=p.scapy_tun_spi, |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 117 | crypt_algo=p.crypt_algo, |
| 118 | crypt_key=crypt_key, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 119 | auth_algo=p.auth_algo, |
| 120 | auth_key=p.auth_key, |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 121 | esn_en=esn_en, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 122 | nat_t_header=outbound_nat_header, |
| 123 | ) |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 124 | p.vpp_tun_sa = SecurityAssociation( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 125 | encryption_type, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 126 | spi=p.vpp_tun_spi, |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 127 | crypt_algo=p.crypt_algo, |
| 128 | crypt_key=crypt_key, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 129 | auth_algo=p.auth_algo, |
| 130 | auth_key=p.auth_key, |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 131 | esn_en=esn_en, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 132 | nat_t_header=p.nat_header, |
| 133 | ) |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 134 | |
| 135 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 136 | class TemplateIpsec4TunProtect(object): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 137 | """IPsec IPv4 Tunnel protect""" |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 138 | |
| 139 | encryption_type = ESP |
| 140 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 141 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 142 | tun4_input_node = "ipsec4-tun-input" |
| 143 | |
| 144 | def config_sa_tra(self, p): |
| 145 | config_tun_params(p, self.encryption_type, p.tun_if) |
| 146 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 147 | p.tun_sa_out = VppIpsecSA( |
| 148 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 149 | p.vpp_tun_sa_id, |
| 150 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 151 | p.auth_algo_vpp_id, |
| 152 | p.auth_key, |
| 153 | p.crypt_algo_vpp_id, |
| 154 | p.crypt_key, |
| 155 | self.vpp_esp_protocol, |
| 156 | flags=p.flags, |
| 157 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 158 | p.tun_sa_out.add_vpp_config() |
| 159 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 160 | p.tun_sa_in = VppIpsecSA( |
| 161 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 162 | p.scapy_tun_sa_id, |
| 163 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 164 | p.auth_algo_vpp_id, |
| 165 | p.auth_key, |
| 166 | p.crypt_algo_vpp_id, |
| 167 | p.crypt_key, |
| 168 | self.vpp_esp_protocol, |
| 169 | flags=p.flags, |
| 170 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 171 | p.tun_sa_in.add_vpp_config() |
| 172 | |
| 173 | def config_sa_tun(self, p): |
| 174 | config_tun_params(p, self.encryption_type, p.tun_if) |
| 175 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 176 | p.tun_sa_out = VppIpsecSA( |
| 177 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 178 | p.vpp_tun_sa_id, |
| 179 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 180 | p.auth_algo_vpp_id, |
| 181 | p.auth_key, |
| 182 | p.crypt_algo_vpp_id, |
| 183 | p.crypt_key, |
| 184 | self.vpp_esp_protocol, |
| 185 | self.tun_if.local_addr[p.addr_type], |
| 186 | self.tun_if.remote_addr[p.addr_type], |
| 187 | flags=p.flags, |
| 188 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 189 | p.tun_sa_out.add_vpp_config() |
| 190 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 191 | p.tun_sa_in = VppIpsecSA( |
| 192 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 193 | p.scapy_tun_sa_id, |
| 194 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 195 | p.auth_algo_vpp_id, |
| 196 | p.auth_key, |
| 197 | p.crypt_algo_vpp_id, |
| 198 | p.crypt_key, |
| 199 | self.vpp_esp_protocol, |
| 200 | self.tun_if.remote_addr[p.addr_type], |
| 201 | self.tun_if.local_addr[p.addr_type], |
| 202 | flags=p.flags, |
| 203 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 204 | p.tun_sa_in.add_vpp_config() |
| 205 | |
| 206 | def config_protect(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 207 | 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] | 208 | p.tun_protect.add_vpp_config() |
| 209 | |
| 210 | def config_network(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 211 | if hasattr(p, "tun_dst"): |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 212 | tun_dst = p.tun_dst |
| 213 | else: |
| 214 | tun_dst = self.pg0.remote_ip4 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 215 | 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] | 216 | p.tun_if.add_vpp_config() |
| 217 | p.tun_if.admin_up() |
| 218 | p.tun_if.config_ip4() |
| 219 | p.tun_if.config_ip6() |
| 220 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 221 | p.route = VppIpRoute( |
| 222 | self, |
| 223 | p.remote_tun_if_host, |
| 224 | 32, |
| 225 | [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)], |
| 226 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 227 | p.route.add_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 228 | r = VppIpRoute( |
| 229 | self, |
| 230 | p.remote_tun_if_host6, |
| 231 | 128, |
| 232 | [ |
| 233 | VppRoutePath( |
| 234 | p.tun_if.remote_ip6, 0xFFFFFFFF, proto=DpoProto.DPO_PROTO_IP6 |
| 235 | ) |
| 236 | ], |
| 237 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 238 | r.add_vpp_config() |
| 239 | |
| 240 | def unconfig_network(self, p): |
| 241 | p.route.remove_vpp_config() |
| 242 | p.tun_if.remove_vpp_config() |
| 243 | |
| 244 | def unconfig_protect(self, p): |
| 245 | p.tun_protect.remove_vpp_config() |
| 246 | |
| 247 | def unconfig_sa(self, p): |
| 248 | p.tun_sa_out.remove_vpp_config() |
| 249 | p.tun_sa_in.remove_vpp_config() |
| 250 | |
| 251 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 252 | class TemplateIpsec4TunIfEsp(TemplateIpsec4TunProtect, TemplateIpsec): |
| 253 | """IPsec tunnel interface tests""" |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 254 | |
| 255 | encryption_type = ESP |
| 256 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 257 | @classmethod |
| 258 | def setUpClass(cls): |
| 259 | super(TemplateIpsec4TunIfEsp, cls).setUpClass() |
| 260 | |
| 261 | @classmethod |
| 262 | def tearDownClass(cls): |
| 263 | super(TemplateIpsec4TunIfEsp, cls).tearDownClass() |
| 264 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 265 | def setUp(self): |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 266 | super(TemplateIpsec4TunIfEsp, self).setUp() |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 267 | |
| 268 | self.tun_if = self.pg0 |
| 269 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 270 | p = self.ipv4_params |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 271 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 272 | self.config_network(p) |
| 273 | self.config_sa_tra(p) |
| 274 | self.config_protect(p) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 275 | |
| 276 | def tearDown(self): |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 277 | super(TemplateIpsec4TunIfEsp, self).tearDown() |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 278 | |
| 279 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 280 | class TemplateIpsec4TunIfEspUdp(TemplateIpsec4TunProtect, TemplateIpsec): |
| 281 | """IPsec UDP tunnel interface tests""" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 282 | |
| 283 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 284 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 285 | encryption_type = ESP |
| 286 | |
| 287 | @classmethod |
| 288 | def setUpClass(cls): |
| 289 | super(TemplateIpsec4TunIfEspUdp, cls).setUpClass() |
| 290 | |
| 291 | @classmethod |
| 292 | def tearDownClass(cls): |
| 293 | super(TemplateIpsec4TunIfEspUdp, cls).tearDownClass() |
| 294 | |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 295 | def verify_encrypted(self, p, sa, rxs): |
| 296 | for rx in rxs: |
| 297 | try: |
| 298 | # ensure the UDP ports are correct before we decrypt |
| 299 | # which strips them |
| 300 | self.assertTrue(rx.haslayer(UDP)) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 301 | self.assert_equal(rx[UDP].sport, p.nat_header.sport) |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 302 | self.assert_equal(rx[UDP].dport, p.nat_header.dport) |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 303 | |
| 304 | pkt = sa.decrypt(rx[IP]) |
| 305 | if not pkt.haslayer(IP): |
| 306 | pkt = IP(pkt[Raw].load) |
| 307 | |
| 308 | self.assert_packet_checksums_valid(pkt) |
| 309 | self.assert_equal(pkt[IP].dst, "1.1.1.1") |
| 310 | self.assert_equal(pkt[IP].src, self.pg1.remote_ip4) |
| 311 | except (IndexError, AssertionError): |
| 312 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 313 | try: |
| 314 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 315 | except: |
| 316 | pass |
| 317 | raise |
| 318 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 319 | def config_sa_tra(self, p): |
| 320 | config_tun_params(p, self.encryption_type, p.tun_if) |
| 321 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 322 | p.tun_sa_out = VppIpsecSA( |
| 323 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 324 | p.vpp_tun_sa_id, |
| 325 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 326 | p.auth_algo_vpp_id, |
| 327 | p.auth_key, |
| 328 | p.crypt_algo_vpp_id, |
| 329 | p.crypt_key, |
| 330 | self.vpp_esp_protocol, |
| 331 | flags=p.flags, |
| 332 | udp_src=p.nat_header.sport, |
| 333 | udp_dst=p.nat_header.dport, |
| 334 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 335 | p.tun_sa_out.add_vpp_config() |
| 336 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 337 | p.tun_sa_in = VppIpsecSA( |
| 338 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 339 | p.scapy_tun_sa_id, |
| 340 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 341 | p.auth_algo_vpp_id, |
| 342 | p.auth_key, |
| 343 | p.crypt_algo_vpp_id, |
| 344 | p.crypt_key, |
| 345 | self.vpp_esp_protocol, |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 346 | flags=p.flags |
| 347 | | 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] | 348 | udp_src=p.nat_header.sport, |
| 349 | udp_dst=p.nat_header.dport, |
| 350 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 351 | p.tun_sa_in.add_vpp_config() |
| 352 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 353 | def setUp(self): |
| 354 | super(TemplateIpsec4TunIfEspUdp, self).setUp() |
| 355 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 356 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 357 | 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] | 358 | p.nat_header = UDP(sport=5454, dport=4500) |
| 359 | |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 360 | self.tun_if = self.pg0 |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 361 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 362 | self.config_network(p) |
| 363 | self.config_sa_tra(p) |
| 364 | self.config_protect(p) |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 365 | |
| 366 | def tearDown(self): |
| 367 | super(TemplateIpsec4TunIfEspUdp, self).tearDown() |
| 368 | |
| 369 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 370 | class TestIpsec4TunIfEsp1(TemplateIpsec4TunIfEsp, IpsecTun4Tests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 371 | """Ipsec ESP - TUN tests""" |
| 372 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 373 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 374 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 375 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 376 | def test_tun_basic64(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 377 | """ipsec 6o4 tunnel basic test""" |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 378 | self.tun4_encrypt_node_name = "esp4-encrypt-tun" |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 379 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 380 | self.verify_tun_64(self.params[socket.AF_INET], count=1) |
| 381 | |
| 382 | def test_tun_burst64(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 383 | """ipsec 6o4 tunnel basic test""" |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 384 | self.tun4_encrypt_node_name = "esp4-encrypt-tun" |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 385 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 386 | self.verify_tun_64(self.params[socket.AF_INET], count=257) |
| 387 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 388 | def test_tun_basic_frag44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 389 | """ipsec 4o4 tunnel frag basic test""" |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 390 | self.tun4_encrypt_node_name = "esp4-encrypt-tun" |
| 391 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 392 | p = self.ipv4_params |
| 393 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 394 | self.vapi.sw_interface_set_mtu(p.tun_if.sw_if_index, [1500, 0, 0, 0]) |
| 395 | self.verify_tun_44( |
| 396 | self.params[socket.AF_INET], count=1, payload_size=1800, n_rx=2 |
| 397 | ) |
| 398 | 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] | 399 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 400 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 401 | class TestIpsec4TunIfEspUdp(TemplateIpsec4TunIfEspUdp, IpsecTun4Tests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 402 | """Ipsec ESP UDP tests""" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 403 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 404 | tun4_input_node = "ipsec4-tun-input" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 405 | |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 406 | def setUp(self): |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 407 | super(TestIpsec4TunIfEspUdp, self).setUp() |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 408 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 409 | def test_keepalive(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 410 | """IPSEC NAT Keepalive""" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 411 | self.verify_keepalive(self.ipv4_params) |
| 412 | |
| 413 | |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 414 | class TestIpsec4TunIfEspUdpGCM(TemplateIpsec4TunIfEspUdp, IpsecTun4Tests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 415 | """Ipsec ESP UDP GCM tests""" |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 416 | |
| 417 | tun4_input_node = "ipsec4-tun-input" |
| 418 | |
| 419 | def setUp(self): |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 420 | super(TestIpsec4TunIfEspUdpGCM, self).setUp() |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 421 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 422 | p.auth_algo_vpp_id = VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE |
| 423 | p.crypt_algo_vpp_id = ( |
| 424 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_GCM_256 |
| 425 | ) |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 426 | p.crypt_algo = "AES-GCM" |
| 427 | p.auth_algo = "NULL" |
| 428 | p.crypt_key = b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h" |
| 429 | p.salt = 0 |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 430 | |
| 431 | |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 432 | class TestIpsec4TunIfEspUdpUpdate(TemplateIpsec4TunIfEspUdp, IpsecTun4Tests): |
| 433 | """Ipsec ESP UDP update tests""" |
| 434 | |
| 435 | tun4_input_node = "ipsec4-tun-input" |
| 436 | |
| 437 | def setUp(self): |
| 438 | super(TestIpsec4TunIfEspUdpUpdate, self).setUp() |
| 439 | p = self.ipv4_params |
| 440 | p.nat_header = UDP(sport=6565, dport=7676) |
| 441 | config_tun_params(p, self.encryption_type, p.tun_if) |
| 442 | p.tun_sa_in.update_vpp_config( |
| 443 | udp_src=p.nat_header.dport, udp_dst=p.nat_header.sport |
| 444 | ) |
| 445 | p.tun_sa_out.update_vpp_config( |
| 446 | udp_src=p.nat_header.sport, udp_dst=p.nat_header.dport |
| 447 | ) |
| 448 | |
| 449 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 450 | class TestIpsec4TunIfEsp2(TemplateIpsec4TunIfEsp, IpsecTcpTests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 451 | """Ipsec ESP - TCP tests""" |
| 452 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 453 | pass |
| 454 | |
| 455 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 456 | class TemplateIpsec6TunProtect(object): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 457 | """IPsec IPv6 Tunnel protect""" |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 458 | |
| 459 | def config_sa_tra(self, p): |
| 460 | config_tun_params(p, self.encryption_type, p.tun_if) |
| 461 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 462 | p.tun_sa_out = VppIpsecSA( |
| 463 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 464 | p.vpp_tun_sa_id, |
| 465 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 466 | p.auth_algo_vpp_id, |
| 467 | p.auth_key, |
| 468 | p.crypt_algo_vpp_id, |
| 469 | p.crypt_key, |
| 470 | self.vpp_esp_protocol, |
| 471 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 472 | p.tun_sa_out.add_vpp_config() |
| 473 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 474 | p.tun_sa_in = VppIpsecSA( |
| 475 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 476 | p.scapy_tun_sa_id, |
| 477 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 478 | p.auth_algo_vpp_id, |
| 479 | p.auth_key, |
| 480 | p.crypt_algo_vpp_id, |
| 481 | p.crypt_key, |
| 482 | self.vpp_esp_protocol, |
| 483 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 484 | p.tun_sa_in.add_vpp_config() |
| 485 | |
| 486 | def config_sa_tun(self, p): |
| 487 | config_tun_params(p, self.encryption_type, p.tun_if) |
| 488 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 489 | p.tun_sa_out = VppIpsecSA( |
| 490 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 491 | p.vpp_tun_sa_id, |
| 492 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 493 | p.auth_algo_vpp_id, |
| 494 | p.auth_key, |
| 495 | p.crypt_algo_vpp_id, |
| 496 | p.crypt_key, |
| 497 | self.vpp_esp_protocol, |
| 498 | self.tun_if.local_addr[p.addr_type], |
| 499 | self.tun_if.remote_addr[p.addr_type], |
| 500 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 501 | p.tun_sa_out.add_vpp_config() |
| 502 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 503 | p.tun_sa_in = VppIpsecSA( |
| 504 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 505 | p.scapy_tun_sa_id, |
| 506 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 507 | p.auth_algo_vpp_id, |
| 508 | p.auth_key, |
| 509 | p.crypt_algo_vpp_id, |
| 510 | p.crypt_key, |
| 511 | self.vpp_esp_protocol, |
| 512 | self.tun_if.remote_addr[p.addr_type], |
| 513 | self.tun_if.local_addr[p.addr_type], |
| 514 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 515 | p.tun_sa_in.add_vpp_config() |
| 516 | |
| 517 | def config_protect(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 518 | 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] | 519 | p.tun_protect.add_vpp_config() |
| 520 | |
| 521 | def config_network(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 522 | if hasattr(p, "tun_dst"): |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 523 | tun_dst = p.tun_dst |
| 524 | else: |
| 525 | tun_dst = self.pg0.remote_ip6 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 526 | 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] | 527 | p.tun_if.add_vpp_config() |
| 528 | p.tun_if.admin_up() |
| 529 | p.tun_if.config_ip6() |
| 530 | p.tun_if.config_ip4() |
| 531 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 532 | p.route = VppIpRoute( |
| 533 | self, |
| 534 | p.remote_tun_if_host, |
| 535 | 128, |
| 536 | [ |
| 537 | VppRoutePath( |
| 538 | p.tun_if.remote_ip6, 0xFFFFFFFF, proto=DpoProto.DPO_PROTO_IP6 |
| 539 | ) |
| 540 | ], |
| 541 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 542 | p.route.add_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 543 | r = VppIpRoute( |
| 544 | self, |
| 545 | p.remote_tun_if_host4, |
| 546 | 32, |
| 547 | [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)], |
| 548 | ) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 549 | r.add_vpp_config() |
| 550 | |
| 551 | def unconfig_network(self, p): |
| 552 | p.route.remove_vpp_config() |
| 553 | p.tun_if.remove_vpp_config() |
| 554 | |
| 555 | def unconfig_protect(self, p): |
| 556 | p.tun_protect.remove_vpp_config() |
| 557 | |
| 558 | def unconfig_sa(self, p): |
| 559 | p.tun_sa_out.remove_vpp_config() |
| 560 | p.tun_sa_in.remove_vpp_config() |
| 561 | |
| 562 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 563 | class TemplateIpsec6TunIfEsp(TemplateIpsec6TunProtect, TemplateIpsec): |
| 564 | """IPsec tunnel interface tests""" |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 565 | |
| 566 | encryption_type = ESP |
| 567 | |
| 568 | def setUp(self): |
| 569 | super(TemplateIpsec6TunIfEsp, self).setUp() |
| 570 | |
| 571 | self.tun_if = self.pg0 |
| 572 | |
| 573 | p = self.ipv6_params |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 574 | self.config_network(p) |
| 575 | self.config_sa_tra(p) |
| 576 | self.config_protect(p) |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 577 | |
| 578 | def tearDown(self): |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 579 | super(TemplateIpsec6TunIfEsp, self).tearDown() |
| 580 | |
| 581 | |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 582 | class TemplateIpsec6TunIfEspUdp(TemplateIpsec6TunProtect, TemplateIpsec): |
| 583 | """IPsec6 UDP tunnel interface tests""" |
| 584 | |
| 585 | tun4_encrypt_node_name = "esp6-encrypt-tun" |
| 586 | tun4_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
| 587 | encryption_type = ESP |
| 588 | |
| 589 | @classmethod |
| 590 | def setUpClass(cls): |
| 591 | super(TemplateIpsec6TunIfEspUdp, cls).setUpClass() |
| 592 | |
| 593 | @classmethod |
| 594 | def tearDownClass(cls): |
| 595 | super(TemplateIpsec6TunIfEspUdp, cls).tearDownClass() |
| 596 | |
| 597 | def verify_encrypted(self, p, sa, rxs): |
| 598 | for rx in rxs: |
| 599 | try: |
| 600 | # ensure the UDP ports are correct before we decrypt |
| 601 | # which strips them |
| 602 | self.assertTrue(rx.haslayer(UDP)) |
| 603 | self.assert_equal(rx[UDP].sport, p.nat_header.sport) |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 604 | self.assert_equal(rx[UDP].dport, p.nat_header.dport) |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 605 | |
| 606 | pkt = sa.decrypt(rx[IP]) |
| 607 | if not pkt.haslayer(IP): |
| 608 | pkt = IP(pkt[Raw].load) |
| 609 | |
| 610 | self.assert_packet_checksums_valid(pkt) |
| 611 | self.assert_equal( |
| 612 | pkt[IP].dst, "1111:1111:1111:1111:1111:1111:1111:1111" |
| 613 | ) |
| 614 | self.assert_equal(pkt[IP].src, self.pg1.remote_ip6) |
| 615 | except (IndexError, AssertionError): |
| 616 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 617 | try: |
| 618 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 619 | except: |
| 620 | pass |
| 621 | raise |
| 622 | |
| 623 | def config_sa_tra(self, p): |
| 624 | config_tun_params(p, self.encryption_type, p.tun_if) |
| 625 | |
| 626 | p.tun_sa_out = VppIpsecSA( |
| 627 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 628 | p.vpp_tun_sa_id, |
| 629 | p.vpp_tun_spi, |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 630 | p.auth_algo_vpp_id, |
| 631 | p.auth_key, |
| 632 | p.crypt_algo_vpp_id, |
| 633 | p.crypt_key, |
| 634 | self.vpp_esp_protocol, |
| 635 | flags=p.flags, |
| 636 | udp_src=p.nat_header.sport, |
| 637 | udp_dst=p.nat_header.dport, |
| 638 | ) |
| 639 | p.tun_sa_out.add_vpp_config() |
| 640 | |
| 641 | p.tun_sa_in = VppIpsecSA( |
| 642 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 643 | p.scapy_tun_sa_id, |
| 644 | p.scapy_tun_spi, |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 645 | p.auth_algo_vpp_id, |
| 646 | p.auth_key, |
| 647 | p.crypt_algo_vpp_id, |
| 648 | p.crypt_key, |
| 649 | self.vpp_esp_protocol, |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 650 | flags=p.flags |
| 651 | | 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] | 652 | udp_src=p.nat_header.sport, |
| 653 | udp_dst=p.nat_header.dport, |
| 654 | ) |
| 655 | p.tun_sa_in.add_vpp_config() |
| 656 | |
| 657 | def setUp(self): |
| 658 | super(TemplateIpsec6TunIfEspUdp, self).setUp() |
| 659 | |
| 660 | p = self.ipv6_params |
| 661 | p.flags = VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_UDP_ENCAP |
| 662 | p.nat_header = UDP(sport=5454, dport=4500) |
| 663 | |
| 664 | self.tun_if = self.pg0 |
| 665 | |
| 666 | self.config_network(p) |
| 667 | self.config_sa_tra(p) |
| 668 | self.config_protect(p) |
| 669 | |
| 670 | def tearDown(self): |
| 671 | super(TemplateIpsec6TunIfEspUdp, self).tearDown() |
| 672 | |
| 673 | |
| 674 | class TestIpsec6TunIfEspUdp(TemplateIpsec6TunIfEspUdp, IpsecTun6Tests): |
| 675 | """Ipsec ESP 6 UDP tests""" |
| 676 | |
| 677 | tun6_input_node = "ipsec6-tun-input" |
| 678 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
| 679 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
| 680 | |
| 681 | def setUp(self): |
| 682 | super(TestIpsec6TunIfEspUdp, self).setUp() |
| 683 | |
| 684 | def test_keepalive(self): |
| 685 | """IPSEC6 NAT Keepalive""" |
| 686 | self.verify_keepalive(self.ipv6_params) |
| 687 | |
| 688 | |
| 689 | class TestIpsec6TunIfEspUdpGCM(TemplateIpsec6TunIfEspUdp, IpsecTun6Tests): |
| 690 | """Ipsec ESP 6 UDP GCM tests""" |
| 691 | |
| 692 | tun6_input_node = "ipsec6-tun-input" |
| 693 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
| 694 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
| 695 | |
| 696 | def setUp(self): |
| 697 | super(TestIpsec6TunIfEspUdpGCM, self).setUp() |
| 698 | p = self.ipv6_params |
| 699 | p.auth_algo_vpp_id = VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE |
| 700 | p.crypt_algo_vpp_id = ( |
| 701 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_GCM_256 |
| 702 | ) |
| 703 | p.crypt_algo = "AES-GCM" |
| 704 | p.auth_algo = "NULL" |
| 705 | p.crypt_key = b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h" |
| 706 | p.salt = 0 |
| 707 | |
| 708 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 709 | class TestIpsec6TunIfEsp1(TemplateIpsec6TunIfEsp, IpsecTun6Tests): |
| 710 | """Ipsec ESP - TUN tests""" |
| 711 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 712 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 713 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 714 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 715 | def test_tun_basic46(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 716 | """ipsec 4o6 tunnel basic test""" |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 717 | self.tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 718 | self.verify_tun_46(self.params[socket.AF_INET6], count=1) |
| 719 | |
| 720 | def test_tun_burst46(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 721 | """ipsec 4o6 tunnel burst test""" |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 722 | self.tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 723 | self.verify_tun_46(self.params[socket.AF_INET6], count=257) |
| 724 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 725 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 726 | class TestIpsec6TunIfEspHandoff(TemplateIpsec6TunIfEsp, IpsecTun6HandoffTests): |
| 727 | """Ipsec ESP 6 Handoff tests""" |
| 728 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 729 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 730 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 731 | |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 732 | def test_tun_handoff_66_police(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 733 | """ESP 6o6 tunnel with policer worker hand-off test""" |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 734 | self.vapi.cli("clear errors") |
| 735 | self.vapi.cli("clear ipsec sa") |
| 736 | |
| 737 | N_PKTS = 15 |
| 738 | p = self.params[socket.AF_INET6] |
| 739 | |
| 740 | action_tx = PolicerAction( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 741 | VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0 |
| 742 | ) |
| 743 | policer = VppPolicer( |
| 744 | self, |
| 745 | "pol1", |
| 746 | 80, |
| 747 | 0, |
| 748 | 1000, |
| 749 | 0, |
| 750 | conform_action=action_tx, |
| 751 | exceed_action=action_tx, |
| 752 | violate_action=action_tx, |
| 753 | ) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 754 | policer.add_vpp_config() |
| 755 | |
| 756 | # Start policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 757 | 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] | 758 | |
| 759 | for pol_bind in [1, 0]: |
| 760 | policer.bind_vpp_config(pol_bind, True) |
| 761 | |
| 762 | # inject alternately on worker 0 and 1. |
| 763 | for worker in [0, 1, 0, 1]: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 764 | send_pkts = self.gen_encrypt_pkts6( |
| 765 | p, |
| 766 | p.scapy_tun_sa, |
| 767 | self.tun_if, |
| 768 | src=p.remote_tun_if_host, |
| 769 | dst=self.pg1.remote_ip6, |
| 770 | count=N_PKTS, |
| 771 | ) |
| 772 | recv_pkts = self.send_and_expect( |
| 773 | self.tun_if, send_pkts, self.pg1, worker=worker |
| 774 | ) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 775 | self.verify_decrypted6(p, recv_pkts) |
| 776 | self.logger.debug(self.vapi.cli("show trace max 100")) |
| 777 | |
| 778 | stats = policer.get_stats() |
| 779 | stats0 = policer.get_stats(worker=0) |
| 780 | stats1 = policer.get_stats(worker=1) |
| 781 | |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 782 | if pol_bind == 1: |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 783 | # First pass: Worker 1, should have done all the policing |
| 784 | self.assertEqual(stats, stats1) |
| 785 | |
| 786 | # Worker 0, should have handed everything off |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 787 | self.assertEqual(stats0["conform_packets"], 0) |
| 788 | self.assertEqual(stats0["exceed_packets"], 0) |
| 789 | self.assertEqual(stats0["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 790 | else: |
| 791 | # Second pass: both workers should have policed equal amounts |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 792 | self.assertGreater(stats1["conform_packets"], 0) |
| 793 | self.assertEqual(stats1["exceed_packets"], 0) |
| 794 | self.assertGreater(stats1["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 795 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 796 | self.assertGreater(stats0["conform_packets"], 0) |
| 797 | self.assertEqual(stats0["exceed_packets"], 0) |
| 798 | self.assertGreater(stats0["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 799 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 800 | self.assertEqual( |
| 801 | stats0["conform_packets"] + stats0["violate_packets"], |
| 802 | stats1["conform_packets"] + stats1["violate_packets"], |
| 803 | ) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 804 | |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 805 | 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] | 806 | policer.remove_vpp_config() |
| 807 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 808 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 809 | class TestIpsec4TunIfEspHandoff(TemplateIpsec4TunIfEsp, IpsecTun4HandoffTests): |
| 810 | """Ipsec ESP 4 Handoff tests""" |
| 811 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 812 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 813 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 814 | |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 815 | def test_tun_handoff_44_police(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 816 | """ESP 4o4 tunnel with policer worker hand-off test""" |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 817 | self.vapi.cli("clear errors") |
| 818 | self.vapi.cli("clear ipsec sa") |
| 819 | |
| 820 | N_PKTS = 15 |
| 821 | p = self.params[socket.AF_INET] |
| 822 | |
| 823 | action_tx = PolicerAction( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 824 | VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0 |
| 825 | ) |
| 826 | policer = VppPolicer( |
| 827 | self, |
| 828 | "pol1", |
| 829 | 80, |
| 830 | 0, |
| 831 | 1000, |
| 832 | 0, |
| 833 | conform_action=action_tx, |
| 834 | exceed_action=action_tx, |
| 835 | violate_action=action_tx, |
| 836 | ) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 837 | policer.add_vpp_config() |
| 838 | |
| 839 | # Start policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 840 | 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] | 841 | |
| 842 | for pol_bind in [1, 0]: |
| 843 | policer.bind_vpp_config(pol_bind, True) |
| 844 | |
| 845 | # inject alternately on worker 0 and 1. |
| 846 | for worker in [0, 1, 0, 1]: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 847 | send_pkts = self.gen_encrypt_pkts( |
| 848 | p, |
| 849 | p.scapy_tun_sa, |
| 850 | self.tun_if, |
| 851 | src=p.remote_tun_if_host, |
| 852 | dst=self.pg1.remote_ip4, |
| 853 | count=N_PKTS, |
| 854 | ) |
| 855 | recv_pkts = self.send_and_expect( |
| 856 | self.tun_if, send_pkts, self.pg1, worker=worker |
| 857 | ) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 858 | self.verify_decrypted(p, recv_pkts) |
| 859 | self.logger.debug(self.vapi.cli("show trace max 100")) |
| 860 | |
| 861 | stats = policer.get_stats() |
| 862 | stats0 = policer.get_stats(worker=0) |
| 863 | stats1 = policer.get_stats(worker=1) |
| 864 | |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 865 | if pol_bind == 1: |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 866 | # First pass: Worker 1, should have done all the policing |
| 867 | self.assertEqual(stats, stats1) |
| 868 | |
| 869 | # Worker 0, should have handed everything off |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 870 | self.assertEqual(stats0["conform_packets"], 0) |
| 871 | self.assertEqual(stats0["exceed_packets"], 0) |
| 872 | self.assertEqual(stats0["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 873 | else: |
| 874 | # Second pass: both workers should have policed equal amounts |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 875 | self.assertGreater(stats1["conform_packets"], 0) |
| 876 | self.assertEqual(stats1["exceed_packets"], 0) |
| 877 | self.assertGreater(stats1["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 878 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 879 | self.assertGreater(stats0["conform_packets"], 0) |
| 880 | self.assertEqual(stats0["exceed_packets"], 0) |
| 881 | self.assertGreater(stats0["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 882 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 883 | self.assertEqual( |
| 884 | stats0["conform_packets"] + stats0["violate_packets"], |
| 885 | stats1["conform_packets"] + stats1["violate_packets"], |
| 886 | ) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 887 | |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 888 | 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] | 889 | policer.remove_vpp_config() |
| 890 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 891 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 892 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 893 | class TestIpsec4MultiTunIfEsp(TemplateIpsec4TunProtect, TemplateIpsec, IpsecTun4): |
| 894 | """IPsec IPv4 Multi Tunnel interface""" |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 895 | |
| 896 | encryption_type = ESP |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 897 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 898 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 899 | |
| 900 | def setUp(self): |
| 901 | super(TestIpsec4MultiTunIfEsp, self).setUp() |
| 902 | |
| 903 | self.tun_if = self.pg0 |
| 904 | |
| 905 | self.multi_params = [] |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 906 | self.pg0.generate_remote_hosts(10) |
| 907 | self.pg0.configure_ipv4_neighbors() |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 908 | |
| 909 | for ii in range(10): |
| 910 | p = copy.copy(self.ipv4_params) |
| 911 | |
| 912 | p.remote_tun_if_host = "1.1.1.%d" % (ii + 1) |
| 913 | p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii |
| 914 | p.scapy_tun_spi = p.scapy_tun_spi + ii |
| 915 | p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii |
| 916 | p.vpp_tun_spi = p.vpp_tun_spi + ii |
| 917 | |
| 918 | p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii |
| 919 | p.scapy_tra_spi = p.scapy_tra_spi + ii |
| 920 | p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii |
| 921 | p.vpp_tra_spi = p.vpp_tra_spi + ii |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 922 | p.tun_dst = self.pg0.remote_hosts[ii].ip4 |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 923 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 924 | self.multi_params.append(p) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 925 | self.config_network(p) |
| 926 | self.config_sa_tra(p) |
| 927 | self.config_protect(p) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 928 | |
| 929 | def tearDown(self): |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 930 | super(TestIpsec4MultiTunIfEsp, self).tearDown() |
| 931 | |
| 932 | def test_tun_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 933 | """Multiple IPSEC tunnel interfaces""" |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 934 | for p in self.multi_params: |
| 935 | self.verify_tun_44(p, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 936 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 937 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 938 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 939 | def test_tun_rr_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 940 | """Round-robin packets acrros multiple interface""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 941 | tx = [] |
| 942 | for p in self.multi_params: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 943 | tx = tx + self.gen_encrypt_pkts( |
| 944 | p, |
| 945 | p.scapy_tun_sa, |
| 946 | self.tun_if, |
| 947 | src=p.remote_tun_if_host, |
| 948 | dst=self.pg1.remote_ip4, |
| 949 | ) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 950 | rxs = self.send_and_expect(self.tun_if, tx, self.pg1) |
| 951 | |
| 952 | for rx, p in zip(rxs, self.multi_params): |
| 953 | self.verify_decrypted(p, [rx]) |
| 954 | |
| 955 | tx = [] |
| 956 | for p in self.multi_params: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 957 | tx = tx + self.gen_pkts( |
| 958 | self.pg1, src=self.pg1.remote_ip4, dst=p.remote_tun_if_host |
| 959 | ) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 960 | rxs = self.send_and_expect(self.pg1, tx, self.tun_if) |
| 961 | |
| 962 | for rx, p in zip(rxs, self.multi_params): |
| 963 | self.verify_encrypted(p, p.vpp_tun_sa, [rx]) |
| 964 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 965 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 966 | class TestIpsec4TunIfEspAll(TemplateIpsec4TunProtect, TemplateIpsec, IpsecTun4): |
| 967 | """IPsec IPv4 Tunnel interface all Algos""" |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 968 | |
| 969 | encryption_type = ESP |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 970 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 971 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 972 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 973 | def setUp(self): |
| 974 | super(TestIpsec4TunIfEspAll, self).setUp() |
| 975 | |
| 976 | self.tun_if = self.pg0 |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 977 | p = self.ipv4_params |
| 978 | |
| 979 | self.config_network(p) |
| 980 | self.config_sa_tra(p) |
| 981 | self.config_protect(p) |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 982 | |
| 983 | def tearDown(self): |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 984 | p = self.ipv4_params |
| 985 | self.unconfig_protect(p) |
| 986 | self.unconfig_network(p) |
| 987 | self.unconfig_sa(p) |
| 988 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 989 | super(TestIpsec4TunIfEspAll, self).tearDown() |
| 990 | |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 991 | def rekey(self, p): |
| 992 | # |
| 993 | # change the key and the SPI |
| 994 | # |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 995 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 996 | p.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 997 | p.scapy_tun_spi += 1 |
| 998 | p.scapy_tun_sa_id += 1 |
| 999 | p.vpp_tun_spi += 1 |
| 1000 | p.vpp_tun_sa_id += 1 |
| 1001 | p.tun_if.local_spi = p.vpp_tun_spi |
| 1002 | p.tun_if.remote_spi = p.scapy_tun_spi |
| 1003 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1004 | config_tun_params(p, self.encryption_type, p.tun_if) |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 1005 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1006 | p.tun_sa_out = VppIpsecSA( |
| 1007 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1008 | p.vpp_tun_sa_id, |
| 1009 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1010 | p.auth_algo_vpp_id, |
| 1011 | p.auth_key, |
| 1012 | p.crypt_algo_vpp_id, |
| 1013 | p.crypt_key, |
| 1014 | self.vpp_esp_protocol, |
| 1015 | flags=p.flags, |
| 1016 | salt=p.salt, |
| 1017 | ) |
| 1018 | p.tun_sa_in = VppIpsecSA( |
| 1019 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1020 | p.scapy_tun_sa_id, |
| 1021 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1022 | p.auth_algo_vpp_id, |
| 1023 | p.auth_key, |
| 1024 | p.crypt_algo_vpp_id, |
| 1025 | p.crypt_key, |
| 1026 | self.vpp_esp_protocol, |
| 1027 | flags=p.flags, |
| 1028 | salt=p.salt, |
| 1029 | ) |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 1030 | p.tun_sa_in.add_vpp_config() |
| 1031 | p.tun_sa_out.add_vpp_config() |
| 1032 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1033 | self.config_protect(p) |
| 1034 | np.tun_sa_out.remove_vpp_config() |
| 1035 | np.tun_sa_in.remove_vpp_config() |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 1036 | self.logger.info(self.vapi.cli("sh ipsec sa")) |
| 1037 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 1038 | def test_tun_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1039 | """IPSEC tunnel all algos""" |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 1040 | |
| 1041 | # foreach VPP crypto engine |
| 1042 | engines = ["ia32", "ipsecmb", "openssl"] |
| 1043 | |
| 1044 | # foreach crypto algorithm |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1045 | algos = [ |
| 1046 | { |
| 1047 | "vpp-crypto": ( |
| 1048 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_GCM_128 |
| 1049 | ), |
| 1050 | "vpp-integ": ( |
| 1051 | VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE |
| 1052 | ), |
| 1053 | "scapy-crypto": "AES-GCM", |
| 1054 | "scapy-integ": "NULL", |
| 1055 | "key": b"JPjyOWBeVEQiMe7h", |
| 1056 | "salt": 3333, |
| 1057 | }, |
| 1058 | { |
| 1059 | "vpp-crypto": ( |
| 1060 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_GCM_192 |
| 1061 | ), |
| 1062 | "vpp-integ": ( |
| 1063 | VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE |
| 1064 | ), |
| 1065 | "scapy-crypto": "AES-GCM", |
| 1066 | "scapy-integ": "NULL", |
| 1067 | "key": b"JPjyOWBeVEQiMe7hJPjyOWBe", |
| 1068 | "salt": 0, |
| 1069 | }, |
| 1070 | { |
| 1071 | "vpp-crypto": ( |
| 1072 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_GCM_256 |
| 1073 | ), |
| 1074 | "vpp-integ": ( |
| 1075 | VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE |
| 1076 | ), |
| 1077 | "scapy-crypto": "AES-GCM", |
| 1078 | "scapy-integ": "NULL", |
| 1079 | "key": b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h", |
| 1080 | "salt": 9999, |
| 1081 | }, |
| 1082 | { |
| 1083 | "vpp-crypto": ( |
| 1084 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_CBC_128 |
| 1085 | ), |
| 1086 | "vpp-integ": ( |
| 1087 | VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA1_96 |
| 1088 | ), |
| 1089 | "scapy-crypto": "AES-CBC", |
| 1090 | "scapy-integ": "HMAC-SHA1-96", |
| 1091 | "salt": 0, |
| 1092 | "key": b"JPjyOWBeVEQiMe7h", |
| 1093 | }, |
| 1094 | { |
| 1095 | "vpp-crypto": ( |
| 1096 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_CBC_192 |
| 1097 | ), |
| 1098 | "vpp-integ": ( |
| 1099 | VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA_512_256 |
| 1100 | ), |
| 1101 | "scapy-crypto": "AES-CBC", |
| 1102 | "scapy-integ": "SHA2-512-256", |
| 1103 | "salt": 0, |
| 1104 | "key": b"JPjyOWBeVEQiMe7hJPjyOWBe", |
| 1105 | }, |
| 1106 | { |
| 1107 | "vpp-crypto": ( |
| 1108 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_AES_CBC_256 |
| 1109 | ), |
| 1110 | "vpp-integ": ( |
| 1111 | VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA_256_128 |
| 1112 | ), |
| 1113 | "scapy-crypto": "AES-CBC", |
| 1114 | "scapy-integ": "SHA2-256-128", |
| 1115 | "salt": 0, |
| 1116 | "key": b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h", |
| 1117 | }, |
| 1118 | { |
| 1119 | "vpp-crypto": ( |
| 1120 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_NONE |
| 1121 | ), |
| 1122 | "vpp-integ": ( |
| 1123 | VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA1_96 |
| 1124 | ), |
| 1125 | "scapy-crypto": "NULL", |
| 1126 | "scapy-integ": "HMAC-SHA1-96", |
| 1127 | "salt": 0, |
| 1128 | "key": b"JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h", |
| 1129 | }, |
| 1130 | ] |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 1131 | |
| 1132 | for engine in engines: |
| 1133 | self.vapi.cli("set crypto handler all %s" % engine) |
| 1134 | |
| 1135 | # |
| 1136 | # loop through each of the algorithms |
| 1137 | # |
| 1138 | for algo in algos: |
| 1139 | # with self.subTest(algo=algo['scapy']): |
| 1140 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1141 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1142 | p.auth_algo_vpp_id = algo["vpp-integ"] |
| 1143 | p.crypt_algo_vpp_id = algo["vpp-crypto"] |
| 1144 | p.crypt_algo = algo["scapy-crypto"] |
| 1145 | p.auth_algo = algo["scapy-integ"] |
| 1146 | p.crypt_key = algo["key"] |
| 1147 | p.salt = algo["salt"] |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 1148 | |
Neale Ranns | d6c9e82 | 2019-04-17 16:29:00 -0700 | [diff] [blame] | 1149 | # |
| 1150 | # rekey the tunnel |
| 1151 | # |
| 1152 | self.rekey(p) |
| 1153 | self.verify_tun_44(p, count=127) |
| 1154 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 1155 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1156 | class TestIpsec4TunIfEspNoAlgo(TemplateIpsec4TunProtect, TemplateIpsec, IpsecTun4): |
| 1157 | """IPsec IPv4 Tunnel interface no Algos""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1158 | |
| 1159 | encryption_type = ESP |
| 1160 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1161 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1162 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1163 | def setUp(self): |
| 1164 | super(TestIpsec4TunIfEspNoAlgo, self).setUp() |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1165 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1166 | self.tun_if = self.pg0 |
| 1167 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1168 | p.auth_algo_vpp_id = VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE |
| 1169 | p.auth_algo = "NULL" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1170 | p.auth_key = [] |
| 1171 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1172 | p.crypt_algo_vpp_id = ( |
| 1173 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_NONE |
| 1174 | ) |
| 1175 | p.crypt_algo = "NULL" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1176 | p.crypt_key = [] |
| 1177 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1178 | def tearDown(self): |
| 1179 | super(TestIpsec4TunIfEspNoAlgo, self).tearDown() |
| 1180 | |
| 1181 | def test_tun_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1182 | """IPSec SA with NULL algos""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1183 | p = self.ipv4_params |
| 1184 | |
| 1185 | self.config_network(p) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1186 | self.config_sa_tra(p) |
| 1187 | self.config_protect(p) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1188 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1189 | 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] | 1190 | self.send_and_assert_no_replies(self.pg1, tx) |
| 1191 | |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1192 | self.unconfig_protect(p) |
| 1193 | self.unconfig_sa(p) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1194 | self.unconfig_network(p) |
| 1195 | |
| 1196 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 1197 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1198 | class TestIpsec6MultiTunIfEsp(TemplateIpsec6TunProtect, TemplateIpsec, IpsecTun6): |
| 1199 | """IPsec IPv6 Multi Tunnel interface""" |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1200 | |
| 1201 | encryption_type = ESP |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 1202 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1203 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1204 | |
| 1205 | def setUp(self): |
| 1206 | super(TestIpsec6MultiTunIfEsp, self).setUp() |
| 1207 | |
| 1208 | self.tun_if = self.pg0 |
| 1209 | |
| 1210 | self.multi_params = [] |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1211 | self.pg0.generate_remote_hosts(10) |
| 1212 | self.pg0.configure_ipv6_neighbors() |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1213 | |
| 1214 | for ii in range(10): |
| 1215 | p = copy.copy(self.ipv6_params) |
| 1216 | |
| 1217 | p.remote_tun_if_host = "1111::%d" % (ii + 1) |
| 1218 | p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii |
| 1219 | p.scapy_tun_spi = p.scapy_tun_spi + ii |
| 1220 | p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii |
| 1221 | p.vpp_tun_spi = p.vpp_tun_spi + ii |
| 1222 | |
| 1223 | p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii |
| 1224 | p.scapy_tra_spi = p.scapy_tra_spi + ii |
| 1225 | p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii |
| 1226 | p.vpp_tra_spi = p.vpp_tra_spi + ii |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1227 | p.tun_dst = self.pg0.remote_hosts[ii].ip6 |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1228 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1229 | self.multi_params.append(p) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1230 | self.config_network(p) |
| 1231 | self.config_sa_tra(p) |
| 1232 | self.config_protect(p) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1233 | |
| 1234 | def tearDown(self): |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1235 | super(TestIpsec6MultiTunIfEsp, self).tearDown() |
| 1236 | |
| 1237 | def test_tun_66(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1238 | """Multiple IPSEC tunnel interfaces""" |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1239 | for p in self.multi_params: |
| 1240 | self.verify_tun_66(p, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 1241 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 1242 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1243 | |
| 1244 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1245 | class TestIpsecGreTebIfEsp(TemplateIpsec, IpsecTun4Tests): |
| 1246 | """Ipsec GRE TEB ESP - TUN tests""" |
| 1247 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1248 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1249 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1250 | encryption_type = ESP |
| 1251 | omac = "00:11:22:33:44:55" |
| 1252 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1253 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 1254 | return [ |
| 1255 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 1256 | / sa.encrypt( |
| 1257 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) |
| 1258 | / GRE() |
| 1259 | / Ether(dst=self.omac) |
| 1260 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 1261 | / UDP(sport=1144, dport=2233) |
| 1262 | / Raw(b"X" * payload_size) |
| 1263 | ) |
| 1264 | for i in range(count) |
| 1265 | ] |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1266 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1267 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100): |
| 1268 | return [ |
| 1269 | Ether(dst=self.omac) |
| 1270 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 1271 | / UDP(sport=1144, dport=2233) |
| 1272 | / Raw(b"X" * payload_size) |
| 1273 | for i in range(count) |
| 1274 | ] |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1275 | |
| 1276 | def verify_decrypted(self, p, rxs): |
| 1277 | for rx in rxs: |
| 1278 | self.assert_equal(rx[Ether].dst, self.omac) |
| 1279 | self.assert_equal(rx[IP].dst, "1.1.1.2") |
| 1280 | |
| 1281 | def verify_encrypted(self, p, sa, rxs): |
| 1282 | for rx in rxs: |
| 1283 | try: |
| 1284 | pkt = sa.decrypt(rx[IP]) |
| 1285 | if not pkt.haslayer(IP): |
| 1286 | pkt = IP(pkt[Raw].load) |
| 1287 | self.assert_packet_checksums_valid(pkt) |
| 1288 | self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4) |
| 1289 | self.assert_equal(pkt[IP].src, self.pg0.local_ip4) |
| 1290 | self.assertTrue(pkt.haslayer(GRE)) |
| 1291 | e = pkt[Ether] |
| 1292 | self.assertEqual(e[Ether].dst, self.omac) |
| 1293 | self.assertEqual(e[IP].dst, "1.1.1.2") |
| 1294 | except (IndexError, AssertionError): |
| 1295 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 1296 | try: |
| 1297 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 1298 | except: |
| 1299 | pass |
| 1300 | raise |
| 1301 | |
| 1302 | def setUp(self): |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1303 | super(TestIpsecGreTebIfEsp, self).setUp() |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1304 | |
| 1305 | self.tun_if = self.pg0 |
| 1306 | |
| 1307 | p = self.ipv4_params |
| 1308 | |
| 1309 | bd1 = VppBridgeDomain(self, 1) |
| 1310 | bd1.add_vpp_config() |
| 1311 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1312 | p.tun_sa_out = VppIpsecSA( |
| 1313 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1314 | p.vpp_tun_sa_id, |
| 1315 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1316 | p.auth_algo_vpp_id, |
| 1317 | p.auth_key, |
| 1318 | p.crypt_algo_vpp_id, |
| 1319 | p.crypt_key, |
| 1320 | self.vpp_esp_protocol, |
| 1321 | self.pg0.local_ip4, |
| 1322 | self.pg0.remote_ip4, |
| 1323 | ) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1324 | p.tun_sa_out.add_vpp_config() |
| 1325 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1326 | p.tun_sa_in = VppIpsecSA( |
| 1327 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1328 | p.scapy_tun_sa_id, |
| 1329 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1330 | p.auth_algo_vpp_id, |
| 1331 | p.auth_key, |
| 1332 | p.crypt_algo_vpp_id, |
| 1333 | p.crypt_key, |
| 1334 | self.vpp_esp_protocol, |
| 1335 | self.pg0.remote_ip4, |
| 1336 | self.pg0.local_ip4, |
| 1337 | ) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1338 | p.tun_sa_in.add_vpp_config() |
| 1339 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1340 | p.tun_if = VppGreInterface( |
| 1341 | self, |
| 1342 | self.pg0.local_ip4, |
| 1343 | self.pg0.remote_ip4, |
| 1344 | type=(VppEnum.vl_api_gre_tunnel_type_t.GRE_API_TUNNEL_TYPE_TEB), |
| 1345 | ) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1346 | p.tun_if.add_vpp_config() |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1347 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1348 | 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] | 1349 | |
| 1350 | p.tun_protect.add_vpp_config() |
| 1351 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1352 | p.tun_if.admin_up() |
| 1353 | p.tun_if.config_ip4() |
| 1354 | config_tun_params(p, self.encryption_type, p.tun_if) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1355 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1356 | VppBridgeDomainPort(self, bd1, p.tun_if).add_vpp_config() |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1357 | VppBridgeDomainPort(self, bd1, self.pg1).add_vpp_config() |
| 1358 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1359 | self.vapi.cli("clear ipsec sa") |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1360 | self.vapi.cli("sh adj") |
| 1361 | self.vapi.cli("sh ipsec tun") |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1362 | |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1363 | def tearDown(self): |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1364 | p = self.ipv4_params |
| 1365 | p.tun_if.unconfig_ip4() |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1366 | super(TestIpsecGreTebIfEsp, self).tearDown() |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1367 | |
| 1368 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1369 | class TestIpsecGreTebVlanIfEsp(TemplateIpsec, IpsecTun4Tests): |
| 1370 | """Ipsec GRE TEB ESP - TUN tests""" |
| 1371 | |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1372 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1373 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1374 | encryption_type = ESP |
| 1375 | omac = "00:11:22:33:44:55" |
| 1376 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1377 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 1378 | return [ |
| 1379 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 1380 | / sa.encrypt( |
| 1381 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) |
| 1382 | / GRE() |
| 1383 | / Ether(dst=self.omac) |
| 1384 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 1385 | / UDP(sport=1144, dport=2233) |
| 1386 | / Raw(b"X" * payload_size) |
| 1387 | ) |
| 1388 | for i in range(count) |
| 1389 | ] |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1390 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1391 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100): |
| 1392 | return [ |
| 1393 | Ether(dst=self.omac) |
| 1394 | / Dot1Q(vlan=11) |
| 1395 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 1396 | / UDP(sport=1144, dport=2233) |
| 1397 | / Raw(b"X" * payload_size) |
| 1398 | for i in range(count) |
| 1399 | ] |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1400 | |
| 1401 | def verify_decrypted(self, p, rxs): |
| 1402 | for rx in rxs: |
| 1403 | self.assert_equal(rx[Ether].dst, self.omac) |
| 1404 | self.assert_equal(rx[Dot1Q].vlan, 11) |
| 1405 | self.assert_equal(rx[IP].dst, "1.1.1.2") |
| 1406 | |
| 1407 | def verify_encrypted(self, p, sa, rxs): |
| 1408 | for rx in rxs: |
| 1409 | try: |
| 1410 | pkt = sa.decrypt(rx[IP]) |
| 1411 | if not pkt.haslayer(IP): |
| 1412 | pkt = IP(pkt[Raw].load) |
| 1413 | self.assert_packet_checksums_valid(pkt) |
| 1414 | self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4) |
| 1415 | self.assert_equal(pkt[IP].src, self.pg0.local_ip4) |
| 1416 | self.assertTrue(pkt.haslayer(GRE)) |
| 1417 | e = pkt[Ether] |
| 1418 | self.assertEqual(e[Ether].dst, self.omac) |
| 1419 | self.assertFalse(e.haslayer(Dot1Q)) |
| 1420 | self.assertEqual(e[IP].dst, "1.1.1.2") |
| 1421 | except (IndexError, AssertionError): |
| 1422 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 1423 | try: |
| 1424 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 1425 | except: |
| 1426 | pass |
| 1427 | raise |
| 1428 | |
| 1429 | def setUp(self): |
| 1430 | super(TestIpsecGreTebVlanIfEsp, self).setUp() |
| 1431 | |
| 1432 | self.tun_if = self.pg0 |
| 1433 | |
| 1434 | p = self.ipv4_params |
| 1435 | |
| 1436 | bd1 = VppBridgeDomain(self, 1) |
| 1437 | bd1.add_vpp_config() |
| 1438 | |
| 1439 | self.pg1_11 = VppDot1QSubint(self, self.pg1, 11) |
| 1440 | self.vapi.l2_interface_vlan_tag_rewrite( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1441 | sw_if_index=self.pg1_11.sw_if_index, |
| 1442 | vtr_op=L2_VTR_OP.L2_POP_1, |
| 1443 | push_dot1q=11, |
| 1444 | ) |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1445 | self.pg1_11.admin_up() |
| 1446 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1447 | p.tun_sa_out = VppIpsecSA( |
| 1448 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1449 | p.vpp_tun_sa_id, |
| 1450 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1451 | p.auth_algo_vpp_id, |
| 1452 | p.auth_key, |
| 1453 | p.crypt_algo_vpp_id, |
| 1454 | p.crypt_key, |
| 1455 | self.vpp_esp_protocol, |
| 1456 | self.pg0.local_ip4, |
| 1457 | self.pg0.remote_ip4, |
| 1458 | ) |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1459 | p.tun_sa_out.add_vpp_config() |
| 1460 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1461 | p.tun_sa_in = VppIpsecSA( |
| 1462 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1463 | p.scapy_tun_sa_id, |
| 1464 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1465 | p.auth_algo_vpp_id, |
| 1466 | p.auth_key, |
| 1467 | p.crypt_algo_vpp_id, |
| 1468 | p.crypt_key, |
| 1469 | self.vpp_esp_protocol, |
| 1470 | self.pg0.remote_ip4, |
| 1471 | self.pg0.local_ip4, |
| 1472 | ) |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1473 | p.tun_sa_in.add_vpp_config() |
| 1474 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1475 | p.tun_if = VppGreInterface( |
| 1476 | self, |
| 1477 | self.pg0.local_ip4, |
| 1478 | self.pg0.remote_ip4, |
| 1479 | type=(VppEnum.vl_api_gre_tunnel_type_t.GRE_API_TUNNEL_TYPE_TEB), |
| 1480 | ) |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 1481 | p.tun_if.add_vpp_config() |
| 1482 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1483 | 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] | 1484 | |
| 1485 | p.tun_protect.add_vpp_config() |
| 1486 | |
| 1487 | p.tun_if.admin_up() |
| 1488 | p.tun_if.config_ip4() |
| 1489 | config_tun_params(p, self.encryption_type, p.tun_if) |
| 1490 | |
| 1491 | VppBridgeDomainPort(self, bd1, p.tun_if).add_vpp_config() |
| 1492 | VppBridgeDomainPort(self, bd1, self.pg1_11).add_vpp_config() |
| 1493 | |
| 1494 | self.vapi.cli("clear ipsec sa") |
| 1495 | |
| 1496 | def tearDown(self): |
| 1497 | p = self.ipv4_params |
| 1498 | p.tun_if.unconfig_ip4() |
| 1499 | super(TestIpsecGreTebVlanIfEsp, self).tearDown() |
| 1500 | self.pg1_11.admin_down() |
| 1501 | self.pg1_11.remove_vpp_config() |
| 1502 | |
| 1503 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1504 | class TestIpsecGreTebIfEspTra(TemplateIpsec, IpsecTun4Tests): |
| 1505 | """Ipsec GRE TEB ESP - Tra tests""" |
| 1506 | |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1507 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1508 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1509 | encryption_type = ESP |
| 1510 | omac = "00:11:22:33:44:55" |
| 1511 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1512 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 1513 | return [ |
| 1514 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 1515 | / sa.encrypt( |
| 1516 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) |
| 1517 | / GRE() |
| 1518 | / Ether(dst=self.omac) |
| 1519 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 1520 | / UDP(sport=1144, dport=2233) |
| 1521 | / Raw(b"X" * payload_size) |
| 1522 | ) |
| 1523 | for i in range(count) |
| 1524 | ] |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1525 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1526 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100): |
| 1527 | return [ |
| 1528 | Ether(dst=self.omac) |
| 1529 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 1530 | / UDP(sport=1144, dport=2233) |
| 1531 | / Raw(b"X" * payload_size) |
| 1532 | for i in range(count) |
| 1533 | ] |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1534 | |
| 1535 | def verify_decrypted(self, p, rxs): |
| 1536 | for rx in rxs: |
| 1537 | self.assert_equal(rx[Ether].dst, self.omac) |
| 1538 | self.assert_equal(rx[IP].dst, "1.1.1.2") |
| 1539 | |
| 1540 | def verify_encrypted(self, p, sa, rxs): |
| 1541 | for rx in rxs: |
| 1542 | try: |
| 1543 | pkt = sa.decrypt(rx[IP]) |
| 1544 | if not pkt.haslayer(IP): |
| 1545 | pkt = IP(pkt[Raw].load) |
| 1546 | self.assert_packet_checksums_valid(pkt) |
| 1547 | self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4) |
| 1548 | self.assert_equal(pkt[IP].src, self.pg0.local_ip4) |
| 1549 | self.assertTrue(pkt.haslayer(GRE)) |
| 1550 | e = pkt[Ether] |
| 1551 | self.assertEqual(e[Ether].dst, self.omac) |
| 1552 | self.assertEqual(e[IP].dst, "1.1.1.2") |
| 1553 | except (IndexError, AssertionError): |
| 1554 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 1555 | try: |
| 1556 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 1557 | except: |
| 1558 | pass |
| 1559 | raise |
| 1560 | |
| 1561 | def setUp(self): |
| 1562 | super(TestIpsecGreTebIfEspTra, self).setUp() |
| 1563 | |
| 1564 | self.tun_if = self.pg0 |
| 1565 | |
| 1566 | p = self.ipv4_params |
| 1567 | |
| 1568 | bd1 = VppBridgeDomain(self, 1) |
| 1569 | bd1.add_vpp_config() |
| 1570 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1571 | p.tun_sa_out = VppIpsecSA( |
| 1572 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1573 | p.vpp_tun_sa_id, |
| 1574 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1575 | p.auth_algo_vpp_id, |
| 1576 | p.auth_key, |
| 1577 | p.crypt_algo_vpp_id, |
| 1578 | p.crypt_key, |
| 1579 | self.vpp_esp_protocol, |
| 1580 | ) |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1581 | p.tun_sa_out.add_vpp_config() |
| 1582 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1583 | p.tun_sa_in = VppIpsecSA( |
| 1584 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1585 | p.scapy_tun_sa_id, |
| 1586 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1587 | p.auth_algo_vpp_id, |
| 1588 | p.auth_key, |
| 1589 | p.crypt_algo_vpp_id, |
| 1590 | p.crypt_key, |
| 1591 | self.vpp_esp_protocol, |
| 1592 | ) |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1593 | p.tun_sa_in.add_vpp_config() |
| 1594 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1595 | p.tun_if = VppGreInterface( |
| 1596 | self, |
| 1597 | self.pg0.local_ip4, |
| 1598 | self.pg0.remote_ip4, |
| 1599 | type=(VppEnum.vl_api_gre_tunnel_type_t.GRE_API_TUNNEL_TYPE_TEB), |
| 1600 | ) |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1601 | p.tun_if.add_vpp_config() |
| 1602 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1603 | 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] | 1604 | |
| 1605 | p.tun_protect.add_vpp_config() |
| 1606 | |
| 1607 | p.tun_if.admin_up() |
| 1608 | p.tun_if.config_ip4() |
| 1609 | config_tra_params(p, self.encryption_type, p.tun_if) |
| 1610 | |
| 1611 | VppBridgeDomainPort(self, bd1, p.tun_if).add_vpp_config() |
| 1612 | VppBridgeDomainPort(self, bd1, self.pg1).add_vpp_config() |
| 1613 | |
| 1614 | self.vapi.cli("clear ipsec sa") |
| 1615 | |
| 1616 | def tearDown(self): |
| 1617 | p = self.ipv4_params |
| 1618 | p.tun_if.unconfig_ip4() |
| 1619 | super(TestIpsecGreTebIfEspTra, self).tearDown() |
| 1620 | |
| 1621 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1622 | class TestIpsecGreTebUdpIfEspTra(TemplateIpsec, IpsecTun4Tests): |
| 1623 | """Ipsec GRE TEB UDP ESP - Tra tests""" |
| 1624 | |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1625 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1626 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1627 | encryption_type = ESP |
| 1628 | omac = "00:11:22:33:44:55" |
| 1629 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1630 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 1631 | return [ |
| 1632 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 1633 | / sa.encrypt( |
| 1634 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) |
| 1635 | / GRE() |
| 1636 | / Ether(dst=self.omac) |
| 1637 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 1638 | / UDP(sport=1144, dport=2233) |
| 1639 | / Raw(b"X" * payload_size) |
| 1640 | ) |
| 1641 | for i in range(count) |
| 1642 | ] |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1643 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1644 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100): |
| 1645 | return [ |
| 1646 | Ether(dst=self.omac) |
| 1647 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 1648 | / UDP(sport=1144, dport=2233) |
| 1649 | / Raw(b"X" * payload_size) |
| 1650 | for i in range(count) |
| 1651 | ] |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1652 | |
| 1653 | def verify_decrypted(self, p, rxs): |
| 1654 | for rx in rxs: |
| 1655 | self.assert_equal(rx[Ether].dst, self.omac) |
| 1656 | self.assert_equal(rx[IP].dst, "1.1.1.2") |
| 1657 | |
| 1658 | def verify_encrypted(self, p, sa, rxs): |
| 1659 | for rx in rxs: |
| 1660 | self.assertTrue(rx.haslayer(UDP)) |
| 1661 | self.assertEqual(rx[UDP].dport, 4545) |
| 1662 | self.assertEqual(rx[UDP].sport, 5454) |
| 1663 | try: |
| 1664 | pkt = sa.decrypt(rx[IP]) |
| 1665 | if not pkt.haslayer(IP): |
| 1666 | pkt = IP(pkt[Raw].load) |
| 1667 | self.assert_packet_checksums_valid(pkt) |
| 1668 | self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4) |
| 1669 | self.assert_equal(pkt[IP].src, self.pg0.local_ip4) |
| 1670 | self.assertTrue(pkt.haslayer(GRE)) |
| 1671 | e = pkt[Ether] |
| 1672 | self.assertEqual(e[Ether].dst, self.omac) |
| 1673 | self.assertEqual(e[IP].dst, "1.1.1.2") |
| 1674 | except (IndexError, AssertionError): |
| 1675 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 1676 | try: |
| 1677 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 1678 | except: |
| 1679 | pass |
| 1680 | raise |
| 1681 | |
| 1682 | def setUp(self): |
| 1683 | super(TestIpsecGreTebUdpIfEspTra, self).setUp() |
| 1684 | |
| 1685 | self.tun_if = self.pg0 |
| 1686 | |
| 1687 | p = self.ipv4_params |
| 1688 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1689 | 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] | 1690 | p.nat_header = UDP(sport=5454, dport=4545) |
| 1691 | |
| 1692 | bd1 = VppBridgeDomain(self, 1) |
| 1693 | bd1.add_vpp_config() |
| 1694 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1695 | p.tun_sa_out = VppIpsecSA( |
| 1696 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1697 | p.vpp_tun_sa_id, |
| 1698 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1699 | p.auth_algo_vpp_id, |
| 1700 | p.auth_key, |
| 1701 | p.crypt_algo_vpp_id, |
| 1702 | p.crypt_key, |
| 1703 | self.vpp_esp_protocol, |
| 1704 | flags=p.flags, |
| 1705 | udp_src=5454, |
| 1706 | udp_dst=4545, |
| 1707 | ) |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1708 | p.tun_sa_out.add_vpp_config() |
| 1709 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1710 | p.tun_sa_in = VppIpsecSA( |
| 1711 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1712 | p.scapy_tun_sa_id, |
| 1713 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1714 | p.auth_algo_vpp_id, |
| 1715 | p.auth_key, |
| 1716 | p.crypt_algo_vpp_id, |
| 1717 | p.crypt_key, |
| 1718 | self.vpp_esp_protocol, |
| 1719 | flags=( |
| 1720 | p.flags | VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_IS_INBOUND |
| 1721 | ), |
| 1722 | udp_src=4545, |
| 1723 | udp_dst=5454, |
| 1724 | ) |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1725 | p.tun_sa_in.add_vpp_config() |
| 1726 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1727 | p.tun_if = VppGreInterface( |
| 1728 | self, |
| 1729 | self.pg0.local_ip4, |
| 1730 | self.pg0.remote_ip4, |
| 1731 | type=(VppEnum.vl_api_gre_tunnel_type_t.GRE_API_TUNNEL_TYPE_TEB), |
| 1732 | ) |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 1733 | p.tun_if.add_vpp_config() |
| 1734 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1735 | 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] | 1736 | |
| 1737 | p.tun_protect.add_vpp_config() |
| 1738 | |
| 1739 | p.tun_if.admin_up() |
| 1740 | p.tun_if.config_ip4() |
| 1741 | config_tra_params(p, self.encryption_type, p.tun_if) |
| 1742 | |
| 1743 | VppBridgeDomainPort(self, bd1, p.tun_if).add_vpp_config() |
| 1744 | VppBridgeDomainPort(self, bd1, self.pg1).add_vpp_config() |
| 1745 | |
| 1746 | self.vapi.cli("clear ipsec sa") |
| 1747 | self.logger.info(self.vapi.cli("sh ipsec sa 0")) |
| 1748 | |
| 1749 | def tearDown(self): |
| 1750 | p = self.ipv4_params |
| 1751 | p.tun_if.unconfig_ip4() |
| 1752 | super(TestIpsecGreTebUdpIfEspTra, self).tearDown() |
| 1753 | |
| 1754 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1755 | class TestIpsecGreIfEsp(TemplateIpsec, IpsecTun4Tests): |
| 1756 | """Ipsec GRE ESP - TUN tests""" |
| 1757 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1758 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1759 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1760 | encryption_type = ESP |
| 1761 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1762 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 1763 | return [ |
| 1764 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 1765 | / sa.encrypt( |
| 1766 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) |
| 1767 | / GRE() |
| 1768 | / IP(src=self.pg1.local_ip4, dst=self.pg1.remote_ip4) |
| 1769 | / UDP(sport=1144, dport=2233) |
| 1770 | / Raw(b"X" * payload_size) |
| 1771 | ) |
| 1772 | for i in range(count) |
| 1773 | ] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1774 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1775 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100): |
| 1776 | return [ |
| 1777 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 1778 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 1779 | / UDP(sport=1144, dport=2233) |
| 1780 | / Raw(b"X" * payload_size) |
| 1781 | for i in range(count) |
| 1782 | ] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1783 | |
| 1784 | def verify_decrypted(self, p, rxs): |
| 1785 | for rx in rxs: |
| 1786 | self.assert_equal(rx[Ether].dst, self.pg1.remote_mac) |
| 1787 | self.assert_equal(rx[IP].dst, self.pg1.remote_ip4) |
| 1788 | |
| 1789 | def verify_encrypted(self, p, sa, rxs): |
| 1790 | for rx in rxs: |
| 1791 | try: |
| 1792 | pkt = sa.decrypt(rx[IP]) |
| 1793 | if not pkt.haslayer(IP): |
| 1794 | pkt = IP(pkt[Raw].load) |
| 1795 | self.assert_packet_checksums_valid(pkt) |
| 1796 | self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4) |
| 1797 | self.assert_equal(pkt[IP].src, self.pg0.local_ip4) |
| 1798 | self.assertTrue(pkt.haslayer(GRE)) |
| 1799 | e = pkt[GRE] |
| 1800 | self.assertEqual(e[IP].dst, "1.1.1.2") |
| 1801 | except (IndexError, AssertionError): |
| 1802 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 1803 | try: |
| 1804 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 1805 | except: |
| 1806 | pass |
| 1807 | raise |
| 1808 | |
| 1809 | def setUp(self): |
| 1810 | super(TestIpsecGreIfEsp, self).setUp() |
| 1811 | |
| 1812 | self.tun_if = self.pg0 |
| 1813 | |
| 1814 | p = self.ipv4_params |
| 1815 | |
| 1816 | bd1 = VppBridgeDomain(self, 1) |
| 1817 | bd1.add_vpp_config() |
| 1818 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1819 | p.tun_sa_out = VppIpsecSA( |
| 1820 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1821 | p.vpp_tun_sa_id, |
| 1822 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1823 | p.auth_algo_vpp_id, |
| 1824 | p.auth_key, |
| 1825 | p.crypt_algo_vpp_id, |
| 1826 | p.crypt_key, |
| 1827 | self.vpp_esp_protocol, |
| 1828 | self.pg0.local_ip4, |
| 1829 | self.pg0.remote_ip4, |
| 1830 | ) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1831 | p.tun_sa_out.add_vpp_config() |
| 1832 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1833 | p.tun_sa_in = VppIpsecSA( |
| 1834 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1835 | p.scapy_tun_sa_id, |
| 1836 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1837 | p.auth_algo_vpp_id, |
| 1838 | p.auth_key, |
| 1839 | p.crypt_algo_vpp_id, |
| 1840 | p.crypt_key, |
| 1841 | self.vpp_esp_protocol, |
| 1842 | self.pg0.remote_ip4, |
| 1843 | self.pg0.local_ip4, |
| 1844 | ) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1845 | p.tun_sa_in.add_vpp_config() |
| 1846 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1847 | 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] | 1848 | p.tun_if.add_vpp_config() |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1849 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1850 | 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] | 1851 | p.tun_protect.add_vpp_config() |
| 1852 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1853 | p.tun_if.admin_up() |
| 1854 | p.tun_if.config_ip4() |
| 1855 | config_tun_params(p, self.encryption_type, p.tun_if) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1856 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1857 | VppIpRoute( |
| 1858 | self, "1.1.1.2", 32, [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)] |
| 1859 | ).add_vpp_config() |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1860 | |
| 1861 | def tearDown(self): |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 1862 | p = self.ipv4_params |
| 1863 | p.tun_if.unconfig_ip4() |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1864 | super(TestIpsecGreIfEsp, self).tearDown() |
| 1865 | |
| 1866 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1867 | class TestIpsecGreIfEspTra(TemplateIpsec, IpsecTun4Tests): |
| 1868 | """Ipsec GRE ESP - TRA tests""" |
| 1869 | |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1870 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1871 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1872 | encryption_type = ESP |
| 1873 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1874 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 1875 | return [ |
| 1876 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 1877 | / sa.encrypt( |
| 1878 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) |
| 1879 | / GRE() |
| 1880 | / IP(src=self.pg1.local_ip4, dst=self.pg1.remote_ip4) |
| 1881 | / UDP(sport=1144, dport=2233) |
| 1882 | / Raw(b"X" * payload_size) |
| 1883 | ) |
| 1884 | for i in range(count) |
| 1885 | ] |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1886 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1887 | def gen_encrypt_non_ip_pkts(self, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 1888 | return [ |
| 1889 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 1890 | / sa.encrypt( |
| 1891 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) |
| 1892 | / GRE() |
| 1893 | / UDP(sport=1144, dport=2233) |
| 1894 | / Raw(b"X" * payload_size) |
| 1895 | ) |
| 1896 | for i in range(count) |
| 1897 | ] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1898 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1899 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100): |
| 1900 | return [ |
| 1901 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 1902 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 1903 | / UDP(sport=1144, dport=2233) |
| 1904 | / Raw(b"X" * payload_size) |
| 1905 | for i in range(count) |
| 1906 | ] |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1907 | |
| 1908 | def verify_decrypted(self, p, rxs): |
| 1909 | for rx in rxs: |
| 1910 | self.assert_equal(rx[Ether].dst, self.pg1.remote_mac) |
| 1911 | self.assert_equal(rx[IP].dst, self.pg1.remote_ip4) |
| 1912 | |
| 1913 | def verify_encrypted(self, p, sa, rxs): |
| 1914 | for rx in rxs: |
| 1915 | try: |
| 1916 | pkt = sa.decrypt(rx[IP]) |
| 1917 | if not pkt.haslayer(IP): |
| 1918 | pkt = IP(pkt[Raw].load) |
| 1919 | self.assert_packet_checksums_valid(pkt) |
| 1920 | self.assertTrue(pkt.haslayer(GRE)) |
| 1921 | e = pkt[GRE] |
| 1922 | self.assertEqual(e[IP].dst, "1.1.1.2") |
| 1923 | except (IndexError, AssertionError): |
| 1924 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 1925 | try: |
| 1926 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 1927 | except: |
| 1928 | pass |
| 1929 | raise |
| 1930 | |
| 1931 | def setUp(self): |
| 1932 | super(TestIpsecGreIfEspTra, self).setUp() |
| 1933 | |
| 1934 | self.tun_if = self.pg0 |
| 1935 | |
| 1936 | p = self.ipv4_params |
| 1937 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1938 | p.tun_sa_out = VppIpsecSA( |
| 1939 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1940 | p.vpp_tun_sa_id, |
| 1941 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1942 | p.auth_algo_vpp_id, |
| 1943 | p.auth_key, |
| 1944 | p.crypt_algo_vpp_id, |
| 1945 | p.crypt_key, |
| 1946 | self.vpp_esp_protocol, |
| 1947 | ) |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1948 | p.tun_sa_out.add_vpp_config() |
| 1949 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1950 | p.tun_sa_in = VppIpsecSA( |
| 1951 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 1952 | p.scapy_tun_sa_id, |
| 1953 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1954 | p.auth_algo_vpp_id, |
| 1955 | p.auth_key, |
| 1956 | p.crypt_algo_vpp_id, |
| 1957 | p.crypt_key, |
| 1958 | self.vpp_esp_protocol, |
| 1959 | ) |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1960 | p.tun_sa_in.add_vpp_config() |
| 1961 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1962 | 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] | 1963 | p.tun_if.add_vpp_config() |
| 1964 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1965 | 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] | 1966 | p.tun_protect.add_vpp_config() |
| 1967 | |
| 1968 | p.tun_if.admin_up() |
| 1969 | p.tun_if.config_ip4() |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1970 | config_tra_params(p, self.encryption_type, p.tun_if) |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1971 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1972 | VppIpRoute( |
| 1973 | self, "1.1.1.2", 32, [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)] |
| 1974 | ).add_vpp_config() |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 1975 | |
| 1976 | def tearDown(self): |
| 1977 | p = self.ipv4_params |
| 1978 | p.tun_if.unconfig_ip4() |
| 1979 | super(TestIpsecGreIfEspTra, self).tearDown() |
| 1980 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1981 | def test_gre_non_ip(self): |
| 1982 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1983 | tx = self.gen_encrypt_non_ip_pkts( |
| 1984 | p.scapy_tun_sa, |
| 1985 | self.tun_if, |
| 1986 | src=p.remote_tun_if_host, |
| 1987 | dst=self.pg1.remote_ip6, |
| 1988 | ) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1989 | self.send_and_assert_no_replies(self.tun_if, tx) |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1990 | node_name = "/err/%s/unsup_payload" % self.tun4_decrypt_node_name[0] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1991 | self.assertEqual(1, self.statistics.get_err_counter(node_name)) |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1992 | err = p.tun_sa_in.get_err("unsup_payload") |
| 1993 | self.assertEqual(err, 1) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1994 | |
| 1995 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 1996 | class TestIpsecGre6IfEspTra(TemplateIpsec, IpsecTun6Tests): |
| 1997 | """Ipsec GRE ESP - TRA tests""" |
| 1998 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1999 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2000 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2001 | encryption_type = ESP |
| 2002 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2003 | def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 2004 | return [ |
| 2005 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2006 | / sa.encrypt( |
| 2007 | IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) |
| 2008 | / GRE() |
| 2009 | / IPv6(src=self.pg1.local_ip6, dst=self.pg1.remote_ip6) |
| 2010 | / UDP(sport=1144, dport=2233) |
| 2011 | / Raw(b"X" * payload_size) |
| 2012 | ) |
| 2013 | for i in range(count) |
| 2014 | ] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2015 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2016 | def gen_pkts6(self, p, sw_intf, src, dst, count=1, payload_size=100): |
| 2017 | return [ |
| 2018 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2019 | / IPv6(src="1::1", dst="1::2") |
| 2020 | / UDP(sport=1144, dport=2233) |
| 2021 | / Raw(b"X" * payload_size) |
| 2022 | for i in range(count) |
| 2023 | ] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2024 | |
| 2025 | def verify_decrypted6(self, p, rxs): |
| 2026 | for rx in rxs: |
| 2027 | self.assert_equal(rx[Ether].dst, self.pg1.remote_mac) |
| 2028 | self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6) |
| 2029 | |
| 2030 | def verify_encrypted6(self, p, sa, rxs): |
| 2031 | for rx in rxs: |
| 2032 | try: |
| 2033 | pkt = sa.decrypt(rx[IPv6]) |
| 2034 | if not pkt.haslayer(IPv6): |
| 2035 | pkt = IPv6(pkt[Raw].load) |
| 2036 | self.assert_packet_checksums_valid(pkt) |
| 2037 | self.assertTrue(pkt.haslayer(GRE)) |
| 2038 | e = pkt[GRE] |
| 2039 | self.assertEqual(e[IPv6].dst, "1::2") |
| 2040 | except (IndexError, AssertionError): |
| 2041 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 2042 | try: |
| 2043 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 2044 | except: |
| 2045 | pass |
| 2046 | raise |
| 2047 | |
| 2048 | def setUp(self): |
| 2049 | super(TestIpsecGre6IfEspTra, self).setUp() |
| 2050 | |
| 2051 | self.tun_if = self.pg0 |
| 2052 | |
| 2053 | p = self.ipv6_params |
| 2054 | |
| 2055 | bd1 = VppBridgeDomain(self, 1) |
| 2056 | bd1.add_vpp_config() |
| 2057 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2058 | p.tun_sa_out = VppIpsecSA( |
| 2059 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2060 | p.vpp_tun_sa_id, |
| 2061 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2062 | p.auth_algo_vpp_id, |
| 2063 | p.auth_key, |
| 2064 | p.crypt_algo_vpp_id, |
| 2065 | p.crypt_key, |
| 2066 | self.vpp_esp_protocol, |
| 2067 | ) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2068 | p.tun_sa_out.add_vpp_config() |
| 2069 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2070 | p.tun_sa_in = VppIpsecSA( |
| 2071 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2072 | p.scapy_tun_sa_id, |
| 2073 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2074 | p.auth_algo_vpp_id, |
| 2075 | p.auth_key, |
| 2076 | p.crypt_algo_vpp_id, |
| 2077 | p.crypt_key, |
| 2078 | self.vpp_esp_protocol, |
| 2079 | ) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2080 | p.tun_sa_in.add_vpp_config() |
| 2081 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2082 | 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] | 2083 | p.tun_if.add_vpp_config() |
| 2084 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2085 | 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] | 2086 | p.tun_protect.add_vpp_config() |
| 2087 | |
| 2088 | p.tun_if.admin_up() |
| 2089 | p.tun_if.config_ip6() |
| 2090 | config_tra_params(p, self.encryption_type, p.tun_if) |
| 2091 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2092 | r = VppIpRoute( |
| 2093 | self, |
| 2094 | "1::2", |
| 2095 | 128, |
| 2096 | [ |
| 2097 | VppRoutePath( |
| 2098 | p.tun_if.remote_ip6, 0xFFFFFFFF, proto=DpoProto.DPO_PROTO_IP6 |
| 2099 | ) |
| 2100 | ], |
| 2101 | ) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2102 | r.add_vpp_config() |
| 2103 | |
| 2104 | def tearDown(self): |
| 2105 | p = self.ipv6_params |
| 2106 | p.tun_if.unconfig_ip6() |
| 2107 | super(TestIpsecGre6IfEspTra, self).tearDown() |
| 2108 | |
Neale Ranns | abde62f | 2019-12-02 22:32:05 +0000 | [diff] [blame] | 2109 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2110 | class TestIpsecMGreIfEspTra4(TemplateIpsec, IpsecTun4): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2111 | """Ipsec mGRE ESP v4 TRA tests""" |
| 2112 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2113 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2114 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2115 | encryption_type = ESP |
| 2116 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2117 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 2118 | return [ |
| 2119 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2120 | / sa.encrypt( |
| 2121 | IP(src=p.tun_dst, dst=self.pg0.local_ip4) |
| 2122 | / GRE() |
| 2123 | / IP(src=self.pg1.local_ip4, dst=self.pg1.remote_ip4) |
| 2124 | / UDP(sport=1144, dport=2233) |
| 2125 | / Raw(b"X" * payload_size) |
| 2126 | ) |
| 2127 | for i in range(count) |
| 2128 | ] |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2129 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2130 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100): |
| 2131 | return [ |
| 2132 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2133 | / IP(src="1.1.1.1", dst=dst) |
| 2134 | / UDP(sport=1144, dport=2233) |
| 2135 | / Raw(b"X" * payload_size) |
| 2136 | for i in range(count) |
| 2137 | ] |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2138 | |
| 2139 | def verify_decrypted(self, p, rxs): |
| 2140 | for rx in rxs: |
| 2141 | self.assert_equal(rx[Ether].dst, self.pg1.remote_mac) |
| 2142 | self.assert_equal(rx[IP].dst, self.pg1.remote_ip4) |
| 2143 | |
| 2144 | def verify_encrypted(self, p, sa, rxs): |
| 2145 | for rx in rxs: |
| 2146 | try: |
| 2147 | pkt = sa.decrypt(rx[IP]) |
| 2148 | if not pkt.haslayer(IP): |
| 2149 | pkt = IP(pkt[Raw].load) |
| 2150 | self.assert_packet_checksums_valid(pkt) |
| 2151 | self.assertTrue(pkt.haslayer(GRE)) |
| 2152 | e = pkt[GRE] |
| 2153 | self.assertEqual(e[IP].dst, p.remote_tun_if_host) |
| 2154 | except (IndexError, AssertionError): |
| 2155 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 2156 | try: |
| 2157 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 2158 | except: |
| 2159 | pass |
| 2160 | raise |
| 2161 | |
| 2162 | def setUp(self): |
| 2163 | super(TestIpsecMGreIfEspTra4, self).setUp() |
| 2164 | |
| 2165 | N_NHS = 16 |
| 2166 | self.tun_if = self.pg0 |
| 2167 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2168 | p.tun_if = VppGreInterface( |
| 2169 | self, |
| 2170 | self.pg0.local_ip4, |
| 2171 | "0.0.0.0", |
| 2172 | mode=(VppEnum.vl_api_tunnel_mode_t.TUNNEL_API_MODE_MP), |
| 2173 | ) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2174 | p.tun_if.add_vpp_config() |
| 2175 | p.tun_if.admin_up() |
| 2176 | p.tun_if.config_ip4() |
| 2177 | p.tun_if.generate_remote_hosts(N_NHS) |
| 2178 | self.pg0.generate_remote_hosts(N_NHS) |
| 2179 | self.pg0.configure_ipv4_neighbors() |
| 2180 | |
| 2181 | # setup some SAs for several next-hops on the interface |
| 2182 | self.multi_params = [] |
| 2183 | |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 2184 | for ii in range(N_NHS): |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2185 | p = copy.copy(self.ipv4_params) |
| 2186 | |
| 2187 | p.remote_tun_if_host = "1.1.1.%d" % (ii + 1) |
| 2188 | p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii |
| 2189 | p.scapy_tun_spi = p.scapy_tun_spi + ii |
| 2190 | p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii |
| 2191 | p.vpp_tun_spi = p.vpp_tun_spi + ii |
| 2192 | |
| 2193 | p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii |
| 2194 | p.scapy_tra_spi = p.scapy_tra_spi + ii |
| 2195 | p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii |
| 2196 | p.vpp_tra_spi = p.vpp_tra_spi + ii |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2197 | p.tun_sa_out = VppIpsecSA( |
| 2198 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2199 | p.vpp_tun_sa_id, |
| 2200 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2201 | p.auth_algo_vpp_id, |
| 2202 | p.auth_key, |
| 2203 | p.crypt_algo_vpp_id, |
| 2204 | p.crypt_key, |
| 2205 | self.vpp_esp_protocol, |
| 2206 | ) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2207 | p.tun_sa_out.add_vpp_config() |
| 2208 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2209 | p.tun_sa_in = VppIpsecSA( |
| 2210 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2211 | p.scapy_tun_sa_id, |
| 2212 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2213 | p.auth_algo_vpp_id, |
| 2214 | p.auth_key, |
| 2215 | p.crypt_algo_vpp_id, |
| 2216 | p.crypt_key, |
| 2217 | self.vpp_esp_protocol, |
| 2218 | ) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2219 | p.tun_sa_in.add_vpp_config() |
| 2220 | |
| 2221 | p.tun_protect = VppIpsecTunProtect( |
| 2222 | self, |
| 2223 | p.tun_if, |
| 2224 | p.tun_sa_out, |
| 2225 | [p.tun_sa_in], |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2226 | nh=p.tun_if.remote_hosts[ii].ip4, |
| 2227 | ) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2228 | p.tun_protect.add_vpp_config() |
| 2229 | config_tra_params(p, self.encryption_type, p.tun_if) |
| 2230 | self.multi_params.append(p) |
| 2231 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2232 | VppIpRoute( |
| 2233 | self, |
| 2234 | p.remote_tun_if_host, |
| 2235 | 32, |
| 2236 | [VppRoutePath(p.tun_if.remote_hosts[ii].ip4, p.tun_if.sw_if_index)], |
| 2237 | ).add_vpp_config() |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2238 | |
| 2239 | # in this v4 variant add the teibs after the protect |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2240 | p.teib = VppTeib( |
| 2241 | self, |
| 2242 | p.tun_if, |
| 2243 | p.tun_if.remote_hosts[ii].ip4, |
| 2244 | self.pg0.remote_hosts[ii].ip4, |
| 2245 | ).add_vpp_config() |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2246 | p.tun_dst = self.pg0.remote_hosts[ii].ip4 |
| 2247 | self.logger.info(self.vapi.cli("sh ipsec protect-hash")) |
| 2248 | |
| 2249 | def tearDown(self): |
| 2250 | p = self.ipv4_params |
| 2251 | p.tun_if.unconfig_ip4() |
| 2252 | super(TestIpsecMGreIfEspTra4, self).tearDown() |
| 2253 | |
| 2254 | def test_tun_44(self): |
| 2255 | """mGRE IPSEC 44""" |
| 2256 | N_PKTS = 63 |
| 2257 | for p in self.multi_params: |
| 2258 | self.verify_tun_44(p, count=N_PKTS) |
| 2259 | p.teib.remove_vpp_config() |
| 2260 | self.verify_tun_dropped_44(p, count=N_PKTS) |
| 2261 | p.teib.add_vpp_config() |
| 2262 | self.verify_tun_44(p, count=N_PKTS) |
| 2263 | |
| 2264 | |
| 2265 | class TestIpsecMGreIfEspTra6(TemplateIpsec, IpsecTun6): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2266 | """Ipsec mGRE ESP v6 TRA tests""" |
| 2267 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2268 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2269 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2270 | encryption_type = ESP |
| 2271 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2272 | def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 2273 | return [ |
| 2274 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2275 | / sa.encrypt( |
| 2276 | IPv6(src=p.tun_dst, dst=self.pg0.local_ip6) |
| 2277 | / GRE() |
| 2278 | / IPv6(src=self.pg1.local_ip6, dst=self.pg1.remote_ip6) |
| 2279 | / UDP(sport=1144, dport=2233) |
| 2280 | / Raw(b"X" * payload_size) |
| 2281 | ) |
| 2282 | for i in range(count) |
| 2283 | ] |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2284 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2285 | def gen_pkts6(self, p, sw_intf, src, dst, count=1, payload_size=100): |
| 2286 | return [ |
| 2287 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2288 | / IPv6(src="1::1", dst=dst) |
| 2289 | / UDP(sport=1144, dport=2233) |
| 2290 | / Raw(b"X" * payload_size) |
| 2291 | for i in range(count) |
| 2292 | ] |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2293 | |
| 2294 | def verify_decrypted6(self, p, rxs): |
| 2295 | for rx in rxs: |
| 2296 | self.assert_equal(rx[Ether].dst, self.pg1.remote_mac) |
| 2297 | self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6) |
| 2298 | |
| 2299 | def verify_encrypted6(self, p, sa, rxs): |
| 2300 | for rx in rxs: |
| 2301 | try: |
| 2302 | pkt = sa.decrypt(rx[IPv6]) |
| 2303 | if not pkt.haslayer(IPv6): |
| 2304 | pkt = IPv6(pkt[Raw].load) |
| 2305 | self.assert_packet_checksums_valid(pkt) |
| 2306 | self.assertTrue(pkt.haslayer(GRE)) |
| 2307 | e = pkt[GRE] |
| 2308 | self.assertEqual(e[IPv6].dst, p.remote_tun_if_host) |
| 2309 | except (IndexError, AssertionError): |
| 2310 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 2311 | try: |
| 2312 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 2313 | except: |
| 2314 | pass |
| 2315 | raise |
| 2316 | |
| 2317 | def setUp(self): |
| 2318 | super(TestIpsecMGreIfEspTra6, self).setUp() |
| 2319 | |
| 2320 | self.vapi.cli("set logging class ipsec level debug") |
| 2321 | |
| 2322 | N_NHS = 16 |
| 2323 | self.tun_if = self.pg0 |
| 2324 | p = self.ipv6_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2325 | p.tun_if = VppGreInterface( |
| 2326 | self, |
| 2327 | self.pg0.local_ip6, |
| 2328 | "::", |
| 2329 | mode=(VppEnum.vl_api_tunnel_mode_t.TUNNEL_API_MODE_MP), |
| 2330 | ) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2331 | p.tun_if.add_vpp_config() |
| 2332 | p.tun_if.admin_up() |
| 2333 | p.tun_if.config_ip6() |
| 2334 | p.tun_if.generate_remote_hosts(N_NHS) |
| 2335 | self.pg0.generate_remote_hosts(N_NHS) |
| 2336 | self.pg0.configure_ipv6_neighbors() |
| 2337 | |
| 2338 | # setup some SAs for several next-hops on the interface |
| 2339 | self.multi_params = [] |
| 2340 | |
| 2341 | for ii in range(N_NHS): |
| 2342 | p = copy.copy(self.ipv6_params) |
| 2343 | |
| 2344 | p.remote_tun_if_host = "1::%d" % (ii + 1) |
| 2345 | p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii |
| 2346 | p.scapy_tun_spi = p.scapy_tun_spi + ii |
| 2347 | p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii |
| 2348 | p.vpp_tun_spi = p.vpp_tun_spi + ii |
| 2349 | |
| 2350 | p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii |
| 2351 | p.scapy_tra_spi = p.scapy_tra_spi + ii |
| 2352 | p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii |
| 2353 | p.vpp_tra_spi = p.vpp_tra_spi + ii |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2354 | p.tun_sa_out = VppIpsecSA( |
| 2355 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2356 | p.vpp_tun_sa_id, |
| 2357 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2358 | p.auth_algo_vpp_id, |
| 2359 | p.auth_key, |
| 2360 | p.crypt_algo_vpp_id, |
| 2361 | p.crypt_key, |
| 2362 | self.vpp_esp_protocol, |
| 2363 | ) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2364 | p.tun_sa_out.add_vpp_config() |
| 2365 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2366 | p.tun_sa_in = VppIpsecSA( |
| 2367 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2368 | p.scapy_tun_sa_id, |
| 2369 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2370 | p.auth_algo_vpp_id, |
| 2371 | p.auth_key, |
| 2372 | p.crypt_algo_vpp_id, |
| 2373 | p.crypt_key, |
| 2374 | self.vpp_esp_protocol, |
| 2375 | ) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2376 | p.tun_sa_in.add_vpp_config() |
| 2377 | |
| 2378 | # in this v6 variant add the teibs first then the protection |
| 2379 | p.tun_dst = self.pg0.remote_hosts[ii].ip6 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2380 | VppTeib( |
| 2381 | self, p.tun_if, p.tun_if.remote_hosts[ii].ip6, p.tun_dst |
| 2382 | ).add_vpp_config() |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2383 | |
| 2384 | p.tun_protect = VppIpsecTunProtect( |
| 2385 | self, |
| 2386 | p.tun_if, |
| 2387 | p.tun_sa_out, |
| 2388 | [p.tun_sa_in], |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2389 | nh=p.tun_if.remote_hosts[ii].ip6, |
| 2390 | ) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2391 | p.tun_protect.add_vpp_config() |
| 2392 | config_tra_params(p, self.encryption_type, p.tun_if) |
| 2393 | self.multi_params.append(p) |
| 2394 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2395 | VppIpRoute( |
| 2396 | self, |
| 2397 | p.remote_tun_if_host, |
| 2398 | 128, |
| 2399 | [VppRoutePath(p.tun_if.remote_hosts[ii].ip6, p.tun_if.sw_if_index)], |
| 2400 | ).add_vpp_config() |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 2401 | p.tun_dst = self.pg0.remote_hosts[ii].ip6 |
| 2402 | |
| 2403 | self.logger.info(self.vapi.cli("sh log")) |
| 2404 | self.logger.info(self.vapi.cli("sh ipsec protect-hash")) |
| 2405 | self.logger.info(self.vapi.cli("sh adj 41")) |
| 2406 | |
| 2407 | def tearDown(self): |
| 2408 | p = self.ipv6_params |
| 2409 | p.tun_if.unconfig_ip6() |
| 2410 | super(TestIpsecMGreIfEspTra6, self).tearDown() |
| 2411 | |
| 2412 | def test_tun_66(self): |
| 2413 | """mGRE IPSec 66""" |
| 2414 | for p in self.multi_params: |
| 2415 | self.verify_tun_66(p, count=63) |
| 2416 | |
| 2417 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 2418 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2419 | class TestIpsec4TunProtect(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4): |
| 2420 | """IPsec IPv4 Tunnel protect - transport mode""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2421 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2422 | def setUp(self): |
| 2423 | super(TestIpsec4TunProtect, self).setUp() |
| 2424 | |
| 2425 | self.tun_if = self.pg0 |
| 2426 | |
| 2427 | def tearDown(self): |
| 2428 | super(TestIpsec4TunProtect, self).tearDown() |
| 2429 | |
| 2430 | def test_tun_44(self): |
| 2431 | """IPSEC tunnel protect""" |
| 2432 | |
| 2433 | p = self.ipv4_params |
| 2434 | |
| 2435 | self.config_network(p) |
| 2436 | self.config_sa_tra(p) |
| 2437 | self.config_protect(p) |
| 2438 | |
| 2439 | self.verify_tun_44(p, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2440 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2441 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2442 | |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 2443 | self.vapi.cli("clear ipsec sa") |
| 2444 | self.verify_tun_64(p, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2445 | self.assertEqual(p.tun_if.get_rx_stats(), 254) |
| 2446 | self.assertEqual(p.tun_if.get_tx_stats(), 254) |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 2447 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2448 | # rekey - create new SAs and update the tunnel protection |
| 2449 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2450 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2451 | np.scapy_tun_spi += 100 |
| 2452 | np.scapy_tun_sa_id += 1 |
| 2453 | np.vpp_tun_spi += 100 |
| 2454 | np.vpp_tun_sa_id += 1 |
| 2455 | np.tun_if.local_spi = p.vpp_tun_spi |
| 2456 | np.tun_if.remote_spi = p.scapy_tun_spi |
| 2457 | |
| 2458 | self.config_sa_tra(np) |
| 2459 | self.config_protect(np) |
| 2460 | self.unconfig_sa(p) |
| 2461 | |
| 2462 | self.verify_tun_44(np, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2463 | self.assertEqual(p.tun_if.get_rx_stats(), 381) |
| 2464 | self.assertEqual(p.tun_if.get_tx_stats(), 381) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2465 | |
| 2466 | # teardown |
| 2467 | self.unconfig_protect(np) |
| 2468 | self.unconfig_sa(np) |
| 2469 | self.unconfig_network(p) |
| 2470 | |
| 2471 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 2472 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2473 | class TestIpsec4TunProtectUdp(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4): |
| 2474 | """IPsec IPv4 Tunnel protect - transport mode""" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 2475 | |
| 2476 | def setUp(self): |
| 2477 | super(TestIpsec4TunProtectUdp, self).setUp() |
| 2478 | |
| 2479 | self.tun_if = self.pg0 |
| 2480 | |
| 2481 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2482 | 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] | 2483 | p.nat_header = UDP(sport=4500, dport=4500) |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 2484 | self.config_network(p) |
| 2485 | self.config_sa_tra(p) |
| 2486 | self.config_protect(p) |
| 2487 | |
| 2488 | def tearDown(self): |
| 2489 | p = self.ipv4_params |
| 2490 | self.unconfig_protect(p) |
| 2491 | self.unconfig_sa(p) |
| 2492 | self.unconfig_network(p) |
| 2493 | super(TestIpsec4TunProtectUdp, self).tearDown() |
| 2494 | |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 2495 | def verify_encrypted(self, p, sa, rxs): |
| 2496 | # ensure encrypted packets are recieved with the default UDP ports |
| 2497 | for rx in rxs: |
| 2498 | self.assertEqual(rx[UDP].sport, 4500) |
| 2499 | self.assertEqual(rx[UDP].dport, 4500) |
| 2500 | super(TestIpsec4TunProtectUdp, self).verify_encrypted(p, sa, rxs) |
| 2501 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 2502 | def test_tun_44(self): |
| 2503 | """IPSEC UDP tunnel protect""" |
| 2504 | |
| 2505 | p = self.ipv4_params |
| 2506 | |
| 2507 | self.verify_tun_44(p, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2508 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2509 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 2510 | |
| 2511 | def test_keepalive(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2512 | """IPSEC NAT Keepalive""" |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 2513 | self.verify_keepalive(self.ipv4_params) |
| 2514 | |
| 2515 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 2516 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2517 | class TestIpsec4TunProtectTun(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4): |
| 2518 | """IPsec IPv4 Tunnel protect - tunnel mode""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2519 | |
| 2520 | encryption_type = ESP |
| 2521 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2522 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2523 | |
| 2524 | def setUp(self): |
| 2525 | super(TestIpsec4TunProtectTun, self).setUp() |
| 2526 | |
| 2527 | self.tun_if = self.pg0 |
| 2528 | |
| 2529 | def tearDown(self): |
| 2530 | super(TestIpsec4TunProtectTun, self).tearDown() |
| 2531 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2532 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 2533 | return [ |
| 2534 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2535 | / sa.encrypt( |
| 2536 | IP(src=sw_intf.remote_ip4, dst=sw_intf.local_ip4) |
| 2537 | / IP(src=src, dst=dst) |
| 2538 | / UDP(sport=1144, dport=2233) |
| 2539 | / Raw(b"X" * payload_size) |
| 2540 | ) |
| 2541 | for i in range(count) |
| 2542 | ] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2543 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2544 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100): |
| 2545 | return [ |
| 2546 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2547 | / IP(src=src, dst=dst) |
| 2548 | / UDP(sport=1144, dport=2233) |
| 2549 | / Raw(b"X" * payload_size) |
| 2550 | for i in range(count) |
| 2551 | ] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2552 | |
| 2553 | def verify_decrypted(self, p, rxs): |
| 2554 | for rx in rxs: |
| 2555 | self.assert_equal(rx[IP].dst, self.pg1.remote_ip4) |
| 2556 | self.assert_equal(rx[IP].src, p.remote_tun_if_host) |
| 2557 | self.assert_packet_checksums_valid(rx) |
| 2558 | |
| 2559 | def verify_encrypted(self, p, sa, rxs): |
| 2560 | for rx in rxs: |
| 2561 | try: |
| 2562 | pkt = sa.decrypt(rx[IP]) |
| 2563 | if not pkt.haslayer(IP): |
| 2564 | pkt = IP(pkt[Raw].load) |
| 2565 | self.assert_packet_checksums_valid(pkt) |
| 2566 | self.assert_equal(pkt[IP].dst, self.pg0.remote_ip4) |
| 2567 | self.assert_equal(pkt[IP].src, self.pg0.local_ip4) |
| 2568 | inner = pkt[IP].payload |
| 2569 | self.assertEqual(inner[IP][IP].dst, p.remote_tun_if_host) |
| 2570 | |
| 2571 | except (IndexError, AssertionError): |
| 2572 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 2573 | try: |
| 2574 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 2575 | except: |
| 2576 | pass |
| 2577 | raise |
| 2578 | |
| 2579 | def test_tun_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2580 | """IPSEC tunnel protect""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2581 | |
| 2582 | p = self.ipv4_params |
| 2583 | |
| 2584 | self.config_network(p) |
| 2585 | self.config_sa_tun(p) |
| 2586 | self.config_protect(p) |
| 2587 | |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 2588 | # also add an output features on the tunnel and physical interface |
| 2589 | # so we test they still work |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2590 | 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] | 2591 | a = VppAcl(self, [r_all]).add_vpp_config() |
| 2592 | |
| 2593 | VppAclInterface(self, self.pg0.sw_if_index, [a]).add_vpp_config() |
| 2594 | VppAclInterface(self, p.tun_if.sw_if_index, [a]).add_vpp_config() |
| 2595 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2596 | self.verify_tun_44(p, count=127) |
| 2597 | |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2598 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2599 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2600 | |
| 2601 | # rekey - create new SAs and update the tunnel protection |
| 2602 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2603 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2604 | np.scapy_tun_spi += 100 |
| 2605 | np.scapy_tun_sa_id += 1 |
| 2606 | np.vpp_tun_spi += 100 |
| 2607 | np.vpp_tun_sa_id += 1 |
| 2608 | np.tun_if.local_spi = p.vpp_tun_spi |
| 2609 | np.tun_if.remote_spi = p.scapy_tun_spi |
| 2610 | |
| 2611 | self.config_sa_tun(np) |
| 2612 | self.config_protect(np) |
| 2613 | self.unconfig_sa(p) |
| 2614 | |
| 2615 | self.verify_tun_44(np, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2616 | self.assertEqual(p.tun_if.get_rx_stats(), 254) |
| 2617 | self.assertEqual(p.tun_if.get_tx_stats(), 254) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2618 | |
| 2619 | # teardown |
| 2620 | self.unconfig_protect(np) |
| 2621 | self.unconfig_sa(np) |
| 2622 | self.unconfig_network(p) |
| 2623 | |
| 2624 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2625 | class TestIpsec4TunProtectTunDrop(TemplateIpsec, TemplateIpsec4TunProtect, IpsecTun4): |
| 2626 | """IPsec IPv4 Tunnel protect - tunnel mode - drop""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2627 | |
| 2628 | encryption_type = ESP |
| 2629 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2630 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2631 | |
| 2632 | def setUp(self): |
| 2633 | super(TestIpsec4TunProtectTunDrop, self).setUp() |
| 2634 | |
| 2635 | self.tun_if = self.pg0 |
| 2636 | |
| 2637 | def tearDown(self): |
| 2638 | super(TestIpsec4TunProtectTunDrop, self).tearDown() |
| 2639 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2640 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 2641 | return [ |
| 2642 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2643 | / sa.encrypt( |
| 2644 | IP(src=sw_intf.remote_ip4, dst="5.5.5.5") |
| 2645 | / IP(src=src, dst=dst) |
| 2646 | / UDP(sport=1144, dport=2233) |
| 2647 | / Raw(b"X" * payload_size) |
| 2648 | ) |
| 2649 | for i in range(count) |
| 2650 | ] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2651 | |
| 2652 | def test_tun_drop_44(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2653 | """IPSEC tunnel protect bogus tunnel header""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2654 | |
| 2655 | p = self.ipv4_params |
| 2656 | |
| 2657 | self.config_network(p) |
| 2658 | self.config_sa_tun(p) |
| 2659 | self.config_protect(p) |
| 2660 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2661 | tx = self.gen_encrypt_pkts( |
| 2662 | p, |
| 2663 | p.scapy_tun_sa, |
| 2664 | self.tun_if, |
| 2665 | src=p.remote_tun_if_host, |
| 2666 | dst=self.pg1.remote_ip4, |
| 2667 | count=63, |
| 2668 | ) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2669 | self.send_and_assert_no_replies(self.tun_if, tx) |
| 2670 | |
| 2671 | # teardown |
| 2672 | self.unconfig_protect(p) |
| 2673 | self.unconfig_sa(p) |
| 2674 | self.unconfig_network(p) |
| 2675 | |
| 2676 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 2677 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2678 | class TestIpsec6TunProtect(TemplateIpsec, TemplateIpsec6TunProtect, IpsecTun6): |
| 2679 | """IPsec IPv6 Tunnel protect - transport mode""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2680 | |
| 2681 | encryption_type = ESP |
| 2682 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2683 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2684 | |
| 2685 | def setUp(self): |
| 2686 | super(TestIpsec6TunProtect, self).setUp() |
| 2687 | |
| 2688 | self.tun_if = self.pg0 |
| 2689 | |
| 2690 | def tearDown(self): |
| 2691 | super(TestIpsec6TunProtect, self).tearDown() |
| 2692 | |
| 2693 | def test_tun_66(self): |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2694 | """IPSEC tunnel protect 6o6""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2695 | |
| 2696 | p = self.ipv6_params |
| 2697 | |
| 2698 | self.config_network(p) |
| 2699 | self.config_sa_tra(p) |
| 2700 | self.config_protect(p) |
| 2701 | |
| 2702 | self.verify_tun_66(p, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2703 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2704 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2705 | |
| 2706 | # rekey - create new SAs and update the tunnel protection |
| 2707 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2708 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2709 | np.scapy_tun_spi += 100 |
| 2710 | np.scapy_tun_sa_id += 1 |
| 2711 | np.vpp_tun_spi += 100 |
| 2712 | np.vpp_tun_sa_id += 1 |
| 2713 | np.tun_if.local_spi = p.vpp_tun_spi |
| 2714 | np.tun_if.remote_spi = p.scapy_tun_spi |
| 2715 | |
| 2716 | self.config_sa_tra(np) |
| 2717 | self.config_protect(np) |
| 2718 | self.unconfig_sa(p) |
| 2719 | |
| 2720 | self.verify_tun_66(np, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2721 | self.assertEqual(p.tun_if.get_rx_stats(), 254) |
| 2722 | self.assertEqual(p.tun_if.get_tx_stats(), 254) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2723 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2724 | # bounce the interface state |
| 2725 | p.tun_if.admin_down() |
| 2726 | self.verify_drop_tun_66(np, count=127) |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 2727 | node = "/err/ipsec6-tun-input/disabled" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2728 | self.assertEqual(127, self.statistics.get_err_counter(node)) |
| 2729 | p.tun_if.admin_up() |
| 2730 | self.verify_tun_66(np, count=127) |
| 2731 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2732 | # 3 phase rekey |
| 2733 | # 1) add two input SAs [old, new] |
| 2734 | # 2) swap output SA to [new] |
| 2735 | # 3) use only [new] input SA |
| 2736 | np3 = copy.copy(np) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2737 | np3.crypt_key = b"Z" + p.crypt_key[1:] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2738 | np3.scapy_tun_spi += 100 |
| 2739 | np3.scapy_tun_sa_id += 1 |
| 2740 | np3.vpp_tun_spi += 100 |
| 2741 | np3.vpp_tun_sa_id += 1 |
| 2742 | np3.tun_if.local_spi = p.vpp_tun_spi |
| 2743 | np3.tun_if.remote_spi = p.scapy_tun_spi |
| 2744 | |
| 2745 | self.config_sa_tra(np3) |
| 2746 | |
| 2747 | # step 1; |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2748 | 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] | 2749 | self.verify_tun_66(np, np, count=127) |
| 2750 | self.verify_tun_66(np3, np, count=127) |
| 2751 | |
| 2752 | # step 2; |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2753 | 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] | 2754 | self.verify_tun_66(np, np3, count=127) |
| 2755 | self.verify_tun_66(np3, np3, count=127) |
| 2756 | |
| 2757 | # step 1; |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2758 | 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] | 2759 | self.verify_tun_66(np3, np3, count=127) |
Neale Ranns | 49378f2 | 2022-01-10 10:38:43 +0000 | [diff] [blame] | 2760 | self.verify_drop_tun_rx_66(np, count=127) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2761 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2762 | self.assertEqual(p.tun_if.get_rx_stats(), 127 * 9) |
| 2763 | self.assertEqual(p.tun_if.get_tx_stats(), 127 * 8) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2764 | self.unconfig_sa(np) |
| 2765 | |
| 2766 | # teardown |
| 2767 | self.unconfig_protect(np3) |
| 2768 | self.unconfig_sa(np3) |
| 2769 | self.unconfig_network(p) |
| 2770 | |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 2771 | def test_tun_46(self): |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2772 | """IPSEC tunnel protect 4o6""" |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 2773 | |
| 2774 | p = self.ipv6_params |
| 2775 | |
| 2776 | self.config_network(p) |
| 2777 | self.config_sa_tra(p) |
| 2778 | self.config_protect(p) |
| 2779 | |
| 2780 | self.verify_tun_46(p, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2781 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2782 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 2783 | |
| 2784 | # teardown |
| 2785 | self.unconfig_protect(p) |
| 2786 | self.unconfig_sa(p) |
| 2787 | self.unconfig_network(p) |
| 2788 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2789 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 2790 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2791 | class TestIpsec6TunProtectTun(TemplateIpsec, TemplateIpsec6TunProtect, IpsecTun6): |
| 2792 | """IPsec IPv6 Tunnel protect - tunnel mode""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2793 | |
| 2794 | encryption_type = ESP |
| 2795 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2796 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2797 | |
| 2798 | def setUp(self): |
| 2799 | super(TestIpsec6TunProtectTun, self).setUp() |
| 2800 | |
| 2801 | self.tun_if = self.pg0 |
| 2802 | |
| 2803 | def tearDown(self): |
| 2804 | super(TestIpsec6TunProtectTun, self).tearDown() |
| 2805 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2806 | def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 2807 | return [ |
| 2808 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2809 | / sa.encrypt( |
| 2810 | IPv6(src=sw_intf.remote_ip6, dst=sw_intf.local_ip6) |
| 2811 | / IPv6(src=src, dst=dst) |
| 2812 | / UDP(sport=1166, dport=2233) |
| 2813 | / Raw(b"X" * payload_size) |
| 2814 | ) |
| 2815 | for i in range(count) |
| 2816 | ] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2817 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2818 | def gen_pkts6(self, p, sw_intf, src, dst, count=1, payload_size=100): |
| 2819 | return [ |
| 2820 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2821 | / IPv6(src=src, dst=dst) |
| 2822 | / UDP(sport=1166, dport=2233) |
| 2823 | / Raw(b"X" * payload_size) |
| 2824 | for i in range(count) |
| 2825 | ] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2826 | |
| 2827 | def verify_decrypted6(self, p, rxs): |
| 2828 | for rx in rxs: |
| 2829 | self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6) |
| 2830 | self.assert_equal(rx[IPv6].src, p.remote_tun_if_host) |
| 2831 | self.assert_packet_checksums_valid(rx) |
| 2832 | |
| 2833 | def verify_encrypted6(self, p, sa, rxs): |
| 2834 | for rx in rxs: |
| 2835 | try: |
| 2836 | pkt = sa.decrypt(rx[IPv6]) |
| 2837 | if not pkt.haslayer(IPv6): |
| 2838 | pkt = IPv6(pkt[Raw].load) |
| 2839 | self.assert_packet_checksums_valid(pkt) |
| 2840 | self.assert_equal(pkt[IPv6].dst, self.pg0.remote_ip6) |
| 2841 | self.assert_equal(pkt[IPv6].src, self.pg0.local_ip6) |
| 2842 | inner = pkt[IPv6].payload |
| 2843 | self.assertEqual(inner[IPv6][IPv6].dst, p.remote_tun_if_host) |
| 2844 | |
| 2845 | except (IndexError, AssertionError): |
| 2846 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 2847 | try: |
| 2848 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 2849 | except: |
| 2850 | pass |
| 2851 | raise |
| 2852 | |
| 2853 | def test_tun_66(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2854 | """IPSEC tunnel protect""" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2855 | |
| 2856 | p = self.ipv6_params |
| 2857 | |
| 2858 | self.config_network(p) |
| 2859 | self.config_sa_tun(p) |
| 2860 | self.config_protect(p) |
| 2861 | |
| 2862 | self.verify_tun_66(p, count=127) |
| 2863 | |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2864 | self.assertEqual(p.tun_if.get_rx_stats(), 127) |
| 2865 | self.assertEqual(p.tun_if.get_tx_stats(), 127) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2866 | |
| 2867 | # rekey - create new SAs and update the tunnel protection |
| 2868 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2869 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2870 | np.scapy_tun_spi += 100 |
| 2871 | np.scapy_tun_sa_id += 1 |
| 2872 | np.vpp_tun_spi += 100 |
| 2873 | np.vpp_tun_sa_id += 1 |
| 2874 | np.tun_if.local_spi = p.vpp_tun_spi |
| 2875 | np.tun_if.remote_spi = p.scapy_tun_spi |
| 2876 | |
| 2877 | self.config_sa_tun(np) |
| 2878 | self.config_protect(np) |
| 2879 | self.unconfig_sa(p) |
| 2880 | |
| 2881 | self.verify_tun_66(np, count=127) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 2882 | self.assertEqual(p.tun_if.get_rx_stats(), 254) |
| 2883 | self.assertEqual(p.tun_if.get_tx_stats(), 254) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 2884 | |
| 2885 | # teardown |
| 2886 | self.unconfig_protect(np) |
| 2887 | self.unconfig_sa(np) |
| 2888 | self.unconfig_network(p) |
| 2889 | |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 2890 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2891 | class TestIpsec6TunProtectTunDrop(TemplateIpsec, TemplateIpsec6TunProtect, IpsecTun6): |
| 2892 | """IPsec IPv6 Tunnel protect - tunnel mode - drop""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2893 | |
| 2894 | encryption_type = ESP |
| 2895 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2896 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2897 | |
| 2898 | def setUp(self): |
| 2899 | super(TestIpsec6TunProtectTunDrop, self).setUp() |
| 2900 | |
| 2901 | self.tun_if = self.pg0 |
| 2902 | |
| 2903 | def tearDown(self): |
| 2904 | super(TestIpsec6TunProtectTunDrop, self).tearDown() |
| 2905 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2906 | 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] | 2907 | # the IP destination of the revelaed packet does not match |
| 2908 | # that assigned to the tunnel |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2909 | return [ |
| 2910 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 2911 | / sa.encrypt( |
| 2912 | IPv6(src=sw_intf.remote_ip6, dst="5::5") |
| 2913 | / IPv6(src=src, dst=dst) |
| 2914 | / UDP(sport=1144, dport=2233) |
| 2915 | / Raw(b"X" * payload_size) |
| 2916 | ) |
| 2917 | for i in range(count) |
| 2918 | ] |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2919 | |
| 2920 | def test_tun_drop_66(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2921 | """IPSEC 6 tunnel protect bogus tunnel header""" |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2922 | |
| 2923 | p = self.ipv6_params |
| 2924 | |
| 2925 | self.config_network(p) |
| 2926 | self.config_sa_tun(p) |
| 2927 | self.config_protect(p) |
| 2928 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2929 | tx = self.gen_encrypt_pkts6( |
| 2930 | p, |
| 2931 | p.scapy_tun_sa, |
| 2932 | self.tun_if, |
| 2933 | src=p.remote_tun_if_host, |
| 2934 | dst=self.pg1.remote_ip6, |
| 2935 | count=63, |
| 2936 | ) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 2937 | self.send_and_assert_no_replies(self.tun_if, tx) |
| 2938 | |
| 2939 | self.unconfig_protect(p) |
| 2940 | self.unconfig_sa(p) |
| 2941 | self.unconfig_network(p) |
| 2942 | |
| 2943 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2944 | class TemplateIpsecItf4(object): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2945 | """IPsec Interface IPv4""" |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2946 | |
| 2947 | encryption_type = ESP |
| 2948 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 2949 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2950 | tun4_input_node = "ipsec4-tun-input" |
| 2951 | |
| 2952 | def config_sa_tun(self, p, src, dst): |
| 2953 | config_tun_params(p, self.encryption_type, None, src, dst) |
| 2954 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2955 | p.tun_sa_out = VppIpsecSA( |
| 2956 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2957 | p.vpp_tun_sa_id, |
| 2958 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2959 | p.auth_algo_vpp_id, |
| 2960 | p.auth_key, |
| 2961 | p.crypt_algo_vpp_id, |
| 2962 | p.crypt_key, |
| 2963 | self.vpp_esp_protocol, |
| 2964 | src, |
| 2965 | dst, |
| 2966 | flags=p.flags, |
| 2967 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2968 | p.tun_sa_out.add_vpp_config() |
| 2969 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2970 | p.tun_sa_in = VppIpsecSA( |
| 2971 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 2972 | p.scapy_tun_sa_id, |
| 2973 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2974 | p.auth_algo_vpp_id, |
| 2975 | p.auth_key, |
| 2976 | p.crypt_algo_vpp_id, |
| 2977 | p.crypt_key, |
| 2978 | self.vpp_esp_protocol, |
| 2979 | dst, |
| 2980 | src, |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 2981 | flags=p.flags |
| 2982 | | 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] | 2983 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2984 | p.tun_sa_in.add_vpp_config() |
| 2985 | |
| 2986 | def config_protect(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2987 | 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] | 2988 | p.tun_protect.add_vpp_config() |
| 2989 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2990 | def config_network(self, p, instance=0xFFFFFFFF): |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 2991 | p.tun_if = VppIpsecInterface(self, instance=instance) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 2992 | |
| 2993 | p.tun_if.add_vpp_config() |
| 2994 | p.tun_if.admin_up() |
| 2995 | p.tun_if.config_ip4() |
| 2996 | p.tun_if.config_ip6() |
| 2997 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 2998 | p.route = VppIpRoute( |
| 2999 | self, |
| 3000 | p.remote_tun_if_host, |
| 3001 | 32, |
| 3002 | [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)], |
| 3003 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3004 | p.route.add_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3005 | r = VppIpRoute( |
| 3006 | self, |
| 3007 | p.remote_tun_if_host6, |
| 3008 | 128, |
| 3009 | [ |
| 3010 | VppRoutePath( |
| 3011 | p.tun_if.remote_ip6, 0xFFFFFFFF, proto=DpoProto.DPO_PROTO_IP6 |
| 3012 | ) |
| 3013 | ], |
| 3014 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3015 | r.add_vpp_config() |
| 3016 | |
| 3017 | def unconfig_network(self, p): |
| 3018 | p.route.remove_vpp_config() |
| 3019 | p.tun_if.remove_vpp_config() |
| 3020 | |
| 3021 | def unconfig_protect(self, p): |
| 3022 | p.tun_protect.remove_vpp_config() |
| 3023 | |
| 3024 | def unconfig_sa(self, p): |
| 3025 | p.tun_sa_out.remove_vpp_config() |
| 3026 | p.tun_sa_in.remove_vpp_config() |
| 3027 | |
| 3028 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 3029 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3030 | class TestIpsecItf4(TemplateIpsec, TemplateIpsecItf4, IpsecTun4): |
| 3031 | """IPsec Interface IPv4""" |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3032 | |
| 3033 | def setUp(self): |
| 3034 | super(TestIpsecItf4, self).setUp() |
| 3035 | |
| 3036 | self.tun_if = self.pg0 |
| 3037 | |
| 3038 | def tearDown(self): |
| 3039 | super(TestIpsecItf4, self).tearDown() |
| 3040 | |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 3041 | def test_tun_instance_44(self): |
| 3042 | p = self.ipv4_params |
| 3043 | self.config_network(p, instance=3) |
| 3044 | |
| 3045 | with self.assertRaises(CliFailedCommandError): |
| 3046 | self.vapi.cli("show interface ipsec0") |
| 3047 | |
| 3048 | output = self.vapi.cli("show interface ipsec3") |
| 3049 | self.assertTrue("unknown" not in output) |
| 3050 | |
| 3051 | self.unconfig_network(p) |
| 3052 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3053 | def test_tun_44(self): |
| 3054 | """IPSEC interface IPv4""" |
| 3055 | |
| 3056 | n_pkts = 127 |
| 3057 | p = self.ipv4_params |
| 3058 | |
| 3059 | self.config_network(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3060 | config_tun_params( |
| 3061 | p, self.encryption_type, None, self.pg0.local_ip4, self.pg0.remote_ip4 |
| 3062 | ) |
Neale Ranns | 49378f2 | 2022-01-10 10:38:43 +0000 | [diff] [blame] | 3063 | self.verify_tun_dropped_44(p, count=n_pkts) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3064 | 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] | 3065 | self.config_protect(p) |
| 3066 | |
| 3067 | self.verify_tun_44(p, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3068 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3069 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3070 | |
| 3071 | p.tun_if.admin_down() |
| 3072 | self.verify_tun_dropped_44(p, count=n_pkts) |
| 3073 | p.tun_if.admin_up() |
| 3074 | self.verify_tun_44(p, count=n_pkts) |
| 3075 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3076 | self.assertEqual(p.tun_if.get_rx_stats(), 3 * n_pkts) |
| 3077 | self.assertEqual(p.tun_if.get_tx_stats(), 2 * n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3078 | |
| 3079 | # it's a v6 packet when its encrypted |
| 3080 | self.tun4_encrypt_node_name = "esp6-encrypt-tun" |
| 3081 | |
| 3082 | self.verify_tun_64(p, count=n_pkts) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3083 | self.assertEqual(p.tun_if.get_rx_stats(), 4 * n_pkts) |
| 3084 | self.assertEqual(p.tun_if.get_tx_stats(), 3 * n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3085 | |
| 3086 | self.tun4_encrypt_node_name = "esp4-encrypt-tun" |
| 3087 | |
Arthur de Kerhor | 4117b24 | 2022-08-31 19:13:03 +0200 | [diff] [blame] | 3088 | # update the SA tunnel |
| 3089 | config_tun_params( |
| 3090 | p, self.encryption_type, None, self.pg2.local_ip4, self.pg2.remote_ip4 |
| 3091 | ) |
| 3092 | p.tun_sa_in.update_vpp_config( |
| 3093 | is_tun=True, tun_src=self.pg2.remote_ip4, tun_dst=self.pg2.local_ip4 |
| 3094 | ) |
| 3095 | p.tun_sa_out.update_vpp_config( |
| 3096 | is_tun=True, tun_src=self.pg2.local_ip4, tun_dst=self.pg2.remote_ip4 |
| 3097 | ) |
| 3098 | self.verify_tun_44(p, count=n_pkts) |
| 3099 | self.assertEqual(p.tun_if.get_rx_stats(), 5 * n_pkts) |
| 3100 | self.assertEqual(p.tun_if.get_tx_stats(), 4 * n_pkts) |
| 3101 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3102 | self.vapi.cli("clear interfaces") |
| 3103 | |
| 3104 | # rekey - create new SAs and update the tunnel protection |
| 3105 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3106 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3107 | np.scapy_tun_spi += 100 |
| 3108 | np.scapy_tun_sa_id += 1 |
| 3109 | np.vpp_tun_spi += 100 |
| 3110 | np.vpp_tun_sa_id += 1 |
| 3111 | np.tun_if.local_spi = p.vpp_tun_spi |
| 3112 | np.tun_if.remote_spi = p.scapy_tun_spi |
| 3113 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3114 | 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] | 3115 | self.config_protect(np) |
| 3116 | self.unconfig_sa(p) |
| 3117 | |
| 3118 | self.verify_tun_44(np, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3119 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3120 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3121 | |
| 3122 | # teardown |
| 3123 | self.unconfig_protect(np) |
| 3124 | self.unconfig_sa(np) |
| 3125 | self.unconfig_network(p) |
| 3126 | |
Neale Ranns | 970187b | 2020-10-07 13:58:56 +0000 | [diff] [blame] | 3127 | def test_tun_44_null(self): |
| 3128 | """IPSEC interface IPv4 NULL auth/crypto""" |
| 3129 | |
| 3130 | n_pkts = 127 |
| 3131 | p = copy.copy(self.ipv4_params) |
| 3132 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3133 | p.auth_algo_vpp_id = VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_NONE |
| 3134 | p.crypt_algo_vpp_id = ( |
| 3135 | VppEnum.vl_api_ipsec_crypto_alg_t.IPSEC_API_CRYPTO_ALG_NONE |
| 3136 | ) |
Neale Ranns | 970187b | 2020-10-07 13:58:56 +0000 | [diff] [blame] | 3137 | p.crypt_algo = "NULL" |
| 3138 | p.auth_algo = "NULL" |
| 3139 | |
| 3140 | self.config_network(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3141 | 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] | 3142 | self.config_protect(p) |
| 3143 | |
Neale Ranns | 49378f2 | 2022-01-10 10:38:43 +0000 | [diff] [blame] | 3144 | self.logger.info(self.vapi.cli("sh ipsec sa")) |
Neale Ranns | 970187b | 2020-10-07 13:58:56 +0000 | [diff] [blame] | 3145 | self.verify_tun_44(p, count=n_pkts) |
| 3146 | |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 3147 | # teardown |
| 3148 | self.unconfig_protect(p) |
| 3149 | self.unconfig_sa(p) |
| 3150 | self.unconfig_network(p) |
| 3151 | |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3152 | def test_tun_44_police(self): |
| 3153 | """IPSEC interface IPv4 with input policer""" |
| 3154 | n_pkts = 127 |
| 3155 | p = self.ipv4_params |
| 3156 | |
| 3157 | self.config_network(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3158 | 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] | 3159 | self.config_protect(p) |
| 3160 | |
| 3161 | action_tx = PolicerAction( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3162 | VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0 |
| 3163 | ) |
| 3164 | policer = VppPolicer( |
| 3165 | self, |
| 3166 | "pol1", |
| 3167 | 80, |
| 3168 | 0, |
| 3169 | 1000, |
| 3170 | 0, |
| 3171 | conform_action=action_tx, |
| 3172 | exceed_action=action_tx, |
| 3173 | violate_action=action_tx, |
| 3174 | ) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3175 | policer.add_vpp_config() |
| 3176 | |
| 3177 | # Start policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 3178 | 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] | 3179 | |
| 3180 | self.verify_tun_44(p, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3181 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3182 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3183 | |
| 3184 | stats = policer.get_stats() |
| 3185 | |
| 3186 | # Single rate, 2 colour policer - expect conform, violate but no exceed |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3187 | self.assertGreater(stats["conform_packets"], 0) |
| 3188 | self.assertEqual(stats["exceed_packets"], 0) |
| 3189 | self.assertGreater(stats["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3190 | |
| 3191 | # Stop policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 3192 | 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] | 3193 | self.verify_tun_44(p, count=n_pkts) |
| 3194 | |
| 3195 | # No new policer stats |
| 3196 | statsnew = policer.get_stats() |
| 3197 | self.assertEqual(stats, statsnew) |
| 3198 | |
| 3199 | # teardown |
| 3200 | policer.remove_vpp_config() |
| 3201 | self.unconfig_protect(p) |
| 3202 | self.unconfig_sa(p) |
| 3203 | self.unconfig_network(p) |
| 3204 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3205 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3206 | class TestIpsecItf4MPLS(TemplateIpsec, TemplateIpsecItf4, IpsecTun4): |
| 3207 | """IPsec Interface MPLSoIPv4""" |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3208 | |
| 3209 | tun4_encrypt_node_name = "esp-mpls-encrypt-tun" |
| 3210 | |
| 3211 | def setUp(self): |
| 3212 | super(TestIpsecItf4MPLS, self).setUp() |
| 3213 | |
| 3214 | self.tun_if = self.pg0 |
| 3215 | |
| 3216 | def tearDown(self): |
| 3217 | super(TestIpsecItf4MPLS, self).tearDown() |
| 3218 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3219 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 3220 | return [ |
| 3221 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 3222 | / sa.encrypt( |
| 3223 | MPLS(label=44, ttl=3) |
| 3224 | / IP(src=src, dst=dst) |
| 3225 | / UDP(sport=1166, dport=2233) |
| 3226 | / Raw(b"X" * payload_size) |
| 3227 | ) |
| 3228 | for i in range(count) |
| 3229 | ] |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3230 | |
| 3231 | def verify_encrypted(self, p, sa, rxs): |
| 3232 | for rx in rxs: |
| 3233 | try: |
| 3234 | pkt = sa.decrypt(rx[IP]) |
| 3235 | if not pkt.haslayer(IP): |
| 3236 | pkt = IP(pkt[Raw].load) |
| 3237 | self.assert_packet_checksums_valid(pkt) |
| 3238 | self.assert_equal(pkt[MPLS].label, 44) |
| 3239 | self.assert_equal(pkt[IP].dst, p.remote_tun_if_host) |
| 3240 | except (IndexError, AssertionError): |
| 3241 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 3242 | try: |
| 3243 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 3244 | except: |
| 3245 | pass |
| 3246 | raise |
| 3247 | |
| 3248 | def test_tun_mpls_o_ip4(self): |
| 3249 | """IPSEC interface MPLS over IPv4""" |
| 3250 | |
| 3251 | n_pkts = 127 |
| 3252 | p = self.ipv4_params |
| 3253 | f = FibPathProto |
| 3254 | |
| 3255 | tbl = VppMplsTable(self, 0) |
| 3256 | tbl.add_vpp_config() |
| 3257 | |
| 3258 | self.config_network(p) |
| 3259 | # deag MPLS routes from the tunnel |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3260 | r4 = VppMplsRoute( |
| 3261 | self, 44, 1, [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)] |
| 3262 | ).add_vpp_config() |
| 3263 | p.route.modify( |
| 3264 | [ |
| 3265 | VppRoutePath( |
| 3266 | p.tun_if.remote_ip4, p.tun_if.sw_if_index, labels=[VppMplsLabel(44)] |
| 3267 | ) |
| 3268 | ] |
| 3269 | ) |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3270 | p.tun_if.enable_mpls() |
| 3271 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3272 | 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] | 3273 | self.config_protect(p) |
| 3274 | |
| 3275 | self.verify_tun_44(p, count=n_pkts) |
| 3276 | |
| 3277 | # cleanup |
| 3278 | p.tun_if.disable_mpls() |
| 3279 | self.unconfig_protect(p) |
| 3280 | self.unconfig_sa(p) |
| 3281 | self.unconfig_network(p) |
| 3282 | |
| 3283 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3284 | class TemplateIpsecItf6(object): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3285 | """IPsec Interface IPv6""" |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3286 | |
| 3287 | encryption_type = ESP |
| 3288 | tun6_encrypt_node_name = "esp6-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 3289 | tun6_decrypt_node_name = ["esp6-decrypt-tun", "esp6-decrypt-tun-post"] |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3290 | tun6_input_node = "ipsec6-tun-input" |
| 3291 | |
| 3292 | def config_sa_tun(self, p, src, dst): |
| 3293 | config_tun_params(p, self.encryption_type, None, src, dst) |
| 3294 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3295 | if not hasattr(p, "tun_flags"): |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3296 | p.tun_flags = None |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3297 | if not hasattr(p, "hop_limit"): |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3298 | p.hop_limit = 255 |
| 3299 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3300 | p.tun_sa_out = VppIpsecSA( |
| 3301 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 3302 | p.vpp_tun_sa_id, |
| 3303 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3304 | p.auth_algo_vpp_id, |
| 3305 | p.auth_key, |
| 3306 | p.crypt_algo_vpp_id, |
| 3307 | p.crypt_key, |
| 3308 | self.vpp_esp_protocol, |
| 3309 | src, |
| 3310 | dst, |
| 3311 | flags=p.flags, |
| 3312 | tun_flags=p.tun_flags, |
| 3313 | hop_limit=p.hop_limit, |
| 3314 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3315 | p.tun_sa_out.add_vpp_config() |
| 3316 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3317 | p.tun_sa_in = VppIpsecSA( |
| 3318 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 3319 | p.scapy_tun_sa_id, |
| 3320 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3321 | p.auth_algo_vpp_id, |
| 3322 | p.auth_key, |
| 3323 | p.crypt_algo_vpp_id, |
| 3324 | p.crypt_key, |
| 3325 | self.vpp_esp_protocol, |
| 3326 | dst, |
| 3327 | src, |
| 3328 | flags=p.flags, |
| 3329 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3330 | p.tun_sa_in.add_vpp_config() |
| 3331 | |
| 3332 | def config_protect(self, p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3333 | 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] | 3334 | p.tun_protect.add_vpp_config() |
| 3335 | |
| 3336 | def config_network(self, p): |
| 3337 | p.tun_if = VppIpsecInterface(self) |
| 3338 | |
| 3339 | p.tun_if.add_vpp_config() |
| 3340 | p.tun_if.admin_up() |
| 3341 | p.tun_if.config_ip4() |
| 3342 | p.tun_if.config_ip6() |
| 3343 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3344 | r = VppIpRoute( |
| 3345 | self, |
| 3346 | p.remote_tun_if_host4, |
| 3347 | 32, |
| 3348 | [VppRoutePath(p.tun_if.remote_ip4, 0xFFFFFFFF)], |
| 3349 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3350 | r.add_vpp_config() |
| 3351 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3352 | p.route = VppIpRoute( |
| 3353 | self, |
| 3354 | p.remote_tun_if_host, |
| 3355 | 128, |
| 3356 | [ |
| 3357 | VppRoutePath( |
| 3358 | p.tun_if.remote_ip6, 0xFFFFFFFF, proto=DpoProto.DPO_PROTO_IP6 |
| 3359 | ) |
| 3360 | ], |
| 3361 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3362 | p.route.add_vpp_config() |
| 3363 | |
| 3364 | def unconfig_network(self, p): |
| 3365 | p.route.remove_vpp_config() |
| 3366 | p.tun_if.remove_vpp_config() |
| 3367 | |
| 3368 | def unconfig_protect(self, p): |
| 3369 | p.tun_protect.remove_vpp_config() |
| 3370 | |
| 3371 | def unconfig_sa(self, p): |
| 3372 | p.tun_sa_out.remove_vpp_config() |
| 3373 | p.tun_sa_in.remove_vpp_config() |
| 3374 | |
| 3375 | |
Andrew Yourtchenko | 8dc0d48 | 2021-01-29 13:17:19 +0000 | [diff] [blame] | 3376 | @tag_fixme_vpp_workers |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3377 | class TestIpsecItf6(TemplateIpsec, TemplateIpsecItf6, IpsecTun6): |
| 3378 | """IPsec Interface IPv6""" |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3379 | |
| 3380 | def setUp(self): |
| 3381 | super(TestIpsecItf6, self).setUp() |
| 3382 | |
| 3383 | self.tun_if = self.pg0 |
| 3384 | |
| 3385 | def tearDown(self): |
| 3386 | super(TestIpsecItf6, self).tearDown() |
| 3387 | |
Neale Ranns | 49378f2 | 2022-01-10 10:38:43 +0000 | [diff] [blame] | 3388 | def test_tun_66(self): |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3389 | """IPSEC interface IPv6""" |
| 3390 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3391 | tf = VppEnum.vl_api_tunnel_encap_decap_flags_t |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3392 | n_pkts = 127 |
| 3393 | p = self.ipv6_params |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3394 | p.inner_hop_limit = 24 |
| 3395 | p.outer_hop_limit = 23 |
| 3396 | p.outer_flow_label = 243224 |
| 3397 | 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] | 3398 | |
| 3399 | self.config_network(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3400 | config_tun_params( |
| 3401 | p, self.encryption_type, None, self.pg0.local_ip6, self.pg0.remote_ip6 |
| 3402 | ) |
Neale Ranns | 49378f2 | 2022-01-10 10:38:43 +0000 | [diff] [blame] | 3403 | self.verify_drop_tun_66(p, count=n_pkts) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3404 | 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] | 3405 | self.config_protect(p) |
| 3406 | |
| 3407 | self.verify_tun_66(p, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3408 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3409 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3410 | |
| 3411 | p.tun_if.admin_down() |
| 3412 | self.verify_drop_tun_66(p, count=n_pkts) |
| 3413 | p.tun_if.admin_up() |
| 3414 | self.verify_tun_66(p, count=n_pkts) |
| 3415 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3416 | self.assertEqual(p.tun_if.get_rx_stats(), 3 * n_pkts) |
| 3417 | self.assertEqual(p.tun_if.get_tx_stats(), 2 * n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3418 | |
| 3419 | # it's a v4 packet when its encrypted |
| 3420 | self.tun6_encrypt_node_name = "esp4-encrypt-tun" |
| 3421 | |
| 3422 | self.verify_tun_46(p, count=n_pkts) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3423 | self.assertEqual(p.tun_if.get_rx_stats(), 4 * n_pkts) |
| 3424 | self.assertEqual(p.tun_if.get_tx_stats(), 3 * n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3425 | |
| 3426 | self.tun6_encrypt_node_name = "esp6-encrypt-tun" |
| 3427 | |
| 3428 | self.vapi.cli("clear interfaces") |
| 3429 | |
| 3430 | # rekey - create new SAs and update the tunnel protection |
| 3431 | np = copy.copy(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3432 | np.crypt_key = b"X" + p.crypt_key[1:] |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3433 | np.scapy_tun_spi += 100 |
| 3434 | np.scapy_tun_sa_id += 1 |
| 3435 | np.vpp_tun_spi += 100 |
| 3436 | np.vpp_tun_sa_id += 1 |
| 3437 | np.tun_if.local_spi = p.vpp_tun_spi |
| 3438 | np.tun_if.remote_spi = p.scapy_tun_spi |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3439 | np.inner_hop_limit = 24 |
| 3440 | np.outer_hop_limit = 128 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3441 | np.inner_flow_label = 0xABCDE |
| 3442 | np.outer_flow_label = 0xABCDE |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3443 | np.hop_limit = 128 |
| 3444 | 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] | 3445 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3446 | 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] | 3447 | self.config_protect(np) |
| 3448 | self.unconfig_sa(p) |
| 3449 | |
| 3450 | self.verify_tun_66(np, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3451 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3452 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3453 | |
| 3454 | # teardown |
| 3455 | self.unconfig_protect(np) |
| 3456 | self.unconfig_sa(np) |
| 3457 | self.unconfig_network(p) |
| 3458 | |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3459 | def test_tun_66_police(self): |
| 3460 | """IPSEC interface IPv6 with input policer""" |
| 3461 | tf = VppEnum.vl_api_tunnel_encap_decap_flags_t |
| 3462 | n_pkts = 127 |
| 3463 | p = self.ipv6_params |
| 3464 | p.inner_hop_limit = 24 |
| 3465 | p.outer_hop_limit = 23 |
| 3466 | p.outer_flow_label = 243224 |
| 3467 | p.tun_flags = tf.TUNNEL_API_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT |
| 3468 | |
| 3469 | self.config_network(p) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3470 | 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] | 3471 | self.config_protect(p) |
| 3472 | |
| 3473 | action_tx = PolicerAction( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3474 | VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0 |
| 3475 | ) |
| 3476 | policer = VppPolicer( |
| 3477 | self, |
| 3478 | "pol1", |
| 3479 | 80, |
| 3480 | 0, |
| 3481 | 1000, |
| 3482 | 0, |
| 3483 | conform_action=action_tx, |
| 3484 | exceed_action=action_tx, |
| 3485 | violate_action=action_tx, |
| 3486 | ) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3487 | policer.add_vpp_config() |
| 3488 | |
| 3489 | # Start policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 3490 | 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] | 3491 | |
| 3492 | self.verify_tun_66(p, count=n_pkts) |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 3493 | self.assertEqual(p.tun_if.get_rx_stats(), n_pkts) |
| 3494 | self.assertEqual(p.tun_if.get_tx_stats(), n_pkts) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3495 | |
| 3496 | stats = policer.get_stats() |
| 3497 | |
| 3498 | # Single rate, 2 colour policer - expect conform, violate but no exceed |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3499 | self.assertGreater(stats["conform_packets"], 0) |
| 3500 | self.assertEqual(stats["exceed_packets"], 0) |
| 3501 | self.assertGreater(stats["violate_packets"], 0) |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 3502 | |
| 3503 | # Stop policing on tun |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 3504 | 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] | 3505 | self.verify_tun_66(p, count=n_pkts) |
| 3506 | |
| 3507 | # No new policer stats |
| 3508 | statsnew = policer.get_stats() |
| 3509 | self.assertEqual(stats, statsnew) |
| 3510 | |
| 3511 | # teardown |
| 3512 | policer.remove_vpp_config() |
| 3513 | self.unconfig_protect(p) |
| 3514 | self.unconfig_sa(p) |
| 3515 | self.unconfig_network(p) |
| 3516 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 3517 | |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3518 | class TestIpsecMIfEsp4(TemplateIpsec, IpsecTun4): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3519 | """Ipsec P2MP ESP v4 tests""" |
| 3520 | |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3521 | tun4_encrypt_node_name = "esp4-encrypt-tun" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 3522 | tun4_decrypt_node_name = ["esp4-decrypt-tun", "esp4-decrypt-tun-post"] |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3523 | encryption_type = ESP |
| 3524 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3525 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 3526 | return [ |
| 3527 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 3528 | / sa.encrypt( |
| 3529 | IP(src=self.pg1.local_ip4, dst=self.pg1.remote_ip4) |
| 3530 | / UDP(sport=1144, dport=2233) |
| 3531 | / Raw(b"X" * payload_size) |
| 3532 | ) |
| 3533 | for i in range(count) |
| 3534 | ] |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3535 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3536 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=100): |
| 3537 | return [ |
| 3538 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 3539 | / IP(src="1.1.1.1", dst=dst) |
| 3540 | / UDP(sport=1144, dport=2233) |
| 3541 | / Raw(b"X" * payload_size) |
| 3542 | for i in range(count) |
| 3543 | ] |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3544 | |
| 3545 | def verify_decrypted(self, p, rxs): |
| 3546 | for rx in rxs: |
| 3547 | self.assert_equal(rx[Ether].dst, self.pg1.remote_mac) |
| 3548 | self.assert_equal(rx[IP].dst, self.pg1.remote_ip4) |
| 3549 | |
| 3550 | def verify_encrypted(self, p, sa, rxs): |
| 3551 | for rx in rxs: |
| 3552 | try: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3553 | self.assertEqual( |
| 3554 | rx[IP].tos, VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_EF << 2 |
| 3555 | ) |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3556 | self.assertEqual(rx[IP].ttl, p.hop_limit) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3557 | pkt = sa.decrypt(rx[IP]) |
| 3558 | if not pkt.haslayer(IP): |
| 3559 | pkt = IP(pkt[Raw].load) |
| 3560 | self.assert_packet_checksums_valid(pkt) |
| 3561 | e = pkt[IP] |
| 3562 | self.assertEqual(e[IP].dst, p.remote_tun_if_host) |
| 3563 | except (IndexError, AssertionError): |
| 3564 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 3565 | try: |
| 3566 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 3567 | except: |
| 3568 | pass |
| 3569 | raise |
| 3570 | |
| 3571 | def setUp(self): |
| 3572 | super(TestIpsecMIfEsp4, self).setUp() |
| 3573 | |
| 3574 | N_NHS = 16 |
| 3575 | self.tun_if = self.pg0 |
| 3576 | p = self.ipv4_params |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3577 | p.tun_if = VppIpsecInterface( |
| 3578 | self, mode=(VppEnum.vl_api_tunnel_mode_t.TUNNEL_API_MODE_MP) |
| 3579 | ) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3580 | p.tun_if.add_vpp_config() |
| 3581 | p.tun_if.admin_up() |
| 3582 | p.tun_if.config_ip4() |
Neale Ranns | cfe949d | 2020-11-25 19:35:38 +0000 | [diff] [blame] | 3583 | p.tun_if.unconfig_ip4() |
| 3584 | p.tun_if.config_ip4() |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3585 | p.tun_if.generate_remote_hosts(N_NHS) |
| 3586 | self.pg0.generate_remote_hosts(N_NHS) |
| 3587 | self.pg0.configure_ipv4_neighbors() |
| 3588 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3589 | 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] | 3590 | a = VppAcl(self, [r_all]).add_vpp_config() |
| 3591 | |
| 3592 | VppAclInterface(self, self.pg0.sw_if_index, [a]).add_vpp_config() |
| 3593 | VppAclInterface(self, p.tun_if.sw_if_index, [a]).add_vpp_config() |
| 3594 | |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3595 | # setup some SAs for several next-hops on the interface |
| 3596 | self.multi_params = [] |
| 3597 | |
| 3598 | for ii in range(N_NHS): |
| 3599 | p = copy.copy(self.ipv4_params) |
| 3600 | |
| 3601 | p.remote_tun_if_host = "1.1.1.%d" % (ii + 1) |
| 3602 | p.scapy_tun_sa_id = p.scapy_tun_sa_id + ii |
| 3603 | p.scapy_tun_spi = p.scapy_tun_spi + ii |
| 3604 | p.vpp_tun_sa_id = p.vpp_tun_sa_id + ii |
| 3605 | p.vpp_tun_spi = p.vpp_tun_spi + ii |
| 3606 | |
| 3607 | p.scapy_tra_sa_id = p.scapy_tra_sa_id + ii |
| 3608 | p.scapy_tra_spi = p.scapy_tra_spi + ii |
| 3609 | p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii |
| 3610 | p.vpp_tra_spi = p.vpp_tra_spi + ii |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3611 | p.hop_limit = ii + 10 |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 3612 | p.tun_sa_out = VppIpsecSA( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3613 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 3614 | p.vpp_tun_sa_id, |
| 3615 | p.vpp_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3616 | p.auth_algo_vpp_id, |
| 3617 | p.auth_key, |
| 3618 | p.crypt_algo_vpp_id, |
| 3619 | p.crypt_key, |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 3620 | self.vpp_esp_protocol, |
| 3621 | self.pg0.local_ip4, |
| 3622 | self.pg0.remote_hosts[ii].ip4, |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3623 | dscp=VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_EF, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3624 | hop_limit=p.hop_limit, |
| 3625 | ) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3626 | p.tun_sa_out.add_vpp_config() |
| 3627 | |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 3628 | p.tun_sa_in = VppIpsecSA( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3629 | self, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 3630 | p.scapy_tun_sa_id, |
| 3631 | p.scapy_tun_spi, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3632 | p.auth_algo_vpp_id, |
| 3633 | p.auth_key, |
| 3634 | p.crypt_algo_vpp_id, |
| 3635 | p.crypt_key, |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 3636 | self.vpp_esp_protocol, |
| 3637 | self.pg0.remote_hosts[ii].ip4, |
| 3638 | self.pg0.local_ip4, |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 3639 | dscp=VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_EF, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3640 | hop_limit=p.hop_limit, |
| 3641 | ) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3642 | p.tun_sa_in.add_vpp_config() |
| 3643 | |
| 3644 | p.tun_protect = VppIpsecTunProtect( |
| 3645 | self, |
| 3646 | p.tun_if, |
| 3647 | p.tun_sa_out, |
| 3648 | [p.tun_sa_in], |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3649 | nh=p.tun_if.remote_hosts[ii].ip4, |
| 3650 | ) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3651 | p.tun_protect.add_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3652 | config_tun_params( |
| 3653 | p, |
| 3654 | self.encryption_type, |
| 3655 | None, |
| 3656 | self.pg0.local_ip4, |
| 3657 | self.pg0.remote_hosts[ii].ip4, |
| 3658 | ) |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3659 | self.multi_params.append(p) |
| 3660 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 3661 | p.via_tun_route = VppIpRoute( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3662 | self, |
| 3663 | p.remote_tun_if_host, |
| 3664 | 32, |
| 3665 | [VppRoutePath(p.tun_if.remote_hosts[ii].ip4, p.tun_if.sw_if_index)], |
| 3666 | ).add_vpp_config() |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3667 | |
| 3668 | p.tun_dst = self.pg0.remote_hosts[ii].ip4 |
| 3669 | |
| 3670 | def tearDown(self): |
| 3671 | p = self.ipv4_params |
| 3672 | p.tun_if.unconfig_ip4() |
| 3673 | super(TestIpsecMIfEsp4, self).tearDown() |
| 3674 | |
| 3675 | def test_tun_44(self): |
| 3676 | """P2MP IPSEC 44""" |
| 3677 | N_PKTS = 63 |
| 3678 | for p in self.multi_params: |
| 3679 | self.verify_tun_44(p, count=N_PKTS) |
| 3680 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 3681 | # remove one tunnel protect, the rest should still work |
| 3682 | self.multi_params[0].tun_protect.remove_vpp_config() |
| 3683 | self.verify_tun_dropped_44(self.multi_params[0], count=N_PKTS) |
| 3684 | self.multi_params[0].via_tun_route.remove_vpp_config() |
| 3685 | self.verify_tun_dropped_44(self.multi_params[0], count=N_PKTS) |
| 3686 | |
| 3687 | for p in self.multi_params[1:]: |
| 3688 | self.verify_tun_44(p, count=N_PKTS) |
| 3689 | |
| 3690 | self.multi_params[0].tun_protect.add_vpp_config() |
| 3691 | self.multi_params[0].via_tun_route.add_vpp_config() |
| 3692 | |
| 3693 | for p in self.multi_params: |
| 3694 | self.verify_tun_44(p, count=N_PKTS) |
| 3695 | |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 3696 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3697 | class TestIpsecItf6MPLS(TemplateIpsec, TemplateIpsecItf6, IpsecTun6): |
| 3698 | """IPsec Interface MPLSoIPv6""" |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3699 | |
| 3700 | tun6_encrypt_node_name = "esp-mpls-encrypt-tun" |
| 3701 | |
| 3702 | def setUp(self): |
| 3703 | super(TestIpsecItf6MPLS, self).setUp() |
| 3704 | |
| 3705 | self.tun_if = self.pg0 |
| 3706 | |
| 3707 | def tearDown(self): |
| 3708 | super(TestIpsecItf6MPLS, self).tearDown() |
| 3709 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3710 | def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, payload_size=100): |
| 3711 | return [ |
| 3712 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 3713 | / sa.encrypt( |
| 3714 | MPLS(label=66, ttl=3) |
| 3715 | / IPv6(src=src, dst=dst) |
| 3716 | / UDP(sport=1166, dport=2233) |
| 3717 | / Raw(b"X" * payload_size) |
| 3718 | ) |
| 3719 | for i in range(count) |
| 3720 | ] |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3721 | |
| 3722 | def verify_encrypted6(self, p, sa, rxs): |
| 3723 | for rx in rxs: |
| 3724 | try: |
| 3725 | pkt = sa.decrypt(rx[IPv6]) |
| 3726 | if not pkt.haslayer(IPv6): |
| 3727 | pkt = IP(pkt[Raw].load) |
| 3728 | self.assert_packet_checksums_valid(pkt) |
| 3729 | self.assert_equal(pkt[MPLS].label, 66) |
| 3730 | self.assert_equal(pkt[IPv6].dst, p.remote_tun_if_host) |
| 3731 | except (IndexError, AssertionError): |
| 3732 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 3733 | try: |
| 3734 | self.logger.debug(ppp("Decrypted packet:", pkt)) |
| 3735 | except: |
| 3736 | pass |
| 3737 | raise |
| 3738 | |
| 3739 | def test_tun_mpls_o_ip6(self): |
| 3740 | """IPSEC interface MPLS over IPv6""" |
| 3741 | |
| 3742 | n_pkts = 127 |
| 3743 | p = self.ipv6_params |
| 3744 | f = FibPathProto |
| 3745 | |
| 3746 | tbl = VppMplsTable(self, 0) |
| 3747 | tbl.add_vpp_config() |
| 3748 | |
| 3749 | self.config_network(p) |
| 3750 | # deag MPLS routes from the tunnel |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3751 | r6 = VppMplsRoute( |
| 3752 | self, |
| 3753 | 66, |
| 3754 | 1, |
| 3755 | [VppRoutePath(self.pg1.remote_ip6, self.pg1.sw_if_index)], |
| 3756 | eos_proto=f.FIB_PATH_NH_PROTO_IP6, |
| 3757 | ).add_vpp_config() |
| 3758 | p.route.modify( |
| 3759 | [ |
| 3760 | VppRoutePath( |
| 3761 | p.tun_if.remote_ip6, p.tun_if.sw_if_index, labels=[VppMplsLabel(66)] |
| 3762 | ) |
| 3763 | ] |
| 3764 | ) |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 3765 | p.tun_if.enable_mpls() |
| 3766 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3767 | 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] | 3768 | self.config_protect(p) |
| 3769 | |
| 3770 | self.verify_tun_66(p, count=n_pkts) |
| 3771 | |
| 3772 | # cleanup |
| 3773 | p.tun_if.disable_mpls() |
| 3774 | self.unconfig_protect(p) |
| 3775 | self.unconfig_sa(p) |
| 3776 | self.unconfig_network(p) |
| 3777 | |
| 3778 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 3779 | if __name__ == "__main__": |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 3780 | unittest.main(testRunner=VppTestRunner) |