blob: cdb9cb438f229b0ec5957fe541c5c8bbe4a91736 [file] [log] [blame]
Klement Sekera4b089f22018-04-17 18:04:57 +02001#!/usr/bin/env python
2
3import socket
4
5from scapy.layers.l2 import Ether
6from scapy.layers.inet import ICMP, IP, TCP, UDP
7from scapy.layers.ipsec import SecurityAssociation, ESP
8from util import ppp, ppc
Klement Sekerabeaded52018-06-24 10:30:37 +02009from template_ipsec import TemplateIpsec
Klement Sekerabf613952019-01-29 11:38:08 +010010from vpp_ipsec import VppIpsecSA, VppIpsecSpd, VppIpsecSpdEntry,\
11 VppIpsecSpdItfBinding
Neale Ranns311124e2019-01-24 04:52:25 -080012from vpp_ip_route import VppIpRoute, VppRoutePath
13from vpp_ip import DpoProto
Neale Ranns17dcec02019-01-09 21:22:20 -080014from vpp_papi import VppEnum
Klement Sekera4b089f22018-04-17 18:04:57 +020015
16
Florin Coras4a7cbcd2019-01-02 17:43:01 +000017class IPSecNATTestCase(TemplateIpsec):
Klement Sekera4b089f22018-04-17 18:04:57 +020018 """ IPSec/NAT
Klement Sekera4b089f22018-04-17 18:04:57 +020019 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 Sekera64526222018-06-15 12:44:16 +020032 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 Ranns8e4a89b2019-01-23 08:16:17 -080039 def setUp(self):
40 super(IPSecNATTestCase, self).setUp()
41 self.tun_if = self.pg0
Neale Ranns311124e2019-01-24 04:52:25 -080042
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 Ranns8e4a89b2019-01-23 08:16:17 -080048 p = self.ipv4_params
49 self.config_esp_tun(p)
50 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Ranns311124e2019-01-24 04:52:25 -080051
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 Sekera4b089f22018-04-17 18:04:57 +020061
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 Sekera4b089f22018-04-17 18:04:57 +020094 def verify_capture_plain(self, capture):
95 for packet in capture:
96 try:
Klement Sekerad81ae412018-05-16 10:52:54 +020097 self.assert_packet_checksums_valid(packet)
Klement Sekerabeaded52018-06-24 10:30:37 +020098 self.assert_equal(packet[IP].src, self.tun_if.remote_ip4,
Klement Sekera4b089f22018-04-17 18:04:57 +020099 "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 Sekera4b089f22018-04-17 18:04:57 +0200108 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 Sekera4b089f22018-04-17 18:04:57 +0200121 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 Sekera64526222018-06-15 12:44:16 +0200129 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 Sekera4b089f22018-04-17 18:04:57 +0200135 self.assertIn(ESP, packet[IP])
136 decrypt_pkt = sa.decrypt(packet[IP])
Klement Sekera64526222018-06-15 12:44:16 +0200137 self.assert_packet_checksums_valid(decrypt_pkt)
Klement Sekera4b089f22018-04-17 18:04:57 +0200138 self.assert_equal(decrypt_pkt[IP].src, self.pg1.remote_ip4,
139 "encrypted packet source address")
Klement Sekerabeaded52018-06-24 10:30:37 +0200140 self.assert_equal(decrypt_pkt[IP].dst, self.tun_if.remote_ip4,
Klement Sekera4b089f22018-04-17 18:04:57 +0200141 "encrypted packet destination address")
Klement Sekera4b089f22018-04-17 18:04:57 +0200142 except Exception:
143 self.logger.error(
144 ppp("Unexpected or invalid encrypted packet:", packet))
145 raise
146
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800147 def config_esp_tun(self, params):
Klement Sekera611864f2018-09-26 11:19:00 +0200148 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 Ranns17dcec02019-01-09 21:22:20 -0800159 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 Ranns311124e2019-01-24 04:52:25 -0800162
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 Ranns17dcec02019-01-09 21:22:20 -0800169 flags=flags).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800170 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 Ranns17dcec02019-01-09 21:22:20 -0800176 flags=flags).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800177
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 Ranns17dcec02019-01-09 21:22:20 -0800205 0, priority=10,
206 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
Neale Ranns311124e2019-01-24 04:52:25 -0800207 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 Ranns17dcec02019-01-09 21:22:20 -0800213 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT,
214 priority=10).add_vpp_config()
Klement Sekera4b089f22018-04-17 18:04:57 +0200215
216 def test_ipsec_nat_tun(self):
217 """ IPSec/NAT tunnel test case """
Klement Sekera611864f2018-09-26 11:19:00 +0200218 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 Sekera4b089f22018-04-17 18:04:57 +0200224 tunnel_header=IP(
225 src=self.pg1.remote_ip4,
Klement Sekerabeaded52018-06-24 10:30:37 +0200226 dst=self.tun_if.remote_ip4),
Klement Sekera4b089f22018-04-17 18:04:57 +0200227 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 Sekerabeaded52018-06-24 10:30:37 +0200233 self.pg1.remote_ip4, self.tun_if.remote_ip4)
Klement Sekera4b089f22018-04-17 18:04:57 +0200234 self.pg1.add_stream(pkts)
235 self.pg_enable_capture(self.pg_interfaces)
236 self.pg_start()
Klement Sekerabeaded52018-06-24 10:30:37 +0200237 capture = self.tun_if.get_capture(len(pkts))
238 self.verify_capture_encrypted(capture, scapy_tun_sa)
Klement Sekera4b089f22018-04-17 18:04:57 +0200239
Klement Sekerabeaded52018-06-24 10:30:37 +0200240 vpp_tun_sa = SecurityAssociation(ESP,
Klement Sekera611864f2018-09-26 11:19:00 +0200241 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 Sekerabeaded52018-06-24 10:30:37 +0200246 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 Sekera4b089f22018-04-17 18:04:57 +0200252
253 # out2in - from public network to private
254 pkts = self.create_stream_encrypted(
Klement Sekerabeaded52018-06-24 10:30:37 +0200255 self.tun_if.remote_mac, self.tun_if.local_mac,
256 self.tun_if.remote_ip4, self.pg1.remote_ip4, vpp_tun_sa)
Klement Sekera4b089f22018-04-17 18:04:57 +0200257 self.logger.info(ppc("Sending packets:", pkts))
Klement Sekerabeaded52018-06-24 10:30:37 +0200258 self.tun_if.add_stream(pkts)
Klement Sekera4b089f22018-04-17 18:04:57 +0200259 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)