X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=test%2Ftest_ipsec_tun_if_esp.py;h=3e578fabaf9723ee248aa7586b033ef910d03846;hb=cbe25aab3be72154f2c706c39eeba6a77f34450f;hp=b61e0ef5a62a3a3d744fb990cf1326c59b7100a0;hpb=770a0deaadfb2d26cbc5da05f84578a7bd92e9d2;p=vpp.git diff --git a/test/test_ipsec_tun_if_esp.py b/test/test_ipsec_tun_if_esp.py index b61e0ef5a62..3e578fabaf9 100644 --- a/test/test_ipsec_tun_if_esp.py +++ b/test/test_ipsec_tun_if_esp.py @@ -2,13 +2,14 @@ import unittest import socket import copy -from scapy.layers.ipsec import ESP -from scapy.layers.l2 import Ether, Raw, GRE +from scapy.layers.ipsec import SecurityAssociation, ESP +from scapy.layers.l2 import Ether, GRE +from scapy.packet import Raw from scapy.layers.inet import IP, UDP from scapy.layers.inet6 import IPv6 from framework import VppTestRunner from template_ipsec import TemplateIpsec, IpsecTun4Tests, IpsecTun6Tests, \ - IpsecTun4, IpsecTun6, IpsecTcpTests, config_tun_params + IpsecTun4, IpsecTun6, IpsecTcpTests, mk_scapy_crypt_key from vpp_ipsec_tun_interface import VppIpsecTunInterface from vpp_gre_interface import VppGreInterface from vpp_ipip_tun_interface import VppIpIpTunInterface @@ -19,6 +20,33 @@ from util import ppp from vpp_papi import VppEnum +def config_tun_params(p, encryption_type, tun_if): + ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6} + esn_en = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t. + IPSEC_API_SAD_FLAG_USE_ESN)) + crypt_key = mk_scapy_crypt_key(p) + p.scapy_tun_sa = SecurityAssociation( + encryption_type, spi=p.vpp_tun_spi, + crypt_algo=p.crypt_algo, + crypt_key=crypt_key, + auth_algo=p.auth_algo, auth_key=p.auth_key, + tunnel_header=ip_class_by_addr_type[p.addr_type]( + src=tun_if.remote_ip, + dst=tun_if.local_ip), + nat_t_header=p.nat_header, + esn_en=esn_en) + p.vpp_tun_sa = SecurityAssociation( + encryption_type, spi=p.scapy_tun_spi, + crypt_algo=p.crypt_algo, + crypt_key=crypt_key, + auth_algo=p.auth_algo, auth_key=p.auth_key, + tunnel_header=ip_class_by_addr_type[p.addr_type]( + dst=tun_if.remote_ip, + src=tun_if.local_ip), + nat_t_header=p.nat_header, + esn_en=esn_en) + + class TemplateIpsec4TunIfEsp(TemplateIpsec): """ IPsec tunnel interface tests """ @@ -48,6 +76,7 @@ class TemplateIpsec4TunIfEsp(TemplateIpsec): p.tun_if.admin_up() p.tun_if.config_ip4() p.tun_if.config_ip6() + config_tun_params(p, self.encryption_type, p.tun_if) r = VppIpRoute(self, p.remote_tun_if_host, 32, [VppRoutePath(p.tun_if.remote_ip4, @@ -67,7 +96,7 @@ class TemplateIpsec4TunIfEspUdp(TemplateIpsec): """ IPsec UDP tunnel interface tests """ tun4_encrypt_node_name = "esp4-encrypt-tun" - tun4_decrypt_node_name = "esp4-decrypt" + tun4_decrypt_node_name = "esp4-decrypt-tun" encryption_type = ESP @classmethod @@ -97,6 +126,7 @@ class TemplateIpsec4TunIfEspUdp(TemplateIpsec): p.tun_if.admin_up() p.tun_if.config_ip4() p.tun_if.config_ip6() + config_tun_params(p, self.encryption_type, p.tun_if) r = VppIpRoute(self, p.remote_tun_if_host, 32, [VppRoutePath(p.tun_if.remote_ip4, @@ -115,17 +145,17 @@ class TemplateIpsec4TunIfEspUdp(TemplateIpsec): class TestIpsec4TunIfEsp1(TemplateIpsec4TunIfEsp, IpsecTun4Tests): """ Ipsec ESP - TUN tests """ tun4_encrypt_node_name = "esp4-encrypt-tun" - tun4_decrypt_node_name = "esp4-decrypt" + tun4_decrypt_node_name = "esp4-decrypt-tun" def test_tun_basic64(self): """ ipsec 6o4 tunnel basic test """ - self.tun4_encrypt_node_name = "esp6-encrypt-tun" + self.tun4_encrypt_node_name = "esp4-encrypt-tun" self.verify_tun_64(self.params[socket.AF_INET], count=1) def test_tun_burst64(self): """ ipsec 6o4 tunnel basic test """ - self.tun4_encrypt_node_name = "esp6-encrypt-tun" + self.tun4_encrypt_node_name = "esp4-encrypt-tun" self.verify_tun_64(self.params[socket.AF_INET], count=257) @@ -146,7 +176,7 @@ class TestIpsec4TunIfEsp1(TemplateIpsec4TunIfEsp, IpsecTun4Tests): class TestIpsec4TunIfEspUdp(TemplateIpsec4TunIfEspUdp, IpsecTun4Tests): """ Ipsec ESP UDP tests """ - tun4_input_node = "ipsec4-if-input" + tun4_input_node = "ipsec4-tun-input" def test_keepalive(self): """ IPSEC NAT Keepalive """ @@ -169,23 +199,24 @@ class TemplateIpsec6TunIfEsp(TemplateIpsec): self.tun_if = self.pg0 p = self.ipv6_params - tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi, - p.scapy_tun_spi, p.crypt_algo_vpp_id, - p.crypt_key, p.crypt_key, - p.auth_algo_vpp_id, p.auth_key, - p.auth_key, is_ip6=True) - tun_if.add_vpp_config() - tun_if.admin_up() - tun_if.config_ip6() - tun_if.config_ip4() + p.tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi, + p.scapy_tun_spi, p.crypt_algo_vpp_id, + p.crypt_key, p.crypt_key, + p.auth_algo_vpp_id, p.auth_key, + p.auth_key, is_ip6=True) + p.tun_if.add_vpp_config() + p.tun_if.admin_up() + p.tun_if.config_ip6() + p.tun_if.config_ip4() + config_tun_params(p, self.encryption_type, p.tun_if) r = VppIpRoute(self, p.remote_tun_if_host, 128, - [VppRoutePath(tun_if.remote_ip6, + [VppRoutePath(p.tun_if.remote_ip6, 0xffffffff, proto=DpoProto.DPO_PROTO_IP6)]) r.add_vpp_config() r = VppIpRoute(self, p.remote_tun_if_host4, 32, - [VppRoutePath(tun_if.remote_ip4, + [VppRoutePath(p.tun_if.remote_ip4, 0xffffffff)]) r.add_vpp_config() @@ -196,16 +227,16 @@ class TemplateIpsec6TunIfEsp(TemplateIpsec): class TestIpsec6TunIfEsp1(TemplateIpsec6TunIfEsp, IpsecTun6Tests): """ Ipsec ESP - TUN tests """ tun6_encrypt_node_name = "esp6-encrypt-tun" - tun6_decrypt_node_name = "esp6-decrypt" + tun6_decrypt_node_name = "esp6-decrypt-tun" def test_tun_basic46(self): """ ipsec 4o6 tunnel basic test """ - self.tun6_encrypt_node_name = "esp4-encrypt-tun" + self.tun6_encrypt_node_name = "esp6-encrypt-tun" self.verify_tun_46(self.params[socket.AF_INET6], count=1) def test_tun_burst46(self): """ ipsec 4o6 tunnel burst test """ - self.tun6_encrypt_node_name = "esp4-encrypt-tun" + self.tun6_encrypt_node_name = "esp6-encrypt-tun" self.verify_tun_46(self.params[socket.AF_INET6], count=257) @@ -214,7 +245,7 @@ class TestIpsec4MultiTunIfEsp(TemplateIpsec, IpsecTun4): encryption_type = ESP tun4_encrypt_node_name = "esp4-encrypt-tun" - tun4_decrypt_node_name = "esp4-decrypt" + tun4_decrypt_node_name = "esp4-decrypt-tun" def setUp(self): super(TestIpsec4MultiTunIfEsp, self).setUp() @@ -222,6 +253,8 @@ class TestIpsec4MultiTunIfEsp(TemplateIpsec, IpsecTun4): self.tun_if = self.pg0 self.multi_params = [] + self.pg0.generate_remote_hosts(10) + self.pg0.configure_ipv4_neighbors() for ii in range(10): p = copy.copy(self.ipv4_params) @@ -237,18 +270,18 @@ class TestIpsec4MultiTunIfEsp(TemplateIpsec, IpsecTun4): p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii p.vpp_tra_spi = p.vpp_tra_spi + ii - config_tun_params(p, self.encryption_type, self.tun_if) - self.multi_params.append(p) - p.tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi, p.scapy_tun_spi, p.crypt_algo_vpp_id, p.crypt_key, p.crypt_key, p.auth_algo_vpp_id, p.auth_key, - p.auth_key) + p.auth_key, + dst=self.pg0.remote_hosts[ii].ip4) p.tun_if.add_vpp_config() p.tun_if.admin_up() p.tun_if.config_ip4() + config_tun_params(p, self.encryption_type, p.tun_if) + self.multi_params.append(p) VppIpRoute(self, p.remote_tun_if_host, 32, [VppRoutePath(p.tun_if.remote_ip4, @@ -272,10 +305,9 @@ class TestIpsec4TunIfEspAll(TemplateIpsec, IpsecTun4): encryption_type = ESP tun4_encrypt_node_name = "esp4-encrypt-tun" - tun4_decrypt_node_name = "esp4-decrypt" + tun4_decrypt_node_name = "esp4-decrypt-tun" def config_network(self, p): - config_tun_params(p, self.encryption_type, self.tun_if) p.tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi, p.scapy_tun_spi, @@ -287,6 +319,7 @@ class TestIpsec4TunIfEspAll(TemplateIpsec, IpsecTun4): p.tun_if.add_vpp_config() p.tun_if.admin_up() p.tun_if.config_ip4() + config_tun_params(p, self.encryption_type, p.tun_if) self.logger.info(self.vapi.cli("sh ipsec sa 0")) self.logger.info(self.vapi.cli("sh ipsec sa 1")) @@ -320,7 +353,7 @@ class TestIpsec4TunIfEspAll(TemplateIpsec, IpsecTun4): p.tun_if.local_spi = p.vpp_tun_spi p.tun_if.remote_spi = p.scapy_tun_spi - config_tun_params(p, self.encryption_type, self.tun_if) + config_tun_params(p, self.encryption_type, p.tun_if) p.tun_sa_in = VppIpsecSA(self, p.scapy_tun_sa_id, @@ -330,8 +363,6 @@ class TestIpsec4TunIfEspAll(TemplateIpsec, IpsecTun4): p.crypt_algo_vpp_id, p.crypt_key, self.vpp_esp_protocol, - self.tun_if.local_addr[p.addr_type], - self.tun_if.remote_addr[p.addr_type], flags=p.flags, salt=p.salt) p.tun_sa_out = VppIpsecSA(self, @@ -342,8 +373,6 @@ class TestIpsec4TunIfEspAll(TemplateIpsec, IpsecTun4): p.crypt_algo_vpp_id, p.crypt_key, self.vpp_esp_protocol, - self.tun_if.remote_addr[p.addr_type], - self.tun_if.local_addr[p.addr_type], flags=p.flags, salt=p.salt) p.tun_sa_in.add_vpp_config() @@ -462,7 +491,7 @@ class TestIpsec6MultiTunIfEsp(TemplateIpsec, IpsecTun6): encryption_type = ESP tun6_encrypt_node_name = "esp6-encrypt-tun" - tun6_decrypt_node_name = "esp6-decrypt" + tun6_decrypt_node_name = "esp6-decrypt-tun" def setUp(self): super(TestIpsec6MultiTunIfEsp, self).setUp() @@ -470,6 +499,8 @@ class TestIpsec6MultiTunIfEsp(TemplateIpsec, IpsecTun6): self.tun_if = self.pg0 self.multi_params = [] + self.pg0.generate_remote_hosts(10) + self.pg0.configure_ipv6_neighbors() for ii in range(10): p = copy.copy(self.ipv6_params) @@ -485,18 +516,18 @@ class TestIpsec6MultiTunIfEsp(TemplateIpsec, IpsecTun6): p.vpp_tra_sa_id = p.vpp_tra_sa_id + ii p.vpp_tra_spi = p.vpp_tra_spi + ii - config_tun_params(p, self.encryption_type, self.tun_if) - self.multi_params.append(p) - p.tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi, p.scapy_tun_spi, p.crypt_algo_vpp_id, p.crypt_key, p.crypt_key, p.auth_algo_vpp_id, p.auth_key, - p.auth_key, is_ip6=True) + p.auth_key, is_ip6=True, + dst=self.pg0.remote_hosts[ii].ip6) p.tun_if.add_vpp_config() p.tun_if.admin_up() p.tun_if.config_ip6() + config_tun_params(p, self.encryption_type, p.tun_if) + self.multi_params.append(p) r = VppIpRoute(self, p.remote_tun_if_host, 128, [VppRoutePath(p.tun_if.remote_ip6, @@ -597,30 +628,32 @@ class TestIpsecGreTebIfEsp(TemplateIpsec, self.pg0.local_ip4) p.tun_sa_in.add_vpp_config() - self.tun = VppGreInterface(self, + p.tun_if = VppGreInterface(self, self.pg0.local_ip4, self.pg0.remote_ip4, type=(VppEnum.vl_api_gre_tunnel_type_t. GRE_API_TUNNEL_TYPE_TEB)) - self.tun.add_vpp_config() + p.tun_if.add_vpp_config() p.tun_protect = VppIpsecTunProtect(self, - self.tun, + p.tun_if, p.tun_sa_out, [p.tun_sa_in]) p.tun_protect.add_vpp_config() - self.tun.admin_up() - self.tun.config_ip4() + p.tun_if.admin_up() + p.tun_if.config_ip4() + config_tun_params(p, self.encryption_type, p.tun_if) - VppBridgeDomainPort(self, bd1, self.tun).add_vpp_config() + VppBridgeDomainPort(self, bd1, p.tun_if).add_vpp_config() VppBridgeDomainPort(self, bd1, self.pg1).add_vpp_config() self.vapi.cli("clear ipsec sa") def tearDown(self): - self.tun.unconfig_ip4() + p = self.ipv4_params + p.tun_if.unconfig_ip4() super(TestIpsecGreTebIfEsp, self).tearDown() @@ -702,29 +735,128 @@ class TestIpsecGreIfEsp(TemplateIpsec, self.pg0.local_ip4) p.tun_sa_in.add_vpp_config() - self.tun = VppGreInterface(self, + p.tun_if = VppGreInterface(self, self.pg0.local_ip4, self.pg0.remote_ip4) - self.tun.add_vpp_config() + p.tun_if.add_vpp_config() p.tun_protect = VppIpsecTunProtect(self, - self.tun, + p.tun_if, p.tun_sa_out, [p.tun_sa_in]) p.tun_protect.add_vpp_config() - self.tun.admin_up() - self.tun.config_ip4() + p.tun_if.admin_up() + p.tun_if.config_ip4() + config_tun_params(p, self.encryption_type, p.tun_if) VppIpRoute(self, "1.1.1.2", 32, - [VppRoutePath(self.tun.remote_ip4, + [VppRoutePath(p.tun_if.remote_ip4, 0xffffffff)]).add_vpp_config() def tearDown(self): - self.tun.unconfig_ip4() + p = self.ipv4_params + p.tun_if.unconfig_ip4() super(TestIpsecGreIfEsp, self).tearDown() +class TestIpsecGreIfEspTra(TemplateIpsec, + IpsecTun4Tests): + """ Ipsec GRE ESP - TRA tests """ + tun4_encrypt_node_name = "esp4-encrypt-tun" + tun4_decrypt_node_name = "esp4-decrypt-tun" + encryption_type = ESP + + def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1, + payload_size=100): + return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / + sa.encrypt(IP(src=self.pg0.remote_ip4, + dst=self.pg0.local_ip4) / + GRE() / + IP(src=self.pg1.local_ip4, + dst=self.pg1.remote_ip4) / + UDP(sport=1144, dport=2233) / + Raw(b'X' * payload_size)) + for i in range(count)] + + def gen_pkts(self, sw_intf, src, dst, count=1, + payload_size=100): + return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / + IP(src="1.1.1.1", dst="1.1.1.2") / + UDP(sport=1144, dport=2233) / + Raw(b'X' * payload_size) + for i in range(count)] + + def verify_decrypted(self, p, rxs): + for rx in rxs: + self.assert_equal(rx[Ether].dst, self.pg1.remote_mac) + self.assert_equal(rx[IP].dst, self.pg1.remote_ip4) + + def verify_encrypted(self, p, sa, rxs): + for rx in rxs: + try: + pkt = sa.decrypt(rx[IP]) + if not pkt.haslayer(IP): + pkt = IP(pkt[Raw].load) + self.assert_packet_checksums_valid(pkt) + self.assertTrue(pkt.haslayer(GRE)) + e = pkt[GRE] + self.assertEqual(e[IP].dst, "1.1.1.2") + except (IndexError, AssertionError): + self.logger.debug(ppp("Unexpected packet:", rx)) + try: + self.logger.debug(ppp("Decrypted packet:", pkt)) + except: + pass + raise + + def setUp(self): + super(TestIpsecGreIfEspTra, self).setUp() + + self.tun_if = self.pg0 + + p = self.ipv4_params + + bd1 = VppBridgeDomain(self, 1) + bd1.add_vpp_config() + + p.tun_sa_out = VppIpsecSA(self, p.scapy_tun_sa_id, p.scapy_tun_spi, + p.auth_algo_vpp_id, p.auth_key, + p.crypt_algo_vpp_id, p.crypt_key, + self.vpp_esp_protocol) + p.tun_sa_out.add_vpp_config() + + p.tun_sa_in = VppIpsecSA(self, p.vpp_tun_sa_id, p.vpp_tun_spi, + p.auth_algo_vpp_id, p.auth_key, + p.crypt_algo_vpp_id, p.crypt_key, + self.vpp_esp_protocol) + p.tun_sa_in.add_vpp_config() + + p.tun_if = VppGreInterface(self, + self.pg0.local_ip4, + self.pg0.remote_ip4) + p.tun_if.add_vpp_config() + + p.tun_protect = VppIpsecTunProtect(self, + p.tun_if, + p.tun_sa_out, + [p.tun_sa_in]) + p.tun_protect.add_vpp_config() + + p.tun_if.admin_up() + p.tun_if.config_ip4() + config_tun_params(p, self.encryption_type, p.tun_if) + + VppIpRoute(self, "1.1.1.2", 32, + [VppRoutePath(p.tun_if.remote_ip4, + 0xffffffff)]).add_vpp_config() + + def tearDown(self): + p = self.ipv4_params + p.tun_if.unconfig_ip4() + super(TestIpsecGreIfEspTra, self).tearDown() + + class TemplateIpsec4TunProtect(object): """ IPsec IPv4 Tunnel protect """ @@ -734,7 +866,7 @@ class TemplateIpsec4TunProtect(object): tun4_input_node = "ipsec4-tun-input" def config_sa_tra(self, p): - config_tun_params(p, self.encryption_type, self.tun_if) + config_tun_params(p, self.encryption_type, p.tun_if) p.tun_sa_out = VppIpsecSA(self, p.scapy_tun_sa_id, p.scapy_tun_spi, p.auth_algo_vpp_id, p.auth_key, @@ -751,7 +883,7 @@ class TemplateIpsec4TunProtect(object): p.tun_sa_in.add_vpp_config() def config_sa_tun(self, p): - config_tun_params(p, self.encryption_type, self.tun_if) + config_tun_params(p, self.encryption_type, p.tun_if) p.tun_sa_out = VppIpsecSA(self, p.scapy_tun_sa_id, p.scapy_tun_spi, p.auth_algo_vpp_id, p.auth_key, @@ -1018,7 +1150,7 @@ class TemplateIpsec6TunProtect(object): """ IPsec IPv6 Tunnel protect """ def config_sa_tra(self, p): - config_tun_params(p, self.encryption_type, self.tun_if) + config_tun_params(p, self.encryption_type, p.tun_if) p.tun_sa_out = VppIpsecSA(self, p.scapy_tun_sa_id, p.scapy_tun_spi, p.auth_algo_vpp_id, p.auth_key, @@ -1033,7 +1165,7 @@ class TemplateIpsec6TunProtect(object): p.tun_sa_in.add_vpp_config() def config_sa_tun(self, p): - config_tun_params(p, self.encryption_type, self.tun_if) + config_tun_params(p, self.encryption_type, p.tun_if) p.tun_sa_out = VppIpsecSA(self, p.scapy_tun_sa_id, p.scapy_tun_spi, p.auth_algo_vpp_id, p.auth_key,