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