blob: 0fb084199d8e30ed45cb565e0916e93258b30d45 [file] [log] [blame]
“mystarrocks”23f0c452017-12-11 07:11:51 -08001import socket
Klement Sekera28fb03f2018-04-17 11:36:55 +02002import unittest
“mystarrocks”23f0c452017-12-11 07:11:51 -08003
Klement Sekera31da2e32018-06-24 22:49:55 +02004from scapy.layers.ipsec import AH
“mystarrocks”23f0c452017-12-11 07:11:51 -08005
Klement Sekera31da2e32018-06-24 22:49:55 +02006from framework import VppTestRunner
Neale Ranns2ac885c2019-03-20 18:24:43 +00007from template_ipsec import TemplateIpsec, IpsecTra46Tests, IpsecTun46Tests, \
Neale Ranns3833ffd2019-03-21 14:34:09 +00008 config_tun_params, config_tra_params, IPsecIPv4Params, IPsecIPv6Params
Klement Sekera31da2e32018-06-24 22:49:55 +02009from template_ipsec import IpsecTcpTests
Klement Sekerabf613952019-01-29 11:38:08 +010010from vpp_ipsec import VppIpsecSA, VppIpsecSpd, VppIpsecSpdEntry,\
11 VppIpsecSpdItfBinding
Neale Ranns311124e2019-01-24 04:52:25 -080012from vpp_ip_route import VppIpRoute, VppRoutePath
13from vpp_ip import DpoProto
Neale Ranns17dcec02019-01-09 21:22:20 -080014from vpp_papi import VppEnum
“mystarrocks”23f0c452017-12-11 07:11:51 -080015
16
Klement Sekera31da2e32018-06-24 22:49:55 +020017class TemplateIpsecAh(TemplateIpsec):
“mystarrocks”23f0c452017-12-11 07:11:51 -080018 """
19 Basic test for IPSEC using AH transport and Tunnel mode
20
“mystarrocks”23f0c452017-12-11 07:11:51 -080021 TRANSPORT MODE:
22
23 --- encrypt ---
24 |pg2| <-------> |VPP|
25 --- decrypt ---
26
27 TUNNEL MODE:
28
29 --- encrypt --- plain ---
Klement Sekera84c62042018-05-11 11:06:09 +020030 |pg0| <------- |VPP| <------ |pg1|
“mystarrocks”23f0c452017-12-11 07:11:51 -080031 --- --- ---
32
33 --- decrypt --- plain ---
Klement Sekera84c62042018-05-11 11:06:09 +020034 |pg0| -------> |VPP| ------> |pg1|
“mystarrocks”23f0c452017-12-11 07:11:51 -080035 --- --- ---
“mystarrocks”23f0c452017-12-11 07:11:51 -080036 """
37
Neale Ranns8e4a89b2019-01-23 08:16:17 -080038 def setUp(self):
39 super(TemplateIpsecAh, self).setUp()
“mystarrocks”23f0c452017-12-11 07:11:51 -080040
Neale Ranns8e4a89b2019-01-23 08:16:17 -080041 self.encryption_type = AH
42 self.tun_if = self.pg0
43 self.tra_if = self.pg2
44 self.logger.info(self.vapi.ppcli("show int addr"))
Neale Ranns311124e2019-01-24 04:52:25 -080045
46 self.tra_spd = VppIpsecSpd(self, self.tra_spd_id)
47 self.tra_spd.add_vpp_config()
48 VppIpsecSpdItfBinding(self, self.tra_spd,
49 self.tra_if).add_vpp_config()
50 self.tun_spd = VppIpsecSpd(self, self.tun_spd_id)
51 self.tun_spd.add_vpp_config()
52 VppIpsecSpdItfBinding(self, self.tun_spd,
53 self.tun_if).add_vpp_config()
54
Neale Ranns8e4a89b2019-01-23 08:16:17 -080055 for _, p in self.params.items():
56 self.config_ah_tra(p)
Neale Ranns2ac885c2019-03-20 18:24:43 +000057 config_tra_params(p, self.encryption_type)
Neale Ranns8e4a89b2019-01-23 08:16:17 -080058 self.logger.info(self.vapi.ppcli("show ipsec"))
59 for _, p in self.params.items():
60 self.config_ah_tun(p)
61 self.logger.info(self.vapi.ppcli("show ipsec"))
62 for _, p in self.params.items():
Neale Ranns311124e2019-01-24 04:52:25 -080063 d = DpoProto.DPO_PROTO_IP6 if p.is_ipv6 else DpoProto.DPO_PROTO_IP4
64 VppIpRoute(self, p.remote_tun_if_host, p.addr_len,
65 [VppRoutePath(self.tun_if.remote_addr[p.addr_type],
66 0xffffffff,
67 proto=d)],
68 is_ip6=p.is_ipv6).add_vpp_config()
“mystarrocks”23f0c452017-12-11 07:11:51 -080069
70 def tearDown(self):
Klement Sekera31da2e32018-06-24 22:49:55 +020071 super(TemplateIpsecAh, self).tearDown()
“mystarrocks”23f0c452017-12-11 07:11:51 -080072 if not self.vpp_dead:
73 self.vapi.cli("show hardware")
74
Neale Ranns311124e2019-01-24 04:52:25 -080075 def config_ah_tun(self, params):
76 addr_type = params.addr_type
77 scapy_tun_sa_id = params.scapy_tun_sa_id
78 scapy_tun_spi = params.scapy_tun_spi
79 vpp_tun_sa_id = params.vpp_tun_sa_id
80 vpp_tun_spi = params.vpp_tun_spi
81 auth_algo_vpp_id = params.auth_algo_vpp_id
82 auth_key = params.auth_key
83 crypt_algo_vpp_id = params.crypt_algo_vpp_id
84 crypt_key = params.crypt_key
85 remote_tun_if_host = params.remote_tun_if_host
86 addr_any = params.addr_any
87 addr_bcast = params.addr_bcast
Neale Ranns3833ffd2019-03-21 14:34:09 +000088 flags = params.flags
Neale Ranns17dcec02019-01-09 21:22:20 -080089 e = VppEnum.vl_api_ipsec_spd_action_t
90
Neale Rannseba31ec2019-02-17 18:04:27 +000091 params.tun_sa_in = VppIpsecSA(self, scapy_tun_sa_id, scapy_tun_spi,
92 auth_algo_vpp_id, auth_key,
93 crypt_algo_vpp_id, crypt_key,
94 self.vpp_ah_protocol,
95 self.tun_if.local_addr[addr_type],
Neale Ranns3833ffd2019-03-21 14:34:09 +000096 self.tun_if.remote_addr[addr_type],
97 flags=flags)
Neale Rannseba31ec2019-02-17 18:04:27 +000098 params.tun_sa_in.add_vpp_config()
99 params.tun_sa_out = VppIpsecSA(self, vpp_tun_sa_id, vpp_tun_spi,
100 auth_algo_vpp_id, auth_key,
101 crypt_algo_vpp_id, crypt_key,
102 self.vpp_ah_protocol,
103 self.tun_if.remote_addr[addr_type],
Neale Ranns3833ffd2019-03-21 14:34:09 +0000104 self.tun_if.local_addr[addr_type],
105 flags=flags)
Neale Rannseba31ec2019-02-17 18:04:27 +0000106 params.tun_sa_out.add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800107
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800108 params.spd_policy_in_any = VppIpsecSpdEntry(self, self.tun_spd,
109 vpp_tun_sa_id,
110 addr_any, addr_bcast,
111 addr_any, addr_bcast,
112 socket.IPPROTO_AH)
113 params.spd_policy_in_any.add_vpp_config()
114 params.spd_policy_out_any = VppIpsecSpdEntry(self, self.tun_spd,
115 vpp_tun_sa_id,
116 addr_any, addr_bcast,
117 addr_any, addr_bcast,
118 socket.IPPROTO_AH,
119 is_outbound=0)
120 params.spd_policy_out_any.add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800121
122 VppIpsecSpdEntry(self, self.tun_spd, vpp_tun_sa_id,
123 remote_tun_if_host,
124 remote_tun_if_host,
125 self.pg1.remote_addr[addr_type],
126 self.pg1.remote_addr[addr_type],
Neale Ranns17dcec02019-01-09 21:22:20 -0800127 0, priority=10,
128 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
Neale Ranns311124e2019-01-24 04:52:25 -0800129 is_outbound=0).add_vpp_config()
130 VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id,
131 self.pg1.remote_addr[addr_type],
132 self.pg1.remote_addr[addr_type],
133 remote_tun_if_host,
134 remote_tun_if_host,
Neale Ranns17dcec02019-01-09 21:22:20 -0800135 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT,
136 priority=10).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800137
138 VppIpsecSpdEntry(self, self.tun_spd, vpp_tun_sa_id,
139 remote_tun_if_host,
140 remote_tun_if_host,
141 self.pg0.local_addr[addr_type],
142 self.pg0.local_addr[addr_type],
Neale Ranns17dcec02019-01-09 21:22:20 -0800143 0, priority=20,
144 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
Neale Ranns311124e2019-01-24 04:52:25 -0800145 is_outbound=0).add_vpp_config()
146 VppIpsecSpdEntry(self, self.tun_spd, scapy_tun_sa_id,
147 self.pg0.local_addr[addr_type],
148 self.pg0.local_addr[addr_type],
149 remote_tun_if_host,
150 remote_tun_if_host,
Neale Ranns17dcec02019-01-09 21:22:20 -0800151 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT,
152 priority=20).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800153
154 def config_ah_tra(self, params):
155 addr_type = params.addr_type
156 scapy_tra_sa_id = params.scapy_tra_sa_id
157 scapy_tra_spi = params.scapy_tra_spi
158 vpp_tra_sa_id = params.vpp_tra_sa_id
159 vpp_tra_spi = params.vpp_tra_spi
160 auth_algo_vpp_id = params.auth_algo_vpp_id
161 auth_key = params.auth_key
162 crypt_algo_vpp_id = params.crypt_algo_vpp_id
163 crypt_key = params.crypt_key
164 addr_any = params.addr_any
165 addr_bcast = params.addr_bcast
Neale Ranns3833ffd2019-03-21 14:34:09 +0000166 flags = params.flags | (VppEnum.vl_api_ipsec_sad_flags_t.
167 IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY)
Neale Ranns17dcec02019-01-09 21:22:20 -0800168 e = VppEnum.vl_api_ipsec_spd_action_t
Neale Ranns311124e2019-01-24 04:52:25 -0800169
Neale Rannseba31ec2019-02-17 18:04:27 +0000170 params.tra_sa_in = VppIpsecSA(self, scapy_tra_sa_id, scapy_tra_spi,
171 auth_algo_vpp_id, auth_key,
172 crypt_algo_vpp_id, crypt_key,
173 self.vpp_ah_protocol,
174 flags=flags)
175 params.tra_sa_in.add_vpp_config()
176 params.tra_sa_out = VppIpsecSA(self, vpp_tra_sa_id, vpp_tra_spi,
177 auth_algo_vpp_id, auth_key,
178 crypt_algo_vpp_id, crypt_key,
179 self.vpp_ah_protocol,
180 flags=flags)
181 params.tra_sa_out.add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800182
183 VppIpsecSpdEntry(self, self.tra_spd, vpp_tra_sa_id,
184 addr_any, addr_bcast,
185 addr_any, addr_bcast,
186 socket.IPPROTO_AH).add_vpp_config()
187 VppIpsecSpdEntry(self, self.tra_spd, scapy_tra_sa_id,
188 addr_any, addr_bcast,
189 addr_any, addr_bcast,
190 socket.IPPROTO_AH,
191 is_outbound=0).add_vpp_config()
192
193 VppIpsecSpdEntry(self, self.tra_spd, vpp_tra_sa_id,
194 self.tra_if.local_addr[addr_type],
195 self.tra_if.local_addr[addr_type],
196 self.tra_if.remote_addr[addr_type],
197 self.tra_if.remote_addr[addr_type],
Neale Ranns17dcec02019-01-09 21:22:20 -0800198 0, priority=10,
199 policy=e.IPSEC_API_SPD_ACTION_PROTECT,
Neale Ranns311124e2019-01-24 04:52:25 -0800200 is_outbound=0).add_vpp_config()
201 VppIpsecSpdEntry(self, self.tra_spd, scapy_tra_sa_id,
202 self.tra_if.local_addr[addr_type],
203 self.tra_if.local_addr[addr_type],
204 self.tra_if.remote_addr[addr_type],
205 self.tra_if.remote_addr[addr_type],
Neale Ranns17dcec02019-01-09 21:22:20 -0800206 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT,
207 priority=10).add_vpp_config()
Neale Ranns311124e2019-01-24 04:52:25 -0800208
“mystarrocks”23f0c452017-12-11 07:11:51 -0800209
Neale Ranns53f526b2019-02-25 14:32:02 +0000210class TestIpsecAh1(TemplateIpsecAh, IpsecTra46Tests, IpsecTun46Tests):
Klement Sekera31da2e32018-06-24 22:49:55 +0200211 """ Ipsec AH - TUN & TRA tests """
Klement Sekerab4d30532018-11-08 13:00:02 +0100212 tra4_encrypt_node_name = "ah4-encrypt"
213 tra4_decrypt_node_name = "ah4-decrypt"
214 tra6_encrypt_node_name = "ah6-encrypt"
215 tra6_decrypt_node_name = "ah6-decrypt"
216 tun4_encrypt_node_name = "ah4-encrypt"
217 tun4_decrypt_node_name = "ah4-decrypt"
218 tun6_encrypt_node_name = "ah6-encrypt"
219 tun6_decrypt_node_name = "ah6-decrypt"
“mystarrocks”23f0c452017-12-11 07:11:51 -0800220
“mystarrocks”23f0c452017-12-11 07:11:51 -0800221
Klement Sekera31da2e32018-06-24 22:49:55 +0200222class TestIpsecAh2(TemplateIpsecAh, IpsecTcpTests):
223 """ Ipsec AH - TCP tests """
224 pass
“mystarrocks”23f0c452017-12-11 07:11:51 -0800225
226
Neale Ranns3833ffd2019-03-21 14:34:09 +0000227class TestIpsecAh3(TemplateIpsecAh, IpsecTra46Tests, IpsecTun46Tests):
228 """ Ipsec AH w/ ESN - TCP tests """
229
230 tra4_encrypt_node_name = "ah4-encrypt"
231 tra4_decrypt_node_name = "ah4-decrypt"
232 tra6_encrypt_node_name = "ah6-encrypt"
233 tra6_decrypt_node_name = "ah6-decrypt"
234 tun4_encrypt_node_name = "ah4-encrypt"
235 tun4_decrypt_node_name = "ah4-decrypt"
236 tun6_encrypt_node_name = "ah6-encrypt"
237 tun6_decrypt_node_name = "ah6-decrypt"
238
239 def setup_params(self):
240 self.ipv4_params = IPsecIPv4Params()
241 self.ipv6_params = IPsecIPv6Params()
242 self.params = {self.ipv4_params.addr_type: self.ipv4_params,
243 self.ipv6_params.addr_type: self.ipv6_params}
244 for _, p in self.params.items():
245 p.flags = (VppEnum.vl_api_ipsec_sad_flags_t.
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100246 IPSEC_API_SAD_FLAG_USE_ESN)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000247
“mystarrocks”23f0c452017-12-11 07:11:51 -0800248if __name__ == '__main__':
249 unittest.main(testRunner=VppTestRunner)