blob: ba67b60a08ed8c040531b6f54e195ec5c44fb294 [file] [log] [blame]
“mystarrocks”23f0c452017-12-11 07:11:51 -08001import socket
Klement Sekera28fb03f2018-04-17 11:36:55 +02002import unittest
Klement Sekera31da2e32018-06-24 22:49:55 +02003from scapy.layers.ipsec import ESP
“mystarrocks”23f0c452017-12-11 07:11:51 -08004
Klement Sekera31da2e32018-06-24 22:49:55 +02005from framework import VppTestRunner
6from template_ipsec import IpsecTraTests, IpsecTunTests
7from template_ipsec import TemplateIpsec, IpsecTcpTests
Klement Sekerabf613952019-01-29 11:38:08 +01008from vpp_ipsec import VppIpsecSpd, VppIpsecSpdEntry, VppIpsecSA,\
9 VppIpsecSpdItfBinding
Neale Ranns311124e2019-01-24 04:52:25 -080010from vpp_ip_route import VppIpRoute, VppRoutePath
11from vpp_ip import DpoProto
Neale Ranns17dcec02019-01-09 21:22:20 -080012from vpp_papi import VppEnum
“mystarrocks”23f0c452017-12-11 07:11:51 -080013
14
Klement Sekera31da2e32018-06-24 22:49:55 +020015class TemplateIpsecEsp(TemplateIpsec):
“mystarrocks”23f0c452017-12-11 07:11:51 -080016 """
17 Basic test for ipsec esp sanity - tunnel and transport modes.
18
19 Below 4 cases are covered as part of this test
20 1) ipsec esp v4 transport basic test - IPv4 Transport mode
21 scenario using HMAC-SHA1-96 intergrity algo
22 2) ipsec esp v4 transport burst test
23 Above test for 257 pkts
24 3) ipsec esp 4o4 tunnel basic test - IPv4 Tunnel mode
25 scenario using HMAC-SHA1-96 intergrity algo
26 4) ipsec esp 4o4 tunnel burst test
27 Above test for 257 pkts
28
29 TRANSPORT MODE:
30
31 --- encrypt ---
32 |pg2| <-------> |VPP|
33 --- decrypt ---
34
35 TUNNEL MODE:
36
37 --- encrypt --- plain ---
Klement Sekera4b089f22018-04-17 18:04:57 +020038 |pg0| <------- |VPP| <------ |pg1|
“mystarrocks”23f0c452017-12-11 07:11:51 -080039 --- --- ---
40
41 --- decrypt --- plain ---
Klement Sekera4b089f22018-04-17 18:04:57 +020042 |pg0| -------> |VPP| ------> |pg1|
“mystarrocks”23f0c452017-12-11 07:11:51 -080043 --- --- ---
“mystarrocks”23f0c452017-12-11 07:11:51 -080044 """
45
Neale Ranns8e4a89b2019-01-23 08:16:17 -080046 def setUp(self):
47 super(TemplateIpsecEsp, self).setUp()
48 self.encryption_type = ESP
49 self.tun_if = self.pg0
50 self.tra_if = self.pg2
51 self.logger.info(self.vapi.ppcli("show int addr"))
Neale Ranns311124e2019-01-24 04:52:25 -080052
53 self.tra_spd = VppIpsecSpd(self, self.tra_spd_id)
54 self.tra_spd.add_vpp_config()
55 VppIpsecSpdItfBinding(self, self.tra_spd,
56 self.tra_if).add_vpp_config()
57
Neale Ranns8e4a89b2019-01-23 08:16:17 -080058 for _, p in self.params.items():
59 self.config_esp_tra(p)
60 self.configure_sa_tra(p)
61 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Ranns311124e2019-01-24 04:52:25 -080062
63 self.tun_spd = VppIpsecSpd(self, self.tun_spd_id)
64 self.tun_spd.add_vpp_config()
65 VppIpsecSpdItfBinding(self, self.tun_spd,
66 self.tun_if).add_vpp_config()
67
Neale Ranns8e4a89b2019-01-23 08:16:17 -080068 for _, p in self.params.items():
69 self.config_esp_tun(p)
70 self.logger.info(self.vapi.ppcli("show ipsec"))
Neale Ranns311124e2019-01-24 04:52:25 -080071
Neale Ranns8e4a89b2019-01-23 08:16:17 -080072 for _, p in self.params.items():
Neale Ranns311124e2019-01-24 04:52:25 -080073 d = DpoProto.DPO_PROTO_IP6 if p.is_ipv6 else DpoProto.DPO_PROTO_IP4
74 VppIpRoute(self, p.remote_tun_if_host, p.addr_len,
75 [VppRoutePath(self.tun_if.remote_addr[p.addr_type],
76 0xffffffff,
77 proto=d)],
78 is_ip6=p.is_ipv6).add_vpp_config()
Klement Sekera611864f2018-09-26 11:19:00 +020079
Neale Ranns8e4a89b2019-01-23 08:16:17 -080080 def tearDown(self):
Neale Ranns8e4a89b2019-01-23 08:16:17 -080081 super(TemplateIpsecEsp, self).tearDown()
82 if not self.vpp_dead:
83 self.vapi.cli("show hardware")
84
85 def config_esp_tun(self, params):
Klement Sekera611864f2018-09-26 11:19:00 +020086 addr_type = params.addr_type
Klement Sekera611864f2018-09-26 11:19:00 +020087 scapy_tun_sa_id = params.scapy_tun_sa_id
88 scapy_tun_spi = params.scapy_tun_spi
89 vpp_tun_sa_id = params.vpp_tun_sa_id
90 vpp_tun_spi = params.vpp_tun_spi
91 auth_algo_vpp_id = params.auth_algo_vpp_id
92 auth_key = params.auth_key
93 crypt_algo_vpp_id = params.crypt_algo_vpp_id
94 crypt_key = params.crypt_key
95 remote_tun_if_host = params.remote_tun_if_host
96 addr_any = params.addr_any
97 addr_bcast = params.addr_bcast
Neale Ranns17dcec02019-01-09 21:22:20 -080098 e = VppEnum.vl_api_ipsec_spd_action_t
Klement Sekera611864f2018-09-26 11:19:00 +020099
Neale Ranns311124e2019-01-24 04:52:25 -0800100 VppIpsecSA(self, scapy_tun_sa_id, scapy_tun_spi,
101 auth_algo_vpp_id, auth_key,
102 crypt_algo_vpp_id, crypt_key,
103 self.vpp_esp_protocol,
104 self.tun_if.local_addr[addr_type],
105 self.tun_if.remote_addr[addr_type]).add_vpp_config()
106 VppIpsecSA(self, vpp_tun_sa_id, vpp_tun_spi,
107 auth_algo_vpp_id, auth_key,
108 crypt_algo_vpp_id, crypt_key,
109 self.vpp_esp_protocol,
110 self.tun_if.remote_addr[addr_type],
111 self.tun_if.local_addr[addr_type]).add_vpp_config()
112
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800113 params.spd_policy_in_any = VppIpsecSpdEntry(self, self.tun_spd,
114 scapy_tun_sa_id,
115 addr_any, addr_bcast,
116 addr_any, addr_bcast,
117 socket.IPPROTO_ESP)
118 params.spd_policy_in_any.add_vpp_config()
119 params.spd_policy_out_any = VppIpsecSpdEntry(self, self.tun_spd,
120 scapy_tun_sa_id,
121 addr_any, addr_bcast,
122 addr_any, addr_bcast,
123 socket.IPPROTO_ESP,
124 is_outbound=0)
125 params.spd_policy_out_any.add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800126
127 VppIpsecSpdEntry(self, self.tun_spd, vpp_tun_sa_id,
128 remote_tun_if_host, remote_tun_if_host,
129 self.pg1.remote_addr[addr_type],
130 self.pg1.remote_addr[addr_type],
131 0,
Neale Ranns17dcec02019-01-09 21:22:20 -0800132 priority=10,
133 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
Neale Ranns311124e2019-01-24 04:52:25 -0800134 is_outbound=0).add_vpp_config()
135 VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id,
136 self.pg1.remote_addr[addr_type],
137 self.pg1.remote_addr[addr_type],
138 remote_tun_if_host, remote_tun_if_host,
139 0,
Neale Ranns17dcec02019-01-09 21:22:20 -0800140 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
141 priority=10).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800142
143 VppIpsecSpdEntry(self, self.tun_spd, vpp_tun_sa_id,
144 remote_tun_if_host, remote_tun_if_host,
145 self.pg0.local_addr[addr_type],
146 self.pg0.local_addr[addr_type],
147 0,
Neale Ranns17dcec02019-01-09 21:22:20 -0800148 priority=20,
149 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
Neale Ranns311124e2019-01-24 04:52:25 -0800150 is_outbound=0).add_vpp_config()
151 VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id,
152 self.pg0.local_addr[addr_type],
153 self.pg0.local_addr[addr_type],
154 remote_tun_if_host, remote_tun_if_host,
155 0,
Neale Ranns17dcec02019-01-09 21:22:20 -0800156 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
157 priority=20).add_vpp_config()
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800158
159 def config_esp_tra(self, params):
Klement Sekera611864f2018-09-26 11:19:00 +0200160 addr_type = params.addr_type
Klement Sekera611864f2018-09-26 11:19:00 +0200161 scapy_tra_sa_id = params.scapy_tra_sa_id
162 scapy_tra_spi = params.scapy_tra_spi
163 vpp_tra_sa_id = params.vpp_tra_sa_id
164 vpp_tra_spi = params.vpp_tra_spi
165 auth_algo_vpp_id = params.auth_algo_vpp_id
166 auth_key = params.auth_key
167 crypt_algo_vpp_id = params.crypt_algo_vpp_id
168 crypt_key = params.crypt_key
169 addr_any = params.addr_any
170 addr_bcast = params.addr_bcast
Neale Ranns17dcec02019-01-09 21:22:20 -0800171 flags = (VppEnum.vl_api_ipsec_sad_flags_t.
172 IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY)
173 e = VppEnum.vl_api_ipsec_spd_action_t
Neale Ranns8e4a89b2019-01-23 08:16:17 -0800174
Neale Ranns311124e2019-01-24 04:52:25 -0800175 VppIpsecSA(self, scapy_tra_sa_id, scapy_tra_spi,
176 auth_algo_vpp_id, auth_key,
177 crypt_algo_vpp_id, crypt_key,
178 self.vpp_esp_protocol,
Neale Ranns17dcec02019-01-09 21:22:20 -0800179 flags=flags).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800180 VppIpsecSA(self, vpp_tra_sa_id, vpp_tra_spi,
181 auth_algo_vpp_id, auth_key,
182 crypt_algo_vpp_id, crypt_key,
183 self.vpp_esp_protocol,
Neale Ranns17dcec02019-01-09 21:22:20 -0800184 flags=flags).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800185
186 VppIpsecSpdEntry(self, self.tra_spd, vpp_tra_sa_id,
187 addr_any, addr_bcast,
188 addr_any, addr_bcast,
189 socket.IPPROTO_ESP).add_vpp_config()
190 VppIpsecSpdEntry(self, self.tra_spd, vpp_tra_sa_id,
191 addr_any, addr_bcast,
192 addr_any, addr_bcast,
193 socket.IPPROTO_ESP,
194 is_outbound=0).add_vpp_config()
195
196 VppIpsecSpdEntry(self, self.tra_spd, vpp_tra_sa_id,
197 self.tra_if.local_addr[addr_type],
198 self.tra_if.local_addr[addr_type],
199 self.tra_if.remote_addr[addr_type],
200 self.tra_if.remote_addr[addr_type],
Neale Ranns17dcec02019-01-09 21:22:20 -0800201 0, priority=10,
202 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
Neale Ranns311124e2019-01-24 04:52:25 -0800203 is_outbound=0).add_vpp_config()
204 VppIpsecSpdEntry(self, self.tra_spd, scapy_tra_sa_id,
205 self.tra_if.local_addr[addr_type],
206 self.tra_if.local_addr[addr_type],
207 self.tra_if.remote_addr[addr_type],
208 self.tra_if.remote_addr[addr_type],
Neale Ranns17dcec02019-01-09 21:22:20 -0800209 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT,
210 priority=10).add_vpp_config()
“mystarrocks”23f0c452017-12-11 07:11:51 -0800211
“mystarrocks”23f0c452017-12-11 07:11:51 -0800212
Klement Sekera31da2e32018-06-24 22:49:55 +0200213class TestIpsecEsp1(TemplateIpsecEsp, IpsecTraTests, IpsecTunTests):
214 """ Ipsec ESP - TUN & TRA tests """
Klement Sekerab4d30532018-11-08 13:00:02 +0100215 tra4_encrypt_node_name = "esp4-encrypt"
216 tra4_decrypt_node_name = "esp4-decrypt"
217 tra6_encrypt_node_name = "esp6-encrypt"
218 tra6_decrypt_node_name = "esp6-decrypt"
219 tun4_encrypt_node_name = "esp4-encrypt"
220 tun4_decrypt_node_name = "esp4-decrypt"
221 tun6_encrypt_node_name = "esp6-encrypt"
222 tun6_decrypt_node_name = "esp6-decrypt"
“mystarrocks”23f0c452017-12-11 07:11:51 -0800223
“mystarrocks”23f0c452017-12-11 07:11:51 -0800224
Klement Sekera31da2e32018-06-24 22:49:55 +0200225class TestIpsecEsp2(TemplateIpsecEsp, IpsecTcpTests):
226 """ Ipsec ESP - TCP tests """
227 pass
“mystarrocks”23f0c452017-12-11 07:11:51 -0800228
229
230if __name__ == '__main__':
231 unittest.main(testRunner=VppTestRunner)