blob: d97fb791cf5f6cb8114167a1e13b6b7d584acc1e [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
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070041 @classmethod
42 def setUpClass(cls):
43 super(IPSecNATTestCase, cls).setUpClass()
44
45 @classmethod
46 def tearDownClass(cls):
47 super(IPSecNATTestCase, cls).tearDownClass()
48
Neale Ranns8e4a89b2019-01-23 08:16:17 -080049 def setUp(self):
50 super(IPSecNATTestCase, self).setUp()
51 self.tun_if = self.pg0
Neale Ranns311124e2019-01-24 04:52:25 -080052
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 Ranns8e4a89b2019-01-23 08:16:17 -080058 p = self.ipv4_params
59 self.config_esp_tun(p)
60 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Ranns311124e2019-01-24 04:52:25 -080061
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 Sekera4b089f22018-04-17 18:04:57 +020071
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 Sekera4b089f22018-04-17 18:04:57 +0200104 def verify_capture_plain(self, capture):
105 for packet in capture:
106 try:
Klement Sekerad81ae412018-05-16 10:52:54 +0200107 self.assert_packet_checksums_valid(packet)
Klement Sekerabeaded52018-06-24 10:30:37 +0200108 self.assert_equal(packet[IP].src, self.tun_if.remote_ip4,
Klement Sekera4b089f22018-04-17 18:04:57 +0200109 "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 Sekera4b089f22018-04-17 18:04:57 +0200118 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 Sekera4b089f22018-04-17 18:04:57 +0200131 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 Vinciguerraa7427ec2019-03-10 10:04:23 -0700139 copy = packet.__class__(scapy.compat.raw(packet))
Klement Sekera64526222018-06-15 12:44:16 +0200140 del copy[UDP].len
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700141 copy = packet.__class__(scapy.compat.raw(copy))
Klement Sekera64526222018-06-15 12:44:16 +0200142 self.assert_equal(packet[UDP].len, copy[UDP].len,
143 "UDP header length")
144 self.assert_packet_checksums_valid(packet)
Klement Sekera4b089f22018-04-17 18:04:57 +0200145 self.assertIn(ESP, packet[IP])
146 decrypt_pkt = sa.decrypt(packet[IP])
Klement Sekera64526222018-06-15 12:44:16 +0200147 self.assert_packet_checksums_valid(decrypt_pkt)
Klement Sekera4b089f22018-04-17 18:04:57 +0200148 self.assert_equal(decrypt_pkt[IP].src, self.pg1.remote_ip4,
149 "encrypted packet source address")
Klement Sekerabeaded52018-06-24 10:30:37 +0200150 self.assert_equal(decrypt_pkt[IP].dst, self.tun_if.remote_ip4,
Klement Sekera4b089f22018-04-17 18:04:57 +0200151 "encrypted packet destination address")
Klement Sekera4b089f22018-04-17 18:04:57 +0200152 except Exception:
153 self.logger.error(
154 ppp("Unexpected or invalid encrypted packet:", packet))
155 raise
156
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800157 def config_esp_tun(self, params):
Klement Sekera611864f2018-09-26 11:19:00 +0200158 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 Ranns17dcec02019-01-09 21:22:20 -0800169 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 Ranns311124e2019-01-24 04:52:25 -0800172
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 Ranns17dcec02019-01-09 21:22:20 -0800179 flags=flags).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800180 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 Ranns17dcec02019-01-09 21:22:20 -0800186 flags=flags).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800187
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 Ranns17dcec02019-01-09 21:22:20 -0800215 0, priority=10,
216 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
Neale Ranns311124e2019-01-24 04:52:25 -0800217 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 Ranns17dcec02019-01-09 21:22:20 -0800223 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT,
224 priority=10).add_vpp_config()
Klement Sekera4b089f22018-04-17 18:04:57 +0200225
226 def test_ipsec_nat_tun(self):
227 """ IPSec/NAT tunnel test case """
Klement Sekera611864f2018-09-26 11:19:00 +0200228 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 Sekera4b089f22018-04-17 18:04:57 +0200234 tunnel_header=IP(
235 src=self.pg1.remote_ip4,
Klement Sekerabeaded52018-06-24 10:30:37 +0200236 dst=self.tun_if.remote_ip4),
Klement Sekera4b089f22018-04-17 18:04:57 +0200237 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 Sekerabeaded52018-06-24 10:30:37 +0200243 self.pg1.remote_ip4, self.tun_if.remote_ip4)
Klement Sekera4b089f22018-04-17 18:04:57 +0200244 self.pg1.add_stream(pkts)
245 self.pg_enable_capture(self.pg_interfaces)
246 self.pg_start()
Klement Sekerabeaded52018-06-24 10:30:37 +0200247 capture = self.tun_if.get_capture(len(pkts))
248 self.verify_capture_encrypted(capture, scapy_tun_sa)
Klement Sekera4b089f22018-04-17 18:04:57 +0200249
Klement Sekerabeaded52018-06-24 10:30:37 +0200250 vpp_tun_sa = SecurityAssociation(ESP,
Klement Sekera611864f2018-09-26 11:19:00 +0200251 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 Sekerabeaded52018-06-24 10:30:37 +0200256 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 Sekera4b089f22018-04-17 18:04:57 +0200262
263 # out2in - from public network to private
264 pkts = self.create_stream_encrypted(
Klement Sekerabeaded52018-06-24 10:30:37 +0200265 self.tun_if.remote_mac, self.tun_if.local_mac,
266 self.tun_if.remote_ip4, self.pg1.remote_ip4, vpp_tun_sa)
Klement Sekera4b089f22018-04-17 18:04:57 +0200267 self.logger.info(ppc("Sending packets:", pkts))
Klement Sekerabeaded52018-06-24 10:30:37 +0200268 self.tun_if.add_stream(pkts)
Klement Sekera4b089f22018-04-17 18:04:57 +0200269 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)