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 | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 475 | 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] | 476 | self.vapi.cli("clear errors") |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 477 | if not n_rx: |
| 478 | n_rx = count |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 479 | try: |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 480 | config_tun_params(p, self.encryption_type, self.tun_if) |
| 481 | 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] | 482 | src=p.remote_tun_if_host, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 483 | dst=self.pg1.remote_ip4, |
| 484 | count=count) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 485 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 486 | for recv_pkt in recv_pkts: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 487 | self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 488 | self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4) |
| 489 | self.assert_packet_checksums_valid(recv_pkt) |
| 490 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 491 | dst=p.remote_tun_if_host, count=count, |
| 492 | payload_size=payload_size) |
| 493 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 494 | self.tun_if, n_rx) |
| 495 | decrypt_pkts = [] |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 496 | for recv_pkt in recv_pkts: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 497 | try: |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 498 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 499 | if not decrypt_pkt.haslayer(IP): |
| 500 | decrypt_pkt = IP(decrypt_pkt[Raw].load) |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 501 | decrypt_pkts.append(decrypt_pkt) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 502 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4) |
| 503 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 504 | except: |
| 505 | self.logger.debug(ppp("Unexpected packet:", recv_pkt)) |
| 506 | try: |
| 507 | self.logger.debug( |
| 508 | ppp("Decrypted packet:", decrypt_pkt)) |
| 509 | except: |
| 510 | pass |
| 511 | raise |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 512 | pkts = reassemble4(decrypt_pkts) |
| 513 | for pkt in pkts: |
| 514 | self.assert_packet_checksums_valid(pkt) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 515 | finally: |
| 516 | self.logger.info(self.vapi.ppcli("show error")) |
| 517 | self.logger.info(self.vapi.ppcli("show ipsec")) |
| 518 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 519 | self.verify_counters(p, count) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 520 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 521 | def verify_tun_64(self, p, count=1): |
| 522 | self.vapi.cli("clear errors") |
| 523 | try: |
| 524 | config_tun_params(p, self.encryption_type, self.tun_if) |
| 525 | send_pkts = self.gen_encrypt_pkts6(p.scapy_tun_sa, self.tun_if, |
| 526 | src=p.remote_tun_if_host6, |
| 527 | dst=self.pg1.remote_ip6, |
| 528 | count=count) |
| 529 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 530 | for recv_pkt in recv_pkts: |
| 531 | self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host6) |
| 532 | self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6) |
| 533 | self.assert_packet_checksums_valid(recv_pkt) |
| 534 | send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6, |
| 535 | dst=p.remote_tun_if_host6, count=count) |
| 536 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 537 | for recv_pkt in recv_pkts: |
| 538 | try: |
| 539 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP]) |
| 540 | if not decrypt_pkt.haslayer(IPv6): |
| 541 | decrypt_pkt = IPv6(decrypt_pkt[Raw].load) |
| 542 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6) |
| 543 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host6) |
| 544 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 545 | except: |
| 546 | self.logger.error(ppp("Unexpected packet:", recv_pkt)) |
| 547 | try: |
| 548 | self.logger.debug( |
| 549 | ppp("Decrypted packet:", decrypt_pkt)) |
| 550 | except: |
| 551 | pass |
| 552 | raise |
| 553 | finally: |
| 554 | self.logger.info(self.vapi.ppcli("show error")) |
| 555 | self.logger.info(self.vapi.ppcli("show ipsec")) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 556 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 557 | self.verify_counters(p, count) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 558 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 559 | |
| 560 | class IpsecTun4Tests(IpsecTun4): |
| 561 | |
| 562 | def test_tun_basic44(self): |
| 563 | """ ipsec 4o4 tunnel basic test """ |
| 564 | self.verify_tun_44(self.params[socket.AF_INET], count=1) |
| 565 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 566 | def test_tun_burst44(self): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 567 | """ ipsec 4o4 tunnel burst test """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 568 | self.verify_tun_44(self.params[socket.AF_INET], count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 569 | |
| 570 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 571 | class IpsecTun6(object): |
| 572 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 573 | def verify_counters(self, p, count): |
| 574 | if (hasattr(p, "tun_sa_in")): |
| 575 | pkts = p.tun_sa_in.get_stats()['packets'] |
| 576 | self.assertEqual(pkts, count, |
| 577 | "incorrect SA in counts: expected %d != %d" % |
| 578 | (count, pkts)) |
| 579 | pkts = p.tun_sa_out.get_stats()['packets'] |
| 580 | self.assertEqual(pkts, count, |
| 581 | "incorrect SA out counts: expected %d != %d" % |
| 582 | (count, pkts)) |
| 583 | self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count) |
| 584 | self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count) |
| 585 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 586 | def verify_tun_66(self, p, count=1): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 587 | """ ipsec 6o6 tunnel basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 588 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 589 | try: |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 590 | config_tun_params(p, self.encryption_type, self.tun_if) |
| 591 | 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] | 592 | src=p.remote_tun_if_host, |
| 593 | dst=self.pg1.remote_ip6, |
| 594 | count=count) |
| 595 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 596 | for recv_pkt in recv_pkts: |
| 597 | self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host) |
| 598 | self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6) |
| 599 | self.assert_packet_checksums_valid(recv_pkt) |
| 600 | send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6, |
| 601 | dst=p.remote_tun_if_host, |
| 602 | count=count) |
| 603 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 604 | for recv_pkt in recv_pkts: |
| 605 | try: |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 606 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 607 | if not decrypt_pkt.haslayer(IPv6): |
| 608 | decrypt_pkt = IPv6(decrypt_pkt[Raw].load) |
| 609 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6) |
| 610 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
| 611 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 612 | except: |
| 613 | self.logger.debug(ppp("Unexpected packet:", recv_pkt)) |
| 614 | try: |
| 615 | self.logger.debug( |
| 616 | ppp("Decrypted packet:", decrypt_pkt)) |
| 617 | except: |
| 618 | pass |
| 619 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 620 | finally: |
| 621 | self.logger.info(self.vapi.ppcli("show error")) |
| 622 | self.logger.info(self.vapi.ppcli("show ipsec")) |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 623 | self.verify_counters(p, count) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 624 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 625 | def verify_tun_46(self, p, count=1): |
| 626 | """ ipsec 4o6 tunnel basic test """ |
| 627 | self.vapi.cli("clear errors") |
| 628 | try: |
| 629 | config_tun_params(p, self.encryption_type, self.tun_if) |
| 630 | send_pkts = self.gen_encrypt_pkts(p.scapy_tun_sa, self.tun_if, |
| 631 | src=p.remote_tun_if_host4, |
| 632 | dst=self.pg1.remote_ip4, |
| 633 | count=count) |
| 634 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 635 | for recv_pkt in recv_pkts: |
| 636 | self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host4) |
| 637 | self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4) |
| 638 | self.assert_packet_checksums_valid(recv_pkt) |
| 639 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
| 640 | dst=p.remote_tun_if_host4, |
| 641 | count=count) |
| 642 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 643 | for recv_pkt in recv_pkts: |
| 644 | try: |
| 645 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6]) |
| 646 | if not decrypt_pkt.haslayer(IP): |
| 647 | decrypt_pkt = IP(decrypt_pkt[Raw].load) |
| 648 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4) |
| 649 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host4) |
| 650 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 651 | except: |
| 652 | self.logger.debug(ppp("Unexpected packet:", recv_pkt)) |
| 653 | try: |
| 654 | self.logger.debug(ppp("Decrypted packet:", |
| 655 | decrypt_pkt)) |
| 656 | except: |
| 657 | pass |
| 658 | raise |
| 659 | finally: |
| 660 | self.logger.info(self.vapi.ppcli("show error")) |
| 661 | self.logger.info(self.vapi.ppcli("show ipsec")) |
| 662 | self.verify_counters(p, count) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 663 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 664 | |
| 665 | class IpsecTun6Tests(IpsecTun6): |
| 666 | |
| 667 | def test_tun_basic66(self): |
| 668 | """ ipsec 6o6 tunnel basic test """ |
| 669 | self.verify_tun_66(self.params[socket.AF_INET6], count=1) |
| 670 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 671 | def test_tun_burst66(self): |
| 672 | """ ipsec 6o6 tunnel burst test """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 673 | self.verify_tun_66(self.params[socket.AF_INET6], count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 674 | |
| 675 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 676 | class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 677 | pass |
| 678 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 679 | |
| 680 | if __name__ == '__main__': |
| 681 | unittest.main(testRunner=VppTestRunner) |