blob: cebbfc8ec67f910621fc0d5b5457e75cb0bb6a0d [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
9from framework import VppTestCase
10
11
12class IPSecNATTestCase(VppTestCase):
13 """ IPSec/NAT
14
15 TRANSPORT MODE:
16
17 --- encrypt ---
18 |pg2| <-------> |VPP|
19 --- decrypt ---
20
21 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
34 remote_pg0_client_addr = '1.1.1.1'
35
36 @classmethod
37 def setUpClass(cls):
38 super(IPSecNATTestCase, cls).setUpClass()
39 cls.create_pg_interfaces(range(2))
40 for i in cls.pg_interfaces:
41 i.configure_ipv4_neighbors()
42 i.admin_up()
43 i.config_ip4()
44 i.resolve_arp()
45
46 cls.tcp_port_in = 6303
47 cls.tcp_port_out = 6303
48 cls.udp_port_in = 6304
49 cls.udp_port_out = 6304
50 cls.icmp_id_in = 6305
51 cls.icmp_id_out = 6305
52 cls.config_esp_tun()
53 cls.logger.info(cls.vapi.ppcli("show ipsec"))
54
55 def create_stream_plain(self, src_mac, dst_mac, src_ip, dst_ip):
56 return [
57 # TCP
58 Ether(src=src_mac, dst=dst_mac) /
59 IP(src=src_ip, dst=dst_ip) /
60 TCP(sport=self.tcp_port_in, dport=20),
61 # UDP
62 Ether(src=src_mac, dst=dst_mac) /
63 IP(src=src_ip, dst=dst_ip) /
64 UDP(sport=self.udp_port_in, dport=20),
65 # ICMP
66 Ether(src=src_mac, dst=dst_mac) /
67 IP(src=src_ip, dst=dst_ip) /
68 ICMP(id=self.icmp_id_in, type='echo-request')
69 ]
70
71 def create_stream_encrypted(self, src_mac, dst_mac, src_ip, dst_ip, sa):
72 return [
73 # TCP
74 Ether(src=src_mac, dst=dst_mac) /
75 sa.encrypt(IP(src=src_ip, dst=dst_ip) /
76 TCP(dport=self.tcp_port_out, sport=20)),
77 # UDP
78 Ether(src=src_mac, dst=dst_mac) /
79 sa.encrypt(IP(src=src_ip, dst=dst_ip) /
80 UDP(dport=self.udp_port_out, sport=20)),
81 # ICMP
82 Ether(src=src_mac, dst=dst_mac) /
83 sa.encrypt(IP(src=src_ip, dst=dst_ip) /
84 ICMP(id=self.icmp_id_out, type='echo-request'))
85 ]
86
Klement Sekera4b089f22018-04-17 18:04:57 +020087 def verify_capture_plain(self, capture):
88 for packet in capture:
89 try:
Klement Sekerad81ae412018-05-16 10:52:54 +020090 self.assert_packet_checksums_valid(packet)
Klement Sekera4b089f22018-04-17 18:04:57 +020091 self.assert_equal(packet[IP].src, self.pg0.remote_ip4,
92 "decrypted packet source address")
93 self.assert_equal(packet[IP].dst, self.pg1.remote_ip4,
94 "decrypted packet destination address")
95 if packet.haslayer(TCP):
96 self.assertFalse(
97 packet.haslayer(UDP),
98 "unexpected UDP header in decrypted packet")
99 self.assert_equal(packet[TCP].dport, self.tcp_port_in,
100 "decrypted packet TCP destination port")
Klement Sekera4b089f22018-04-17 18:04:57 +0200101 elif packet.haslayer(UDP):
102 if packet[UDP].payload:
103 self.assertFalse(
104 packet[UDP][1].haslayer(UDP),
105 "unexpected UDP header in decrypted packet")
106 self.assert_equal(packet[UDP].dport, self.udp_port_in,
107 "decrypted packet UDP destination port")
108 else:
109 self.assertFalse(
110 packet.haslayer(UDP),
111 "unexpected UDP header in decrypted packet")
112 self.assert_equal(packet[ICMP].id, self.icmp_id_in,
113 "decrypted packet ICMP ID")
Klement Sekera4b089f22018-04-17 18:04:57 +0200114 except Exception:
115 self.logger.error(
116 ppp("Unexpected or invalid plain packet:", packet))
117 raise
118
119 def verify_capture_encrypted(self, capture, sa):
120 for packet in capture:
121 try:
122 self.assertIn(ESP, packet[IP])
123 decrypt_pkt = sa.decrypt(packet[IP])
124 self.assert_equal(decrypt_pkt[IP].src, self.pg1.remote_ip4,
125 "encrypted packet source address")
126 self.assert_equal(decrypt_pkt[IP].dst, self.pg0.remote_ip4,
127 "encrypted packet destination address")
128 # if decrypt_pkt.haslayer(TCP):
129 # self.tcp_port_out = decrypt_pkt[TCP].sport
130 # elif decrypt_pkt.haslayer(UDP):
131 # self.udp_port_out = decrypt_pkt[UDP].sport
132 # else:
133 # self.icmp_id_out = decrypt_pkt[ICMP].id
134 except Exception:
135 self.logger.error(
136 ppp("Unexpected or invalid encrypted packet:", packet))
137 raise
138
139 @classmethod
140 def config_esp_tun(cls):
141 spd_id = 1
142 remote_sa_id = 10
143 local_sa_id = 20
Ole Troane0d2bd62018-06-22 22:36:46 +0200144 remote_tun_spi = 1001
145 local_tun_spi = 1000
Klement Sekera4b089f22018-04-17 18:04:57 +0200146 client = socket.inet_pton(socket.AF_INET, cls.remote_pg0_client_addr)
147 cls.vapi.ip_add_del_route(client, 32, cls.pg0.remote_ip4n)
Ole Troane0d2bd62018-06-22 22:36:46 +0200148 cls.vapi.ipsec_sad_add_del_entry(remote_sa_id, remote_tun_spi,
Klement Sekera4b089f22018-04-17 18:04:57 +0200149 cls.pg1.remote_ip4n,
150 cls.pg0.remote_ip4n,
151 integrity_key_length=20,
152 crypto_key_length=16,
153 protocol=1, udp_encap=1)
Ole Troane0d2bd62018-06-22 22:36:46 +0200154 cls.vapi.ipsec_sad_add_del_entry(local_sa_id, local_tun_spi,
Klement Sekera4b089f22018-04-17 18:04:57 +0200155 cls.pg0.remote_ip4n,
156 cls.pg1.remote_ip4n,
157 integrity_key_length=20,
158 crypto_key_length=16,
159 protocol=1, udp_encap=1)
160 cls.vapi.ipsec_spd_add_del(spd_id)
161 cls.vapi.ipsec_interface_add_del_spd(spd_id, cls.pg0.sw_if_index)
162 l_startaddr = r_startaddr = socket.inet_pton(socket.AF_INET,
163 "0.0.0.0")
164 l_stopaddr = r_stopaddr = socket.inet_pton(socket.AF_INET,
165 "255.255.255.255")
166 cls.vapi.ipsec_spd_add_del_entry(spd_id, l_startaddr, l_stopaddr,
167 r_startaddr, r_stopaddr,
168 protocol=socket.IPPROTO_ESP)
169 cls.vapi.ipsec_spd_add_del_entry(spd_id, l_startaddr, l_stopaddr,
170 r_startaddr, r_stopaddr,
171 protocol=socket.IPPROTO_ESP,
172 is_outbound=0)
173 cls.vapi.ipsec_spd_add_del_entry(spd_id, l_startaddr, l_stopaddr,
174 r_startaddr, r_stopaddr,
175 remote_port_start=4500,
176 remote_port_stop=4500,
177 protocol=socket.IPPROTO_UDP)
178 cls.vapi.ipsec_spd_add_del_entry(spd_id, l_startaddr, l_stopaddr,
179 r_startaddr, r_stopaddr,
180 remote_port_start=4500,
181 remote_port_stop=4500,
182 protocol=socket.IPPROTO_UDP,
183 is_outbound=0)
184 l_startaddr = l_stopaddr = cls.pg0.remote_ip4n
185 r_startaddr = r_stopaddr = cls.pg1.remote_ip4n
186 cls.vapi.ipsec_spd_add_del_entry(spd_id, l_startaddr, l_stopaddr,
187 r_startaddr, r_stopaddr,
188 priority=10, policy=3,
189 is_outbound=0, sa_id=local_sa_id)
190 cls.vapi.ipsec_spd_add_del_entry(spd_id, r_startaddr, r_stopaddr,
191 l_startaddr, l_stopaddr,
192 priority=10, policy=3,
193 sa_id=remote_sa_id)
194
195 def test_ipsec_nat_tun(self):
196 """ IPSec/NAT tunnel test case """
197 local_tun_sa = SecurityAssociation(ESP, spi=0x000003e9,
198 crypt_algo='AES-CBC',
199 crypt_key='JPjyOWBeVEQiMe7h',
200 auth_algo='HMAC-SHA1-96',
201 auth_key='C91KUR9GYMm5GfkEvNjX',
202 tunnel_header=IP(
203 src=self.pg1.remote_ip4,
204 dst=self.pg0.remote_ip4),
205 nat_t_header=UDP(
206 sport=4500,
207 dport=4500))
208 # in2out - from private network to public
209 pkts = self.create_stream_plain(
210 self.pg1.remote_mac, self.pg1.local_mac,
211 self.pg1.remote_ip4, self.pg0.remote_ip4)
212 self.pg1.add_stream(pkts)
213 self.pg_enable_capture(self.pg_interfaces)
214 self.pg_start()
215 capture = self.pg0.get_capture(len(pkts))
216 self.verify_capture_encrypted(capture, local_tun_sa)
217
218 remote_tun_sa = SecurityAssociation(ESP, spi=0x000003e8,
219 crypt_algo='AES-CBC',
220 crypt_key='JPjyOWBeVEQiMe7h',
221 auth_algo='HMAC-SHA1-96',
222 auth_key='C91KUR9GYMm5GfkEvNjX',
223 tunnel_header=IP(
224 src=self.pg0.remote_ip4,
225 dst=self.pg1.remote_ip4),
226 nat_t_header=UDP(
227 sport=4500,
228 dport=4500))
229
230 # out2in - from public network to private
231 pkts = self.create_stream_encrypted(
232 self.pg0.remote_mac, self.pg0.local_mac,
233 self.pg0.remote_ip4, self.pg1.remote_ip4, remote_tun_sa)
234 self.logger.info(ppc("Sending packets:", pkts))
235 self.pg0.add_stream(pkts)
236 self.pg_enable_capture(self.pg_interfaces)
237 self.pg_start()
238 capture = self.pg1.get_capture(len(pkts))
239 self.verify_capture_plain(capture)