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