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 |
| 10 | from util import ppp |
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' |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 24 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 25 | self.scapy_tun_sa_id = 10 |
| 26 | self.scapy_tun_spi = 1001 |
| 27 | self.vpp_tun_sa_id = 20 |
| 28 | self.vpp_tun_spi = 1000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 29 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 30 | self.scapy_tra_sa_id = 30 |
| 31 | self.scapy_tra_spi = 2001 |
| 32 | self.vpp_tra_sa_id = 40 |
| 33 | self.vpp_tra_spi = 2000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 34 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 35 | self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t. |
| 36 | IPSEC_API_INTEG_ALG_SHA1_96) |
| 37 | self.auth_algo = 'HMAC-SHA1-96' # scapy name |
| 38 | self.auth_key = 'C91KUR9GYMm5GfkEvNjX' |
| 39 | |
| 40 | self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t. |
| 41 | IPSEC_API_CRYPTO_ALG_AES_CBC_128) |
| 42 | self.crypt_algo = 'AES-CBC' # scapy name |
| 43 | self.crypt_key = 'JPjyOWBeVEQiMe7h' |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 44 | self.flags = 0 |
| 45 | self.nat_header = None |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 46 | |
| 47 | |
| 48 | class IPsecIPv6Params(object): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 49 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 50 | addr_type = socket.AF_INET6 |
| 51 | addr_any = "0::0" |
| 52 | addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" |
| 53 | addr_len = 128 |
| 54 | is_ipv6 = 1 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 55 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 56 | def __init__(self): |
| 57 | self.remote_tun_if_host = '1111:1111:1111:1111:1111:1111:1111:1111' |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 58 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 59 | self.scapy_tun_sa_id = 50 |
| 60 | self.scapy_tun_spi = 3001 |
| 61 | self.vpp_tun_sa_id = 60 |
| 62 | self.vpp_tun_spi = 3000 |
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_tra_sa_id = 70 |
| 65 | self.scapy_tra_spi = 4001 |
| 66 | self.vpp_tra_sa_id = 80 |
| 67 | self.vpp_tra_spi = 4000 |
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.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t. |
| 70 | IPSEC_API_INTEG_ALG_SHA_256_128) |
| 71 | self.auth_algo = 'SHA2-256-128' # scapy name |
| 72 | self.auth_key = 'C91KUR9GYMm5GfkEvNjX' |
| 73 | |
| 74 | self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t. |
| 75 | IPSEC_API_CRYPTO_ALG_AES_CBC_256) |
| 76 | self.crypt_algo = 'AES-CBC' # scapy name |
| 77 | self.crypt_key = 'JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h' |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 78 | self.flags = 0 |
| 79 | self.nat_header = None |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 80 | |
| 81 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 82 | def config_tun_params(p, encryption_type, tun_if): |
| 83 | ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6} |
| 84 | p.scapy_tun_sa = SecurityAssociation( |
| 85 | encryption_type, spi=p.vpp_tun_spi, |
| 86 | crypt_algo=p.crypt_algo, crypt_key=p.crypt_key, |
| 87 | auth_algo=p.auth_algo, auth_key=p.auth_key, |
| 88 | tunnel_header=ip_class_by_addr_type[p.addr_type]( |
| 89 | src=tun_if.remote_addr[p.addr_type], |
| 90 | dst=tun_if.local_addr[p.addr_type]), |
| 91 | nat_t_header=p.nat_header) |
| 92 | p.vpp_tun_sa = SecurityAssociation( |
| 93 | encryption_type, spi=p.scapy_tun_spi, |
| 94 | crypt_algo=p.crypt_algo, crypt_key=p.crypt_key, |
| 95 | auth_algo=p.auth_algo, auth_key=p.auth_key, |
| 96 | tunnel_header=ip_class_by_addr_type[p.addr_type]( |
| 97 | dst=tun_if.remote_addr[p.addr_type], |
| 98 | src=tun_if.local_addr[p.addr_type]), |
| 99 | nat_t_header=p.nat_header) |
| 100 | |
| 101 | |
| 102 | def config_tra_params(p, encryption_type): |
| 103 | p.scapy_tra_sa = SecurityAssociation( |
| 104 | encryption_type, |
| 105 | spi=p.vpp_tra_spi, |
| 106 | crypt_algo=p.crypt_algo, |
| 107 | crypt_key=p.crypt_key, |
| 108 | auth_algo=p.auth_algo, |
| 109 | auth_key=p.auth_key, |
| 110 | nat_t_header=p.nat_header) |
| 111 | p.vpp_tra_sa = SecurityAssociation( |
| 112 | encryption_type, |
| 113 | spi=p.scapy_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, |
| 118 | nat_t_header=p.nat_header) |
| 119 | |
| 120 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 121 | class TemplateIpsec(VppTestCase): |
| 122 | """ |
| 123 | TRANSPORT MODE: |
| 124 | |
| 125 | ------ encrypt --- |
| 126 | |tra_if| <-------> |VPP| |
| 127 | ------ decrypt --- |
| 128 | |
| 129 | TUNNEL MODE: |
| 130 | |
| 131 | ------ encrypt --- plain --- |
| 132 | |tun_if| <------- |VPP| <------ |pg1| |
| 133 | ------ --- --- |
| 134 | |
| 135 | ------ decrypt --- plain --- |
| 136 | |tun_if| -------> |VPP| ------> |pg1| |
| 137 | ------ --- --- |
| 138 | """ |
| 139 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 140 | def ipsec_select_backend(self): |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 141 | """ empty method to be overloaded when necessary """ |
| 142 | pass |
| 143 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 144 | def setUp(self): |
| 145 | super(TemplateIpsec, self).setUp() |
| 146 | |
| 147 | self.ipv4_params = IPsecIPv4Params() |
| 148 | self.ipv6_params = IPsecIPv6Params() |
| 149 | self.params = {self.ipv4_params.addr_type: self.ipv4_params, |
| 150 | self.ipv6_params.addr_type: self.ipv6_params} |
| 151 | |
| 152 | self.payload = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"\ |
| 153 | "XXXXXXXXXXXXXXXXXXXXX" |
| 154 | |
| 155 | self.tun_spd_id = 1 |
| 156 | self.tra_spd_id = 2 |
| 157 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 158 | self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t. |
| 159 | IPSEC_API_PROTO_ESP) |
| 160 | self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t. |
| 161 | IPSEC_API_PROTO_AH) |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 162 | |
| 163 | self.create_pg_interfaces(range(3)) |
| 164 | self.interfaces = list(self.pg_interfaces) |
| 165 | for i in self.interfaces: |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 166 | i.admin_up() |
| 167 | i.config_ip4() |
| 168 | i.resolve_arp() |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 169 | i.config_ip6() |
| 170 | i.resolve_ndp() |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 171 | self.ipsec_select_backend() |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 172 | |
| 173 | def tearDown(self): |
| 174 | super(TemplateIpsec, self).tearDown() |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 175 | |
| 176 | for i in self.interfaces: |
| 177 | i.admin_down() |
| 178 | i.unconfig_ip4() |
| 179 | i.unconfig_ip6() |
| 180 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 181 | if not self.vpp_dead: |
| 182 | self.vapi.cli("show hardware") |
| 183 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 184 | def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1): |
| 185 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 186 | sa.encrypt(IP(src=src, dst=dst) / ICMP() / self.payload) |
| 187 | for i in range(count)] |
| 188 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 189 | def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1): |
| 190 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 191 | sa.encrypt(IPv6(src=src, dst=dst) / |
| 192 | ICMPv6EchoRequest(id=0, seq=1, data=self.payload)) |
| 193 | for i in range(count)] |
| 194 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 195 | def gen_pkts(self, sw_intf, src, dst, count=1): |
| 196 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 197 | IP(src=src, dst=dst) / ICMP() / self.payload |
| 198 | for i in range(count)] |
| 199 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 200 | def gen_pkts6(self, sw_intf, src, dst, count=1): |
| 201 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 202 | IPv6(src=src, dst=dst) / |
| 203 | ICMPv6EchoRequest(id=0, seq=1, data=self.payload) |
| 204 | for i in range(count)] |
| 205 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 206 | def configure_sa_tra(self, params): |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 207 | params.scapy_tra_sa = SecurityAssociation( |
| 208 | self.encryption_type, |
| 209 | spi=params.vpp_tra_spi, |
| 210 | crypt_algo=params.crypt_algo, |
| 211 | crypt_key=params.crypt_key, |
| 212 | auth_algo=params.auth_algo, |
| 213 | auth_key=params.auth_key, |
| 214 | nat_t_header=params.nat_header) |
| 215 | params.vpp_tra_sa = SecurityAssociation( |
| 216 | self.encryption_type, |
| 217 | spi=params.scapy_tra_spi, |
| 218 | crypt_algo=params.crypt_algo, |
| 219 | crypt_key=params.crypt_key, |
| 220 | auth_algo=params.auth_algo, |
| 221 | auth_key=params.auth_key, |
| 222 | nat_t_header=params.nat_header) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 223 | |
| 224 | |
| 225 | class IpsecTcpTests(object): |
| 226 | def test_tcp_checksum(self): |
| 227 | """ verify checksum correctness for vpp generated packets """ |
| 228 | self.vapi.cli("test http server") |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 229 | p = self.params[socket.AF_INET] |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 230 | config_tun_params(p, self.encryption_type, self.tun_if) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 231 | 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] | 232 | p.scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host, |
| 233 | dst=self.tun_if.local_ip4) / |
| 234 | TCP(flags='S', dport=80))) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 235 | self.logger.debug(ppp("Sending packet:", send)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 236 | recv = self.send_and_expect(self.tun_if, [send], self.tun_if) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 237 | recv = recv[0] |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 238 | decrypted = p.vpp_tun_sa.decrypt(recv[IP]) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 239 | self.assert_packet_checksums_valid(decrypted) |
| 240 | |
| 241 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 242 | class IpsecTra4Tests(object): |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 243 | def test_tra_anti_replay(self, count=1): |
| 244 | """ ipsec v4 transport anti-reply test """ |
| 245 | p = self.params[socket.AF_INET] |
| 246 | |
| 247 | # fire in a packet with seq number 1 |
| 248 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 249 | dst=self.tra_if.local_mac) / |
| 250 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 251 | dst=self.tra_if.local_ip4) / |
| 252 | ICMP(), |
| 253 | seq_num=1)) |
| 254 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 255 | |
| 256 | # now move the window over to 235 |
| 257 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 258 | dst=self.tra_if.local_mac) / |
| 259 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 260 | dst=self.tra_if.local_ip4) / |
| 261 | ICMP(), |
| 262 | seq_num=235)) |
| 263 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 264 | |
| 265 | # the window size is 64 packets |
| 266 | # in window are still accepted |
| 267 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 268 | dst=self.tra_if.local_mac) / |
| 269 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 270 | dst=self.tra_if.local_ip4) / |
| 271 | ICMP(), |
| 272 | seq_num=172)) |
| 273 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 274 | |
| 275 | # out of window are dropped |
| 276 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 277 | dst=self.tra_if.local_mac) / |
| 278 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 279 | dst=self.tra_if.local_ip4) / |
| 280 | ICMP(), |
| 281 | seq_num=17)) |
| 282 | self.send_and_assert_no_replies(self.tra_if, pkt * 17) |
| 283 | |
Klement Sekera | 14d7e90 | 2018-12-10 13:46:09 +0100 | [diff] [blame] | 284 | self.assert_packet_counter_equal( |
| 285 | '/err/%s/SA replayed packet' % self.tra4_decrypt_node_name, 17) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 286 | |
| 287 | # a packet that does not decrypt does not move the window forward |
| 288 | bogus_sa = SecurityAssociation(self.encryption_type, |
| 289 | p.vpp_tra_spi) |
| 290 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 291 | dst=self.tra_if.local_mac) / |
| 292 | bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 293 | dst=self.tra_if.local_ip4) / |
| 294 | ICMP(), |
| 295 | seq_num=350)) |
| 296 | self.send_and_assert_no_replies(self.tra_if, pkt * 17) |
| 297 | |
Klement Sekera | 14d7e90 | 2018-12-10 13:46:09 +0100 | [diff] [blame] | 298 | self.assert_packet_counter_equal( |
| 299 | '/err/%s/Integrity check failed' % self.tra4_decrypt_node_name, 17) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 300 | |
| 301 | # which we can determine since this packet is still in the window |
| 302 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 303 | dst=self.tra_if.local_mac) / |
| 304 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 305 | dst=self.tra_if.local_ip4) / |
| 306 | ICMP(), |
| 307 | seq_num=234)) |
Klement Sekera | 14d7e90 | 2018-12-10 13:46:09 +0100 | [diff] [blame] | 308 | self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 309 | |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 310 | # move VPP's SA to just before the seq-number wrap |
| 311 | self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.scapy_tra_sa_id) |
| 312 | |
| 313 | # then fire in a packet that VPP should drop becuase it causes the |
| 314 | # seq number to wrap |
| 315 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 316 | dst=self.tra_if.local_mac) / |
| 317 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 318 | dst=self.tra_if.local_ip4) / |
| 319 | ICMP(), |
| 320 | seq_num=236)) |
| 321 | self.send_and_assert_no_replies(self.tra_if, [pkt]) |
| 322 | self.assert_packet_counter_equal( |
| 323 | '/err/%s/sequence number cycled' % self.tra4_encrypt_node_name, 1) |
| 324 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 325 | # move the security-associations seq number on to the last we used |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 326 | 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] | 327 | p.scapy_tra_sa.seq_num = 351 |
| 328 | p.vpp_tra_sa.seq_num = 351 |
| 329 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 330 | def test_tra_basic(self, count=1): |
| 331 | """ ipsec v4 transport basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 332 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 333 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 334 | p = self.params[socket.AF_INET] |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 335 | 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] | 336 | src=self.tra_if.remote_ip4, |
| 337 | dst=self.tra_if.local_ip4, |
| 338 | count=count) |
| 339 | recv_pkts = self.send_and_expect(self.tra_if, send_pkts, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 340 | self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 341 | for rx in recv_pkts: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 342 | try: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 343 | decrypted = p.vpp_tra_sa.decrypt(rx[IP]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 344 | self.assert_packet_checksums_valid(decrypted) |
| 345 | except: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 346 | self.logger.debug(ppp("Unexpected packet:", rx)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 347 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 348 | finally: |
| 349 | self.logger.info(self.vapi.ppcli("show error")) |
| 350 | self.logger.info(self.vapi.ppcli("show ipsec")) |
| 351 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 352 | pkts = p.tra_sa_in.get_stats()['packets'] |
| 353 | self.assertEqual(pkts, count, |
| 354 | "incorrect SA in counts: expected %d != %d" % |
| 355 | (count, pkts)) |
| 356 | pkts = p.tra_sa_out.get_stats()['packets'] |
| 357 | self.assertEqual(pkts, count, |
| 358 | "incorrect SA out counts: expected %d != %d" % |
| 359 | (count, pkts)) |
| 360 | |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 361 | self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count) |
| 362 | self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count) |
| 363 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 364 | def test_tra_burst(self): |
| 365 | """ ipsec v4 transport burst test """ |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 366 | self.test_tra_basic(count=257) |
| 367 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 368 | |
| 369 | class IpsecTra6Tests(object): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 370 | def test_tra_basic6(self, count=1): |
| 371 | """ ipsec v6 transport basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 372 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 373 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 374 | p = self.params[socket.AF_INET6] |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 375 | 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] | 376 | src=self.tra_if.remote_ip6, |
| 377 | dst=self.tra_if.local_ip6, |
| 378 | count=count) |
| 379 | recv_pkts = self.send_and_expect(self.tra_if, send_pkts, |
| 380 | self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 381 | for rx in recv_pkts: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 382 | try: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 383 | decrypted = p.vpp_tra_sa.decrypt(rx[IPv6]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 384 | self.assert_packet_checksums_valid(decrypted) |
| 385 | except: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 386 | self.logger.debug(ppp("Unexpected packet:", rx)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 387 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 388 | finally: |
| 389 | self.logger.info(self.vapi.ppcli("show error")) |
| 390 | self.logger.info(self.vapi.ppcli("show ipsec")) |
| 391 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 392 | pkts = p.tra_sa_in.get_stats()['packets'] |
| 393 | self.assertEqual(pkts, count, |
| 394 | "incorrect SA in counts: expected %d != %d" % |
| 395 | (count, pkts)) |
| 396 | pkts = p.tra_sa_out.get_stats()['packets'] |
| 397 | self.assertEqual(pkts, count, |
| 398 | "incorrect SA out counts: expected %d != %d" % |
| 399 | (count, pkts)) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 400 | self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count) |
| 401 | self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count) |
| 402 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 403 | def test_tra_burst6(self): |
| 404 | """ ipsec v6 transport burst test """ |
| 405 | self.test_tra_basic6(count=257) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 406 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 407 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 408 | class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests): |
| 409 | pass |
| 410 | |
| 411 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 412 | class IpsecTun4(object): |
| 413 | |
| 414 | def verify_tun_44(self, p, count=1): |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 415 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 416 | try: |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 417 | config_tun_params(p, self.encryption_type, self.tun_if) |
| 418 | 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] | 419 | src=p.remote_tun_if_host, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 420 | dst=self.pg1.remote_ip4, |
| 421 | count=count) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 422 | 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] | 423 | for recv_pkt in recv_pkts: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 424 | self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 425 | self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4) |
| 426 | self.assert_packet_checksums_valid(recv_pkt) |
| 427 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 428 | dst=p.remote_tun_if_host, count=count) |
| 429 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 430 | for recv_pkt in recv_pkts: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 431 | try: |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 432 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 433 | if not decrypt_pkt.haslayer(IP): |
| 434 | decrypt_pkt = IP(decrypt_pkt[Raw].load) |
| 435 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4) |
| 436 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
| 437 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 438 | except: |
| 439 | self.logger.debug(ppp("Unexpected packet:", recv_pkt)) |
| 440 | try: |
| 441 | self.logger.debug( |
| 442 | ppp("Decrypted packet:", decrypt_pkt)) |
| 443 | except: |
| 444 | pass |
| 445 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 446 | finally: |
| 447 | self.logger.info(self.vapi.ppcli("show error")) |
| 448 | self.logger.info(self.vapi.ppcli("show ipsec")) |
| 449 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 450 | if (hasattr(p, "spd_policy_in_any")): |
| 451 | pkts = p.spd_policy_in_any.get_stats()['packets'] |
| 452 | self.assertEqual(pkts, count, |
| 453 | "incorrect SPD any policy: expected %d != %d" % |
| 454 | (count, pkts)) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 455 | |
| 456 | if (hasattr(p, "tun_sa_in")): |
| 457 | pkts = p.tun_sa_in.get_stats()['packets'] |
| 458 | self.assertEqual(pkts, count, |
| 459 | "incorrect SA in counts: expected %d != %d" % |
| 460 | (count, pkts)) |
| 461 | pkts = p.tun_sa_out.get_stats()['packets'] |
| 462 | self.assertEqual(pkts, count, |
| 463 | "incorrect SA out counts: expected %d != %d" % |
| 464 | (count, pkts)) |
| 465 | |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 466 | self.assert_packet_counter_equal(self.tun4_encrypt_node_name, count) |
| 467 | self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count) |
| 468 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 469 | |
| 470 | class IpsecTun4Tests(IpsecTun4): |
| 471 | |
| 472 | def test_tun_basic44(self): |
| 473 | """ ipsec 4o4 tunnel basic test """ |
| 474 | self.verify_tun_44(self.params[socket.AF_INET], count=1) |
| 475 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 476 | def test_tun_burst44(self): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 477 | """ ipsec 4o4 tunnel burst test """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 478 | self.verify_tun_44(self.params[socket.AF_INET], count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 479 | |
| 480 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 481 | class IpsecTun6(object): |
| 482 | |
| 483 | def verify_tun_66(self, p, count=1): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 484 | """ ipsec 6o6 tunnel basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 485 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 486 | try: |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 487 | config_tun_params(p, self.encryption_type, self.tun_if) |
| 488 | 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] | 489 | src=p.remote_tun_if_host, |
| 490 | dst=self.pg1.remote_ip6, |
| 491 | count=count) |
| 492 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 493 | for recv_pkt in recv_pkts: |
| 494 | self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host) |
| 495 | self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6) |
| 496 | self.assert_packet_checksums_valid(recv_pkt) |
| 497 | send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6, |
| 498 | dst=p.remote_tun_if_host, |
| 499 | count=count) |
| 500 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 501 | for recv_pkt in recv_pkts: |
| 502 | try: |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 503 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 504 | if not decrypt_pkt.haslayer(IPv6): |
| 505 | decrypt_pkt = IPv6(decrypt_pkt[Raw].load) |
| 506 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6) |
| 507 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
| 508 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 509 | except: |
| 510 | self.logger.debug(ppp("Unexpected packet:", recv_pkt)) |
| 511 | try: |
| 512 | self.logger.debug( |
| 513 | ppp("Decrypted packet:", decrypt_pkt)) |
| 514 | except: |
| 515 | pass |
| 516 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 517 | finally: |
| 518 | self.logger.info(self.vapi.ppcli("show error")) |
| 519 | self.logger.info(self.vapi.ppcli("show ipsec")) |
| 520 | |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 521 | if (hasattr(p, "tun_sa_in")): |
| 522 | pkts = p.tun_sa_in.get_stats()['packets'] |
| 523 | self.assertEqual(pkts, count, |
| 524 | "incorrect SA in counts: expected %d != %d" % |
| 525 | (count, pkts)) |
| 526 | pkts = p.tun_sa_out.get_stats()['packets'] |
| 527 | self.assertEqual(pkts, count, |
| 528 | "incorrect SA out counts: expected %d != %d" % |
| 529 | (count, pkts)) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 530 | self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count) |
| 531 | self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count) |
| 532 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 533 | |
| 534 | class IpsecTun6Tests(IpsecTun6): |
| 535 | |
| 536 | def test_tun_basic66(self): |
| 537 | """ ipsec 6o6 tunnel basic test """ |
| 538 | self.verify_tun_66(self.params[socket.AF_INET6], count=1) |
| 539 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 540 | def test_tun_burst66(self): |
| 541 | """ ipsec 6o6 tunnel burst test """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 542 | self.verify_tun_66(self.params[socket.AF_INET6], count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 543 | |
| 544 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 545 | class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 546 | pass |
| 547 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 548 | |
| 549 | if __name__ == '__main__': |
| 550 | unittest.main(testRunner=VppTestRunner) |