blob: 9c22fbb559ac95551aa0e6e188fa28f5bcad5912 [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
87 def check_checksum(self, pkt, layer):
88 """ Check checksum of the packet on given layer """
89 new = pkt.__class__(str(pkt))
90 del new[layer].chksum
91 new = new.__class__(str(new))
92 self.assertEqual(new[layer].chksum, pkt[layer].chksum)
93
94 def check_ip_checksum(self, pkt):
95 return self.check_checksum(pkt, 'IP')
96
97 def check_tcp_checksum(self, pkt):
98 return self.check_checksum(pkt, 'TCP')
99
100 def check_udp_checksum(self, pkt):
101 return self.check_checksum(pkt, 'UDP')
102
103 def check_icmp_checksum(self, pkt):
104 return self.check_checksum(pkt, 'ICMP')
105
106 def verify_capture_plain(self, capture):
107 for packet in capture:
108 try:
109 self.check_ip_checksum(packet)
110 self.assert_equal(packet[IP].src, self.pg0.remote_ip4,
111 "decrypted packet source address")
112 self.assert_equal(packet[IP].dst, self.pg1.remote_ip4,
113 "decrypted packet destination address")
114 if packet.haslayer(TCP):
115 self.assertFalse(
116 packet.haslayer(UDP),
117 "unexpected UDP header in decrypted packet")
118 self.assert_equal(packet[TCP].dport, self.tcp_port_in,
119 "decrypted packet TCP destination port")
120 self.check_tcp_checksum(packet)
121 elif packet.haslayer(UDP):
122 if packet[UDP].payload:
123 self.assertFalse(
124 packet[UDP][1].haslayer(UDP),
125 "unexpected UDP header in decrypted packet")
126 self.assert_equal(packet[UDP].dport, self.udp_port_in,
127 "decrypted packet UDP destination port")
128 else:
129 self.assertFalse(
130 packet.haslayer(UDP),
131 "unexpected UDP header in decrypted packet")
132 self.assert_equal(packet[ICMP].id, self.icmp_id_in,
133 "decrypted packet ICMP ID")
134 self.check_icmp_checksum(packet)
135 except Exception:
136 self.logger.error(
137 ppp("Unexpected or invalid plain packet:", packet))
138 raise
139
140 def verify_capture_encrypted(self, capture, sa):
141 for packet in capture:
142 try:
143 self.assertIn(ESP, packet[IP])
144 decrypt_pkt = sa.decrypt(packet[IP])
145 self.assert_equal(decrypt_pkt[IP].src, self.pg1.remote_ip4,
146 "encrypted packet source address")
147 self.assert_equal(decrypt_pkt[IP].dst, self.pg0.remote_ip4,
148 "encrypted packet destination address")
149 # if decrypt_pkt.haslayer(TCP):
150 # self.tcp_port_out = decrypt_pkt[TCP].sport
151 # elif decrypt_pkt.haslayer(UDP):
152 # self.udp_port_out = decrypt_pkt[UDP].sport
153 # else:
154 # self.icmp_id_out = decrypt_pkt[ICMP].id
155 except Exception:
156 self.logger.error(
157 ppp("Unexpected or invalid encrypted packet:", packet))
158 raise
159
160 @classmethod
161 def config_esp_tun(cls):
162 spd_id = 1
163 remote_sa_id = 10
164 local_sa_id = 20
165 remote_tun_spi = 1001
166 local_tun_spi = 1000
167 client = socket.inet_pton(socket.AF_INET, cls.remote_pg0_client_addr)
168 cls.vapi.ip_add_del_route(client, 32, cls.pg0.remote_ip4n)
169 cls.vapi.ipsec_sad_add_del_entry(remote_sa_id, remote_tun_spi,
170 cls.pg1.remote_ip4n,
171 cls.pg0.remote_ip4n,
172 integrity_key_length=20,
173 crypto_key_length=16,
174 protocol=1, udp_encap=1)
175 cls.vapi.ipsec_sad_add_del_entry(local_sa_id, local_tun_spi,
176 cls.pg0.remote_ip4n,
177 cls.pg1.remote_ip4n,
178 integrity_key_length=20,
179 crypto_key_length=16,
180 protocol=1, udp_encap=1)
181 cls.vapi.ipsec_spd_add_del(spd_id)
182 cls.vapi.ipsec_interface_add_del_spd(spd_id, cls.pg0.sw_if_index)
183 l_startaddr = r_startaddr = socket.inet_pton(socket.AF_INET,
184 "0.0.0.0")
185 l_stopaddr = r_stopaddr = socket.inet_pton(socket.AF_INET,
186 "255.255.255.255")
187 cls.vapi.ipsec_spd_add_del_entry(spd_id, l_startaddr, l_stopaddr,
188 r_startaddr, r_stopaddr,
189 protocol=socket.IPPROTO_ESP)
190 cls.vapi.ipsec_spd_add_del_entry(spd_id, l_startaddr, l_stopaddr,
191 r_startaddr, r_stopaddr,
192 protocol=socket.IPPROTO_ESP,
193 is_outbound=0)
194 cls.vapi.ipsec_spd_add_del_entry(spd_id, l_startaddr, l_stopaddr,
195 r_startaddr, r_stopaddr,
196 remote_port_start=4500,
197 remote_port_stop=4500,
198 protocol=socket.IPPROTO_UDP)
199 cls.vapi.ipsec_spd_add_del_entry(spd_id, l_startaddr, l_stopaddr,
200 r_startaddr, r_stopaddr,
201 remote_port_start=4500,
202 remote_port_stop=4500,
203 protocol=socket.IPPROTO_UDP,
204 is_outbound=0)
205 l_startaddr = l_stopaddr = cls.pg0.remote_ip4n
206 r_startaddr = r_stopaddr = cls.pg1.remote_ip4n
207 cls.vapi.ipsec_spd_add_del_entry(spd_id, l_startaddr, l_stopaddr,
208 r_startaddr, r_stopaddr,
209 priority=10, policy=3,
210 is_outbound=0, sa_id=local_sa_id)
211 cls.vapi.ipsec_spd_add_del_entry(spd_id, r_startaddr, r_stopaddr,
212 l_startaddr, l_stopaddr,
213 priority=10, policy=3,
214 sa_id=remote_sa_id)
215
216 def test_ipsec_nat_tun(self):
217 """ IPSec/NAT tunnel test case """
218 local_tun_sa = SecurityAssociation(ESP, spi=0x000003e9,
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.pg1.remote_ip4,
225 dst=self.pg0.remote_ip4),
226 nat_t_header=UDP(
227 sport=4500,
228 dport=4500))
229 # in2out - from private network to public
230 pkts = self.create_stream_plain(
231 self.pg1.remote_mac, self.pg1.local_mac,
232 self.pg1.remote_ip4, self.pg0.remote_ip4)
233 self.pg1.add_stream(pkts)
234 self.pg_enable_capture(self.pg_interfaces)
235 self.pg_start()
236 capture = self.pg0.get_capture(len(pkts))
237 self.verify_capture_encrypted(capture, local_tun_sa)
238
239 remote_tun_sa = SecurityAssociation(ESP, spi=0x000003e8,
240 crypt_algo='AES-CBC',
241 crypt_key='JPjyOWBeVEQiMe7h',
242 auth_algo='HMAC-SHA1-96',
243 auth_key='C91KUR9GYMm5GfkEvNjX',
244 tunnel_header=IP(
245 src=self.pg0.remote_ip4,
246 dst=self.pg1.remote_ip4),
247 nat_t_header=UDP(
248 sport=4500,
249 dport=4500))
250
251 # out2in - from public network to private
252 pkts = self.create_stream_encrypted(
253 self.pg0.remote_mac, self.pg0.local_mac,
254 self.pg0.remote_ip4, self.pg1.remote_ip4, remote_tun_sa)
255 self.logger.info(ppc("Sending packets:", pkts))
256 self.pg0.add_stream(pkts)
257 self.pg_enable_capture(self.pg_interfaces)
258 self.pg_start()
259 capture = self.pg1.get_capture(len(pkts))
260 self.verify_capture_plain(capture)