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