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 |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 7 | from scapy.layers.l2 import Ether |
| 8 | from scapy.packet import Raw |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 9 | from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 10 | |
| 11 | from framework import VppTestCase, VppTestRunner |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 12 | from util import ppp, reassemble4, fragment_rfc791, fragment_rfc8200 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 13 | from vpp_papi import VppEnum |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 14 | |
| 15 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 16 | class IPsecIPv4Params(object): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 17 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 18 | addr_type = socket.AF_INET |
| 19 | addr_any = "0.0.0.0" |
| 20 | addr_bcast = "255.255.255.255" |
| 21 | addr_len = 32 |
| 22 | is_ipv6 = 0 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 23 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 24 | def __init__(self): |
| 25 | self.remote_tun_if_host = '1.1.1.1' |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 26 | self.remote_tun_if_host6 = '1111::1' |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 27 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 28 | self.scapy_tun_sa_id = 10 |
| 29 | self.scapy_tun_spi = 1001 |
| 30 | self.vpp_tun_sa_id = 20 |
| 31 | self.vpp_tun_spi = 1000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 32 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 33 | self.scapy_tra_sa_id = 30 |
| 34 | self.scapy_tra_spi = 2001 |
| 35 | self.vpp_tra_sa_id = 40 |
| 36 | self.vpp_tra_spi = 2000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 37 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 38 | self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t. |
| 39 | IPSEC_API_INTEG_ALG_SHA1_96) |
| 40 | self.auth_algo = 'HMAC-SHA1-96' # scapy name |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 41 | self.auth_key = b'C91KUR9GYMm5GfkEvNjX' |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 42 | |
| 43 | self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t. |
| 44 | IPSEC_API_CRYPTO_ALG_AES_CBC_128) |
| 45 | self.crypt_algo = 'AES-CBC' # scapy name |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 46 | self.crypt_key = b'JPjyOWBeVEQiMe7h' |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 47 | self.salt = 0 |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 48 | self.flags = 0 |
| 49 | self.nat_header = None |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 50 | |
| 51 | |
| 52 | class IPsecIPv6Params(object): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 53 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 54 | addr_type = socket.AF_INET6 |
| 55 | addr_any = "0::0" |
| 56 | addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" |
| 57 | addr_len = 128 |
| 58 | is_ipv6 = 1 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 59 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 60 | def __init__(self): |
| 61 | 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] | 62 | self.remote_tun_if_host4 = '1.1.1.1' |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 63 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 64 | self.scapy_tun_sa_id = 50 |
| 65 | self.scapy_tun_spi = 3001 |
| 66 | self.vpp_tun_sa_id = 60 |
| 67 | self.vpp_tun_spi = 3000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 68 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 69 | self.scapy_tra_sa_id = 70 |
| 70 | self.scapy_tra_spi = 4001 |
| 71 | self.vpp_tra_sa_id = 80 |
| 72 | self.vpp_tra_spi = 4000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 73 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 74 | self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t. |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 75 | IPSEC_API_INTEG_ALG_SHA1_96) |
| 76 | self.auth_algo = 'HMAC-SHA1-96' # scapy name |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 77 | self.auth_key = b'C91KUR9GYMm5GfkEvNjX' |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 78 | |
| 79 | self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t. |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 80 | IPSEC_API_CRYPTO_ALG_AES_CBC_128) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 81 | self.crypt_algo = 'AES-CBC' # scapy name |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 82 | self.crypt_key = b'JPjyOWBeVEQiMe7h' |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 83 | self.salt = 0 |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 84 | self.flags = 0 |
| 85 | self.nat_header = None |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 86 | |
| 87 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 88 | def mk_scapy_crypt_key(p): |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 89 | if p.crypt_algo == "AES-GCM": |
| 90 | return p.crypt_key + struct.pack("!I", p.salt) |
| 91 | else: |
| 92 | return p.crypt_key |
| 93 | |
| 94 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 95 | def config_tun_params(p, encryption_type, tun_if): |
| 96 | ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6} |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 97 | esn_en = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t. |
| 98 | IPSEC_API_SAD_FLAG_USE_ESN)) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 99 | crypt_key = mk_scapy_crypt_key(p) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 100 | p.scapy_tun_sa = SecurityAssociation( |
| 101 | encryption_type, spi=p.vpp_tun_spi, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 102 | crypt_algo=p.crypt_algo, |
| 103 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 104 | auth_algo=p.auth_algo, auth_key=p.auth_key, |
| 105 | tunnel_header=ip_class_by_addr_type[p.addr_type]( |
| 106 | src=tun_if.remote_addr[p.addr_type], |
| 107 | dst=tun_if.local_addr[p.addr_type]), |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 108 | nat_t_header=p.nat_header, |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 109 | esn_en=esn_en) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 110 | p.vpp_tun_sa = SecurityAssociation( |
| 111 | encryption_type, spi=p.scapy_tun_spi, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 112 | crypt_algo=p.crypt_algo, |
| 113 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 114 | auth_algo=p.auth_algo, auth_key=p.auth_key, |
| 115 | tunnel_header=ip_class_by_addr_type[p.addr_type]( |
| 116 | dst=tun_if.remote_addr[p.addr_type], |
| 117 | src=tun_if.local_addr[p.addr_type]), |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 118 | nat_t_header=p.nat_header, |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 119 | esn_en=esn_en) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 120 | |
| 121 | |
| 122 | def config_tra_params(p, encryption_type): |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 123 | esn_en = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t. |
| 124 | IPSEC_API_SAD_FLAG_USE_ESN)) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 125 | crypt_key = mk_scapy_crypt_key(p) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 126 | p.scapy_tra_sa = SecurityAssociation( |
| 127 | encryption_type, |
| 128 | spi=p.vpp_tra_spi, |
| 129 | crypt_algo=p.crypt_algo, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 130 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 131 | auth_algo=p.auth_algo, |
| 132 | auth_key=p.auth_key, |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 133 | nat_t_header=p.nat_header, |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 134 | esn_en=esn_en) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 135 | p.vpp_tra_sa = SecurityAssociation( |
| 136 | encryption_type, |
| 137 | spi=p.scapy_tra_spi, |
| 138 | crypt_algo=p.crypt_algo, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 139 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 140 | auth_algo=p.auth_algo, |
| 141 | auth_key=p.auth_key, |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 142 | nat_t_header=p.nat_header, |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 143 | esn_en=esn_en) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 144 | |
| 145 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 146 | class TemplateIpsec(VppTestCase): |
| 147 | """ |
| 148 | TRANSPORT MODE: |
| 149 | |
| 150 | ------ encrypt --- |
| 151 | |tra_if| <-------> |VPP| |
| 152 | ------ decrypt --- |
| 153 | |
| 154 | TUNNEL MODE: |
| 155 | |
| 156 | ------ encrypt --- plain --- |
| 157 | |tun_if| <------- |VPP| <------ |pg1| |
| 158 | ------ --- --- |
| 159 | |
| 160 | ------ decrypt --- plain --- |
| 161 | |tun_if| -------> |VPP| ------> |pg1| |
| 162 | ------ --- --- |
| 163 | """ |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 164 | tun_spd_id = 1 |
| 165 | tra_spd_id = 2 |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 166 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 167 | def ipsec_select_backend(self): |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 168 | """ empty method to be overloaded when necessary """ |
| 169 | pass |
| 170 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 171 | @classmethod |
| 172 | def setUpClass(cls): |
| 173 | super(TemplateIpsec, cls).setUpClass() |
| 174 | |
| 175 | @classmethod |
| 176 | def tearDownClass(cls): |
| 177 | super(TemplateIpsec, cls).tearDownClass() |
| 178 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 179 | def setup_params(self): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 180 | self.ipv4_params = IPsecIPv4Params() |
| 181 | self.ipv6_params = IPsecIPv6Params() |
| 182 | self.params = {self.ipv4_params.addr_type: self.ipv4_params, |
| 183 | self.ipv6_params.addr_type: self.ipv6_params} |
| 184 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 185 | def config_interfaces(self): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 186 | self.create_pg_interfaces(range(3)) |
| 187 | self.interfaces = list(self.pg_interfaces) |
| 188 | for i in self.interfaces: |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 189 | i.admin_up() |
| 190 | i.config_ip4() |
| 191 | i.resolve_arp() |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 192 | i.config_ip6() |
| 193 | i.resolve_ndp() |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 194 | |
| 195 | def setUp(self): |
| 196 | super(TemplateIpsec, self).setUp() |
| 197 | |
| 198 | self.setup_params() |
| 199 | |
| 200 | self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t. |
| 201 | IPSEC_API_PROTO_ESP) |
| 202 | self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t. |
| 203 | IPSEC_API_PROTO_AH) |
| 204 | |
| 205 | self.config_interfaces() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 206 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 207 | self.ipsec_select_backend() |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 208 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 209 | def unconfig_interfaces(self): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 210 | for i in self.interfaces: |
| 211 | i.admin_down() |
| 212 | i.unconfig_ip4() |
| 213 | i.unconfig_ip6() |
| 214 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 215 | def tearDown(self): |
| 216 | super(TemplateIpsec, self).tearDown() |
| 217 | |
| 218 | self.unconfig_interfaces() |
| 219 | |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 220 | def show_commands_at_teardown(self): |
| 221 | self.logger.info(self.vapi.cli("show hardware")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 222 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 223 | def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1, |
| 224 | payload_size=54): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 225 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 226 | sa.encrypt(IP(src=src, dst=dst) / |
Ole Troan | 8b76c23 | 2019-10-21 20:55:13 +0200 | [diff] [blame] | 227 | ICMP() / Raw(b'X' * payload_size)) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 228 | for i in range(count)] |
| 229 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 230 | def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1, |
| 231 | payload_size=54): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 232 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 233 | sa.encrypt(IPv6(src=src, dst=dst) / |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 234 | ICMPv6EchoRequest(id=0, seq=1, |
| 235 | data='X' * payload_size)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 236 | for i in range(count)] |
| 237 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 238 | 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] | 239 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
Ole Troan | 8b76c23 | 2019-10-21 20:55:13 +0200 | [diff] [blame] | 240 | IP(src=src, dst=dst) / ICMP() / Raw(b'X' * payload_size) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 241 | for i in range(count)] |
| 242 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 243 | 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] | 244 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 245 | IPv6(src=src, dst=dst) / |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 246 | ICMPv6EchoRequest(id=0, seq=1, data='X' * payload_size) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 247 | for i in range(count)] |
| 248 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 249 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 250 | class IpsecTcp(object): |
| 251 | def verify_tcp_checksum(self): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 252 | self.vapi.cli("test http server") |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 253 | p = self.params[socket.AF_INET] |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 254 | 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] | 255 | p.scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host, |
| 256 | dst=self.tun_if.local_ip4) / |
| 257 | TCP(flags='S', dport=80))) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 258 | self.logger.debug(ppp("Sending packet:", send)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 259 | recv = self.send_and_expect(self.tun_if, [send], self.tun_if) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 260 | recv = recv[0] |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 261 | decrypted = p.vpp_tun_sa.decrypt(recv[IP]) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 262 | self.assert_packet_checksums_valid(decrypted) |
| 263 | |
| 264 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 265 | class IpsecTcpTests(IpsecTcp): |
| 266 | def test_tcp_checksum(self): |
| 267 | """ verify checksum correctness for vpp generated packets """ |
| 268 | self.verify_tcp_checksum() |
| 269 | |
| 270 | |
| 271 | class IpsecTra4(object): |
| 272 | """ verify methods for Transport v4 """ |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 273 | def verify_tra_anti_replay(self): |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 274 | p = self.params[socket.AF_INET] |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 275 | esn_en = p.vpp_tra_sa.esn_en |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 276 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 277 | seq_cycle_node_name = ('/err/%s/sequence number cycled' % |
| 278 | self.tra4_encrypt_node_name) |
| 279 | replay_node_name = ('/err/%s/SA replayed packet' % |
| 280 | self.tra4_decrypt_node_name) |
| 281 | if ESP == self.encryption_type and p.crypt_algo == "AES-GCM": |
| 282 | hash_failed_node_name = ('/err/%s/ESP decryption failed' % |
| 283 | self.tra4_decrypt_node_name) |
| 284 | else: |
| 285 | hash_failed_node_name = ('/err/%s/Integrity check failed' % |
| 286 | self.tra4_decrypt_node_name) |
| 287 | replay_count = self.statistics.get_err_counter(replay_node_name) |
| 288 | hash_failed_count = self.statistics.get_err_counter( |
| 289 | hash_failed_node_name) |
| 290 | seq_cycle_count = self.statistics.get_err_counter(seq_cycle_node_name) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 291 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 292 | if ESP == self.encryption_type: |
| 293 | undersize_node_name = ('/err/%s/undersized packet' % |
| 294 | self.tra4_decrypt_node_name) |
| 295 | undersize_count = self.statistics.get_err_counter( |
| 296 | undersize_node_name) |
| 297 | |
| 298 | # |
| 299 | # send packets with seq numbers 1->34 |
| 300 | # this means the window size is still in Case B (see RFC4303 |
| 301 | # Appendix A) |
| 302 | # |
| 303 | # for reasons i haven't investigated Scapy won't create a packet with |
| 304 | # seq_num=0 |
| 305 | # |
| 306 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 307 | dst=self.tra_if.local_mac) / |
| 308 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 309 | dst=self.tra_if.local_ip4) / |
| 310 | ICMP(), |
| 311 | seq_num=seq)) |
| 312 | for seq in range(1, 34)] |
| 313 | recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 314 | |
| 315 | # replayed packets are dropped |
| 316 | self.send_and_assert_no_replies(self.tra_if, pkts) |
| 317 | replay_count += len(pkts) |
| 318 | self.assert_error_counter_equal(replay_node_name, replay_count) |
| 319 | |
| 320 | # |
Neale Ranns | 3b9374f | 2019-08-01 04:45:15 -0700 | [diff] [blame] | 321 | # now send a batch of packets all with the same sequence number |
| 322 | # the first packet in the batch is legitimate, the rest bogus |
| 323 | # |
| 324 | pkts = (Ether(src=self.tra_if.remote_mac, |
| 325 | dst=self.tra_if.local_mac) / |
| 326 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 327 | dst=self.tra_if.local_ip4) / |
| 328 | ICMP(), |
| 329 | seq_num=35)) |
| 330 | recv_pkts = self.send_and_expect(self.tra_if, pkts * 8, |
| 331 | self.tra_if, n_rx=1) |
| 332 | replay_count += 7 |
| 333 | self.assert_error_counter_equal(replay_node_name, replay_count) |
| 334 | |
| 335 | # |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 336 | # now move the window over to 257 (more than one byte) and into Case A |
| 337 | # |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 338 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 339 | dst=self.tra_if.local_mac) / |
| 340 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 341 | dst=self.tra_if.local_ip4) / |
| 342 | ICMP(), |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 343 | seq_num=257)) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 344 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 345 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 346 | # replayed packets are dropped |
| 347 | self.send_and_assert_no_replies(self.tra_if, pkt * 3) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 348 | replay_count += 3 |
| 349 | self.assert_error_counter_equal(replay_node_name, replay_count) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 350 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 351 | # the window size is 64 packets |
| 352 | # in window are still accepted |
| 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(), |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 358 | seq_num=200)) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 359 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 360 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 361 | # a packet that does not decrypt does not move the window forward |
| 362 | bogus_sa = SecurityAssociation(self.encryption_type, |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 363 | p.vpp_tra_spi, |
| 364 | crypt_algo=p.crypt_algo, |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 365 | crypt_key=mk_scapy_crypt_key(p)[::-1], |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 366 | auth_algo=p.auth_algo, |
| 367 | auth_key=p.auth_key[::-1]) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 368 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 369 | dst=self.tra_if.local_mac) / |
| 370 | bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 371 | dst=self.tra_if.local_ip4) / |
| 372 | ICMP(), |
| 373 | seq_num=350)) |
| 374 | self.send_and_assert_no_replies(self.tra_if, pkt * 17) |
| 375 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 376 | hash_failed_count += 17 |
| 377 | self.assert_error_counter_equal(hash_failed_node_name, |
| 378 | hash_failed_count) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 379 | |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 380 | # a malformed 'runt' packet |
| 381 | # created by a mis-constructed SA |
Neale Ranns | 2cdcd0c | 2019-08-27 12:26:14 +0000 | [diff] [blame] | 382 | if (ESP == self.encryption_type and p.crypt_algo != "NULL"): |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 383 | bogus_sa = SecurityAssociation(self.encryption_type, |
| 384 | p.vpp_tra_spi) |
| 385 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 386 | dst=self.tra_if.local_mac) / |
| 387 | bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 388 | dst=self.tra_if.local_ip4) / |
| 389 | ICMP(), |
| 390 | seq_num=350)) |
| 391 | self.send_and_assert_no_replies(self.tra_if, pkt * 17) |
| 392 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 393 | undersize_count += 17 |
| 394 | self.assert_error_counter_equal(undersize_node_name, |
| 395 | undersize_count) |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 396 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 397 | # which we can determine since this packet is still in the window |
| 398 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 399 | dst=self.tra_if.local_mac) / |
| 400 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 401 | dst=self.tra_if.local_ip4) / |
| 402 | ICMP(), |
| 403 | seq_num=234)) |
Klement Sekera | 14d7e90 | 2018-12-10 13:46:09 +0100 | [diff] [blame] | 404 | self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 405 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 406 | # |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 407 | # out of window are dropped |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 408 | # this is Case B. So VPP will consider this to be a high seq num wrap |
| 409 | # and so the decrypt attempt will fail |
| 410 | # |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 411 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 412 | dst=self.tra_if.local_mac) / |
| 413 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 414 | dst=self.tra_if.local_ip4) / |
| 415 | ICMP(), |
| 416 | seq_num=17)) |
| 417 | self.send_and_assert_no_replies(self.tra_if, pkt * 17) |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 418 | |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 419 | if esn_en: |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 420 | # an out of window error with ESN looks like a high sequence |
| 421 | # wrap. but since it isn't then the verify will fail. |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 422 | hash_failed_count += 17 |
| 423 | self.assert_error_counter_equal(hash_failed_node_name, |
| 424 | hash_failed_count) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 425 | |
| 426 | else: |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 427 | replay_count += 17 |
| 428 | self.assert_error_counter_equal(replay_node_name, |
| 429 | replay_count) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 430 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 431 | # valid packet moves the window over to 258 |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 432 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 433 | dst=self.tra_if.local_mac) / |
| 434 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 435 | dst=self.tra_if.local_ip4) / |
| 436 | ICMP(), |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 437 | seq_num=258)) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 438 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 439 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 440 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 441 | # |
| 442 | # move VPP's SA TX seq-num to just before the seq-number wrap. |
| 443 | # then fire in a packet that VPP should drop on TX because it |
| 444 | # causes the TX seq number to wrap; unless we're using extened sequence |
| 445 | # numbers. |
| 446 | # |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 447 | self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.scapy_tra_sa_id) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 448 | self.logger.info(self.vapi.ppcli("show ipsec sa 0")) |
| 449 | self.logger.info(self.vapi.ppcli("show ipsec sa 1")) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 450 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 451 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 452 | dst=self.tra_if.local_mac) / |
| 453 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 454 | dst=self.tra_if.local_ip4) / |
| 455 | ICMP(), |
| 456 | seq_num=seq)) |
| 457 | for seq in range(259, 280)] |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 458 | |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 459 | if esn_en: |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 460 | rxs = self.send_and_expect(self.tra_if, pkts, self.tra_if) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 461 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 462 | # |
| 463 | # in order for scapy to decrypt its SA's high order number needs |
| 464 | # to wrap |
| 465 | # |
| 466 | p.vpp_tra_sa.seq_num = 0x100000000 |
| 467 | for rx in rxs: |
| 468 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 469 | |
| 470 | # |
| 471 | # wrap scapy's TX high sequence number. VPP is in case B, so it |
| 472 | # will consider this a high seq wrap also. |
| 473 | # The low seq num we set it to will place VPP's RX window in Case A |
| 474 | # |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 475 | p.scapy_tra_sa.seq_num = 0x100000005 |
| 476 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 477 | dst=self.tra_if.local_mac) / |
| 478 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 479 | dst=self.tra_if.local_ip4) / |
| 480 | ICMP(), |
| 481 | seq_num=0x100000005)) |
| 482 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 483 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 484 | |
| 485 | # |
| 486 | # A packet that has seq num between (2^32-64) and 5 is within |
| 487 | # the window |
| 488 | # |
| 489 | p.scapy_tra_sa.seq_num = 0xfffffffd |
| 490 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 491 | dst=self.tra_if.local_mac) / |
| 492 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 493 | dst=self.tra_if.local_ip4) / |
| 494 | ICMP(), |
| 495 | seq_num=0xfffffffd)) |
| 496 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 497 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 498 | |
| 499 | # |
| 500 | # While in case A we cannot wrap the high sequence number again |
| 501 | # becuase VPP will consider this packet to be one that moves the |
| 502 | # window forward |
| 503 | # |
| 504 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 505 | dst=self.tra_if.local_mac) / |
| 506 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 507 | dst=self.tra_if.local_ip4) / |
| 508 | ICMP(), |
| 509 | seq_num=0x200000999)) |
| 510 | self.send_and_assert_no_replies(self.tra_if, [pkt], self.tra_if) |
| 511 | |
| 512 | hash_failed_count += 1 |
| 513 | self.assert_error_counter_equal(hash_failed_node_name, |
| 514 | hash_failed_count) |
| 515 | |
| 516 | # |
| 517 | # but if we move the wondow forward to case B, then we can wrap |
| 518 | # again |
| 519 | # |
| 520 | p.scapy_tra_sa.seq_num = 0x100000555 |
| 521 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 522 | dst=self.tra_if.local_mac) / |
| 523 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 524 | dst=self.tra_if.local_ip4) / |
| 525 | ICMP(), |
| 526 | seq_num=0x100000555)) |
| 527 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 528 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 529 | |
| 530 | p.scapy_tra_sa.seq_num = 0x200000444 |
| 531 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 532 | dst=self.tra_if.local_mac) / |
| 533 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 534 | dst=self.tra_if.local_ip4) / |
| 535 | ICMP(), |
| 536 | seq_num=0x200000444)) |
| 537 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 538 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 539 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 540 | else: |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 541 | # |
| 542 | # without ESN TX sequence numbers can't wrap and packets are |
| 543 | # dropped from here on out. |
| 544 | # |
| 545 | self.send_and_assert_no_replies(self.tra_if, pkts) |
| 546 | seq_cycle_count += len(pkts) |
| 547 | self.assert_error_counter_equal(seq_cycle_node_name, |
| 548 | seq_cycle_count) |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 549 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 550 | # move the security-associations seq number on to the last we used |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 551 | 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] | 552 | p.scapy_tra_sa.seq_num = 351 |
| 553 | p.vpp_tra_sa.seq_num = 351 |
| 554 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 555 | def verify_tra_basic4(self, count=1): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 556 | """ ipsec v4 transport basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 557 | self.vapi.cli("clear errors") |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 558 | self.vapi.cli("clear ipsec sa") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 559 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 560 | p = self.params[socket.AF_INET] |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 561 | 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] | 562 | src=self.tra_if.remote_ip4, |
| 563 | dst=self.tra_if.local_ip4, |
| 564 | count=count) |
| 565 | recv_pkts = self.send_and_expect(self.tra_if, send_pkts, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 566 | self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 567 | for rx in recv_pkts: |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 568 | self.assertEqual(len(rx) - len(Ether()), rx[IP].len) |
| 569 | self.assert_packet_checksums_valid(rx) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 570 | try: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 571 | decrypted = p.vpp_tra_sa.decrypt(rx[IP]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 572 | self.assert_packet_checksums_valid(decrypted) |
| 573 | except: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 574 | self.logger.debug(ppp("Unexpected packet:", rx)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 575 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 576 | finally: |
| 577 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 578 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 579 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 580 | pkts = p.tra_sa_in.get_stats()['packets'] |
| 581 | self.assertEqual(pkts, count, |
| 582 | "incorrect SA in counts: expected %d != %d" % |
| 583 | (count, pkts)) |
| 584 | pkts = p.tra_sa_out.get_stats()['packets'] |
| 585 | self.assertEqual(pkts, count, |
| 586 | "incorrect SA out counts: expected %d != %d" % |
| 587 | (count, pkts)) |
| 588 | |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 589 | self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count) |
| 590 | self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count) |
| 591 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 592 | |
| 593 | class IpsecTra4Tests(IpsecTra4): |
| 594 | """ UT test methods for Transport v4 """ |
| 595 | def test_tra_anti_replay(self): |
| 596 | """ ipsec v4 transport anti-reply test """ |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 597 | self.verify_tra_anti_replay() |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 598 | |
| 599 | def test_tra_basic(self, count=1): |
| 600 | """ ipsec v4 transport basic test """ |
| 601 | self.verify_tra_basic4(count=1) |
| 602 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 603 | def test_tra_burst(self): |
| 604 | """ ipsec v4 transport burst test """ |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 605 | self.verify_tra_basic4(count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 606 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 607 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 608 | class IpsecTra6(object): |
| 609 | """ verify methods for Transport v6 """ |
| 610 | def verify_tra_basic6(self, count=1): |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 611 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 612 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 613 | p = self.params[socket.AF_INET6] |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 614 | 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] | 615 | src=self.tra_if.remote_ip6, |
| 616 | dst=self.tra_if.local_ip6, |
| 617 | count=count) |
| 618 | recv_pkts = self.send_and_expect(self.tra_if, send_pkts, |
| 619 | self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 620 | for rx in recv_pkts: |
Neale Ranns | d207fd7 | 2019-04-18 17:18:12 -0700 | [diff] [blame] | 621 | self.assertEqual(len(rx) - len(Ether()) - len(IPv6()), |
| 622 | rx[IPv6].plen) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 623 | try: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 624 | decrypted = p.vpp_tra_sa.decrypt(rx[IPv6]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 625 | self.assert_packet_checksums_valid(decrypted) |
| 626 | except: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 627 | self.logger.debug(ppp("Unexpected packet:", rx)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 628 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 629 | finally: |
| 630 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 631 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 632 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 633 | pkts = p.tra_sa_in.get_stats()['packets'] |
| 634 | self.assertEqual(pkts, count, |
| 635 | "incorrect SA in counts: expected %d != %d" % |
| 636 | (count, pkts)) |
| 637 | pkts = p.tra_sa_out.get_stats()['packets'] |
| 638 | self.assertEqual(pkts, count, |
| 639 | "incorrect SA out counts: expected %d != %d" % |
| 640 | (count, pkts)) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 641 | self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count) |
| 642 | self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count) |
| 643 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 644 | |
| 645 | class IpsecTra6Tests(IpsecTra6): |
| 646 | """ UT test methods for Transport v6 """ |
| 647 | def test_tra_basic6(self): |
| 648 | """ ipsec v6 transport basic test """ |
| 649 | self.verify_tra_basic6(count=1) |
| 650 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 651 | def test_tra_burst6(self): |
| 652 | """ ipsec v6 transport burst test """ |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 653 | self.verify_tra_basic6(count=257) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 654 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 655 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 656 | class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 657 | """ UT test methods for Transport v6 and v4""" |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 658 | pass |
| 659 | |
| 660 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 661 | class IpsecTun4(object): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 662 | """ verify methods for Tunnel v4 """ |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 663 | def verify_counters4(self, p, count, n_frags=None, worker=None): |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 664 | if not n_frags: |
| 665 | n_frags = count |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 666 | if (hasattr(p, "spd_policy_in_any")): |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 667 | pkts = p.spd_policy_in_any.get_stats(worker)['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 668 | self.assertEqual(pkts, count, |
| 669 | "incorrect SPD any policy: expected %d != %d" % |
| 670 | (count, pkts)) |
| 671 | |
| 672 | if (hasattr(p, "tun_sa_in")): |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 673 | pkts = p.tun_sa_in.get_stats(worker)['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 674 | self.assertEqual(pkts, count, |
| 675 | "incorrect SA in counts: expected %d != %d" % |
| 676 | (count, pkts)) |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 677 | pkts = p.tun_sa_out.get_stats(worker)['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 678 | self.assertEqual(pkts, count, |
| 679 | "incorrect SA out counts: expected %d != %d" % |
| 680 | (count, pkts)) |
| 681 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 682 | self.assert_packet_counter_equal(self.tun4_encrypt_node_name, n_frags) |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 683 | self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count) |
| 684 | |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 685 | def verify_decrypted(self, p, rxs): |
| 686 | for rx in rxs: |
| 687 | self.assert_equal(rx[IP].src, p.remote_tun_if_host) |
| 688 | self.assert_equal(rx[IP].dst, self.pg1.remote_ip4) |
| 689 | self.assert_packet_checksums_valid(rx) |
| 690 | |
| 691 | def verify_encrypted(self, p, sa, rxs): |
| 692 | decrypt_pkts = [] |
| 693 | for rx in rxs: |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 694 | if p.nat_header: |
| 695 | self.assertEqual(rx[UDP].dport, 4500) |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 696 | self.assert_packet_checksums_valid(rx) |
| 697 | self.assertEqual(len(rx) - len(Ether()), rx[IP].len) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 698 | try: |
| 699 | decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IP]) |
| 700 | if not decrypt_pkt.haslayer(IP): |
| 701 | decrypt_pkt = IP(decrypt_pkt[Raw].load) |
| 702 | decrypt_pkts.append(decrypt_pkt) |
| 703 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4) |
| 704 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
| 705 | except: |
| 706 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 707 | try: |
| 708 | self.logger.debug(ppp("Decrypted packet:", decrypt_pkt)) |
| 709 | except: |
| 710 | pass |
| 711 | raise |
| 712 | pkts = reassemble4(decrypt_pkts) |
| 713 | for pkt in pkts: |
| 714 | self.assert_packet_checksums_valid(pkt) |
| 715 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 716 | 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] | 717 | self.vapi.cli("clear errors") |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 718 | if not n_rx: |
| 719 | n_rx = count |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 720 | try: |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 721 | 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] | 722 | src=p.remote_tun_if_host, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 723 | dst=self.pg1.remote_ip4, |
| 724 | count=count) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 725 | 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] | 726 | self.verify_decrypted(p, recv_pkts) |
| 727 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 728 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 729 | dst=p.remote_tun_if_host, count=count, |
| 730 | payload_size=payload_size) |
| 731 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 732 | self.tun_if, n_rx) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 733 | self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts) |
| 734 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 735 | finally: |
| 736 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 737 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 738 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 739 | self.verify_counters4(p, count, n_rx) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 740 | |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 741 | def verify_tun_reass_44(self, p): |
| 742 | self.vapi.cli("clear errors") |
| 743 | self.vapi.ip_reassembly_enable_disable( |
| 744 | sw_if_index=self.tun_if.sw_if_index, enable_ip4=True) |
| 745 | |
| 746 | try: |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 747 | send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if, |
| 748 | src=p.remote_tun_if_host, |
| 749 | dst=self.pg1.remote_ip4, |
| 750 | payload_size=1900, |
| 751 | count=1) |
| 752 | send_pkts = fragment_rfc791(send_pkts[0], 1400) |
| 753 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, |
| 754 | self.pg1, n_rx=1) |
| 755 | self.verify_decrypted(p, recv_pkts) |
| 756 | |
| 757 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
| 758 | dst=p.remote_tun_if_host, count=1) |
| 759 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 760 | self.tun_if) |
| 761 | self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts) |
| 762 | |
| 763 | finally: |
| 764 | self.logger.info(self.vapi.ppcli("show error")) |
| 765 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
| 766 | |
| 767 | self.verify_counters4(p, 1, 1) |
| 768 | self.vapi.ip_reassembly_enable_disable( |
| 769 | sw_if_index=self.tun_if.sw_if_index, enable_ip4=False) |
| 770 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 771 | def verify_tun_64(self, p, count=1): |
| 772 | self.vapi.cli("clear errors") |
| 773 | try: |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 774 | send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if, |
| 775 | src=p.remote_tun_if_host6, |
| 776 | dst=self.pg1.remote_ip6, |
| 777 | count=count) |
| 778 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 779 | for recv_pkt in recv_pkts: |
| 780 | self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host6) |
| 781 | self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6) |
| 782 | self.assert_packet_checksums_valid(recv_pkt) |
| 783 | send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6, |
| 784 | dst=p.remote_tun_if_host6, count=count) |
| 785 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 786 | for recv_pkt in recv_pkts: |
| 787 | try: |
| 788 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP]) |
| 789 | if not decrypt_pkt.haslayer(IPv6): |
| 790 | decrypt_pkt = IPv6(decrypt_pkt[Raw].load) |
| 791 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6) |
| 792 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host6) |
| 793 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 794 | except: |
| 795 | self.logger.error(ppp("Unexpected packet:", recv_pkt)) |
| 796 | try: |
| 797 | self.logger.debug( |
| 798 | ppp("Decrypted packet:", decrypt_pkt)) |
| 799 | except: |
| 800 | pass |
| 801 | raise |
| 802 | finally: |
| 803 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 804 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 805 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 806 | self.verify_counters4(p, count) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 807 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 808 | def verify_keepalive(self, p): |
| 809 | pkt = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) / |
| 810 | IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4) / |
| 811 | UDP(sport=333, dport=4500) / |
Ole Troan | 8b76c23 | 2019-10-21 20:55:13 +0200 | [diff] [blame] | 812 | Raw(b'\xff')) |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 813 | self.send_and_assert_no_replies(self.tun_if, pkt*31) |
| 814 | self.assert_error_counter_equal( |
| 815 | '/err/%s/NAT Keepalive' % self.tun4_input_node, 31) |
| 816 | |
| 817 | pkt = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) / |
| 818 | IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4) / |
| 819 | UDP(sport=333, dport=4500) / |
Ole Troan | 8b76c23 | 2019-10-21 20:55:13 +0200 | [diff] [blame] | 820 | Raw(b'\xfe')) |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 821 | self.send_and_assert_no_replies(self.tun_if, pkt*31) |
| 822 | self.assert_error_counter_equal( |
| 823 | '/err/%s/Too Short' % self.tun4_input_node, 31) |
| 824 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 825 | |
| 826 | class IpsecTun4Tests(IpsecTun4): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 827 | """ UT test methods for Tunnel v4 """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 828 | def test_tun_basic44(self): |
| 829 | """ ipsec 4o4 tunnel basic test """ |
| 830 | self.verify_tun_44(self.params[socket.AF_INET], count=1) |
| 831 | |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 832 | def test_tun_reass_basic44(self): |
| 833 | """ ipsec 4o4 tunnel basic reassembly test """ |
| 834 | self.verify_tun_reass_44(self.params[socket.AF_INET]) |
| 835 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 836 | def test_tun_burst44(self): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 837 | """ ipsec 4o4 tunnel burst test """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 838 | self.verify_tun_44(self.params[socket.AF_INET], count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 839 | |
| 840 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 841 | class IpsecTun6(object): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 842 | """ verify methods for Tunnel v6 """ |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 843 | def verify_counters6(self, p_in, p_out, count, worker=None): |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 844 | if (hasattr(p_in, "tun_sa_in")): |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 845 | pkts = p_in.tun_sa_in.get_stats(worker)['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 846 | self.assertEqual(pkts, count, |
| 847 | "incorrect SA in counts: expected %d != %d" % |
| 848 | (count, pkts)) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 849 | if (hasattr(p_out, "tun_sa_out")): |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 850 | pkts = p_out.tun_sa_out.get_stats(worker)['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 851 | self.assertEqual(pkts, count, |
| 852 | "incorrect SA out counts: expected %d != %d" % |
| 853 | (count, pkts)) |
| 854 | self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count) |
| 855 | self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count) |
| 856 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 857 | def verify_decrypted6(self, p, rxs): |
| 858 | for rx in rxs: |
| 859 | self.assert_equal(rx[IPv6].src, p.remote_tun_if_host) |
| 860 | self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6) |
| 861 | self.assert_packet_checksums_valid(rx) |
| 862 | |
| 863 | def verify_encrypted6(self, p, sa, rxs): |
| 864 | for rx in rxs: |
| 865 | self.assert_packet_checksums_valid(rx) |
| 866 | self.assertEqual(len(rx) - len(Ether()) - len(IPv6()), |
| 867 | rx[IPv6].plen) |
| 868 | try: |
| 869 | decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IPv6]) |
| 870 | if not decrypt_pkt.haslayer(IPv6): |
| 871 | decrypt_pkt = IPv6(decrypt_pkt[Raw].load) |
| 872 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 873 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6) |
| 874 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
| 875 | except: |
| 876 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 877 | try: |
| 878 | self.logger.debug(ppp("Decrypted packet:", decrypt_pkt)) |
| 879 | except: |
| 880 | pass |
| 881 | raise |
| 882 | |
| 883 | 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] | 884 | self.vapi.cli("clear errors") |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 885 | self.vapi.cli("clear ipsec sa") |
| 886 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 887 | send_pkts = self.gen_encrypt_pkts6(p_in.scapy_tun_sa, self.tun_if, |
| 888 | src=p_in.remote_tun_if_host, |
| 889 | dst=self.pg1.remote_ip6, |
| 890 | count=count) |
| 891 | self.send_and_assert_no_replies(self.tun_if, send_pkts) |
| 892 | self.logger.info(self.vapi.cli("sh punt stats")) |
| 893 | |
| 894 | def verify_tun_66(self, p_in, p_out=None, count=1, payload_size=64): |
| 895 | self.vapi.cli("clear errors") |
| 896 | self.vapi.cli("clear ipsec sa") |
| 897 | if not p_out: |
| 898 | p_out = p_in |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 899 | try: |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 900 | send_pkts = self.gen_encrypt_pkts6(p_in.scapy_tun_sa, self.tun_if, |
| 901 | src=p_in.remote_tun_if_host, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 902 | dst=self.pg1.remote_ip6, |
| 903 | count=count) |
| 904 | 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] | 905 | self.verify_decrypted6(p_in, recv_pkts) |
| 906 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 907 | send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 908 | dst=p_out.remote_tun_if_host, |
| 909 | count=count, |
| 910 | payload_size=payload_size) |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 911 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 912 | self.verify_encrypted6(p_out, p_out.vpp_tun_sa, recv_pkts) |
| 913 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 914 | finally: |
| 915 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 916 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 917 | self.verify_counters6(p_in, p_out, count) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 918 | |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 919 | def verify_tun_reass_66(self, p): |
| 920 | self.vapi.cli("clear errors") |
| 921 | self.vapi.ip_reassembly_enable_disable( |
| 922 | sw_if_index=self.tun_if.sw_if_index, enable_ip6=True) |
| 923 | |
| 924 | try: |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 925 | send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if, |
| 926 | src=p.remote_tun_if_host, |
| 927 | dst=self.pg1.remote_ip6, |
| 928 | count=1, |
| 929 | payload_size=1900) |
| 930 | send_pkts = fragment_rfc8200(send_pkts[0], 1, 1400, self.logger) |
| 931 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, |
| 932 | self.pg1, n_rx=1) |
| 933 | self.verify_decrypted6(p, recv_pkts) |
| 934 | |
| 935 | send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6, |
| 936 | dst=p.remote_tun_if_host, |
| 937 | count=1, |
| 938 | payload_size=64) |
| 939 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 940 | self.tun_if) |
| 941 | self.verify_encrypted6(p, p.vpp_tun_sa, recv_pkts) |
| 942 | finally: |
| 943 | self.logger.info(self.vapi.ppcli("show error")) |
| 944 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
| 945 | self.verify_counters6(p, p, 1) |
| 946 | self.vapi.ip_reassembly_enable_disable( |
| 947 | sw_if_index=self.tun_if.sw_if_index, enable_ip6=False) |
| 948 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 949 | def verify_tun_46(self, p, count=1): |
| 950 | """ ipsec 4o6 tunnel basic test """ |
| 951 | self.vapi.cli("clear errors") |
| 952 | try: |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 953 | send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if, |
| 954 | src=p.remote_tun_if_host4, |
| 955 | dst=self.pg1.remote_ip4, |
| 956 | count=count) |
| 957 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 958 | for recv_pkt in recv_pkts: |
| 959 | self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host4) |
| 960 | self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4) |
| 961 | self.assert_packet_checksums_valid(recv_pkt) |
| 962 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
| 963 | dst=p.remote_tun_if_host4, |
| 964 | count=count) |
| 965 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 966 | for recv_pkt in recv_pkts: |
| 967 | try: |
| 968 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6]) |
| 969 | if not decrypt_pkt.haslayer(IP): |
| 970 | decrypt_pkt = IP(decrypt_pkt[Raw].load) |
| 971 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4) |
| 972 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host4) |
| 973 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 974 | except: |
| 975 | self.logger.debug(ppp("Unexpected packet:", recv_pkt)) |
| 976 | try: |
| 977 | self.logger.debug(ppp("Decrypted packet:", |
| 978 | decrypt_pkt)) |
| 979 | except: |
| 980 | pass |
| 981 | raise |
| 982 | finally: |
| 983 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 984 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 985 | self.verify_counters6(p, p, count) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 986 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 987 | |
| 988 | class IpsecTun6Tests(IpsecTun6): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 989 | """ UT test methods for Tunnel v6 """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 990 | |
| 991 | def test_tun_basic66(self): |
| 992 | """ ipsec 6o6 tunnel basic test """ |
| 993 | self.verify_tun_66(self.params[socket.AF_INET6], count=1) |
| 994 | |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 995 | def test_tun_reass_basic66(self): |
| 996 | """ ipsec 6o6 tunnel basic reassembly test """ |
| 997 | self.verify_tun_reass_66(self.params[socket.AF_INET6]) |
| 998 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 999 | def test_tun_burst66(self): |
| 1000 | """ ipsec 6o6 tunnel burst test """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1001 | self.verify_tun_66(self.params[socket.AF_INET6], count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1002 | |
| 1003 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1004 | class IpsecTun6HandoffTests(IpsecTun6): |
| 1005 | """ UT test methods for Tunnel v6 with multiple workers """ |
| 1006 | worker_config = "workers 2" |
| 1007 | |
| 1008 | def test_tun_handoff_66(self): |
| 1009 | """ ipsec 6o6 tunnel worker hand-off test """ |
| 1010 | N_PKTS = 15 |
| 1011 | p = self.params[socket.AF_INET6] |
| 1012 | |
| 1013 | # inject alternately on worker 0 and 1. all counts on the SA |
| 1014 | # should be against worker 0 |
| 1015 | for worker in [0, 1, 0, 1]: |
| 1016 | send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if, |
| 1017 | src=p.remote_tun_if_host, |
| 1018 | dst=self.pg1.remote_ip6, |
| 1019 | count=N_PKTS) |
| 1020 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, |
| 1021 | self.pg1, worker=worker) |
| 1022 | self.verify_decrypted6(p, recv_pkts) |
| 1023 | |
| 1024 | send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6, |
| 1025 | dst=p.remote_tun_if_host, |
| 1026 | count=N_PKTS) |
| 1027 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 1028 | self.tun_if, worker=worker) |
| 1029 | self.verify_encrypted6(p, p.vpp_tun_sa, recv_pkts) |
| 1030 | |
| 1031 | # all counts against the first worker that was used |
| 1032 | self.verify_counters6(p, p, 4*N_PKTS, worker=0) |
| 1033 | |
| 1034 | |
| 1035 | class IpsecTun4HandoffTests(IpsecTun4): |
| 1036 | """ UT test methods for Tunnel v4 with multiple workers """ |
| 1037 | worker_config = "workers 2" |
| 1038 | |
| 1039 | def test_tun_handooff_44(self): |
| 1040 | """ ipsec 4o4 tunnel worker hand-off test """ |
| 1041 | N_PKTS = 15 |
| 1042 | p = self.params[socket.AF_INET] |
| 1043 | |
| 1044 | # inject alternately on worker 0 and 1. all counts on the SA |
| 1045 | # should be against worker 0 |
| 1046 | for worker in [0, 1, 0, 1]: |
| 1047 | send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if, |
| 1048 | src=p.remote_tun_if_host, |
| 1049 | dst=self.pg1.remote_ip4, |
| 1050 | count=N_PKTS) |
| 1051 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, |
| 1052 | self.pg1, worker=worker) |
| 1053 | self.verify_decrypted(p, recv_pkts) |
| 1054 | |
| 1055 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
| 1056 | dst=p.remote_tun_if_host, |
| 1057 | count=N_PKTS) |
| 1058 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 1059 | self.tun_if, worker=worker) |
| 1060 | self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts) |
| 1061 | |
| 1062 | # all counts against the first worker that was used |
| 1063 | self.verify_counters4(p, 4*N_PKTS, worker=0) |
| 1064 | |
| 1065 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 1066 | class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 1067 | """ UT test methods for Tunnel v6 & v4 """ |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1068 | pass |
| 1069 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1070 | |
| 1071 | if __name__ == '__main__': |
| 1072 | unittest.main(testRunner=VppTestRunner) |