Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 1 | from vpp_object import * |
| 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 | |
| 27 | def __str__(self): |
| 28 | return self.object_id() |
| 29 | |
| 30 | def object_id(self): |
| 31 | return "ipsec-spd-%d" % self.id |
| 32 | |
| 33 | def query_vpp_config(self): |
| 34 | spds = self.test.vapi.ipsec_spds_dump() |
| 35 | for spd in spds: |
| 36 | if spd.spd_id == self.id: |
| 37 | return True |
| 38 | return False |
| 39 | |
| 40 | |
| 41 | class VppIpsecSpdItfBinding(VppObject): |
| 42 | """ |
| 43 | VPP SPD DB to interface binding |
| 44 | (i.e. this SPD is used on this interfce) |
| 45 | """ |
| 46 | |
| 47 | def __init__(self, test, spd, itf): |
| 48 | self.test = test |
| 49 | self.spd = spd |
| 50 | self.itf = itf |
| 51 | |
| 52 | def add_vpp_config(self): |
| 53 | self.test.vapi.ipsec_interface_add_del_spd(self.spd.id, |
| 54 | self.itf.sw_if_index) |
| 55 | self.test.registry.register(self, self.test.logger) |
| 56 | |
| 57 | def remove_vpp_config(self): |
| 58 | self.test.vapi.ipsec_interface_add_del_spd(self.spd.id, |
| 59 | self.itf.sw_if_index, |
| 60 | is_add=0) |
| 61 | |
| 62 | def __str__(self): |
| 63 | return self.object_id() |
| 64 | |
| 65 | def object_id(self): |
| 66 | return "bind-%s-to-%s" % (self.spd.id, self.itf) |
| 67 | |
| 68 | def query_vpp_config(self): |
| 69 | bs = self.test.vapi.ipsec_spd_interface_dump() |
| 70 | for b in bs: |
| 71 | if b.sw_if_index == self.itf.sw_if_index: |
| 72 | return True |
| 73 | return False |
| 74 | |
| 75 | |
| 76 | class VppIpsecSpdEntry(VppObject): |
| 77 | """ |
| 78 | VPP SPD DB Entry |
| 79 | """ |
| 80 | |
| 81 | def __init__(self, test, spd, sa_id, |
| 82 | local_start, local_stop, |
| 83 | remote_start, remote_stop, |
| 84 | proto, |
| 85 | priority=100, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 86 | policy=None, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 87 | is_outbound=1, |
| 88 | remote_port_start=0, |
| 89 | remote_port_stop=65535, |
| 90 | local_port_start=0, |
| 91 | local_port_stop=65535): |
| 92 | self.test = test |
| 93 | self.spd = spd |
| 94 | self.sa_id = sa_id |
| 95 | self.local_start = ip_address(text_type(local_start)) |
| 96 | self.local_stop = ip_address(text_type(local_stop)) |
| 97 | self.remote_start = ip_address(text_type(remote_start)) |
| 98 | self.remote_stop = ip_address(text_type(remote_stop)) |
| 99 | self.proto = proto |
| 100 | self.is_outbound = is_outbound |
| 101 | self.priority = priority |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 102 | if not policy: |
| 103 | self.policy = (VppEnum.vl_api_ipsec_spd_action_t. |
| 104 | IPSEC_API_SPD_ACTION_BYPASS) |
| 105 | else: |
| 106 | self.policy = policy |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 107 | self.is_ipv6 = (0 if self.local_start.version == 4 else 1) |
| 108 | self.local_port_start = local_port_start |
| 109 | self.local_port_stop = local_port_stop |
| 110 | self.remote_port_start = remote_port_start |
| 111 | self.remote_port_stop = remote_port_stop |
| 112 | |
| 113 | def add_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 114 | self.test.vapi.ipsec_spd_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 115 | self.spd.id, |
| 116 | self.sa_id, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 117 | self.local_start, |
| 118 | self.local_stop, |
| 119 | self.remote_start, |
| 120 | self.remote_stop, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 121 | protocol=self.proto, |
| 122 | is_ipv6=self.is_ipv6, |
| 123 | is_outbound=self.is_outbound, |
| 124 | priority=self.priority, |
| 125 | policy=self.policy, |
| 126 | local_port_start=self.local_port_start, |
| 127 | local_port_stop=self.local_port_stop, |
| 128 | remote_port_start=self.remote_port_start, |
| 129 | remote_port_stop=self.remote_port_stop) |
| 130 | self.test.registry.register(self, self.test.logger) |
| 131 | |
| 132 | def remove_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 133 | self.test.vapi.ipsec_spd_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 134 | self.spd.id, |
| 135 | self.sa_id, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 136 | self.local_start, |
| 137 | self.local_stop, |
| 138 | self.remote_start, |
| 139 | self.remote_stop, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 140 | protocol=self.proto, |
| 141 | is_ipv6=self.is_ipv6, |
| 142 | is_outbound=self.is_outbound, |
| 143 | priority=self.priority, |
| 144 | policy=self.policy, |
| 145 | local_port_start=self.local_port_start, |
| 146 | local_port_stop=self.local_port_stop, |
| 147 | remote_port_start=self.remote_port_start, |
| 148 | remote_port_stop=self.remote_port_stop, |
| 149 | is_add=0) |
| 150 | |
| 151 | def __str__(self): |
| 152 | return self.object_id() |
| 153 | |
| 154 | def object_id(self): |
| 155 | return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id, |
| 156 | self.priority, |
| 157 | self.policy, |
| 158 | self.is_outbound, |
| 159 | self.is_ipv6, |
| 160 | self.remote_port_start) |
| 161 | |
| 162 | def query_vpp_config(self): |
| 163 | ss = self.test.vapi.ipsec_spd_dump(self.spd.id) |
| 164 | for s in ss: |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 165 | if s.entry.sa_id == self.sa_id and \ |
| 166 | s.entry.is_outbound == self.is_outbound and \ |
| 167 | s.entry.priority == self.priority and \ |
| 168 | s.entry.policy == self.policy and \ |
| 169 | s.entry.remote_address_start == self.remote_start and \ |
| 170 | s.entry.remote_port_start == self.remote_port_start: |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 171 | return True |
| 172 | return False |
| 173 | |
| 174 | |
| 175 | class VppIpsecSA(VppObject): |
| 176 | """ |
| 177 | VPP SAD Entry |
| 178 | """ |
| 179 | |
| 180 | def __init__(self, test, id, spi, |
| 181 | integ_alg, integ_key, |
| 182 | crypto_alg, crypto_key, |
| 183 | proto, |
| 184 | tun_src=None, tun_dst=None, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 185 | flags=None): |
| 186 | e = VppEnum.vl_api_ipsec_sad_flags_t |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 187 | self.test = test |
| 188 | self.id = id |
| 189 | self.spi = spi |
| 190 | self.integ_alg = integ_alg |
| 191 | self.integ_key = integ_key |
| 192 | self.crypto_alg = crypto_alg |
| 193 | self.crypto_key = crypto_key |
| 194 | self.proto = proto |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 195 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 196 | self.tun_src = tun_src |
| 197 | self.tun_dst = tun_dst |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 198 | if not flags: |
| 199 | self.flags = e.IPSEC_API_SAD_FLAG_NONE |
| 200 | else: |
| 201 | self.flags = flags |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 202 | if (tun_src): |
| 203 | self.tun_src = ip_address(text_type(tun_src)) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 204 | self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 205 | if (self.tun_src.version == 6): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 206 | self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6 |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 207 | if (tun_dst): |
| 208 | self.tun_dst = ip_address(text_type(tun_dst)) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 209 | |
| 210 | def add_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 211 | self.test.vapi.ipsec_sad_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 212 | self.id, |
| 213 | self.spi, |
| 214 | self.integ_alg, |
| 215 | self.integ_key, |
| 216 | self.crypto_alg, |
| 217 | self.crypto_key, |
| 218 | self.proto, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 219 | (self.tun_src if self.tun_src else []), |
| 220 | (self.tun_dst if self.tun_dst else []), |
| 221 | flags=self.flags) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 222 | self.test.registry.register(self, self.test.logger) |
| 223 | |
| 224 | def remove_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 225 | self.test.vapi.ipsec_sad_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 226 | self.id, |
| 227 | self.spi, |
| 228 | self.integ_alg, |
| 229 | self.integ_key, |
| 230 | self.crypto_alg, |
| 231 | self.crypto_key, |
| 232 | self.proto, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 233 | (self.tun_src if self.tun_src else []), |
| 234 | (self.tun_dst if self.tun_dst else []), |
| 235 | flags=self.flags, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 236 | is_add=0) |
| 237 | |
| 238 | def __str__(self): |
| 239 | return self.object_id() |
| 240 | |
| 241 | def object_id(self): |
| 242 | return "ipsec-sa-%d" % self.id |
| 243 | |
| 244 | def query_vpp_config(self): |
| 245 | bs = self.test.vapi.ipsec_sa_dump() |
| 246 | for b in bs: |
| 247 | if b.sa_id == self.id: |
| 248 | return True |
| 249 | return False |