Paul Vinciguerra | a279d9c | 2019-02-28 09:00:09 -0800 | [diff] [blame] | 1 | from vpp_object import VppObject |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 2 | from ipaddress import ip_address |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 3 | from vpp_papi import VppEnum |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 4 | |
| 5 | try: |
| 6 | text_type = unicode |
| 7 | except NameError: |
| 8 | text_type = str |
| 9 | |
| 10 | |
| 11 | class VppIpsecSpd(VppObject): |
| 12 | """ |
| 13 | VPP SPD DB |
| 14 | """ |
| 15 | |
| 16 | def __init__(self, test, id): |
| 17 | self.test = test |
| 18 | self.id = id |
| 19 | |
| 20 | def add_vpp_config(self): |
| 21 | self.test.vapi.ipsec_spd_add_del(self.id) |
| 22 | self.test.registry.register(self, self.test.logger) |
| 23 | |
| 24 | def remove_vpp_config(self): |
| 25 | self.test.vapi.ipsec_spd_add_del(self.id, is_add=0) |
| 26 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 27 | def object_id(self): |
| 28 | return "ipsec-spd-%d" % self.id |
| 29 | |
| 30 | def query_vpp_config(self): |
| 31 | spds = self.test.vapi.ipsec_spds_dump() |
| 32 | for spd in spds: |
| 33 | if spd.spd_id == self.id: |
| 34 | return True |
| 35 | return False |
| 36 | |
| 37 | |
| 38 | class VppIpsecSpdItfBinding(VppObject): |
| 39 | """ |
| 40 | VPP SPD DB to interface binding |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 41 | (i.e. this SPD is used on this interface) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 42 | """ |
| 43 | |
| 44 | def __init__(self, test, spd, itf): |
| 45 | self.test = test |
| 46 | self.spd = spd |
| 47 | self.itf = itf |
| 48 | |
| 49 | def add_vpp_config(self): |
| 50 | self.test.vapi.ipsec_interface_add_del_spd(self.spd.id, |
| 51 | self.itf.sw_if_index) |
| 52 | self.test.registry.register(self, self.test.logger) |
| 53 | |
| 54 | def remove_vpp_config(self): |
| 55 | self.test.vapi.ipsec_interface_add_del_spd(self.spd.id, |
| 56 | self.itf.sw_if_index, |
| 57 | is_add=0) |
| 58 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 59 | def object_id(self): |
| 60 | return "bind-%s-to-%s" % (self.spd.id, self.itf) |
| 61 | |
| 62 | def query_vpp_config(self): |
| 63 | bs = self.test.vapi.ipsec_spd_interface_dump() |
| 64 | for b in bs: |
| 65 | if b.sw_if_index == self.itf.sw_if_index: |
| 66 | return True |
| 67 | return False |
| 68 | |
| 69 | |
| 70 | class VppIpsecSpdEntry(VppObject): |
| 71 | """ |
| 72 | VPP SPD DB Entry |
| 73 | """ |
| 74 | |
| 75 | def __init__(self, test, spd, sa_id, |
| 76 | local_start, local_stop, |
| 77 | remote_start, remote_stop, |
| 78 | proto, |
| 79 | priority=100, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 80 | policy=None, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 81 | is_outbound=1, |
| 82 | remote_port_start=0, |
| 83 | remote_port_stop=65535, |
| 84 | local_port_start=0, |
| 85 | local_port_stop=65535): |
| 86 | self.test = test |
| 87 | self.spd = spd |
| 88 | self.sa_id = sa_id |
| 89 | self.local_start = ip_address(text_type(local_start)) |
| 90 | self.local_stop = ip_address(text_type(local_stop)) |
| 91 | self.remote_start = ip_address(text_type(remote_start)) |
| 92 | self.remote_stop = ip_address(text_type(remote_stop)) |
| 93 | self.proto = proto |
| 94 | self.is_outbound = is_outbound |
| 95 | self.priority = priority |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 96 | if not policy: |
| 97 | self.policy = (VppEnum.vl_api_ipsec_spd_action_t. |
| 98 | IPSEC_API_SPD_ACTION_BYPASS) |
| 99 | else: |
| 100 | self.policy = policy |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 101 | self.is_ipv6 = (0 if self.local_start.version == 4 else 1) |
| 102 | self.local_port_start = local_port_start |
| 103 | self.local_port_stop = local_port_stop |
| 104 | self.remote_port_start = remote_port_start |
| 105 | self.remote_port_stop = remote_port_stop |
| 106 | |
| 107 | def add_vpp_config(self): |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 108 | rv = self.test.vapi.ipsec_spd_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 109 | self.spd.id, |
| 110 | self.sa_id, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 111 | self.local_start, |
| 112 | self.local_stop, |
| 113 | self.remote_start, |
| 114 | self.remote_stop, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 115 | protocol=self.proto, |
| 116 | is_ipv6=self.is_ipv6, |
| 117 | is_outbound=self.is_outbound, |
| 118 | priority=self.priority, |
| 119 | policy=self.policy, |
| 120 | local_port_start=self.local_port_start, |
| 121 | local_port_stop=self.local_port_stop, |
| 122 | remote_port_start=self.remote_port_start, |
| 123 | remote_port_stop=self.remote_port_stop) |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 124 | self.stat_index = rv.stat_index |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 125 | self.test.registry.register(self, self.test.logger) |
| 126 | |
| 127 | def remove_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 128 | self.test.vapi.ipsec_spd_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 129 | self.spd.id, |
| 130 | self.sa_id, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 131 | self.local_start, |
| 132 | self.local_stop, |
| 133 | self.remote_start, |
| 134 | self.remote_stop, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 135 | protocol=self.proto, |
| 136 | is_ipv6=self.is_ipv6, |
| 137 | is_outbound=self.is_outbound, |
| 138 | priority=self.priority, |
| 139 | policy=self.policy, |
| 140 | local_port_start=self.local_port_start, |
| 141 | local_port_stop=self.local_port_stop, |
| 142 | remote_port_start=self.remote_port_start, |
| 143 | remote_port_stop=self.remote_port_stop, |
| 144 | is_add=0) |
| 145 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 146 | def object_id(self): |
| 147 | return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id, |
| 148 | self.priority, |
| 149 | self.policy, |
| 150 | self.is_outbound, |
| 151 | self.is_ipv6, |
| 152 | self.remote_port_start) |
| 153 | |
| 154 | def query_vpp_config(self): |
| 155 | ss = self.test.vapi.ipsec_spd_dump(self.spd.id) |
| 156 | for s in ss: |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 157 | if s.entry.sa_id == self.sa_id and \ |
| 158 | s.entry.is_outbound == self.is_outbound and \ |
| 159 | s.entry.priority == self.priority and \ |
| 160 | s.entry.policy == self.policy and \ |
| 161 | s.entry.remote_address_start == self.remote_start and \ |
| 162 | s.entry.remote_port_start == self.remote_port_start: |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 163 | return True |
| 164 | return False |
| 165 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 166 | def get_stats(self): |
| 167 | c = self.test.statistics.get_counter("/net/ipsec/policy") |
| 168 | return c[0][self.stat_index] |
| 169 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 170 | |
| 171 | class VppIpsecSA(VppObject): |
| 172 | """ |
| 173 | VPP SAD Entry |
| 174 | """ |
| 175 | |
| 176 | def __init__(self, test, id, spi, |
| 177 | integ_alg, integ_key, |
| 178 | crypto_alg, crypto_key, |
| 179 | proto, |
| 180 | tun_src=None, tun_dst=None, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 181 | flags=None, salt=0): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 182 | e = VppEnum.vl_api_ipsec_sad_flags_t |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 183 | self.test = test |
| 184 | self.id = id |
| 185 | self.spi = spi |
| 186 | self.integ_alg = integ_alg |
| 187 | self.integ_key = integ_key |
| 188 | self.crypto_alg = crypto_alg |
| 189 | self.crypto_key = crypto_key |
| 190 | self.proto = proto |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 191 | self.salt = salt |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 192 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 193 | self.tun_src = tun_src |
| 194 | self.tun_dst = tun_dst |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 195 | if not flags: |
| 196 | self.flags = e.IPSEC_API_SAD_FLAG_NONE |
| 197 | else: |
| 198 | self.flags = flags |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 199 | if (tun_src): |
| 200 | self.tun_src = ip_address(text_type(tun_src)) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 201 | self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 202 | if (self.tun_src.version == 6): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 203 | self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6 |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 204 | if (tun_dst): |
| 205 | self.tun_dst = ip_address(text_type(tun_dst)) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 206 | |
| 207 | def add_vpp_config(self): |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 208 | r = self.test.vapi.ipsec_sad_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 209 | self.id, |
| 210 | self.spi, |
| 211 | self.integ_alg, |
| 212 | self.integ_key, |
| 213 | self.crypto_alg, |
| 214 | self.crypto_key, |
| 215 | self.proto, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 216 | (self.tun_src if self.tun_src else []), |
| 217 | (self.tun_dst if self.tun_dst else []), |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 218 | flags=self.flags, |
| 219 | salt=self.salt) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 220 | self.stat_index = r.stat_index |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 221 | self.test.registry.register(self, self.test.logger) |
| 222 | |
| 223 | def remove_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 224 | self.test.vapi.ipsec_sad_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 225 | self.id, |
| 226 | self.spi, |
| 227 | self.integ_alg, |
| 228 | self.integ_key, |
| 229 | self.crypto_alg, |
| 230 | self.crypto_key, |
| 231 | self.proto, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 232 | (self.tun_src if self.tun_src else []), |
| 233 | (self.tun_dst if self.tun_dst else []), |
| 234 | flags=self.flags, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 235 | is_add=0) |
| 236 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 237 | def object_id(self): |
| 238 | return "ipsec-sa-%d" % self.id |
| 239 | |
| 240 | def query_vpp_config(self): |
| 241 | bs = self.test.vapi.ipsec_sa_dump() |
| 242 | for b in bs: |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 243 | if b.entry.sad_id == self.id: |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 244 | return True |
| 245 | return False |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 246 | |
| 247 | def get_stats(self): |
| 248 | c = self.test.statistics.get_counter("/net/ipsec/sa") |
| 249 | return c[0][self.stat_index] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 250 | |
| 251 | |
| 252 | class VppIpsecTunProtect(VppObject): |
| 253 | """ |
| 254 | VPP IPSEC tunnel protection |
| 255 | """ |
| 256 | |
| 257 | def __init__(self, test, itf, sa_out, sas_in): |
| 258 | self.test = test |
| 259 | self.itf = itf |
| 260 | self.sas_in = [] |
| 261 | for sa in sas_in: |
| 262 | self.sas_in.append(sa.id) |
| 263 | self.sa_out = sa_out.id |
| 264 | |
| 265 | def update_vpp_config(self, sa_out, sas_in): |
| 266 | self.sas_in = [] |
| 267 | for sa in sas_in: |
| 268 | self.sas_in.append(sa.id) |
| 269 | self.sa_out = sa_out.id |
| 270 | self.test.vapi.ipsec_tunnel_protect_update( |
| 271 | tunnel={ |
| 272 | 'sw_if_index': self.itf._sw_if_index, |
| 273 | 'n_sa_in': len(self.sas_in), |
| 274 | 'sa_out': self.sa_out, |
| 275 | 'sa_in': self.sas_in}) |
| 276 | |
| 277 | def object_id(self): |
| 278 | return "ipsec-tun-protect-%s" % self.itf |
| 279 | |
| 280 | def add_vpp_config(self): |
| 281 | self.test.vapi.ipsec_tunnel_protect_update( |
| 282 | tunnel={ |
| 283 | 'sw_if_index': self.itf._sw_if_index, |
| 284 | 'n_sa_in': len(self.sas_in), |
| 285 | 'sa_out': self.sa_out, |
| 286 | 'sa_in': self.sas_in}) |
| 287 | self.test.registry.register(self, self.test.logger) |
| 288 | |
| 289 | def remove_vpp_config(self): |
| 290 | self.test.vapi.ipsec_tunnel_protect_del( |
| 291 | sw_if_index=self.itf.sw_if_index) |
| 292 | |
| 293 | def query_vpp_config(self): |
| 294 | bs = self.test.vapi.ipsec_tunnel_protect_dump( |
| 295 | sw_if_index=self.itf.sw_if_index) |
| 296 | for b in bs: |
| 297 | if b.tun.sw_if_index == self.itf.sw_if_index: |
| 298 | return True |
| 299 | return False |