blob: bb45696d21c44e8fb8dd44541e7d5805f0acc91b [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
Klement Sekerab4d30532018-11-08 13:00:02 +010099 def ipsec_select_backend(cls):
100 """ empty method to be overloaded when necessary """
101 pass
102
103 @classmethod
Klement Sekera31da2e32018-06-24 22:49:55 +0200104 def setUpClass(cls):
105 super(TemplateIpsec, cls).setUpClass()
106 cls.create_pg_interfaces(range(3))
107 cls.interfaces = list(cls.pg_interfaces)
108 for i in cls.interfaces:
109 i.admin_up()
110 i.config_ip4()
111 i.resolve_arp()
Klement Sekera611864f2018-09-26 11:19:00 +0200112 i.config_ip6()
113 i.resolve_ndp()
Klement Sekerab4d30532018-11-08 13:00:02 +0100114 cls.ipsec_select_backend()
Klement Sekera31da2e32018-06-24 22:49:55 +0200115
116 def tearDown(self):
117 super(TemplateIpsec, self).tearDown()
118 if not self.vpp_dead:
119 self.vapi.cli("show hardware")
120
Klement Sekera31da2e32018-06-24 22:49:55 +0200121 def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1):
122 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
123 sa.encrypt(IP(src=src, dst=dst) / ICMP() / self.payload)
124 for i in range(count)]
125
Klement Sekera611864f2018-09-26 11:19:00 +0200126 def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1):
127 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
128 sa.encrypt(IPv6(src=src, dst=dst) /
129 ICMPv6EchoRequest(id=0, seq=1, data=self.payload))
130 for i in range(count)]
131
Klement Sekera31da2e32018-06-24 22:49:55 +0200132 def gen_pkts(self, sw_intf, src, dst, count=1):
133 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
134 IP(src=src, dst=dst) / ICMP() / self.payload
135 for i in range(count)]
136
Klement Sekera611864f2018-09-26 11:19:00 +0200137 def gen_pkts6(self, sw_intf, src, dst, count=1):
138 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
139 IPv6(src=src, dst=dst) /
140 ICMPv6EchoRequest(id=0, seq=1, data=self.payload)
141 for i in range(count)]
142
143 def configure_sa_tun(self, params):
144 ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
145 scapy_tun_sa = SecurityAssociation(
146 self.encryption_type, spi=params.vpp_tun_spi,
147 crypt_algo=params.crypt_algo, crypt_key=params.crypt_key,
148 auth_algo=params.auth_algo, auth_key=params.auth_key,
149 tunnel_header=ip_class_by_addr_type[params.addr_type](
150 src=self.tun_if.remote_addr[params.addr_type],
151 dst=self.tun_if.local_addr[params.addr_type]))
152 vpp_tun_sa = SecurityAssociation(
153 self.encryption_type, spi=params.scapy_tun_spi,
154 crypt_algo=params.crypt_algo, crypt_key=params.crypt_key,
155 auth_algo=params.auth_algo, auth_key=params.auth_key,
156 tunnel_header=ip_class_by_addr_type[params.addr_type](
157 dst=self.tun_if.remote_addr[params.addr_type],
158 src=self.tun_if.local_addr[params.addr_type]))
Klement Sekera31da2e32018-06-24 22:49:55 +0200159 return vpp_tun_sa, scapy_tun_sa
160
Klement Sekera611864f2018-09-26 11:19:00 +0200161 def configure_sa_tra(self, params):
Klement Sekera31da2e32018-06-24 22:49:55 +0200162 scapy_tra_sa = SecurityAssociation(self.encryption_type,
Klement Sekera611864f2018-09-26 11:19:00 +0200163 spi=params.vpp_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 vpp_tra_sa = SecurityAssociation(self.encryption_type,
Klement Sekera611864f2018-09-26 11:19:00 +0200169 spi=params.scapy_tra_spi,
170 crypt_algo=params.crypt_algo,
171 crypt_key=params.crypt_key,
172 auth_algo=params.auth_algo,
173 auth_key=params.auth_key)
Klement Sekera31da2e32018-06-24 22:49:55 +0200174 return vpp_tra_sa, scapy_tra_sa
175
176
177class IpsecTcpTests(object):
178 def test_tcp_checksum(self):
179 """ verify checksum correctness for vpp generated packets """
180 self.vapi.cli("test http server")
Klement Sekera611864f2018-09-26 11:19:00 +0200181 p = self.params[socket.AF_INET]
182 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p)
Klement Sekera31da2e32018-06-24 22:49:55 +0200183 send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
Klement Sekera611864f2018-09-26 11:19:00 +0200184 scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200185 dst=self.tun_if.local_ip4) /
186 TCP(flags='S', dport=80)))
187 self.logger.debug(ppp("Sending packet:", send))
Klement Sekera611864f2018-09-26 11:19:00 +0200188 recv = self.send_and_expect(self.tun_if, [send], self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200189 recv = recv[0]
190 decrypted = vpp_tun_sa.decrypt(recv[IP])
191 self.assert_packet_checksums_valid(decrypted)
192
193
194class IpsecTraTests(object):
195 def test_tra_basic(self, count=1):
196 """ ipsec v4 transport basic test """
197 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200198 p = self.params[socket.AF_INET]
199 vpp_tra_sa, scapy_tra_sa = self.configure_sa_tra(p)
Klement Sekera31da2e32018-06-24 22:49:55 +0200200 send_pkts = self.gen_encrypt_pkts(scapy_tra_sa, self.tra_if,
201 src=self.tra_if.remote_ip4,
202 dst=self.tra_if.local_ip4,
203 count=count)
204 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
Klement Sekera611864f2018-09-26 11:19:00 +0200205 self.tra_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200206 for p in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200207 try:
208 decrypted = vpp_tra_sa.decrypt(p[IP])
209 self.assert_packet_checksums_valid(decrypted)
210 except:
211 self.logger.debug(ppp("Unexpected packet:", p))
212 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200213 finally:
214 self.logger.info(self.vapi.ppcli("show error"))
215 self.logger.info(self.vapi.ppcli("show ipsec"))
216
217 def test_tra_burst(self):
218 """ ipsec v4 transport burst test """
Klement Sekera611864f2018-09-26 11:19:00 +0200219 self.test_tra_basic(count=257)
220
221 def test_tra_basic6(self, count=1):
222 """ ipsec v6 transport basic test """
Klement Sekera31da2e32018-06-24 22:49:55 +0200223 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200224 p = self.params[socket.AF_INET6]
225 vpp_tra_sa, scapy_tra_sa = self.configure_sa_tra(p)
226 send_pkts = self.gen_encrypt_pkts6(scapy_tra_sa, self.tra_if,
227 src=self.tra_if.remote_ip6,
228 dst=self.tra_if.local_ip6,
229 count=count)
230 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
231 self.tra_if)
232 for p in recv_pkts:
233 try:
234 decrypted = vpp_tra_sa.decrypt(p[IPv6])
235 self.assert_packet_checksums_valid(decrypted)
236 except:
237 self.logger.debug(ppp("Unexpected packet:", p))
238 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200239 finally:
240 self.logger.info(self.vapi.ppcli("show error"))
241 self.logger.info(self.vapi.ppcli("show ipsec"))
242
Klement Sekera611864f2018-09-26 11:19:00 +0200243 def test_tra_burst6(self):
244 """ ipsec v6 transport burst test """
245 self.test_tra_basic6(count=257)
Klement Sekera31da2e32018-06-24 22:49:55 +0200246
Klement Sekera611864f2018-09-26 11:19:00 +0200247
248class IpsecTun4Tests(object):
249 def test_tun_basic44(self, count=1):
Klement Sekera31da2e32018-06-24 22:49:55 +0200250 """ ipsec 4o4 tunnel basic test """
251 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200252 p = self.params[socket.AF_INET]
253 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p)
Klement Sekera31da2e32018-06-24 22:49:55 +0200254 send_pkts = self.gen_encrypt_pkts(scapy_tun_sa, self.tun_if,
Klement Sekera611864f2018-09-26 11:19:00 +0200255 src=p.remote_tun_if_host,
Klement Sekera31da2e32018-06-24 22:49:55 +0200256 dst=self.pg1.remote_ip4,
257 count=count)
Klement Sekera611864f2018-09-26 11:19:00 +0200258 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
Klement Sekera31da2e32018-06-24 22:49:55 +0200259 for recv_pkt in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200260 self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host)
Klement Sekera31da2e32018-06-24 22:49:55 +0200261 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
262 self.assert_packet_checksums_valid(recv_pkt)
263 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
Klement Sekera611864f2018-09-26 11:19:00 +0200264 dst=p.remote_tun_if_host, count=count)
265 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
Klement Sekera31da2e32018-06-24 22:49:55 +0200266 for recv_pkt in recv_pkts:
Klement Sekera611864f2018-09-26 11:19:00 +0200267 try:
268 decrypt_pkt = vpp_tun_sa.decrypt(recv_pkt[IP])
269 if not decrypt_pkt.haslayer(IP):
270 decrypt_pkt = IP(decrypt_pkt[Raw].load)
271 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
272 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
273 self.assert_packet_checksums_valid(decrypt_pkt)
274 except:
275 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
276 try:
277 self.logger.debug(
278 ppp("Decrypted packet:", decrypt_pkt))
279 except:
280 pass
281 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200282 finally:
283 self.logger.info(self.vapi.ppcli("show error"))
284 self.logger.info(self.vapi.ppcli("show ipsec"))
285
Klement Sekera611864f2018-09-26 11:19:00 +0200286 def test_tun_burst44(self):
Klement Sekera31da2e32018-06-24 22:49:55 +0200287 """ ipsec 4o4 tunnel burst test """
Klement Sekera611864f2018-09-26 11:19:00 +0200288 self.test_tun_basic44(count=257)
289
290
291class IpsecTun6Tests(object):
292 def test_tun_basic66(self, count=1):
293 """ ipsec 6o6 tunnel basic test """
Klement Sekera31da2e32018-06-24 22:49:55 +0200294 try:
Klement Sekera611864f2018-09-26 11:19:00 +0200295 p = self.params[socket.AF_INET6]
296 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p)
297 send_pkts = self.gen_encrypt_pkts6(scapy_tun_sa, self.tun_if,
298 src=p.remote_tun_if_host,
299 dst=self.pg1.remote_ip6,
300 count=count)
301 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1)
302 for recv_pkt in recv_pkts:
303 self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host)
304 self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6)
305 self.assert_packet_checksums_valid(recv_pkt)
306 send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6,
307 dst=p.remote_tun_if_host,
308 count=count)
309 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if)
310 for recv_pkt in recv_pkts:
311 try:
312 decrypt_pkt = vpp_tun_sa.decrypt(recv_pkt[IPv6])
313 if not decrypt_pkt.haslayer(IPv6):
314 decrypt_pkt = IPv6(decrypt_pkt[Raw].load)
315 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6)
316 self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host)
317 self.assert_packet_checksums_valid(decrypt_pkt)
318 except:
319 self.logger.debug(ppp("Unexpected packet:", recv_pkt))
320 try:
321 self.logger.debug(
322 ppp("Decrypted packet:", decrypt_pkt))
323 except:
324 pass
325 raise
Klement Sekera31da2e32018-06-24 22:49:55 +0200326 finally:
327 self.logger.info(self.vapi.ppcli("show error"))
328 self.logger.info(self.vapi.ppcli("show ipsec"))
329
Klement Sekera611864f2018-09-26 11:19:00 +0200330 def test_tun_burst66(self):
331 """ ipsec 6o6 tunnel burst test """
332 self.test_tun_basic66(count=257)
333
334
335class IpsecTunTests(IpsecTun4Tests, IpsecTun6Tests):
336 pass
337
Klement Sekera31da2e32018-06-24 22:49:55 +0200338
339if __name__ == '__main__':
340 unittest.main(testRunner=VppTestRunner)