blob: dcedf64b52d79e8b0a676d776f73c174480899fe [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Klement Sekera4b089f22018-04-17 18:04:57 +02002
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)
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -040060 self.logger.info(self.vapi.ppcli("show ipsec all"))
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,
Neale Ranns097fa662018-05-01 05:17:55 -070066 proto=d)]).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -080067
68 def tearDown(self):
69 super(IPSecNATTestCase, self).tearDown()
Klement Sekera4b089f22018-04-17 18:04:57 +020070
71 def create_stream_plain(self, src_mac, dst_mac, src_ip, dst_ip):
72 return [
73 # TCP
74 Ether(src=src_mac, dst=dst_mac) /
75 IP(src=src_ip, dst=dst_ip) /
76 TCP(sport=self.tcp_port_in, dport=20),
77 # UDP
78 Ether(src=src_mac, dst=dst_mac) /
79 IP(src=src_ip, dst=dst_ip) /
80 UDP(sport=self.udp_port_in, dport=20),
81 # ICMP
82 Ether(src=src_mac, dst=dst_mac) /
83 IP(src=src_ip, dst=dst_ip) /
84 ICMP(id=self.icmp_id_in, type='echo-request')
85 ]
86
87 def create_stream_encrypted(self, src_mac, dst_mac, src_ip, dst_ip, sa):
88 return [
89 # TCP
90 Ether(src=src_mac, dst=dst_mac) /
91 sa.encrypt(IP(src=src_ip, dst=dst_ip) /
92 TCP(dport=self.tcp_port_out, sport=20)),
93 # UDP
94 Ether(src=src_mac, dst=dst_mac) /
95 sa.encrypt(IP(src=src_ip, dst=dst_ip) /
96 UDP(dport=self.udp_port_out, sport=20)),
97 # ICMP
98 Ether(src=src_mac, dst=dst_mac) /
99 sa.encrypt(IP(src=src_ip, dst=dst_ip) /
100 ICMP(id=self.icmp_id_out, type='echo-request'))
101 ]
102
Klement Sekera4b089f22018-04-17 18:04:57 +0200103 def verify_capture_plain(self, capture):
104 for packet in capture:
105 try:
Klement Sekerad81ae412018-05-16 10:52:54 +0200106 self.assert_packet_checksums_valid(packet)
Klement Sekerabeaded52018-06-24 10:30:37 +0200107 self.assert_equal(packet[IP].src, self.tun_if.remote_ip4,
Klement Sekera4b089f22018-04-17 18:04:57 +0200108 "decrypted packet source address")
109 self.assert_equal(packet[IP].dst, self.pg1.remote_ip4,
110 "decrypted packet destination address")
111 if packet.haslayer(TCP):
112 self.assertFalse(
113 packet.haslayer(UDP),
114 "unexpected UDP header in decrypted packet")
115 self.assert_equal(packet[TCP].dport, self.tcp_port_in,
116 "decrypted packet TCP destination port")
Klement Sekera4b089f22018-04-17 18:04:57 +0200117 elif packet.haslayer(UDP):
118 if packet[UDP].payload:
119 self.assertFalse(
120 packet[UDP][1].haslayer(UDP),
121 "unexpected UDP header in decrypted packet")
122 self.assert_equal(packet[UDP].dport, self.udp_port_in,
123 "decrypted packet UDP destination port")
124 else:
125 self.assertFalse(
126 packet.haslayer(UDP),
127 "unexpected UDP header in decrypted packet")
128 self.assert_equal(packet[ICMP].id, self.icmp_id_in,
129 "decrypted packet ICMP ID")
Klement Sekera4b089f22018-04-17 18:04:57 +0200130 except Exception:
131 self.logger.error(
132 ppp("Unexpected or invalid plain packet:", packet))
133 raise
134
135 def verify_capture_encrypted(self, capture, sa):
136 for packet in capture:
137 try:
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700138 copy = packet.__class__(scapy.compat.raw(packet))
Klement Sekera64526222018-06-15 12:44:16 +0200139 del copy[UDP].len
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700140 copy = packet.__class__(scapy.compat.raw(copy))
Klement Sekera64526222018-06-15 12:44:16 +0200141 self.assert_equal(packet[UDP].len, copy[UDP].len,
142 "UDP header length")
143 self.assert_packet_checksums_valid(packet)
Klement Sekera4b089f22018-04-17 18:04:57 +0200144 self.assertIn(ESP, packet[IP])
145 decrypt_pkt = sa.decrypt(packet[IP])
Klement Sekera64526222018-06-15 12:44:16 +0200146 self.assert_packet_checksums_valid(decrypt_pkt)
Klement Sekera4b089f22018-04-17 18:04:57 +0200147 self.assert_equal(decrypt_pkt[IP].src, self.pg1.remote_ip4,
148 "encrypted packet source address")
Klement Sekerabeaded52018-06-24 10:30:37 +0200149 self.assert_equal(decrypt_pkt[IP].dst, self.tun_if.remote_ip4,
Klement Sekera4b089f22018-04-17 18:04:57 +0200150 "encrypted packet destination address")
Klement Sekera4b089f22018-04-17 18:04:57 +0200151 except Exception:
152 self.logger.error(
153 ppp("Unexpected or invalid encrypted packet:", packet))
154 raise
155
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800156 def config_esp_tun(self, params):
Klement Sekera611864f2018-09-26 11:19:00 +0200157 addr_type = params.addr_type
158 scapy_tun_sa_id = params.scapy_tun_sa_id
159 scapy_tun_spi = params.scapy_tun_spi
160 vpp_tun_sa_id = params.vpp_tun_sa_id
161 vpp_tun_spi = params.vpp_tun_spi
162 auth_algo_vpp_id = params.auth_algo_vpp_id
163 auth_key = params.auth_key
164 crypt_algo_vpp_id = params.crypt_algo_vpp_id
165 crypt_key = params.crypt_key
166 addr_any = params.addr_any
167 addr_bcast = params.addr_bcast
Neale Ranns17dcec02019-01-09 21:22:20 -0800168 flags = (VppEnum.vl_api_ipsec_sad_flags_t.
169 IPSEC_API_SAD_FLAG_UDP_ENCAP)
170 e = VppEnum.vl_api_ipsec_spd_action_t
Neale Ranns311124e2019-01-24 04:52:25 -0800171
172 VppIpsecSA(self, scapy_tun_sa_id, scapy_tun_spi,
173 auth_algo_vpp_id, auth_key,
174 crypt_algo_vpp_id, crypt_key,
175 self.vpp_esp_protocol,
176 self.pg1.remote_addr[addr_type],
177 self.tun_if.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 VppIpsecSA(self, vpp_tun_sa_id, vpp_tun_spi,
180 auth_algo_vpp_id, auth_key,
181 crypt_algo_vpp_id, crypt_key,
182 self.vpp_esp_protocol,
183 self.tun_if.remote_addr[addr_type],
184 self.pg1.remote_addr[addr_type],
Neale Ranns17dcec02019-01-09 21:22:20 -0800185 flags=flags).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800186
187 VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id,
188 addr_any, addr_bcast,
189 addr_any, addr_bcast,
190 socket.IPPROTO_ESP).add_vpp_config()
191 VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id,
192 addr_any, addr_bcast,
193 addr_any, addr_bcast,
194 socket.IPPROTO_ESP,
195 is_outbound=0).add_vpp_config()
196 VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id,
197 addr_any, addr_bcast,
198 addr_any, addr_bcast,
199 socket.IPPROTO_UDP,
200 remote_port_start=4500,
201 remote_port_stop=4500).add_vpp_config()
202 VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id,
203 addr_any, addr_bcast,
204 addr_any, addr_bcast,
205 socket.IPPROTO_UDP,
206 remote_port_start=4500,
207 remote_port_stop=4500,
208 is_outbound=0).add_vpp_config()
209 VppIpsecSpdEntry(self, self.tun_spd, vpp_tun_sa_id,
210 self.tun_if.remote_addr[addr_type],
211 self.tun_if.remote_addr[addr_type],
212 self.pg1.remote_addr[addr_type],
213 self.pg1.remote_addr[addr_type],
Neale Ranns17dcec02019-01-09 21:22:20 -0800214 0, priority=10,
215 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
Neale Ranns311124e2019-01-24 04:52:25 -0800216 is_outbound=0).add_vpp_config()
217 VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id,
218 self.pg1.remote_addr[addr_type],
219 self.pg1.remote_addr[addr_type],
220 self.tun_if.remote_addr[addr_type],
221 self.tun_if.remote_addr[addr_type],
Neale Ranns17dcec02019-01-09 21:22:20 -0800222 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT,
223 priority=10).add_vpp_config()
Klement Sekera4b089f22018-04-17 18:04:57 +0200224
225 def test_ipsec_nat_tun(self):
226 """ IPSec/NAT tunnel test case """
Klement Sekera611864f2018-09-26 11:19:00 +0200227 p = self.ipv4_params
228 scapy_tun_sa = SecurityAssociation(ESP, spi=p.scapy_tun_spi,
229 crypt_algo=p.crypt_algo,
230 crypt_key=p.crypt_key,
231 auth_algo=p.auth_algo,
232 auth_key=p.auth_key,
Klement Sekera4b089f22018-04-17 18:04:57 +0200233 tunnel_header=IP(
234 src=self.pg1.remote_ip4,
Klement Sekerabeaded52018-06-24 10:30:37 +0200235 dst=self.tun_if.remote_ip4),
Klement Sekera4b089f22018-04-17 18:04:57 +0200236 nat_t_header=UDP(
237 sport=4500,
238 dport=4500))
239 # in2out - from private network to public
240 pkts = self.create_stream_plain(
241 self.pg1.remote_mac, self.pg1.local_mac,
Klement Sekerabeaded52018-06-24 10:30:37 +0200242 self.pg1.remote_ip4, self.tun_if.remote_ip4)
Klement Sekera4b089f22018-04-17 18:04:57 +0200243 self.pg1.add_stream(pkts)
244 self.pg_enable_capture(self.pg_interfaces)
245 self.pg_start()
Klement Sekerabeaded52018-06-24 10:30:37 +0200246 capture = self.tun_if.get_capture(len(pkts))
247 self.verify_capture_encrypted(capture, scapy_tun_sa)
Klement Sekera4b089f22018-04-17 18:04:57 +0200248
Klement Sekerabeaded52018-06-24 10:30:37 +0200249 vpp_tun_sa = SecurityAssociation(ESP,
Klement Sekera611864f2018-09-26 11:19:00 +0200250 spi=p.vpp_tun_spi,
251 crypt_algo=p.crypt_algo,
252 crypt_key=p.crypt_key,
253 auth_algo=p.auth_algo,
254 auth_key=p.auth_key,
Klement Sekerabeaded52018-06-24 10:30:37 +0200255 tunnel_header=IP(
256 src=self.tun_if.remote_ip4,
257 dst=self.pg1.remote_ip4),
258 nat_t_header=UDP(
259 sport=4500,
260 dport=4500))
Klement Sekera4b089f22018-04-17 18:04:57 +0200261
262 # out2in - from public network to private
263 pkts = self.create_stream_encrypted(
Klement Sekerabeaded52018-06-24 10:30:37 +0200264 self.tun_if.remote_mac, self.tun_if.local_mac,
265 self.tun_if.remote_ip4, self.pg1.remote_ip4, vpp_tun_sa)
Klement Sekera4b089f22018-04-17 18:04:57 +0200266 self.logger.info(ppc("Sending packets:", pkts))
Klement Sekerabeaded52018-06-24 10:30:37 +0200267 self.tun_if.add_stream(pkts)
Klement Sekera4b089f22018-04-17 18:04:57 +0200268 self.pg_enable_capture(self.pg_interfaces)
269 self.pg_start()
270 capture = self.pg1.get_capture(len(pkts))
271 self.verify_capture_plain(capture)