“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 1 | import socket |
Klement Sekera | 28fb03f | 2018-04-17 11:36:55 +0200 | [diff] [blame] | 2 | import unittest |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 3 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 4 | from scapy.layers.ipsec import AH |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 5 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 6 | from framework import VppTestRunner |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 7 | from template_ipsec import TemplateIpsec, IpsecTra46Tests, IpsecTun46Tests, \ |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 8 | config_tun_params, config_tra_params, IPsecIPv4Params, IPsecIPv6Params |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 9 | from template_ipsec import IpsecTcpTests |
Klement Sekera | bf61395 | 2019-01-29 11:38:08 +0100 | [diff] [blame] | 10 | from vpp_ipsec import VppIpsecSA, VppIpsecSpd, VppIpsecSpdEntry,\ |
| 11 | VppIpsecSpdItfBinding |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 12 | from vpp_ip_route import VppIpRoute, VppRoutePath |
| 13 | from vpp_ip import DpoProto |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 14 | from vpp_papi import VppEnum |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 15 | |
| 16 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 17 | class TemplateIpsecAh(TemplateIpsec): |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 18 | """ |
| 19 | Basic test for IPSEC using AH transport and Tunnel mode |
| 20 | |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 21 | TRANSPORT MODE: |
| 22 | |
| 23 | --- encrypt --- |
| 24 | |pg2| <-------> |VPP| |
| 25 | --- decrypt --- |
| 26 | |
| 27 | TUNNEL MODE: |
| 28 | |
| 29 | --- encrypt --- plain --- |
Klement Sekera | 84c6204 | 2018-05-11 11:06:09 +0200 | [diff] [blame] | 30 | |pg0| <------- |VPP| <------ |pg1| |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 31 | --- --- --- |
| 32 | |
| 33 | --- decrypt --- plain --- |
Klement Sekera | 84c6204 | 2018-05-11 11:06:09 +0200 | [diff] [blame] | 34 | |pg0| -------> |VPP| ------> |pg1| |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 35 | --- --- --- |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 36 | """ |
| 37 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 38 | def setUp(self): |
| 39 | super(TemplateIpsecAh, self).setUp() |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 40 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 41 | 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 Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 45 | |
| 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 Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 55 | for _, p in self.params.items(): |
| 56 | self.config_ah_tra(p) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 57 | config_tra_params(p, self.encryption_type) |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 58 | 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 Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 63 | 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” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 69 | |
| 70 | def tearDown(self): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 71 | super(TemplateIpsecAh, self).tearDown() |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 72 | if not self.vpp_dead: |
| 73 | self.vapi.cli("show hardware") |
| 74 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 75 | 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 Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 88 | flags = params.flags |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 89 | e = VppEnum.vl_api_ipsec_spd_action_t |
| 90 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 91 | 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 Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 96 | self.tun_if.remote_addr[addr_type], |
| 97 | flags=flags) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 98 | 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 Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 104 | self.tun_if.local_addr[addr_type], |
| 105 | flags=flags) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 106 | params.tun_sa_out.add_vpp_config() |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 107 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 108 | 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 Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 121 | |
| 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 Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 127 | 0, priority=10, |
| 128 | policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 129 | 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 Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 135 | 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
| 136 | priority=10).add_vpp_config() |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 137 | |
| 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 Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 143 | 0, priority=20, |
| 144 | policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 145 | 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 Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 151 | 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
| 152 | priority=20).add_vpp_config() |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 153 | |
| 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 Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 166 | flags = params.flags | (VppEnum.vl_api_ipsec_sad_flags_t. |
| 167 | IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 168 | e = VppEnum.vl_api_ipsec_spd_action_t |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 169 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 170 | 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 Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 182 | |
| 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 Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 198 | 0, priority=10, |
| 199 | policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 200 | 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 Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 206 | 0, policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
| 207 | priority=10).add_vpp_config() |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 208 | |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 209 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 210 | class TestIpsecAh1(TemplateIpsecAh, IpsecTra46Tests, IpsecTun46Tests): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 211 | """ Ipsec AH - TUN & TRA tests """ |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 212 | 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” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 220 | |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 221 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 222 | class TestIpsecAh2(TemplateIpsecAh, IpsecTcpTests): |
| 223 | """ Ipsec AH - TCP tests """ |
| 224 | pass |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 225 | |
| 226 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 227 | class 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 Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 246 | IPSEC_API_SAD_FLAG_USE_ESN) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 247 | |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 248 | if __name__ == '__main__': |
| 249 | unittest.main(testRunner=VppTestRunner) |