blob: e364f5ffe965062cd0e1481eca80d5fdf05b331c [file] [log] [blame]
Klement Sekera4b089f22018-04-17 18:04:57 +02001#!/usr/bin/env python
2
3import socket
4
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -07005import scapy.compat
Klement Sekera4b089f22018-04-17 18:04:57 +02006from scapy.layers.l2 import Ether
7from scapy.layers.inet import ICMP, IP, TCP, UDP
8from scapy.layers.ipsec import SecurityAssociation, ESP
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -07009
Klement Sekera4b089f22018-04-17 18:04:57 +020010from util import ppp, ppc
Klement Sekerabeaded52018-06-24 10:30:37 +020011from template_ipsec import TemplateIpsec
Klement Sekerabf613952019-01-29 11:38:08 +010012from vpp_ipsec import VppIpsecSA, VppIpsecSpd, VppIpsecSpdEntry,\
13 VppIpsecSpdItfBinding
Neale Ranns311124e2019-01-24 04:52:25 -080014from vpp_ip_route import VppIpRoute, VppRoutePath
15from vpp_ip import DpoProto
Neale Ranns17dcec02019-01-09 21:22:20 -080016from vpp_papi import VppEnum
Klement Sekera4b089f22018-04-17 18:04:57 +020017
18
Florin Coras4a7cbcd2019-01-02 17:43:01 +000019class IPSecNATTestCase(TemplateIpsec):
Klement Sekera4b089f22018-04-17 18:04:57 +020020 """ IPSec/NAT
Klement Sekera4b089f22018-04-17 18:04:57 +020021 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 Sekera64526222018-06-15 12:44:16 +020034 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 Ranns8e4a89b2019-01-23 08:16:17 -080041 def setUp(self):
42 super(IPSecNATTestCase, self).setUp()
43 self.tun_if = self.pg0
Neale Ranns311124e2019-01-24 04:52:25 -080044
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 Ranns8e4a89b2019-01-23 08:16:17 -080050 p = self.ipv4_params
51 self.config_esp_tun(p)
52 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Ranns311124e2019-01-24 04:52:25 -080053
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 Sekera4b089f22018-04-17 18:04:57 +020063
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 Sekera4b089f22018-04-17 18:04:57 +020096 def verify_capture_plain(self, capture):
97 for packet in capture:
98 try:
Klement Sekerad81ae412018-05-16 10:52:54 +020099 self.assert_packet_checksums_valid(packet)
Klement Sekerabeaded52018-06-24 10:30:37 +0200100 self.assert_equal(packet[IP].src, self.tun_if.remote_ip4,
Klement Sekera4b089f22018-04-17 18:04:57 +0200101 "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 Sekera4b089f22018-04-17 18:04:57 +0200110 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 Sekera4b089f22018-04-17 18:04:57 +0200123 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 Vinciguerraa7427ec2019-03-10 10:04:23 -0700131 copy = packet.__class__(scapy.compat.raw(packet))
Klement Sekera64526222018-06-15 12:44:16 +0200132 del copy[UDP].len
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700133 copy = packet.__class__(scapy.compat.raw(copy))
Klement Sekera64526222018-06-15 12:44:16 +0200134 self.assert_equal(packet[UDP].len, copy[UDP].len,
135 "UDP header length")
136 self.assert_packet_checksums_valid(packet)
Klement Sekera4b089f22018-04-17 18:04:57 +0200137 self.assertIn(ESP, packet[IP])
138 decrypt_pkt = sa.decrypt(packet[IP])
Klement Sekera64526222018-06-15 12:44:16 +0200139 self.assert_packet_checksums_valid(decrypt_pkt)
Klement Sekera4b089f22018-04-17 18:04:57 +0200140 self.assert_equal(decrypt_pkt[IP].src, self.pg1.remote_ip4,
141 "encrypted packet source address")
Klement Sekerabeaded52018-06-24 10:30:37 +0200142 self.assert_equal(decrypt_pkt[IP].dst, self.tun_if.remote_ip4,
Klement Sekera4b089f22018-04-17 18:04:57 +0200143 "encrypted packet destination address")
Klement Sekera4b089f22018-04-17 18:04:57 +0200144 except Exception:
145 self.logger.error(
146 ppp("Unexpected or invalid encrypted packet:", packet))
147 raise
148
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800149 def config_esp_tun(self, params):
Klement Sekera611864f2018-09-26 11:19:00 +0200150 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 Ranns17dcec02019-01-09 21:22:20 -0800161 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 Ranns311124e2019-01-24 04:52:25 -0800164
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 Ranns17dcec02019-01-09 21:22:20 -0800171 flags=flags).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800172 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 Ranns17dcec02019-01-09 21:22:20 -0800178 flags=flags).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800179
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 Ranns17dcec02019-01-09 21:22:20 -0800207 0, priority=10,
208 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
Neale Ranns311124e2019-01-24 04:52:25 -0800209 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 Ranns17dcec02019-01-09 21:22:20 -0800215 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT,
216 priority=10).add_vpp_config()
Klement Sekera4b089f22018-04-17 18:04:57 +0200217
218 def test_ipsec_nat_tun(self):
219 """ IPSec/NAT tunnel test case """
Klement Sekera611864f2018-09-26 11:19:00 +0200220 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 Sekera4b089f22018-04-17 18:04:57 +0200226 tunnel_header=IP(
227 src=self.pg1.remote_ip4,
Klement Sekerabeaded52018-06-24 10:30:37 +0200228 dst=self.tun_if.remote_ip4),
Klement Sekera4b089f22018-04-17 18:04:57 +0200229 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 Sekerabeaded52018-06-24 10:30:37 +0200235 self.pg1.remote_ip4, self.tun_if.remote_ip4)
Klement Sekera4b089f22018-04-17 18:04:57 +0200236 self.pg1.add_stream(pkts)
237 self.pg_enable_capture(self.pg_interfaces)
238 self.pg_start()
Klement Sekerabeaded52018-06-24 10:30:37 +0200239 capture = self.tun_if.get_capture(len(pkts))
240 self.verify_capture_encrypted(capture, scapy_tun_sa)
Klement Sekera4b089f22018-04-17 18:04:57 +0200241
Klement Sekerabeaded52018-06-24 10:30:37 +0200242 vpp_tun_sa = SecurityAssociation(ESP,
Klement Sekera611864f2018-09-26 11:19:00 +0200243 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 Sekerabeaded52018-06-24 10:30:37 +0200248 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 Sekera4b089f22018-04-17 18:04:57 +0200254
255 # out2in - from public network to private
256 pkts = self.create_stream_encrypted(
Klement Sekerabeaded52018-06-24 10:30:37 +0200257 self.tun_if.remote_mac, self.tun_if.local_mac,
258 self.tun_if.remote_ip4, self.pg1.remote_ip4, vpp_tun_sa)
Klement Sekera4b089f22018-04-17 18:04:57 +0200259 self.logger.info(ppc("Sending packets:", pkts))
Klement Sekerabeaded52018-06-24 10:30:37 +0200260 self.tun_if.add_stream(pkts)
Klement Sekera4b089f22018-04-17 18:04:57 +0200261 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)