blob: 9d95185cb9e6eb52d29ea3564ef19c4e9a0fe757 [file] [log] [blame]
Klement Sekera31da2e32018-06-24 22:49:55 +02001import unittest
2
3from scapy.layers.inet import IP, ICMP, TCP
4from scapy.layers.ipsec import SecurityAssociation
5from scapy.layers.l2 import Ether, Raw
6
7from framework import VppTestCase, VppTestRunner
8from util import ppp
9
10
11class TemplateIpsec(VppTestCase):
12 """
13 TRANSPORT MODE:
14
15 ------ encrypt ---
16 |tra_if| <-------> |VPP|
17 ------ decrypt ---
18
19 TUNNEL MODE:
20
21 ------ encrypt --- plain ---
22 |tun_if| <------- |VPP| <------ |pg1|
23 ------ --- ---
24
25 ------ decrypt --- plain ---
26 |tun_if| -------> |VPP| ------> |pg1|
27 ------ --- ---
28 """
29
30 remote_tun_if_host = '1.1.1.1'
31 payload = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
32
33 tun_spd_id = 1
34 scapy_tun_sa_id = 10
35 scapy_tun_spi = 1001
36 vpp_tun_sa_id = 20
37 vpp_tun_spi = 1000
38
39 tra_spd_id = 2
40 scapy_tra_sa_id = 30
41 scapy_tra_spi = 2001
42 vpp_tra_sa_id = 40
43 vpp_tra_spi = 2000
44
45 vpp_esp_protocol = 1
46 vpp_ah_protocol = 0
47
48 auth_algo_vpp_id = 2 # internal VPP enum value for SHA1_96
49 auth_algo = 'HMAC-SHA1-96' # scapy name
50 auth_key = 'C91KUR9GYMm5GfkEvNjX'
51
52 crypt_algo_vpp_id = 1 # internal VPP enum value for AES_CBC_128
53 crypt_algo = 'AES-CBC' # scapy name
54 crypt_key = 'JPjyOWBeVEQiMe7h'
55
56 @classmethod
57 def setUpClass(cls):
58 super(TemplateIpsec, cls).setUpClass()
59 cls.create_pg_interfaces(range(3))
60 cls.interfaces = list(cls.pg_interfaces)
61 for i in cls.interfaces:
62 i.admin_up()
63 i.config_ip4()
64 i.resolve_arp()
65
66 def tearDown(self):
67 super(TemplateIpsec, self).tearDown()
68 if not self.vpp_dead:
69 self.vapi.cli("show hardware")
70
71 def send_and_expect(self, input, pkts, output, count=1):
72 input.add_stream(pkts)
73 self.pg_enable_capture(self.pg_interfaces)
74 self.pg_start()
75 rx = output.get_capture(count)
76 return rx
77
78 def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1):
79 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
80 sa.encrypt(IP(src=src, dst=dst) / ICMP() / self.payload)
81 for i in range(count)]
82
83 def gen_pkts(self, sw_intf, src, dst, count=1):
84 return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) /
85 IP(src=src, dst=dst) / ICMP() / self.payload
86 for i in range(count)]
87
88 def configure_sa_tun(self):
89 scapy_tun_sa = SecurityAssociation(self.encryption_type,
90 spi=self.vpp_tun_spi,
91 crypt_algo=self.crypt_algo,
92 crypt_key=self.crypt_key,
93 auth_algo=self.auth_algo,
94 auth_key=self.auth_key,
95 tunnel_header=IP(
96 src=self.tun_if.remote_ip4,
97 dst=self.tun_if.local_ip4))
98 vpp_tun_sa = SecurityAssociation(self.encryption_type,
99 spi=self.scapy_tun_spi,
100 crypt_algo=self.crypt_algo,
101 crypt_key=self.crypt_key,
102 auth_algo=self.auth_algo,
103 auth_key=self.auth_key,
104 tunnel_header=IP(
105 dst=self.tun_if.remote_ip4,
106 src=self.tun_if.local_ip4))
107 return vpp_tun_sa, scapy_tun_sa
108
109 def configure_sa_tra(self):
110 scapy_tra_sa = SecurityAssociation(self.encryption_type,
111 spi=self.vpp_tra_spi,
112 crypt_algo=self.crypt_algo,
113 crypt_key=self.crypt_key,
114 auth_algo=self.auth_algo,
115 auth_key=self.auth_key)
116 vpp_tra_sa = SecurityAssociation(self.encryption_type,
117 spi=self.scapy_tra_spi,
118 crypt_algo=self.crypt_algo,
119 crypt_key=self.crypt_key,
120 auth_algo=self.auth_algo,
121 auth_key=self.auth_key)
122 return vpp_tra_sa, scapy_tra_sa
123
124
125class IpsecTcpTests(object):
126 def test_tcp_checksum(self):
127 """ verify checksum correctness for vpp generated packets """
128 self.vapi.cli("test http server")
129 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun()
130 send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) /
131 scapy_tun_sa.encrypt(IP(src=self.remote_tun_if_host,
132 dst=self.tun_if.local_ip4) /
133 TCP(flags='S', dport=80)))
134 self.logger.debug(ppp("Sending packet:", send))
135 recv = self.send_and_expect(self.tun_if, [send], self.tun_if, 1)
136 recv = recv[0]
137 decrypted = vpp_tun_sa.decrypt(recv[IP])
138 self.assert_packet_checksums_valid(decrypted)
139
140
141class IpsecTraTests(object):
142 def test_tra_basic(self, count=1):
143 """ ipsec v4 transport basic test """
144 try:
145 vpp_tra_sa, scapy_tra_sa = self.configure_sa_tra()
146 send_pkts = self.gen_encrypt_pkts(scapy_tra_sa, self.tra_if,
147 src=self.tra_if.remote_ip4,
148 dst=self.tra_if.local_ip4,
149 count=count)
150 recv_pkts = self.send_and_expect(self.tra_if, send_pkts,
151 self.tra_if, count=count)
152 for p in recv_pkts:
153 decrypted = vpp_tra_sa.decrypt(p[IP])
154 self.assert_packet_checksums_valid(decrypted)
155 finally:
156 self.logger.info(self.vapi.ppcli("show error"))
157 self.logger.info(self.vapi.ppcli("show ipsec"))
158
159 def test_tra_burst(self):
160 """ ipsec v4 transport burst test """
161 try:
162 self.test_tra_basic(count=257)
163 finally:
164 self.logger.info(self.vapi.ppcli("show error"))
165 self.logger.info(self.vapi.ppcli("show ipsec"))
166
167
168class IpsecTunTests(object):
169 def test_tun_basic(self, count=1):
170 """ ipsec 4o4 tunnel basic test """
171 try:
172 vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun()
173 send_pkts = self.gen_encrypt_pkts(scapy_tun_sa, self.tun_if,
174 src=self.remote_tun_if_host,
175 dst=self.pg1.remote_ip4,
176 count=count)
177 recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1,
178 count=count)
179 for recv_pkt in recv_pkts:
180 self.assert_equal(recv_pkt[IP].src, self.remote_tun_if_host)
181 self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4)
182 self.assert_packet_checksums_valid(recv_pkt)
183 send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4,
184 dst=self.remote_tun_if_host, count=count)
185 recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if,
186 count=count)
187 for recv_pkt in recv_pkts:
188 decrypt_pkt = vpp_tun_sa.decrypt(recv_pkt[IP])
189 if not decrypt_pkt.haslayer(IP):
190 decrypt_pkt = IP(decrypt_pkt[Raw].load)
191 self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4)
192 self.assert_equal(decrypt_pkt.dst, self.remote_tun_if_host)
193 self.assert_packet_checksums_valid(decrypt_pkt)
194 finally:
195 self.logger.info(self.vapi.ppcli("show error"))
196 self.logger.info(self.vapi.ppcli("show ipsec"))
197
198 def test_tun_burst(self):
199 """ ipsec 4o4 tunnel burst test """
200 try:
201 self.test_tun_basic(count=257)
202 finally:
203 self.logger.info(self.vapi.ppcli("show error"))
204 self.logger.info(self.vapi.ppcli("show ipsec"))
205
206
207if __name__ == '__main__':
208 unittest.main(testRunner=VppTestRunner)