blob: bf13d71f631de77e09e90c8b74a08a1d3c52dbe0 [file] [log] [blame]
Klement Sekera31da2e32018-06-24 22:49:55 +02001import unittest
Klement Sekera611864f2018-09-26 11:19:00 +02002import socket
Klement Sekera31da2e32018-06-24 22:49:55 +02003
4from scapy.layers.inet import IP, ICMP, TCP
5from scapy.layers.ipsec import SecurityAssociation
6from scapy.layers.l2 import Ether, Raw
Klement Sekera611864f2018-09-26 11:19:00 +02007from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest
Klement Sekera31da2e32018-06-24 22:49:55 +02008
9from framework import VppTestCase, VppTestRunner
10from util import ppp
11
12
Klement Sekera611864f2018-09-26 11:19:00 +020013class IPsecIPv4Params(object):
14 addr_type = socket.AF_INET
15 addr_any = "0.0.0.0"
16 addr_bcast = "255.255.255.255"
17 addr_len = 32
18 is_ipv6 = 0
19 remote_tun_if_host = '1.1.1.1'
20
21 scapy_tun_sa_id = 10
22 scapy_tun_spi = 1001
23 vpp_tun_sa_id = 20
24 vpp_tun_spi = 1000
25
26 scapy_tra_sa_id = 30
27 scapy_tra_spi = 2001
28 vpp_tra_sa_id = 40
29 vpp_tra_spi = 2000
30
31 auth_algo_vpp_id = 2 # internal VPP enum value for SHA1_96
32 auth_algo = 'HMAC-SHA1-96' # scapy name
33 auth_key = 'C91KUR9GYMm5GfkEvNjX'
34
35 crypt_algo_vpp_id = 1 # internal VPP enum value for AES_CBC_128
36 crypt_algo = 'AES-CBC' # scapy name
37 crypt_key = 'JPjyOWBeVEQiMe7h'
38
39
40class IPsecIPv6Params(object):
41 addr_type = socket.AF_INET6
42 addr_any = "0::0"
43 addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
44 addr_len = 128
45 is_ipv6 = 1
46 remote_tun_if_host = '1111:1111:1111:1111:1111:1111:1111:1111'
47
48 scapy_tun_sa_id = 50
49 scapy_tun_spi = 3001
50 vpp_tun_sa_id = 60
51 vpp_tun_spi = 3000
52
53 scapy_tra_sa_id = 70
54 scapy_tra_spi = 4001
55 vpp_tra_sa_id = 80
56 vpp_tra_spi = 4000
57
58 auth_algo_vpp_id = 4 # internal VPP enum value for SHA_256_128
59 auth_algo = 'SHA2-256-128' # scapy name
60 auth_key = 'C91KUR9GYMm5GfkEvNjX'
61
62 crypt_algo_vpp_id = 3 # internal VPP enum value for AES_CBC_256
63 crypt_algo = 'AES-CBC' # scapy name
64 crypt_key = 'JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h'
65
66
Klement Sekera31da2e32018-06-24 22:49:55 +020067class TemplateIpsec(VppTestCase):
68 """
69 TRANSPORT MODE:
70
71 ------ encrypt ---
72 |tra_if| <-------> |VPP|
73 ------ decrypt ---
74
75 TUNNEL MODE:
76
77 ------ encrypt --- plain ---
78 |tun_if| <------- |VPP| <------ |pg1|
79 ------ --- ---
80
81 ------ decrypt --- plain ---
82 |tun_if| -------> |VPP| ------> |pg1|
83 ------ --- ---
84 """
Klement Sekera611864f2018-09-26 11:19:00 +020085 ipv4_params = IPsecIPv4Params()
86 ipv6_params = IPsecIPv6Params()
87 params = {ipv4_params.addr_type: ipv4_params,
88 ipv6_params.addr_type: ipv6_params}
Klement Sekera31da2e32018-06-24 22:49:55 +020089
Klement Sekera31da2e32018-06-24 22:49:55 +020090 payload = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
91
92 tun_spd_id = 1
Klement Sekera31da2e32018-06-24 22:49:55 +020093 tra_spd_id = 2
Klement Sekera31da2e32018-06-24 22:49:55 +020094
95 vpp_esp_protocol = 1
96 vpp_ah_protocol = 0
97
Klement Sekera31da2e32018-06-24 22:49:55 +020098 @classmethod
99 def setUpClass(cls):
100 super(TemplateIpsec, cls).setUpClass()
101 cls.create_pg_interfaces(range(3))
102 cls.interfaces = list(cls.pg_interfaces)
103 for i in cls.interfaces:
104 i.admin_up()
105 i.config_ip4()
106 i.resolve_arp()
Klement Sekera611864f2018-09-26 11:19:00 +0200107 i.config_ip6()
108 i.resolve_ndp()
Klement Sekera31da2e32018-06-24 22:49:55 +0200109
110 def tearDown(self):
111 super(TemplateIpsec, self).tearDown()
112 if not self.vpp_dead:
113 self.vapi.cli("show hardware")
114
Klement Sekera31da2e32018-06-24 22:49:55 +0200115 def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1):
116 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
117 sa.encrypt(IP(src=src, dst=dst) / ICMP() / self.payload)
118 for i in range(count)]
119
Klement Sekera611864f2018-09-26 11:19:00 +0200120 def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1):
121 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
122 sa.encrypt(IPv6(src=src, dst=dst) /
123 ICMPv6EchoRequest(id=0, seq=1, data=self.payload))
124 for i in range(count)]
125
Klement Sekera31da2e32018-06-24 22:49:55 +0200126 def gen_pkts(self, sw_intf, src, dst, count=1):
127 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
128 IP(src=src, dst=dst) / ICMP() / self.payload
129 for i in range(count)]
130
Klement Sekera611864f2018-09-26 11:19:00 +0200131 def gen_pkts6(self, sw_intf, src, dst, count=1):
132 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
133 IPv6(src=src, dst=dst) /
134 ICMPv6EchoRequest(id=0, seq=1, data=self.payload)
135 for i in range(count)]
136
137 def configure_sa_tun(self, params):
138 ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
139 scapy_tun_sa = SecurityAssociation(
140 self.encryption_type, spi=params.vpp_tun_spi,
141 crypt_algo=params.crypt_algo, crypt_key=params.crypt_key,
142 auth_algo=params.auth_algo, auth_key=params.auth_key,
143 tunnel_header=ip_class_by_addr_type[params.addr_type](
144 src=self.tun_if.remote_addr[params.addr_type],
145 dst=self.tun_if.local_addr[params.addr_type]))
146 vpp_tun_sa = SecurityAssociation(
147 self.encryption_type, spi=params.scapy_tun_spi,
148 crypt_algo=params.crypt_algo, crypt_key=params.crypt_key,
149 auth_algo=params.auth_algo, auth_key=params.auth_key,
150 tunnel_header=ip_class_by_addr_type[params.addr_type](
151 dst=self.tun_if.remote_addr[params.addr_type],
152 src=self.tun_if.local_addr[params.addr_type]))
Klement Sekera31da2e32018-06-24 22:49:55 +0200153 return vpp_tun_sa, scapy_tun_sa
154
Klement Sekera611864f2018-09-26 11:19:00 +0200155 def configure_sa_tra(self, params):
Klement Sekera31da2e32018-06-24 22:49:55 +0200156 scapy_tra_sa = SecurityAssociation(self.encryption_type,
Klement Sekera611864f2018-09-26 11:19:00 +0200157 spi=params.vpp_tra_spi,
158 crypt_algo=params.crypt_algo,
159 crypt_key=params.crypt_key,
160 auth_algo=params.auth_algo,
161 auth_key=params.auth_key)
Klement Sekera31da2e32018-06-24 22:49:55 +0200162 vpp_tra_sa = SecurityAssociation(self.encryption_type,
Klement Sekera611864f2018-09-26 11:19:00 +0200163 spi=params.scapy_tra_spi,
164 crypt_algo=params.crypt_algo,
165 crypt_key=params.crypt_key,
166 auth_algo=params.auth_algo,
167 auth_key=params.auth_key)
Klement Sekera31da2e32018-06-24 22:49:55 +0200168 return vpp_tra_sa, scapy_tra_sa
169
170
171class IpsecTcpTests(object):
172 def test_tcp_checksum(self):
173 """ verify checksum correctness for vpp generated packets """
174 self.vapi.cli("test http server")
Klement Sekera611864f2018-09-26 11:19:00 +0200175 p = self.params[socket.AF_INET]
176 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p)
Klement Sekera31da2e32018-06-24 22:49:55 +0200177 send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
Klement Sekera611864f2018-09-26 11:19:00 +0200178 scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200179 dst=self.tun_if.local_ip4) /
180 TCP(flags='S', dport=80)))
181 self.logger.debug(ppp("Sending packet:", send))
Klement Sekera611864f2018-09-26 11:19:00 +0200182 recv = self.send_and_expect(self.tun_if, [send], self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200183 recv = recv[0]
184 decrypted = vpp_tun_sa.decrypt(recv[IP])
185 self.assert_packet_checksums_valid(decrypted)
186
187
188class IpsecTraTests(object):
189 def test_tra_basic(self, count=1):
190 """ ipsec v4 transport basic test """
191 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200192 p = self.params[socket.AF_INET]
193 vpp_tra_sa, scapy_tra_sa = self.configure_sa_tra(p)
Klement Sekera31da2e32018-06-24 22:49:55 +0200194 send_pkts = self.gen_encrypt_pkts(scapy_tra_sa, self.tra_if,
195 src=self.tra_if.remote_ip4,
196 dst=self.tra_if.local_ip4,
197 count=count)
198 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
Klement Sekera611864f2018-09-26 11:19:00 +0200199 self.tra_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200200 for p in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200201 try:
202 decrypted = vpp_tra_sa.decrypt(p[IP])
203 self.assert_packet_checksums_valid(decrypted)
204 except:
205 self.logger.debug(ppp("Unexpected packet:", p))
206 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200207 finally:
208 self.logger.info(self.vapi.ppcli("show error"))
209 self.logger.info(self.vapi.ppcli("show ipsec"))
210
211 def test_tra_burst(self):
212 """ ipsec v4 transport burst test """
Klement Sekera611864f2018-09-26 11:19:00 +0200213 self.test_tra_basic(count=257)
214
215 def test_tra_basic6(self, count=1):
216 """ ipsec v6 transport basic test """
Klement Sekera31da2e32018-06-24 22:49:55 +0200217 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200218 p = self.params[socket.AF_INET6]
219 vpp_tra_sa, scapy_tra_sa = self.configure_sa_tra(p)
220 send_pkts = self.gen_encrypt_pkts6(scapy_tra_sa, self.tra_if,
221 src=self.tra_if.remote_ip6,
222 dst=self.tra_if.local_ip6,
223 count=count)
224 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
225 self.tra_if)
226 for p in recv_pkts:
227 try:
228 decrypted = vpp_tra_sa.decrypt(p[IPv6])
229 self.assert_packet_checksums_valid(decrypted)
230 except:
231 self.logger.debug(ppp("Unexpected packet:", p))
232 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200233 finally:
234 self.logger.info(self.vapi.ppcli("show error"))
235 self.logger.info(self.vapi.ppcli("show ipsec"))
236
Klement Sekera611864f2018-09-26 11:19:00 +0200237 def test_tra_burst6(self):
238 """ ipsec v6 transport burst test """
239 self.test_tra_basic6(count=257)
Klement Sekera31da2e32018-06-24 22:49:55 +0200240
Klement Sekera611864f2018-09-26 11:19:00 +0200241
242class IpsecTun4Tests(object):
243 def test_tun_basic44(self, count=1):
Klement Sekera31da2e32018-06-24 22:49:55 +0200244 """ ipsec 4o4 tunnel basic test """
245 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200246 p = self.params[socket.AF_INET]
247 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p)
Klement Sekera31da2e32018-06-24 22:49:55 +0200248 send_pkts = self.gen_encrypt_pkts(scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200249 src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200250 dst=self.pg1.remote_ip4,
251 count=count)
Klement Sekera611864f2018-09-26 11:19:00 +0200252 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Klement Sekera31da2e32018-06-24 22:49:55 +0200253 for recv_pkt in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200254 self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host)
Klement Sekera31da2e32018-06-24 22:49:55 +0200255 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
256 self.assert_packet_checksums_valid(recv_pkt)
257 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
Klement Sekera611864f2018-09-26 11:19:00 +0200258 dst=p.remote_tun_if_host, count=count)
259 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200260 for recv_pkt in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200261 try:
262 decrypt_pkt = vpp_tun_sa.decrypt(recv_pkt[IP])
263 if not decrypt_pkt.haslayer(IP):
264 decrypt_pkt = IP(decrypt_pkt[Raw].load)
265 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
266 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
267 self.assert_packet_checksums_valid(decrypt_pkt)
268 except:
269 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
270 try:
271 self.logger.debug(
272 ppp("Decrypted packet:", decrypt_pkt))
273 except:
274 pass
275 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200276 finally:
277 self.logger.info(self.vapi.ppcli("show error"))
278 self.logger.info(self.vapi.ppcli("show ipsec"))
279
Klement Sekera611864f2018-09-26 11:19:00 +0200280 def test_tun_burst44(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200281 """ ipsec 4o4 tunnel burst test """
Klement Sekera611864f2018-09-26 11:19:00 +0200282 self.test_tun_basic44(count=257)
283
284
285class IpsecTun6Tests(object):
286 def test_tun_basic66(self, count=1):
287 """ ipsec 6o6 tunnel basic test """
Klement Sekera31da2e32018-06-24 22:49:55 +0200288 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200289 p = self.params[socket.AF_INET6]
290 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p)
291 send_pkts = self.gen_encrypt_pkts6(scapy_tun_sa, self.tun_if,
292 src=p.remote_tun_if_host,
293 dst=self.pg1.remote_ip6,
294 count=count)
295 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
296 for recv_pkt in recv_pkts:
297 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host)
298 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
299 self.assert_packet_checksums_valid(recv_pkt)
300 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
301 dst=p.remote_tun_if_host,
302 count=count)
303 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
304 for recv_pkt in recv_pkts:
305 try:
306 decrypt_pkt = vpp_tun_sa.decrypt(recv_pkt[IPv6])
307 if not decrypt_pkt.haslayer(IPv6):
308 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
309 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
310 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
311 self.assert_packet_checksums_valid(decrypt_pkt)
312 except:
313 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
314 try:
315 self.logger.debug(
316 ppp("Decrypted packet:", decrypt_pkt))
317 except:
318 pass
319 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200320 finally:
321 self.logger.info(self.vapi.ppcli("show error"))
322 self.logger.info(self.vapi.ppcli("show ipsec"))
323
Klement Sekera611864f2018-09-26 11:19:00 +0200324 def test_tun_burst66(self):
325 """ ipsec 6o6 tunnel burst test """
326 self.test_tun_basic66(count=257)
327
328
329class IpsecTunTests(IpsecTun4Tests, IpsecTun6Tests):
330 pass
331
Klement Sekera31da2e32018-06-24 22:49:55 +0200332
333if __name__ == '__main__':
334 unittest.main(testRunner=VppTestRunner)