Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1 | import unittest |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 2 | import socket |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 3 | import struct |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 4 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 5 | from scapy.layers.inet import IP, ICMP, TCP, UDP |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 6 | from scapy.layers.ipsec import SecurityAssociation, ESP |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 7 | from scapy.layers.l2 import Ether, Raw |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 8 | from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 9 | |
| 10 | from framework import VppTestCase, VppTestRunner |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 11 | from util import ppp, reassemble4 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 12 | from vpp_papi import VppEnum |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 13 | |
| 14 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 15 | class IPsecIPv4Params(object): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 16 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 17 | addr_type = socket.AF_INET |
| 18 | addr_any = "0.0.0.0" |
| 19 | addr_bcast = "255.255.255.255" |
| 20 | addr_len = 32 |
| 21 | is_ipv6 = 0 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 22 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 23 | def __init__(self): |
| 24 | self.remote_tun_if_host = '1.1.1.1' |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 25 | self.remote_tun_if_host6 = '1111::1' |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 26 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 27 | self.scapy_tun_sa_id = 10 |
| 28 | self.scapy_tun_spi = 1001 |
| 29 | self.vpp_tun_sa_id = 20 |
| 30 | self.vpp_tun_spi = 1000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 31 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 32 | self.scapy_tra_sa_id = 30 |
| 33 | self.scapy_tra_spi = 2001 |
| 34 | self.vpp_tra_sa_id = 40 |
| 35 | self.vpp_tra_spi = 2000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 36 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 37 | self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t. |
| 38 | IPSEC_API_INTEG_ALG_SHA1_96) |
| 39 | self.auth_algo = 'HMAC-SHA1-96' # scapy name |
| 40 | self.auth_key = 'C91KUR9GYMm5GfkEvNjX' |
| 41 | |
| 42 | self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t. |
| 43 | IPSEC_API_CRYPTO_ALG_AES_CBC_128) |
| 44 | self.crypt_algo = 'AES-CBC' # scapy name |
| 45 | self.crypt_key = 'JPjyOWBeVEQiMe7h' |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 46 | self.salt = 0 |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 47 | self.flags = 0 |
| 48 | self.nat_header = None |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 49 | |
| 50 | |
| 51 | class IPsecIPv6Params(object): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 52 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 53 | addr_type = socket.AF_INET6 |
| 54 | addr_any = "0::0" |
| 55 | addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" |
| 56 | addr_len = 128 |
| 57 | is_ipv6 = 1 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 58 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 59 | def __init__(self): |
| 60 | self.remote_tun_if_host = '1111:1111:1111:1111:1111:1111:1111:1111' |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 61 | self.remote_tun_if_host4 = '1.1.1.1' |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 62 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 63 | self.scapy_tun_sa_id = 50 |
| 64 | self.scapy_tun_spi = 3001 |
| 65 | self.vpp_tun_sa_id = 60 |
| 66 | self.vpp_tun_spi = 3000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 67 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 68 | self.scapy_tra_sa_id = 70 |
| 69 | self.scapy_tra_spi = 4001 |
| 70 | self.vpp_tra_sa_id = 80 |
| 71 | self.vpp_tra_spi = 4000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 72 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 73 | self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t. |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 74 | IPSEC_API_INTEG_ALG_SHA1_96) |
| 75 | self.auth_algo = 'HMAC-SHA1-96' # scapy name |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 76 | self.auth_key = 'C91KUR9GYMm5GfkEvNjX' |
| 77 | |
| 78 | self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t. |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 79 | IPSEC_API_CRYPTO_ALG_AES_CBC_128) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 80 | self.crypt_algo = 'AES-CBC' # scapy name |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 81 | self.crypt_key = 'JPjyOWBeVEQiMe7h' |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 82 | self.salt = 0 |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 83 | self.flags = 0 |
| 84 | self.nat_header = None |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 85 | |
| 86 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 87 | def config_tun_params(p, encryption_type, tun_if): |
| 88 | ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6} |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 89 | use_esn = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t. |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 90 | IPSEC_API_SAD_FLAG_USE_ESN)) |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 91 | if p.crypt_algo == "AES-GCM": |
| 92 | crypt_key = p.crypt_key + struct.pack("!I", p.salt) |
| 93 | else: |
| 94 | crypt_key = p.crypt_key |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 95 | p.scapy_tun_sa = SecurityAssociation( |
| 96 | encryption_type, spi=p.vpp_tun_spi, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 97 | crypt_algo=p.crypt_algo, |
| 98 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 99 | auth_algo=p.auth_algo, auth_key=p.auth_key, |
| 100 | tunnel_header=ip_class_by_addr_type[p.addr_type]( |
| 101 | src=tun_if.remote_addr[p.addr_type], |
| 102 | dst=tun_if.local_addr[p.addr_type]), |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 103 | nat_t_header=p.nat_header, |
| 104 | use_esn=use_esn) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 105 | p.vpp_tun_sa = SecurityAssociation( |
| 106 | encryption_type, spi=p.scapy_tun_spi, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 107 | crypt_algo=p.crypt_algo, |
| 108 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 109 | auth_algo=p.auth_algo, auth_key=p.auth_key, |
| 110 | tunnel_header=ip_class_by_addr_type[p.addr_type]( |
| 111 | dst=tun_if.remote_addr[p.addr_type], |
| 112 | src=tun_if.local_addr[p.addr_type]), |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 113 | nat_t_header=p.nat_header, |
| 114 | use_esn=use_esn) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def config_tra_params(p, encryption_type): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 118 | use_esn = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t. |
| 119 | IPSEC_API_SAD_FLAG_USE_ESN)) |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 120 | if p.crypt_algo == "AES-GCM": |
| 121 | crypt_key = p.crypt_key + struct.pack("!I", p.salt) |
| 122 | else: |
| 123 | crypt_key = p.crypt_key |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 124 | p.scapy_tra_sa = SecurityAssociation( |
| 125 | encryption_type, |
| 126 | spi=p.vpp_tra_spi, |
| 127 | crypt_algo=p.crypt_algo, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 128 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 129 | auth_algo=p.auth_algo, |
| 130 | auth_key=p.auth_key, |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 131 | nat_t_header=p.nat_header, |
| 132 | use_esn=use_esn) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 133 | p.vpp_tra_sa = SecurityAssociation( |
| 134 | encryption_type, |
| 135 | spi=p.scapy_tra_spi, |
| 136 | crypt_algo=p.crypt_algo, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 137 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 138 | auth_algo=p.auth_algo, |
| 139 | auth_key=p.auth_key, |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 140 | nat_t_header=p.nat_header, |
| 141 | use_esn=use_esn) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 142 | |
| 143 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 144 | class TemplateIpsec(VppTestCase): |
| 145 | """ |
| 146 | TRANSPORT MODE: |
| 147 | |
| 148 | ------ encrypt --- |
| 149 | |tra_if| <-------> |VPP| |
| 150 | ------ decrypt --- |
| 151 | |
| 152 | TUNNEL MODE: |
| 153 | |
| 154 | ------ encrypt --- plain --- |
| 155 | |tun_if| <------- |VPP| <------ |pg1| |
| 156 | ------ --- --- |
| 157 | |
| 158 | ------ decrypt --- plain --- |
| 159 | |tun_if| -------> |VPP| ------> |pg1| |
| 160 | ------ --- --- |
| 161 | """ |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 162 | tun_spd_id = 1 |
| 163 | tra_spd_id = 2 |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 164 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 165 | def ipsec_select_backend(self): |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 166 | """ empty method to be overloaded when necessary """ |
| 167 | pass |
| 168 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 169 | @classmethod |
| 170 | def setUpClass(cls): |
| 171 | super(TemplateIpsec, cls).setUpClass() |
| 172 | |
| 173 | @classmethod |
| 174 | def tearDownClass(cls): |
| 175 | super(TemplateIpsec, cls).tearDownClass() |
| 176 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 177 | def setup_params(self): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 178 | self.ipv4_params = IPsecIPv4Params() |
| 179 | self.ipv6_params = IPsecIPv6Params() |
| 180 | self.params = {self.ipv4_params.addr_type: self.ipv4_params, |
| 181 | self.ipv6_params.addr_type: self.ipv6_params} |
| 182 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 183 | def config_interfaces(self): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 184 | self.create_pg_interfaces(range(3)) |
| 185 | self.interfaces = list(self.pg_interfaces) |
| 186 | for i in self.interfaces: |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 187 | i.admin_up() |
| 188 | i.config_ip4() |
| 189 | i.resolve_arp() |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 190 | i.config_ip6() |
| 191 | i.resolve_ndp() |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 192 | |
| 193 | def setUp(self): |
| 194 | super(TemplateIpsec, self).setUp() |
| 195 | |
| 196 | self.setup_params() |
| 197 | |
| 198 | self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t. |
| 199 | IPSEC_API_PROTO_ESP) |
| 200 | self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t. |
| 201 | IPSEC_API_PROTO_AH) |
| 202 | |
| 203 | self.config_interfaces() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 204 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 205 | self.ipsec_select_backend() |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 206 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 207 | def unconfig_interfaces(self): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 208 | for i in self.interfaces: |
| 209 | i.admin_down() |
| 210 | i.unconfig_ip4() |
| 211 | i.unconfig_ip6() |
| 212 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 213 | def tearDown(self): |
| 214 | super(TemplateIpsec, self).tearDown() |
| 215 | |
| 216 | self.unconfig_interfaces() |
| 217 | |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 218 | def show_commands_at_teardown(self): |
| 219 | self.logger.info(self.vapi.cli("show hardware")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 220 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 221 | def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1, |
| 222 | payload_size=54): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 223 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 224 | sa.encrypt(IP(src=src, dst=dst) / |
| 225 | ICMP() / Raw('X' * payload_size)) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 226 | for i in range(count)] |
| 227 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 228 | def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1, |
| 229 | payload_size=54): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 230 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 231 | sa.encrypt(IPv6(src=src, dst=dst) / |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 232 | ICMPv6EchoRequest(id=0, seq=1, |
| 233 | data='X' * payload_size)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 234 | for i in range(count)] |
| 235 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 236 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=54): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 237 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 238 | IP(src=src, dst=dst) / ICMP() / Raw('X' * payload_size) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 239 | for i in range(count)] |
| 240 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 241 | def gen_pkts6(self, sw_intf, src, dst, count=1, payload_size=54): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 242 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 243 | IPv6(src=src, dst=dst) / |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 244 | ICMPv6EchoRequest(id=0, seq=1, data='X' * payload_size) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 245 | for i in range(count)] |
| 246 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 247 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 248 | class IpsecTcp(object): |
| 249 | def verify_tcp_checksum(self): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 250 | self.vapi.cli("test http server") |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 251 | p = self.params[socket.AF_INET] |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 252 | config_tun_params(p, self.encryption_type, self.tun_if) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 253 | send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) / |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 254 | p.scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host, |
| 255 | dst=self.tun_if.local_ip4) / |
| 256 | TCP(flags='S', dport=80))) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 257 | self.logger.debug(ppp("Sending packet:", send)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 258 | recv = self.send_and_expect(self.tun_if, [send], self.tun_if) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 259 | recv = recv[0] |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 260 | decrypted = p.vpp_tun_sa.decrypt(recv[IP]) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 261 | self.assert_packet_checksums_valid(decrypted) |
| 262 | |
| 263 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 264 | class IpsecTcpTests(IpsecTcp): |
| 265 | def test_tcp_checksum(self): |
| 266 | """ verify checksum correctness for vpp generated packets """ |
| 267 | self.verify_tcp_checksum() |
| 268 | |
| 269 | |
| 270 | class IpsecTra4(object): |
| 271 | """ verify methods for Transport v4 """ |
| 272 | def verify_tra_anti_replay(self, count=1): |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 273 | p = self.params[socket.AF_INET] |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 274 | use_esn = p.vpp_tra_sa.use_esn |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 275 | |
| 276 | # fire in a packet with seq number 1 |
| 277 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 278 | dst=self.tra_if.local_mac) / |
| 279 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 280 | dst=self.tra_if.local_ip4) / |
| 281 | ICMP(), |
| 282 | seq_num=1)) |
| 283 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 284 | |
| 285 | # now move the window over to 235 |
| 286 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 287 | dst=self.tra_if.local_mac) / |
| 288 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 289 | dst=self.tra_if.local_ip4) / |
| 290 | ICMP(), |
| 291 | seq_num=235)) |
| 292 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 293 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 294 | # replayed packets are dropped |
| 295 | self.send_and_assert_no_replies(self.tra_if, pkt * 3) |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 296 | self.assert_error_counter_equal( |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 297 | '/err/%s/SA replayed packet' % self.tra4_decrypt_node_name, 3) |
| 298 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 299 | # the window size is 64 packets |
| 300 | # in window are still accepted |
| 301 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 302 | dst=self.tra_if.local_mac) / |
| 303 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 304 | dst=self.tra_if.local_ip4) / |
| 305 | ICMP(), |
| 306 | seq_num=172)) |
| 307 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 308 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 309 | # a packet that does not decrypt does not move the window forward |
| 310 | bogus_sa = SecurityAssociation(self.encryption_type, |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 311 | p.vpp_tra_spi, |
| 312 | crypt_algo=p.crypt_algo, |
| 313 | crypt_key=p.crypt_key[::-1], |
| 314 | auth_algo=p.auth_algo, |
| 315 | auth_key=p.auth_key[::-1]) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 316 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 317 | dst=self.tra_if.local_mac) / |
| 318 | bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 319 | dst=self.tra_if.local_ip4) / |
| 320 | ICMP(), |
| 321 | seq_num=350)) |
| 322 | self.send_and_assert_no_replies(self.tra_if, pkt * 17) |
| 323 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 324 | self.assert_error_counter_equal( |
Klement Sekera | 14d7e90 | 2018-12-10 13:46:09 +0100 | [diff] [blame] | 325 | '/err/%s/Integrity check failed' % self.tra4_decrypt_node_name, 17) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 326 | |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 327 | # a malformed 'runt' packet |
| 328 | # created by a mis-constructed SA |
| 329 | if (ESP == self.encryption_type): |
| 330 | bogus_sa = SecurityAssociation(self.encryption_type, |
| 331 | p.vpp_tra_spi) |
| 332 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 333 | dst=self.tra_if.local_mac) / |
| 334 | bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 335 | dst=self.tra_if.local_ip4) / |
| 336 | ICMP(), |
| 337 | seq_num=350)) |
| 338 | self.send_and_assert_no_replies(self.tra_if, pkt * 17) |
| 339 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 340 | self.assert_error_counter_equal( |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 341 | '/err/%s/undersized packet' % self.tra4_decrypt_node_name, 17) |
| 342 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 343 | # which we can determine since this packet is still in the window |
| 344 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 345 | dst=self.tra_if.local_mac) / |
| 346 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 347 | dst=self.tra_if.local_ip4) / |
| 348 | ICMP(), |
| 349 | seq_num=234)) |
Klement Sekera | 14d7e90 | 2018-12-10 13:46:09 +0100 | [diff] [blame] | 350 | self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 351 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 352 | # out of window are dropped |
| 353 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 354 | dst=self.tra_if.local_mac) / |
| 355 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 356 | dst=self.tra_if.local_ip4) / |
| 357 | ICMP(), |
| 358 | seq_num=17)) |
| 359 | self.send_and_assert_no_replies(self.tra_if, pkt * 17) |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 360 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 361 | if use_esn: |
| 362 | # an out of window error with ESN looks like a high sequence |
| 363 | # wrap. but since it isn't then the verify will fail. |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 364 | self.assert_error_counter_equal( |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 365 | '/err/%s/Integrity check failed' % |
| 366 | self.tra4_decrypt_node_name, 34) |
| 367 | |
| 368 | else: |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 369 | self.assert_error_counter_equal( |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 370 | '/err/%s/SA replayed packet' % |
| 371 | self.tra4_decrypt_node_name, 20) |
| 372 | |
| 373 | # valid packet moves the window over to 236 |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 374 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 375 | dst=self.tra_if.local_mac) / |
| 376 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 377 | dst=self.tra_if.local_ip4) / |
| 378 | ICMP(), |
| 379 | seq_num=236)) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 380 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 381 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 382 | |
| 383 | # move VPP's SA to just before the seq-number wrap |
| 384 | self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.scapy_tra_sa_id) |
| 385 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 386 | # then fire in a packet that VPP should drop because it causes the |
| 387 | # seq number to wrap unless we're using extended. |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 388 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 389 | dst=self.tra_if.local_mac) / |
| 390 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 391 | dst=self.tra_if.local_ip4) / |
| 392 | ICMP(), |
| 393 | seq_num=237)) |
| 394 | |
| 395 | if use_esn: |
| 396 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 397 | # in order to decrpyt the high order number needs to wrap |
| 398 | p.vpp_tra_sa.seq_num = 0x100000000 |
| 399 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 400 | |
| 401 | # send packets with high bits set |
| 402 | p.scapy_tra_sa.seq_num = 0x100000005 |
| 403 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 404 | dst=self.tra_if.local_mac) / |
| 405 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 406 | dst=self.tra_if.local_ip4) / |
| 407 | ICMP(), |
| 408 | seq_num=0x100000005)) |
| 409 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 410 | # in order to decrpyt the high order number needs to wrap |
| 411 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 412 | else: |
| 413 | self.send_and_assert_no_replies(self.tra_if, [pkt]) |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 414 | self.assert_error_counter_equal( |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 415 | '/err/%s/sequence number cycled' % |
| 416 | self.tra4_encrypt_node_name, 1) |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 417 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 418 | # move the security-associations seq number on to the last we used |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 419 | self.vapi.cli("test ipsec sa %d seq 0x15f" % p.scapy_tra_sa_id) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 420 | p.scapy_tra_sa.seq_num = 351 |
| 421 | p.vpp_tra_sa.seq_num = 351 |
| 422 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 423 | def verify_tra_basic4(self, count=1): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 424 | """ ipsec v4 transport basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 425 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 426 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 427 | p = self.params[socket.AF_INET] |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 428 | send_pkts = self.gen_encrypt_pkts(p.scapy_tra_sa, self.tra_if, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 429 | src=self.tra_if.remote_ip4, |
| 430 | dst=self.tra_if.local_ip4, |
| 431 | count=count) |
| 432 | recv_pkts = self.send_and_expect(self.tra_if, send_pkts, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 433 | self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 434 | for rx in recv_pkts: |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 435 | self.assertEqual(len(rx) - len(Ether()), rx[IP].len) |
| 436 | self.assert_packet_checksums_valid(rx) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 437 | try: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 438 | decrypted = p.vpp_tra_sa.decrypt(rx[IP]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 439 | self.assert_packet_checksums_valid(decrypted) |
| 440 | except: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 441 | self.logger.debug(ppp("Unexpected packet:", rx)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 442 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 443 | finally: |
| 444 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 445 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 446 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 447 | pkts = p.tra_sa_in.get_stats()['packets'] |
| 448 | self.assertEqual(pkts, count, |
| 449 | "incorrect SA in counts: expected %d != %d" % |
| 450 | (count, pkts)) |
| 451 | pkts = p.tra_sa_out.get_stats()['packets'] |
| 452 | self.assertEqual(pkts, count, |
| 453 | "incorrect SA out counts: expected %d != %d" % |
| 454 | (count, pkts)) |
| 455 | |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 456 | self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count) |
| 457 | self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count) |
| 458 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 459 | |
| 460 | class IpsecTra4Tests(IpsecTra4): |
| 461 | """ UT test methods for Transport v4 """ |
| 462 | def test_tra_anti_replay(self): |
| 463 | """ ipsec v4 transport anti-reply test """ |
| 464 | self.verify_tra_anti_replay(count=1) |
| 465 | |
| 466 | def test_tra_basic(self, count=1): |
| 467 | """ ipsec v4 transport basic test """ |
| 468 | self.verify_tra_basic4(count=1) |
| 469 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 470 | def test_tra_burst(self): |
| 471 | """ ipsec v4 transport burst test """ |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 472 | self.verify_tra_basic4(count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 473 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 474 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 475 | class IpsecTra6(object): |
| 476 | """ verify methods for Transport v6 """ |
| 477 | def verify_tra_basic6(self, count=1): |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 478 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 479 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 480 | p = self.params[socket.AF_INET6] |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 481 | send_pkts = self.gen_encrypt_pkts6(p.scapy_tra_sa, self.tra_if, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 482 | src=self.tra_if.remote_ip6, |
| 483 | dst=self.tra_if.local_ip6, |
| 484 | count=count) |
| 485 | recv_pkts = self.send_and_expect(self.tra_if, send_pkts, |
| 486 | self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 487 | for rx in recv_pkts: |
Neale Ranns | d207fd7 | 2019-04-18 17:18:12 -0700 | [diff] [blame] | 488 | self.assertEqual(len(rx) - len(Ether()) - len(IPv6()), |
| 489 | rx[IPv6].plen) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 490 | try: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 491 | decrypted = p.vpp_tra_sa.decrypt(rx[IPv6]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 492 | self.assert_packet_checksums_valid(decrypted) |
| 493 | except: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 494 | self.logger.debug(ppp("Unexpected packet:", rx)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 495 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 496 | finally: |
| 497 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 498 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 499 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 500 | pkts = p.tra_sa_in.get_stats()['packets'] |
| 501 | self.assertEqual(pkts, count, |
| 502 | "incorrect SA in counts: expected %d != %d" % |
| 503 | (count, pkts)) |
| 504 | pkts = p.tra_sa_out.get_stats()['packets'] |
| 505 | self.assertEqual(pkts, count, |
| 506 | "incorrect SA out counts: expected %d != %d" % |
| 507 | (count, pkts)) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 508 | self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count) |
| 509 | self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count) |
| 510 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 511 | |
| 512 | class IpsecTra6Tests(IpsecTra6): |
| 513 | """ UT test methods for Transport v6 """ |
| 514 | def test_tra_basic6(self): |
| 515 | """ ipsec v6 transport basic test """ |
| 516 | self.verify_tra_basic6(count=1) |
| 517 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 518 | def test_tra_burst6(self): |
| 519 | """ ipsec v6 transport burst test """ |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 520 | self.verify_tra_basic6(count=257) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 521 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 522 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 523 | class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 524 | """ UT test methods for Transport v6 and v4""" |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 525 | pass |
| 526 | |
| 527 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 528 | class IpsecTun4(object): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 529 | """ verify methods for Tunnel v4 """ |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 530 | def verify_counters4(self, p, count, n_frags=None): |
| 531 | if not n_frags: |
| 532 | n_frags = count |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 533 | if (hasattr(p, "spd_policy_in_any")): |
| 534 | pkts = p.spd_policy_in_any.get_stats()['packets'] |
| 535 | self.assertEqual(pkts, count, |
| 536 | "incorrect SPD any policy: expected %d != %d" % |
| 537 | (count, pkts)) |
| 538 | |
| 539 | if (hasattr(p, "tun_sa_in")): |
| 540 | pkts = p.tun_sa_in.get_stats()['packets'] |
| 541 | self.assertEqual(pkts, count, |
| 542 | "incorrect SA in counts: expected %d != %d" % |
| 543 | (count, pkts)) |
| 544 | pkts = p.tun_sa_out.get_stats()['packets'] |
| 545 | self.assertEqual(pkts, count, |
| 546 | "incorrect SA out counts: expected %d != %d" % |
| 547 | (count, pkts)) |
| 548 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 549 | self.assert_packet_counter_equal(self.tun4_encrypt_node_name, n_frags) |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 550 | self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count) |
| 551 | |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 552 | def verify_decrypted(self, p, rxs): |
| 553 | for rx in rxs: |
| 554 | self.assert_equal(rx[IP].src, p.remote_tun_if_host) |
| 555 | self.assert_equal(rx[IP].dst, self.pg1.remote_ip4) |
| 556 | self.assert_packet_checksums_valid(rx) |
| 557 | |
| 558 | def verify_encrypted(self, p, sa, rxs): |
| 559 | decrypt_pkts = [] |
| 560 | for rx in rxs: |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 561 | self.assert_packet_checksums_valid(rx) |
| 562 | self.assertEqual(len(rx) - len(Ether()), rx[IP].len) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 563 | try: |
| 564 | decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IP]) |
| 565 | if not decrypt_pkt.haslayer(IP): |
| 566 | decrypt_pkt = IP(decrypt_pkt[Raw].load) |
| 567 | decrypt_pkts.append(decrypt_pkt) |
| 568 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4) |
| 569 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
| 570 | except: |
| 571 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 572 | try: |
| 573 | self.logger.debug(ppp("Decrypted packet:", decrypt_pkt)) |
| 574 | except: |
| 575 | pass |
| 576 | raise |
| 577 | pkts = reassemble4(decrypt_pkts) |
| 578 | for pkt in pkts: |
| 579 | self.assert_packet_checksums_valid(pkt) |
| 580 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 581 | def verify_tun_44(self, p, count=1, payload_size=64, n_rx=None): |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 582 | self.vapi.cli("clear errors") |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 583 | if not n_rx: |
| 584 | n_rx = count |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 585 | try: |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 586 | config_tun_params(p, self.encryption_type, self.tun_if) |
| 587 | send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 588 | src=p.remote_tun_if_host, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 589 | dst=self.pg1.remote_ip4, |
| 590 | count=count) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 591 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 592 | self.verify_decrypted(p, recv_pkts) |
| 593 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 594 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 595 | dst=p.remote_tun_if_host, count=count, |
| 596 | payload_size=payload_size) |
| 597 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 598 | self.tun_if, n_rx) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 599 | self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts) |
| 600 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 601 | finally: |
| 602 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 603 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 604 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 605 | self.verify_counters4(p, count, n_rx) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 606 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 607 | def verify_tun_64(self, p, count=1): |
| 608 | self.vapi.cli("clear errors") |
| 609 | try: |
| 610 | config_tun_params(p, self.encryption_type, self.tun_if) |
| 611 | send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if, |
| 612 | src=p.remote_tun_if_host6, |
| 613 | dst=self.pg1.remote_ip6, |
| 614 | count=count) |
| 615 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 616 | for recv_pkt in recv_pkts: |
| 617 | self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host6) |
| 618 | self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6) |
| 619 | self.assert_packet_checksums_valid(recv_pkt) |
| 620 | send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6, |
| 621 | dst=p.remote_tun_if_host6, count=count) |
| 622 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 623 | for recv_pkt in recv_pkts: |
| 624 | try: |
| 625 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP]) |
| 626 | if not decrypt_pkt.haslayer(IPv6): |
| 627 | decrypt_pkt = IPv6(decrypt_pkt[Raw].load) |
| 628 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6) |
| 629 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host6) |
| 630 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 631 | except: |
| 632 | self.logger.error(ppp("Unexpected packet:", recv_pkt)) |
| 633 | try: |
| 634 | self.logger.debug( |
| 635 | ppp("Decrypted packet:", decrypt_pkt)) |
| 636 | except: |
| 637 | pass |
| 638 | raise |
| 639 | finally: |
| 640 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 641 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 642 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 643 | self.verify_counters4(p, count) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 644 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 645 | |
| 646 | class IpsecTun4Tests(IpsecTun4): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 647 | """ UT test methods for Tunnel v4 """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 648 | def test_tun_basic44(self): |
| 649 | """ ipsec 4o4 tunnel basic test """ |
| 650 | self.verify_tun_44(self.params[socket.AF_INET], count=1) |
| 651 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 652 | def test_tun_burst44(self): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 653 | """ ipsec 4o4 tunnel burst test """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 654 | self.verify_tun_44(self.params[socket.AF_INET], count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 655 | |
| 656 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 657 | class IpsecTun6(object): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 658 | """ verify methods for Tunnel v6 """ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 659 | def verify_counters6(self, p_in, p_out, count): |
| 660 | if (hasattr(p_in, "tun_sa_in")): |
| 661 | pkts = p_in.tun_sa_in.get_stats()['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 662 | self.assertEqual(pkts, count, |
| 663 | "incorrect SA in counts: expected %d != %d" % |
| 664 | (count, pkts)) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 665 | if (hasattr(p_out, "tun_sa_out")): |
| 666 | pkts = p_out.tun_sa_out.get_stats()['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 667 | self.assertEqual(pkts, count, |
| 668 | "incorrect SA out counts: expected %d != %d" % |
| 669 | (count, pkts)) |
| 670 | self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count) |
| 671 | self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count) |
| 672 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 673 | def verify_decrypted6(self, p, rxs): |
| 674 | for rx in rxs: |
| 675 | self.assert_equal(rx[IPv6].src, p.remote_tun_if_host) |
| 676 | self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6) |
| 677 | self.assert_packet_checksums_valid(rx) |
| 678 | |
| 679 | def verify_encrypted6(self, p, sa, rxs): |
| 680 | for rx in rxs: |
| 681 | self.assert_packet_checksums_valid(rx) |
| 682 | self.assertEqual(len(rx) - len(Ether()) - len(IPv6()), |
| 683 | rx[IPv6].plen) |
| 684 | try: |
| 685 | decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IPv6]) |
| 686 | if not decrypt_pkt.haslayer(IPv6): |
| 687 | decrypt_pkt = IPv6(decrypt_pkt[Raw].load) |
| 688 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 689 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6) |
| 690 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
| 691 | except: |
| 692 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 693 | try: |
| 694 | self.logger.debug(ppp("Decrypted packet:", decrypt_pkt)) |
| 695 | except: |
| 696 | pass |
| 697 | raise |
| 698 | |
| 699 | def verify_drop_tun_66(self, p_in, count=1, payload_size=64): |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 700 | self.vapi.cli("clear errors") |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 701 | self.vapi.cli("clear ipsec sa") |
| 702 | |
| 703 | config_tun_params(p_in, self.encryption_type, self.tun_if) |
| 704 | send_pkts = self.gen_encrypt_pkts6(p_in.scapy_tun_sa, self.tun_if, |
| 705 | src=p_in.remote_tun_if_host, |
| 706 | dst=self.pg1.remote_ip6, |
| 707 | count=count) |
| 708 | self.send_and_assert_no_replies(self.tun_if, send_pkts) |
| 709 | self.logger.info(self.vapi.cli("sh punt stats")) |
| 710 | |
| 711 | def verify_tun_66(self, p_in, p_out=None, count=1, payload_size=64): |
| 712 | self.vapi.cli("clear errors") |
| 713 | self.vapi.cli("clear ipsec sa") |
| 714 | if not p_out: |
| 715 | p_out = p_in |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 716 | try: |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 717 | config_tun_params(p_in, self.encryption_type, self.tun_if) |
| 718 | config_tun_params(p_out, self.encryption_type, self.tun_if) |
| 719 | send_pkts = self.gen_encrypt_pkts6(p_in.scapy_tun_sa, self.tun_if, |
| 720 | src=p_in.remote_tun_if_host, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 721 | dst=self.pg1.remote_ip6, |
| 722 | count=count) |
| 723 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 724 | self.verify_decrypted6(p_in, recv_pkts) |
| 725 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 726 | send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 727 | dst=p_out.remote_tun_if_host, |
| 728 | count=count, |
| 729 | payload_size=payload_size) |
| 730 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 731 | self.tun_if) |
| 732 | self.verify_encrypted6(p_out, p_out.vpp_tun_sa, recv_pkts) |
| 733 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 734 | finally: |
| 735 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 736 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 737 | self.verify_counters6(p_in, p_out, count) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 738 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 739 | def verify_tun_46(self, p, count=1): |
| 740 | """ ipsec 4o6 tunnel basic test """ |
| 741 | self.vapi.cli("clear errors") |
| 742 | try: |
| 743 | config_tun_params(p, self.encryption_type, self.tun_if) |
| 744 | send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if, |
| 745 | src=p.remote_tun_if_host4, |
| 746 | dst=self.pg1.remote_ip4, |
| 747 | count=count) |
| 748 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 749 | for recv_pkt in recv_pkts: |
| 750 | self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host4) |
| 751 | self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4) |
| 752 | self.assert_packet_checksums_valid(recv_pkt) |
| 753 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
| 754 | dst=p.remote_tun_if_host4, |
| 755 | count=count) |
| 756 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 757 | for recv_pkt in recv_pkts: |
| 758 | try: |
| 759 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6]) |
| 760 | if not decrypt_pkt.haslayer(IP): |
| 761 | decrypt_pkt = IP(decrypt_pkt[Raw].load) |
| 762 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4) |
| 763 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host4) |
| 764 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 765 | except: |
| 766 | self.logger.debug(ppp("Unexpected packet:", recv_pkt)) |
| 767 | try: |
| 768 | self.logger.debug(ppp("Decrypted packet:", |
| 769 | decrypt_pkt)) |
| 770 | except: |
| 771 | pass |
| 772 | raise |
| 773 | finally: |
| 774 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 775 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 776 | self.verify_counters6(p, p, count) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 777 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 778 | |
| 779 | class IpsecTun6Tests(IpsecTun6): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 780 | """ UT test methods for Tunnel v6 """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 781 | |
| 782 | def test_tun_basic66(self): |
| 783 | """ ipsec 6o6 tunnel basic test """ |
| 784 | self.verify_tun_66(self.params[socket.AF_INET6], count=1) |
| 785 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 786 | def test_tun_burst66(self): |
| 787 | """ ipsec 6o6 tunnel burst test """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 788 | self.verify_tun_66(self.params[socket.AF_INET6], count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 789 | |
| 790 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 791 | class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 792 | """ UT test methods for Tunnel v6 & v4 """ |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 793 | pass |
| 794 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 795 | |
| 796 | if __name__ == '__main__': |
| 797 | unittest.main(testRunner=VppTestRunner) |