Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import socket |
| 4 | |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 5 | import scapy.compat |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 6 | from scapy.layers.l2 import Ether |
| 7 | from scapy.layers.inet import ICMP, IP, TCP, UDP |
| 8 | from scapy.layers.ipsec import SecurityAssociation, ESP |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 9 | |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 10 | from util import ppp, ppc |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 11 | from template_ipsec import TemplateIpsec |
Klement Sekera | bf61395 | 2019-01-29 11:38:08 +0100 | [diff] [blame] | 12 | from vpp_ipsec import VppIpsecSA, VppIpsecSpd, VppIpsecSpdEntry,\ |
| 13 | VppIpsecSpdItfBinding |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 14 | from vpp_ip_route import VppIpRoute, VppRoutePath |
| 15 | from vpp_ip import DpoProto |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 16 | from vpp_papi import VppEnum |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 17 | |
| 18 | |
Florin Coras | 4a7cbcd | 2019-01-02 17:43:01 +0000 | [diff] [blame] | 19 | class IPSecNATTestCase(TemplateIpsec): |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 20 | """ IPSec/NAT |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 21 | TUNNEL MODE: |
| 22 | |
| 23 | |
| 24 | public network | private network |
| 25 | --- encrypt --- plain --- |
| 26 | |pg0| <------- |VPP| <------ |pg1| |
| 27 | --- --- --- |
| 28 | |
| 29 | --- decrypt --- plain --- |
| 30 | |pg0| -------> |VPP| ------> |pg1| |
| 31 | --- --- --- |
| 32 | """ |
| 33 | |
Klement Sekera | 6452622 | 2018-06-15 12:44:16 +0200 | [diff] [blame] | 34 | tcp_port_in = 6303 |
| 35 | tcp_port_out = 6303 |
| 36 | udp_port_in = 6304 |
| 37 | udp_port_out = 6304 |
| 38 | icmp_id_in = 6305 |
| 39 | icmp_id_out = 6305 |
| 40 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 41 | def setUp(self): |
| 42 | super(IPSecNATTestCase, self).setUp() |
| 43 | self.tun_if = self.pg0 |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 44 | |
| 45 | self.tun_spd = VppIpsecSpd(self, self.tun_spd_id) |
| 46 | self.tun_spd.add_vpp_config() |
| 47 | VppIpsecSpdItfBinding(self, self.tun_spd, |
| 48 | self.tun_if).add_vpp_config() |
| 49 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 50 | p = self.ipv4_params |
| 51 | self.config_esp_tun(p) |
| 52 | self.logger.info(self.vapi.ppcli("show ipsec")) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 53 | |
| 54 | d = DpoProto.DPO_PROTO_IP6 if p.is_ipv6 else DpoProto.DPO_PROTO_IP4 |
| 55 | VppIpRoute(self, p.remote_tun_if_host, p.addr_len, |
| 56 | [VppRoutePath(self.tun_if.remote_addr[p.addr_type], |
| 57 | 0xffffffff, |
| 58 | proto=d)], |
| 59 | is_ip6=p.is_ipv6).add_vpp_config() |
| 60 | |
| 61 | def tearDown(self): |
| 62 | super(IPSecNATTestCase, self).tearDown() |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 63 | |
| 64 | def create_stream_plain(self, src_mac, dst_mac, src_ip, dst_ip): |
| 65 | return [ |
| 66 | # TCP |
| 67 | Ether(src=src_mac, dst=dst_mac) / |
| 68 | IP(src=src_ip, dst=dst_ip) / |
| 69 | TCP(sport=self.tcp_port_in, dport=20), |
| 70 | # UDP |
| 71 | Ether(src=src_mac, dst=dst_mac) / |
| 72 | IP(src=src_ip, dst=dst_ip) / |
| 73 | UDP(sport=self.udp_port_in, dport=20), |
| 74 | # ICMP |
| 75 | Ether(src=src_mac, dst=dst_mac) / |
| 76 | IP(src=src_ip, dst=dst_ip) / |
| 77 | ICMP(id=self.icmp_id_in, type='echo-request') |
| 78 | ] |
| 79 | |
| 80 | def create_stream_encrypted(self, src_mac, dst_mac, src_ip, dst_ip, sa): |
| 81 | return [ |
| 82 | # TCP |
| 83 | Ether(src=src_mac, dst=dst_mac) / |
| 84 | sa.encrypt(IP(src=src_ip, dst=dst_ip) / |
| 85 | TCP(dport=self.tcp_port_out, sport=20)), |
| 86 | # UDP |
| 87 | Ether(src=src_mac, dst=dst_mac) / |
| 88 | sa.encrypt(IP(src=src_ip, dst=dst_ip) / |
| 89 | UDP(dport=self.udp_port_out, sport=20)), |
| 90 | # ICMP |
| 91 | Ether(src=src_mac, dst=dst_mac) / |
| 92 | sa.encrypt(IP(src=src_ip, dst=dst_ip) / |
| 93 | ICMP(id=self.icmp_id_out, type='echo-request')) |
| 94 | ] |
| 95 | |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 96 | def verify_capture_plain(self, capture): |
| 97 | for packet in capture: |
| 98 | try: |
Klement Sekera | d81ae41 | 2018-05-16 10:52:54 +0200 | [diff] [blame] | 99 | self.assert_packet_checksums_valid(packet) |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 100 | self.assert_equal(packet[IP].src, self.tun_if.remote_ip4, |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 101 | "decrypted packet source address") |
| 102 | self.assert_equal(packet[IP].dst, self.pg1.remote_ip4, |
| 103 | "decrypted packet destination address") |
| 104 | if packet.haslayer(TCP): |
| 105 | self.assertFalse( |
| 106 | packet.haslayer(UDP), |
| 107 | "unexpected UDP header in decrypted packet") |
| 108 | self.assert_equal(packet[TCP].dport, self.tcp_port_in, |
| 109 | "decrypted packet TCP destination port") |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 110 | elif packet.haslayer(UDP): |
| 111 | if packet[UDP].payload: |
| 112 | self.assertFalse( |
| 113 | packet[UDP][1].haslayer(UDP), |
| 114 | "unexpected UDP header in decrypted packet") |
| 115 | self.assert_equal(packet[UDP].dport, self.udp_port_in, |
| 116 | "decrypted packet UDP destination port") |
| 117 | else: |
| 118 | self.assertFalse( |
| 119 | packet.haslayer(UDP), |
| 120 | "unexpected UDP header in decrypted packet") |
| 121 | self.assert_equal(packet[ICMP].id, self.icmp_id_in, |
| 122 | "decrypted packet ICMP ID") |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 123 | except Exception: |
| 124 | self.logger.error( |
| 125 | ppp("Unexpected or invalid plain packet:", packet)) |
| 126 | raise |
| 127 | |
| 128 | def verify_capture_encrypted(self, capture, sa): |
| 129 | for packet in capture: |
| 130 | try: |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 131 | copy = packet.__class__(scapy.compat.raw(packet)) |
Klement Sekera | 6452622 | 2018-06-15 12:44:16 +0200 | [diff] [blame] | 132 | del copy[UDP].len |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 133 | copy = packet.__class__(scapy.compat.raw(copy)) |
Klement Sekera | 6452622 | 2018-06-15 12:44:16 +0200 | [diff] [blame] | 134 | self.assert_equal(packet[UDP].len, copy[UDP].len, |
| 135 | "UDP header length") |
| 136 | self.assert_packet_checksums_valid(packet) |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 137 | self.assertIn(ESP, packet[IP]) |
| 138 | decrypt_pkt = sa.decrypt(packet[IP]) |
Klement Sekera | 6452622 | 2018-06-15 12:44:16 +0200 | [diff] [blame] | 139 | self.assert_packet_checksums_valid(decrypt_pkt) |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 140 | self.assert_equal(decrypt_pkt[IP].src, self.pg1.remote_ip4, |
| 141 | "encrypted packet source address") |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 142 | self.assert_equal(decrypt_pkt[IP].dst, self.tun_if.remote_ip4, |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 143 | "encrypted packet destination address") |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 144 | except Exception: |
| 145 | self.logger.error( |
| 146 | ppp("Unexpected or invalid encrypted packet:", packet)) |
| 147 | raise |
| 148 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 149 | def config_esp_tun(self, params): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 150 | addr_type = params.addr_type |
| 151 | scapy_tun_sa_id = params.scapy_tun_sa_id |
| 152 | scapy_tun_spi = params.scapy_tun_spi |
| 153 | vpp_tun_sa_id = params.vpp_tun_sa_id |
| 154 | vpp_tun_spi = params.vpp_tun_spi |
| 155 | auth_algo_vpp_id = params.auth_algo_vpp_id |
| 156 | auth_key = params.auth_key |
| 157 | crypt_algo_vpp_id = params.crypt_algo_vpp_id |
| 158 | crypt_key = params.crypt_key |
| 159 | addr_any = params.addr_any |
| 160 | addr_bcast = params.addr_bcast |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 161 | flags = (VppEnum.vl_api_ipsec_sad_flags_t. |
| 162 | IPSEC_API_SAD_FLAG_UDP_ENCAP) |
| 163 | e = VppEnum.vl_api_ipsec_spd_action_t |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 164 | |
| 165 | VppIpsecSA(self, scapy_tun_sa_id, scapy_tun_spi, |
| 166 | auth_algo_vpp_id, auth_key, |
| 167 | crypt_algo_vpp_id, crypt_key, |
| 168 | self.vpp_esp_protocol, |
| 169 | self.pg1.remote_addr[addr_type], |
| 170 | self.tun_if.remote_addr[addr_type], |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 171 | flags=flags).add_vpp_config() |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 172 | VppIpsecSA(self, vpp_tun_sa_id, vpp_tun_spi, |
| 173 | auth_algo_vpp_id, auth_key, |
| 174 | crypt_algo_vpp_id, crypt_key, |
| 175 | self.vpp_esp_protocol, |
| 176 | self.tun_if.remote_addr[addr_type], |
| 177 | self.pg1.remote_addr[addr_type], |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 178 | flags=flags).add_vpp_config() |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 179 | |
| 180 | VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id, |
| 181 | addr_any, addr_bcast, |
| 182 | addr_any, addr_bcast, |
| 183 | socket.IPPROTO_ESP).add_vpp_config() |
| 184 | VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id, |
| 185 | addr_any, addr_bcast, |
| 186 | addr_any, addr_bcast, |
| 187 | socket.IPPROTO_ESP, |
| 188 | is_outbound=0).add_vpp_config() |
| 189 | VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id, |
| 190 | addr_any, addr_bcast, |
| 191 | addr_any, addr_bcast, |
| 192 | socket.IPPROTO_UDP, |
| 193 | remote_port_start=4500, |
| 194 | remote_port_stop=4500).add_vpp_config() |
| 195 | VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id, |
| 196 | addr_any, addr_bcast, |
| 197 | addr_any, addr_bcast, |
| 198 | socket.IPPROTO_UDP, |
| 199 | remote_port_start=4500, |
| 200 | remote_port_stop=4500, |
| 201 | is_outbound=0).add_vpp_config() |
| 202 | VppIpsecSpdEntry(self, self.tun_spd, vpp_tun_sa_id, |
| 203 | self.tun_if.remote_addr[addr_type], |
| 204 | self.tun_if.remote_addr[addr_type], |
| 205 | self.pg1.remote_addr[addr_type], |
| 206 | self.pg1.remote_addr[addr_type], |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 207 | 0, priority=10, |
| 208 | policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 209 | is_outbound=0).add_vpp_config() |
| 210 | VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id, |
| 211 | self.pg1.remote_addr[addr_type], |
| 212 | self.pg1.remote_addr[addr_type], |
| 213 | self.tun_if.remote_addr[addr_type], |
| 214 | self.tun_if.remote_addr[addr_type], |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 215 | 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
| 216 | priority=10).add_vpp_config() |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 217 | |
| 218 | def test_ipsec_nat_tun(self): |
| 219 | """ IPSec/NAT tunnel test case """ |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 220 | p = self.ipv4_params |
| 221 | scapy_tun_sa = SecurityAssociation(ESP, spi=p.scapy_tun_spi, |
| 222 | crypt_algo=p.crypt_algo, |
| 223 | crypt_key=p.crypt_key, |
| 224 | auth_algo=p.auth_algo, |
| 225 | auth_key=p.auth_key, |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 226 | tunnel_header=IP( |
| 227 | src=self.pg1.remote_ip4, |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 228 | dst=self.tun_if.remote_ip4), |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 229 | nat_t_header=UDP( |
| 230 | sport=4500, |
| 231 | dport=4500)) |
| 232 | # in2out - from private network to public |
| 233 | pkts = self.create_stream_plain( |
| 234 | self.pg1.remote_mac, self.pg1.local_mac, |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 235 | self.pg1.remote_ip4, self.tun_if.remote_ip4) |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 236 | self.pg1.add_stream(pkts) |
| 237 | self.pg_enable_capture(self.pg_interfaces) |
| 238 | self.pg_start() |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 239 | capture = self.tun_if.get_capture(len(pkts)) |
| 240 | self.verify_capture_encrypted(capture, scapy_tun_sa) |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 241 | |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 242 | vpp_tun_sa = SecurityAssociation(ESP, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 243 | spi=p.vpp_tun_spi, |
| 244 | crypt_algo=p.crypt_algo, |
| 245 | crypt_key=p.crypt_key, |
| 246 | auth_algo=p.auth_algo, |
| 247 | auth_key=p.auth_key, |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 248 | tunnel_header=IP( |
| 249 | src=self.tun_if.remote_ip4, |
| 250 | dst=self.pg1.remote_ip4), |
| 251 | nat_t_header=UDP( |
| 252 | sport=4500, |
| 253 | dport=4500)) |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 254 | |
| 255 | # out2in - from public network to private |
| 256 | pkts = self.create_stream_encrypted( |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 257 | self.tun_if.remote_mac, self.tun_if.local_mac, |
| 258 | self.tun_if.remote_ip4, self.pg1.remote_ip4, vpp_tun_sa) |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 259 | self.logger.info(ppc("Sending packets:", pkts)) |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 260 | self.tun_if.add_stream(pkts) |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 261 | self.pg_enable_capture(self.pg_interfaces) |
| 262 | self.pg_start() |
| 263 | capture = self.pg1.get_capture(len(pkts)) |
| 264 | self.verify_capture_plain(capture) |