“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 |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 5 | from scapy.layers.inet import IP, UDP |
| 6 | from scapy.layers.inet6 import IPv6 |
| 7 | from scapy.layers.l2 import Ether |
| 8 | from scapy.packet import Raw |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 9 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 10 | from asfframework import VppTestRunner |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 11 | from template_ipsec import ( |
| 12 | TemplateIpsec, |
| 13 | IpsecTra46Tests, |
| 14 | IpsecTun46Tests, |
| 15 | config_tun_params, |
| 16 | config_tra_params, |
| 17 | IPsecIPv4Params, |
| 18 | IPsecIPv6Params, |
| 19 | IpsecTra4, |
| 20 | IpsecTun4, |
| 21 | IpsecTra6, |
| 22 | IpsecTun6, |
| 23 | IpsecTun6HandoffTests, |
| 24 | IpsecTun4HandoffTests, |
| 25 | ) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 26 | from template_ipsec import IpsecTcpTests |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 27 | from vpp_ipsec import VppIpsecSA, VppIpsecSpd, VppIpsecSpdEntry, VppIpsecSpdItfBinding |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 28 | from vpp_ip_route import VppIpRoute, VppRoutePath |
| 29 | from vpp_ip import DpoProto |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 30 | from vpp_papi import VppEnum |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 31 | from config import config |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 32 | |
| 33 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 34 | class ConfigIpsecAH(TemplateIpsec): |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 35 | """ |
| 36 | Basic test for IPSEC using AH transport and Tunnel mode |
| 37 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 38 | TRANSPORT MODE:: |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 39 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 40 | --- encrypt --- |
| 41 | |pg2| <-------> |VPP| |
| 42 | --- decrypt --- |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 43 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 44 | TUNNEL MODE:: |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 45 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 46 | --- encrypt --- plain --- |
| 47 | |pg0| <------- |VPP| <------ |pg1| |
| 48 | --- --- --- |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 49 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 50 | --- decrypt --- plain --- |
| 51 | |pg0| -------> |VPP| ------> |pg1| |
| 52 | --- --- --- |
| 53 | |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 54 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 55 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 56 | encryption_type = AH |
| 57 | net_objs = [] |
| 58 | tra4_encrypt_node_name = "ah4-encrypt" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 59 | tra4_decrypt_node_name = ["ah4-decrypt", "ah4-decrypt"] |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 60 | tra6_encrypt_node_name = "ah6-encrypt" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 61 | tra6_decrypt_node_name = ["ah6-decrypt", "ah6-decrypt"] |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 62 | tun4_encrypt_node_name = "ah4-encrypt" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 63 | tun4_decrypt_node_name = ["ah4-decrypt", "ah4-decrypt"] |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 64 | tun6_encrypt_node_name = "ah6-encrypt" |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 65 | tun6_decrypt_node_name = ["ah6-decrypt", "ah6-decrypt"] |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 66 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 67 | @classmethod |
| 68 | def setUpClass(cls): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 69 | super(ConfigIpsecAH, cls).setUpClass() |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 70 | |
| 71 | @classmethod |
| 72 | def tearDownClass(cls): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 73 | super(ConfigIpsecAH, cls).tearDownClass() |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 74 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 75 | def setUp(self): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 76 | super(ConfigIpsecAH, self).setUp() |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 77 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 78 | def tearDown(self): |
| 79 | super(ConfigIpsecAH, self).tearDown() |
| 80 | |
| 81 | def config_network(self, params): |
| 82 | self.net_objs = [] |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 83 | self.tun_if = self.pg0 |
| 84 | self.tra_if = self.pg2 |
| 85 | self.logger.info(self.vapi.ppcli("show int addr")) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 86 | |
| 87 | self.tra_spd = VppIpsecSpd(self, self.tra_spd_id) |
| 88 | self.tra_spd.add_vpp_config() |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 89 | self.net_objs.append(self.tra_spd) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 90 | self.tun_spd = VppIpsecSpd(self, self.tun_spd_id) |
| 91 | self.tun_spd.add_vpp_config() |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 92 | self.net_objs.append(self.tun_spd) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 93 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 94 | b = VppIpsecSpdItfBinding(self, self.tra_spd, self.tra_if) |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 95 | b.add_vpp_config() |
| 96 | self.net_objs.append(b) |
| 97 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 98 | b = VppIpsecSpdItfBinding(self, self.tun_spd, self.tun_if) |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 99 | b.add_vpp_config() |
| 100 | self.net_objs.append(b) |
| 101 | |
| 102 | for p in params: |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 103 | self.config_ah_tra(p) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 104 | config_tra_params(p, self.encryption_type) |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 105 | for p in params: |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 106 | self.config_ah_tun(p) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 107 | config_tun_params(p, self.encryption_type, self.tun_if) |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 108 | for p in params: |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 109 | d = DpoProto.DPO_PROTO_IP6 if p.is_ipv6 else DpoProto.DPO_PROTO_IP4 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 110 | r = VppIpRoute( |
| 111 | self, |
| 112 | p.remote_tun_if_host, |
| 113 | p.addr_len, |
| 114 | [ |
| 115 | VppRoutePath( |
| 116 | self.tun_if.remote_addr[p.addr_type], 0xFFFFFFFF, proto=d |
| 117 | ) |
| 118 | ], |
| 119 | ) |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 120 | r.add_vpp_config() |
| 121 | self.net_objs.append(r) |
| 122 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 123 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 124 | def unconfig_network(self): |
| 125 | for o in reversed(self.net_objs): |
| 126 | o.remove_vpp_config() |
| 127 | self.net_objs = [] |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 128 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 129 | def config_ah_tun(self, params): |
| 130 | addr_type = params.addr_type |
| 131 | scapy_tun_sa_id = params.scapy_tun_sa_id |
| 132 | scapy_tun_spi = params.scapy_tun_spi |
| 133 | vpp_tun_sa_id = params.vpp_tun_sa_id |
| 134 | vpp_tun_spi = params.vpp_tun_spi |
| 135 | auth_algo_vpp_id = params.auth_algo_vpp_id |
| 136 | auth_key = params.auth_key |
| 137 | crypt_algo_vpp_id = params.crypt_algo_vpp_id |
| 138 | crypt_key = params.crypt_key |
| 139 | remote_tun_if_host = params.remote_tun_if_host |
| 140 | addr_any = params.addr_any |
| 141 | addr_bcast = params.addr_bcast |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 142 | flags = params.flags |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 143 | tun_flags = params.tun_flags |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 144 | e = VppEnum.vl_api_ipsec_spd_action_t |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 145 | objs = [] |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 146 | params.outer_hop_limit = 253 |
| 147 | params.outer_flow_label = 0x12345 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 148 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 149 | params.tun_sa_in = VppIpsecSA( |
| 150 | self, |
| 151 | scapy_tun_sa_id, |
| 152 | scapy_tun_spi, |
| 153 | auth_algo_vpp_id, |
| 154 | auth_key, |
| 155 | crypt_algo_vpp_id, |
| 156 | crypt_key, |
| 157 | self.vpp_ah_protocol, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 158 | self.tun_if.remote_addr[addr_type], |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 159 | self.tun_if.local_addr[addr_type], |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 160 | tun_flags=tun_flags, |
| 161 | flags=flags, |
| 162 | dscp=params.dscp, |
| 163 | ) |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 164 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 165 | params.tun_sa_out = VppIpsecSA( |
| 166 | self, |
| 167 | vpp_tun_sa_id, |
| 168 | vpp_tun_spi, |
| 169 | auth_algo_vpp_id, |
| 170 | auth_key, |
| 171 | crypt_algo_vpp_id, |
| 172 | crypt_key, |
| 173 | self.vpp_ah_protocol, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 174 | self.tun_if.local_addr[addr_type], |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 175 | self.tun_if.remote_addr[addr_type], |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 176 | tun_flags=tun_flags, |
| 177 | flags=flags, |
| 178 | dscp=params.dscp, |
| 179 | ) |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 180 | |
| 181 | objs.append(params.tun_sa_in) |
| 182 | objs.append(params.tun_sa_out) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 183 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 184 | params.spd_policy_in_any = VppIpsecSpdEntry( |
| 185 | self, |
| 186 | self.tun_spd, |
| 187 | vpp_tun_sa_id, |
| 188 | addr_any, |
| 189 | addr_bcast, |
| 190 | addr_any, |
| 191 | addr_bcast, |
| 192 | socket.IPPROTO_AH, |
| 193 | ) |
| 194 | params.spd_policy_out_any = VppIpsecSpdEntry( |
| 195 | self, |
| 196 | self.tun_spd, |
| 197 | vpp_tun_sa_id, |
| 198 | addr_any, |
| 199 | addr_bcast, |
| 200 | addr_any, |
| 201 | addr_bcast, |
| 202 | socket.IPPROTO_AH, |
| 203 | is_outbound=0, |
| 204 | ) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 205 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 206 | objs.append(params.spd_policy_out_any) |
| 207 | objs.append(params.spd_policy_in_any) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 208 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 209 | e1 = VppIpsecSpdEntry( |
| 210 | self, |
| 211 | self.tun_spd, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 212 | scapy_tun_sa_id, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 213 | remote_tun_if_host, |
| 214 | remote_tun_if_host, |
| 215 | self.pg1.remote_addr[addr_type], |
| 216 | self.pg1.remote_addr[addr_type], |
Piotr Bronowski | 815c6a4 | 2022-06-09 09:09:28 +0000 | [diff] [blame] | 217 | socket.IPPROTO_RAW, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 218 | priority=10, |
| 219 | policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
| 220 | is_outbound=0, |
| 221 | ) |
| 222 | e2 = VppIpsecSpdEntry( |
| 223 | self, |
| 224 | self.tun_spd, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 225 | vpp_tun_sa_id, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 226 | self.pg1.remote_addr[addr_type], |
| 227 | self.pg1.remote_addr[addr_type], |
| 228 | remote_tun_if_host, |
| 229 | remote_tun_if_host, |
Piotr Bronowski | 815c6a4 | 2022-06-09 09:09:28 +0000 | [diff] [blame] | 230 | socket.IPPROTO_RAW, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 231 | policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
| 232 | priority=10, |
| 233 | ) |
| 234 | e3 = VppIpsecSpdEntry( |
| 235 | self, |
| 236 | self.tun_spd, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 237 | scapy_tun_sa_id, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 238 | remote_tun_if_host, |
| 239 | remote_tun_if_host, |
| 240 | self.pg0.local_addr[addr_type], |
| 241 | self.pg0.local_addr[addr_type], |
Piotr Bronowski | 815c6a4 | 2022-06-09 09:09:28 +0000 | [diff] [blame] | 242 | socket.IPPROTO_RAW, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 243 | priority=20, |
| 244 | policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
| 245 | is_outbound=0, |
| 246 | ) |
| 247 | e4 = VppIpsecSpdEntry( |
| 248 | self, |
| 249 | self.tun_spd, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 250 | vpp_tun_sa_id, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 251 | self.pg0.local_addr[addr_type], |
| 252 | self.pg0.local_addr[addr_type], |
| 253 | remote_tun_if_host, |
| 254 | remote_tun_if_host, |
Piotr Bronowski | 815c6a4 | 2022-06-09 09:09:28 +0000 | [diff] [blame] | 255 | socket.IPPROTO_RAW, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 256 | policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
| 257 | priority=20, |
| 258 | ) |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 259 | |
| 260 | objs = objs + [e1, e2, e3, e4] |
| 261 | |
| 262 | for o in objs: |
| 263 | o.add_vpp_config() |
| 264 | |
| 265 | self.net_objs = self.net_objs + objs |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 266 | |
| 267 | def config_ah_tra(self, params): |
| 268 | addr_type = params.addr_type |
| 269 | scapy_tra_sa_id = params.scapy_tra_sa_id |
| 270 | scapy_tra_spi = params.scapy_tra_spi |
| 271 | vpp_tra_sa_id = params.vpp_tra_sa_id |
| 272 | vpp_tra_spi = params.vpp_tra_spi |
| 273 | auth_algo_vpp_id = params.auth_algo_vpp_id |
| 274 | auth_key = params.auth_key |
| 275 | crypt_algo_vpp_id = params.crypt_algo_vpp_id |
| 276 | crypt_key = params.crypt_key |
| 277 | addr_any = params.addr_any |
| 278 | addr_bcast = params.addr_bcast |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 279 | flags = params.flags | ( |
| 280 | VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY |
| 281 | ) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 282 | e = VppEnum.vl_api_ipsec_spd_action_t |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 283 | objs = [] |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 284 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 285 | params.tra_sa_in = VppIpsecSA( |
| 286 | self, |
| 287 | scapy_tra_sa_id, |
| 288 | scapy_tra_spi, |
| 289 | auth_algo_vpp_id, |
| 290 | auth_key, |
| 291 | crypt_algo_vpp_id, |
| 292 | crypt_key, |
| 293 | self.vpp_ah_protocol, |
| 294 | flags=flags, |
| 295 | ) |
| 296 | params.tra_sa_out = VppIpsecSA( |
| 297 | self, |
| 298 | vpp_tra_sa_id, |
| 299 | vpp_tra_spi, |
| 300 | auth_algo_vpp_id, |
| 301 | auth_key, |
| 302 | crypt_algo_vpp_id, |
| 303 | crypt_key, |
| 304 | self.vpp_ah_protocol, |
| 305 | flags=flags, |
| 306 | ) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 307 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 308 | objs.append(params.tra_sa_in) |
| 309 | objs.append(params.tra_sa_out) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 310 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 311 | objs.append( |
| 312 | VppIpsecSpdEntry( |
| 313 | self, |
| 314 | self.tra_spd, |
| 315 | vpp_tra_sa_id, |
| 316 | addr_any, |
| 317 | addr_bcast, |
| 318 | addr_any, |
| 319 | addr_bcast, |
| 320 | socket.IPPROTO_AH, |
| 321 | ) |
| 322 | ) |
| 323 | objs.append( |
| 324 | VppIpsecSpdEntry( |
| 325 | self, |
| 326 | self.tra_spd, |
| 327 | scapy_tra_sa_id, |
| 328 | addr_any, |
| 329 | addr_bcast, |
| 330 | addr_any, |
| 331 | addr_bcast, |
| 332 | socket.IPPROTO_AH, |
| 333 | is_outbound=0, |
| 334 | ) |
| 335 | ) |
| 336 | objs.append( |
| 337 | VppIpsecSpdEntry( |
| 338 | self, |
| 339 | self.tra_spd, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 340 | scapy_tra_sa_id, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 341 | self.tra_if.local_addr[addr_type], |
| 342 | self.tra_if.local_addr[addr_type], |
| 343 | self.tra_if.remote_addr[addr_type], |
| 344 | self.tra_if.remote_addr[addr_type], |
Piotr Bronowski | 815c6a4 | 2022-06-09 09:09:28 +0000 | [diff] [blame] | 345 | socket.IPPROTO_RAW, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 346 | priority=10, |
| 347 | policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
| 348 | is_outbound=0, |
| 349 | ) |
| 350 | ) |
| 351 | objs.append( |
| 352 | VppIpsecSpdEntry( |
| 353 | self, |
| 354 | self.tra_spd, |
Arthur de Kerhor | 0df06b6 | 2022-11-16 18:45:24 +0100 | [diff] [blame] | 355 | vpp_tra_sa_id, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 356 | self.tra_if.local_addr[addr_type], |
| 357 | self.tra_if.local_addr[addr_type], |
| 358 | self.tra_if.remote_addr[addr_type], |
| 359 | self.tra_if.remote_addr[addr_type], |
Piotr Bronowski | 815c6a4 | 2022-06-09 09:09:28 +0000 | [diff] [blame] | 360 | socket.IPPROTO_RAW, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 361 | policy=e.IPSEC_API_SPD_ACTION_PROTECT, |
| 362 | priority=10, |
| 363 | ) |
| 364 | ) |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 365 | |
| 366 | for o in objs: |
| 367 | o.add_vpp_config() |
| 368 | self.net_objs = self.net_objs + objs |
| 369 | |
| 370 | |
| 371 | class TemplateIpsecAh(ConfigIpsecAH): |
| 372 | """ |
| 373 | Basic test for IPSEC using AH transport and Tunnel mode |
| 374 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 375 | TRANSPORT MODE:: |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 376 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 377 | --- encrypt --- |
| 378 | |pg2| <-------> |VPP| |
| 379 | --- decrypt --- |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 380 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 381 | TUNNEL MODE:: |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 382 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 383 | --- encrypt --- plain --- |
| 384 | |pg0| <------- |VPP| <------ |pg1| |
| 385 | --- --- --- |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 386 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 387 | --- decrypt --- plain --- |
| 388 | |pg0| -------> |VPP| ------> |pg1| |
| 389 | --- --- --- |
| 390 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 391 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 392 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 393 | @classmethod |
| 394 | def setUpClass(cls): |
| 395 | super(TemplateIpsecAh, cls).setUpClass() |
| 396 | |
| 397 | @classmethod |
| 398 | def tearDownClass(cls): |
| 399 | super(TemplateIpsecAh, cls).tearDownClass() |
| 400 | |
| 401 | def setUp(self): |
| 402 | super(TemplateIpsecAh, self).setUp() |
| 403 | self.config_network(self.params.values()) |
| 404 | |
| 405 | def tearDown(self): |
| 406 | self.unconfig_network() |
| 407 | super(TemplateIpsecAh, self).tearDown() |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 408 | |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 409 | |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 410 | class TestIpsecAh1(TemplateIpsecAh, IpsecTcpTests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 411 | """Ipsec AH - TCP tests""" |
| 412 | |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 413 | pass |
| 414 | |
| 415 | |
| 416 | class TestIpsecAh2(TemplateIpsecAh, IpsecTra46Tests, IpsecTun46Tests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 417 | """Ipsec AH w/ SHA1""" |
| 418 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 419 | pass |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 420 | |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 421 | |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 422 | class TestIpsecAhTun(TemplateIpsecAh, IpsecTun46Tests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 423 | """Ipsec AH - TUN encap tests""" |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 424 | |
| 425 | def setUp(self): |
| 426 | self.ipv4_params = IPsecIPv4Params() |
| 427 | self.ipv6_params = IPsecIPv6Params() |
| 428 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 429 | c = ( |
| 430 | VppEnum.vl_api_tunnel_encap_decap_flags_t.TUNNEL_API_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
| 431 | ) |
| 432 | c1 = c | ( |
| 433 | VppEnum.vl_api_tunnel_encap_decap_flags_t.TUNNEL_API_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN |
| 434 | ) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 435 | |
| 436 | self.ipv4_params.tun_flags = c |
| 437 | self.ipv6_params.tun_flags = c1 |
| 438 | |
| 439 | super(TestIpsecAhTun, self).setUp() |
| 440 | |
| 441 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=54): |
| 442 | # set the DSCP + ECN - flags are set to copy only DSCP |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 443 | return [ |
| 444 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 445 | / IP(src=src, dst=dst, tos=5) |
| 446 | / UDP(sport=4444, dport=4444) |
| 447 | / Raw(b"X" * payload_size) |
| 448 | for i in range(count) |
| 449 | ] |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 450 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 451 | def gen_pkts6(self, p, sw_intf, src, dst, count=1, payload_size=54): |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 452 | # set the DSCP + ECN - flags are set to copy both |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 453 | return [ |
| 454 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 455 | / IPv6(src=src, dst=dst, tc=5) |
| 456 | / UDP(sport=4444, dport=4444) |
| 457 | / Raw(b"X" * payload_size) |
| 458 | for i in range(count) |
| 459 | ] |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 460 | |
| 461 | def verify_encrypted(self, p, sa, rxs): |
| 462 | # just check that only the DSCP is copied |
| 463 | for rx in rxs: |
| 464 | self.assertEqual(rx[IP].tos, 4) |
| 465 | |
| 466 | def verify_encrypted6(self, p, sa, rxs): |
| 467 | # just check that the DSCP & ECN are copied |
| 468 | for rx in rxs: |
| 469 | self.assertEqual(rx[IPv6].tc, 5) |
| 470 | |
| 471 | |
| 472 | class TestIpsecAhTun2(TemplateIpsecAh, IpsecTun46Tests): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 473 | """Ipsec AH - TUN encap tests""" |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 474 | |
| 475 | def setUp(self): |
| 476 | self.ipv4_params = IPsecIPv4Params() |
| 477 | self.ipv6_params = IPsecIPv6Params() |
| 478 | |
| 479 | self.ipv4_params.dscp = 3 |
| 480 | self.ipv6_params.dscp = 4 |
| 481 | |
| 482 | super(TestIpsecAhTun2, self).setUp() |
| 483 | |
| 484 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=54): |
| 485 | # set the DSCP + ECN - flags are set to copy only DSCP |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 486 | return [ |
| 487 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 488 | / IP(src=src, dst=dst, tos=0) |
| 489 | / UDP(sport=4444, dport=4444) |
| 490 | / Raw(b"X" * payload_size) |
| 491 | for i in range(count) |
| 492 | ] |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 493 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 494 | def gen_pkts6(self, p, sw_intf, src, dst, count=1, payload_size=54): |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 495 | # set the DSCP + ECN - flags are set to copy both |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 496 | return [ |
| 497 | Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) |
| 498 | / IPv6(src=src, dst=dst, tc=0) |
| 499 | / UDP(sport=4444, dport=4444) |
| 500 | / Raw(b"X" * payload_size) |
| 501 | for i in range(count) |
| 502 | ] |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 503 | |
| 504 | def verify_encrypted(self, p, sa, rxs): |
| 505 | # just check that only the DSCP is copied |
| 506 | for rx in rxs: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 507 | self.assertEqual(rx[IP].tos, 0xC) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 508 | |
| 509 | def verify_encrypted6(self, p, sa, rxs): |
| 510 | # just check that the DSCP & ECN are copied |
| 511 | for rx in rxs: |
| 512 | self.assertEqual(rx[IPv6].tc, 0x10) |
| 513 | |
| 514 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 515 | class TestIpsecAhHandoff(TemplateIpsecAh, IpsecTun6HandoffTests, IpsecTun4HandoffTests): |
| 516 | """Ipsec AH Handoff""" |
| 517 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 518 | pass |
| 519 | |
| 520 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 521 | class TestIpsecAhAll(ConfigIpsecAH, IpsecTra4, IpsecTra6, IpsecTun4, IpsecTun6): |
| 522 | """Ipsec AH all Algos""" |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 523 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 524 | def setUp(self): |
| 525 | super(TestIpsecAhAll, self).setUp() |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 526 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 527 | def tearDown(self): |
| 528 | super(TestIpsecAhAll, self).tearDown() |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 529 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 530 | @unittest.skipIf( |
| 531 | "ping" in config.excluded_plugins, "Exclude tests requiring Ping plugin" |
| 532 | ) |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 533 | def test_integ_algs(self): |
Damjan Marion | 4cb8381 | 2019-04-24 17:32:01 +0200 | [diff] [blame] | 534 | """All Engines SHA[1_96, 256, 384, 512] w/ & w/o ESN""" |
Neale Ranns | 92e9384 | 2019-04-08 07:36:50 +0000 | [diff] [blame] | 535 | # foreach VPP crypto engine |
| 536 | engines = ["ia32", "ipsecmb", "openssl"] |
| 537 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 538 | algos = [ |
| 539 | { |
| 540 | "vpp": VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA1_96, |
| 541 | "scapy": "HMAC-SHA1-96", |
| 542 | }, |
| 543 | { |
| 544 | "vpp": VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA_256_128, |
| 545 | "scapy": "SHA2-256-128", |
| 546 | }, |
| 547 | { |
| 548 | "vpp": VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA_384_192, |
| 549 | "scapy": "SHA2-384-192", |
| 550 | }, |
| 551 | { |
| 552 | "vpp": VppEnum.vl_api_ipsec_integ_alg_t.IPSEC_API_INTEG_ALG_SHA_512_256, |
| 553 | "scapy": "SHA2-512-256", |
| 554 | }, |
| 555 | ] |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 556 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 557 | flags = [0, (VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN)] |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 558 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 559 | # |
Neale Ranns | 92e9384 | 2019-04-08 07:36:50 +0000 | [diff] [blame] | 560 | # loop through the VPP engines |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 561 | # |
Neale Ranns | 92e9384 | 2019-04-08 07:36:50 +0000 | [diff] [blame] | 562 | for engine in engines: |
Neale Ranns | 21ada3b | 2019-04-11 08:18:34 +0000 | [diff] [blame] | 563 | self.vapi.cli("set crypto handler all %s" % engine) |
Neale Ranns | 92e9384 | 2019-04-08 07:36:50 +0000 | [diff] [blame] | 564 | # |
| 565 | # loop through each of the algorithms |
| 566 | # |
| 567 | for algo in algos: |
| 568 | # with self.subTest(algo=algo['scapy']): |
| 569 | for flag in flags: |
| 570 | # |
| 571 | # setup up the config paramters |
| 572 | # |
| 573 | self.ipv4_params = IPsecIPv4Params() |
| 574 | self.ipv6_params = IPsecIPv6Params() |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 575 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 576 | self.params = { |
| 577 | self.ipv4_params.addr_type: self.ipv4_params, |
| 578 | self.ipv6_params.addr_type: self.ipv6_params, |
| 579 | } |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 580 | |
Neale Ranns | 92e9384 | 2019-04-08 07:36:50 +0000 | [diff] [blame] | 581 | for _, p in self.params.items(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 582 | p.auth_algo_vpp_id = algo["vpp"] |
| 583 | p.auth_algo = algo["scapy"] |
Neale Ranns | 92e9384 | 2019-04-08 07:36:50 +0000 | [diff] [blame] | 584 | p.flags = p.flags | flag |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 585 | |
Neale Ranns | 92e9384 | 2019-04-08 07:36:50 +0000 | [diff] [blame] | 586 | # |
| 587 | # configure the SPDs. SAs, etc |
| 588 | # |
| 589 | self.config_network(self.params.values()) |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 590 | |
Neale Ranns | 92e9384 | 2019-04-08 07:36:50 +0000 | [diff] [blame] | 591 | # |
| 592 | # run some traffic. |
| 593 | # An exhautsive 4o6, 6o4 is not necessary for each algo |
| 594 | # |
| 595 | self.verify_tra_basic6(count=17) |
| 596 | self.verify_tra_basic4(count=17) |
| 597 | self.verify_tun_66(self.params[socket.AF_INET6], count=17) |
| 598 | self.verify_tun_44(self.params[socket.AF_INET], count=17) |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 599 | |
Neale Ranns | 92e9384 | 2019-04-08 07:36:50 +0000 | [diff] [blame] | 600 | # |
| 601 | # remove the SPDs, SAs, etc |
| 602 | # |
| 603 | self.unconfig_network() |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 604 | |
| 605 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 606 | if __name__ == "__main__": |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 607 | unittest.main(testRunner=VppTestRunner) |