Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * ipsec_api.c - ipsec api |
| 4 | * |
| 5 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/vnet.h> |
| 21 | #include <vlibmemory/api.h> |
| 22 | |
| 23 | #include <vnet/interface.h> |
| 24 | #include <vnet/api_errno.h> |
| 25 | #include <vnet/ip/ip.h> |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 26 | #include <vnet/ip/ip_types_api.h> |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 27 | #include <vnet/fib/fib.h> |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 28 | |
| 29 | #include <vnet/vnet_msg_enum.h> |
| 30 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 31 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 32 | #include <vnet/ipsec/ipsec.h> |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 33 | #endif /* IPSEC */ |
| 34 | |
| 35 | #define vl_typedefs /* define message structures */ |
| 36 | #include <vnet/vnet_all_api_h.h> |
| 37 | #undef vl_typedefs |
| 38 | |
| 39 | #define vl_endianfun /* define message structures */ |
| 40 | #include <vnet/vnet_all_api_h.h> |
| 41 | #undef vl_endianfun |
| 42 | |
| 43 | /* instantiate all the print functions we know about */ |
| 44 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 45 | #define vl_printfun |
| 46 | #include <vnet/vnet_all_api_h.h> |
| 47 | #undef vl_printfun |
| 48 | |
| 49 | #include <vlibapi/api_helper_macros.h> |
| 50 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 51 | #define foreach_vpe_api_msg \ |
| 52 | _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \ |
| 53 | _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \ |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 54 | _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \ |
| 55 | _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \ |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 56 | _(IPSEC_SA_SET_KEY, ipsec_sa_set_key) \ |
| 57 | _(IPSEC_SA_DUMP, ipsec_sa_dump) \ |
| 58 | _(IPSEC_SPDS_DUMP, ipsec_spds_dump) \ |
| 59 | _(IPSEC_SPD_DUMP, ipsec_spd_dump) \ |
| 60 | _(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \ |
| 61 | _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \ |
| 62 | _(IPSEC_TUNNEL_IF_SET_KEY, ipsec_tunnel_if_set_key) \ |
| 63 | _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \ |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 64 | _(IPSEC_SELECT_BACKEND, ipsec_select_backend) \ |
| 65 | _(IPSEC_BACKEND_DUMP, ipsec_backend_dump) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 66 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 67 | static void |
| 68 | vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 69 | { |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 70 | #if WITH_LIBSSL == 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 71 | clib_warning ("unimplemented"); |
| 72 | #else |
| 73 | |
| 74 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 75 | vl_api_ipsec_spd_add_del_reply_t *rmp; |
| 76 | int rv; |
| 77 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 78 | rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 79 | |
| 80 | REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | static void vl_api_ipsec_interface_add_del_spd_t_handler |
| 85 | (vl_api_ipsec_interface_add_del_spd_t * mp) |
| 86 | { |
| 87 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 88 | vl_api_ipsec_interface_add_del_spd_reply_t *rmp; |
| 89 | int rv; |
| 90 | u32 sw_if_index __attribute__ ((unused)); |
| 91 | u32 spd_id __attribute__ ((unused)); |
| 92 | |
| 93 | sw_if_index = ntohl (mp->sw_if_index); |
| 94 | spd_id = ntohl (mp->spd_id); |
| 95 | |
| 96 | VALIDATE_SW_IF_INDEX (mp); |
| 97 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 98 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 99 | rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add); |
| 100 | #else |
| 101 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 102 | #endif |
| 103 | |
| 104 | BAD_SW_IF_INDEX_LABEL; |
| 105 | |
| 106 | REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY); |
| 107 | } |
| 108 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 109 | static int |
| 110 | ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in, |
| 111 | ipsec_policy_action_t * out) |
| 112 | { |
| 113 | in = clib_net_to_host_u32 (in); |
| 114 | |
| 115 | switch (in) |
| 116 | { |
| 117 | #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \ |
| 118 | *out = IPSEC_POLICY_ACTION_##f; \ |
| 119 | return (0); |
| 120 | foreach_ipsec_policy_action |
| 121 | #undef _ |
| 122 | } |
| 123 | return (VNET_API_ERROR_UNIMPLEMENTED); |
| 124 | } |
| 125 | |
| 126 | static void vl_api_ipsec_spd_entry_add_del_t_handler |
| 127 | (vl_api_ipsec_spd_entry_add_del_t * mp) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 128 | { |
| 129 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 130 | vl_api_ipsec_spd_entry_add_del_reply_t *rmp; |
| 131 | ip46_type_t itype; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 132 | u32 stat_index; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 133 | int rv; |
| 134 | |
Neale Ranns | 8c2dd1b | 2019-02-19 08:27:34 -0800 | [diff] [blame] | 135 | stat_index = ~0; |
| 136 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 137 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 138 | ipsec_policy_t p; |
| 139 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 140 | clib_memset (&p, 0, sizeof (p)); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 141 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 142 | p.id = ntohl (mp->entry.spd_id); |
| 143 | p.priority = ntohl (mp->entry.priority); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 144 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 145 | itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start); |
| 146 | ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop); |
| 147 | ip_address_decode (&mp->entry.local_address_start, &p.laddr.start); |
| 148 | ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop); |
| 149 | |
| 150 | p.is_ipv6 = (itype == IP46_TYPE_IP6); |
| 151 | |
| 152 | p.protocol = mp->entry.protocol; |
Neale Ranns | 231c469 | 2019-03-18 17:11:28 +0000 | [diff] [blame] | 153 | /* leave the ports in network order */ |
| 154 | p.rport.start = mp->entry.remote_port_start; |
| 155 | p.rport.stop = mp->entry.remote_port_stop; |
| 156 | p.lport.start = mp->entry.local_port_start; |
| 157 | p.lport.stop = mp->entry.local_port_stop; |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 158 | |
| 159 | rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy); |
| 160 | |
| 161 | if (rv) |
| 162 | goto out; |
| 163 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 164 | /* policy action resolve unsupported */ |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 165 | if (p.policy == IPSEC_POLICY_ACTION_RESOLVE) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 166 | { |
| 167 | clib_warning ("unsupported action: 'resolve'"); |
| 168 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 169 | goto out; |
| 170 | } |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 171 | p.sa_id = ntohl (mp->entry.sa_id); |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 172 | rv = |
| 173 | ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy, |
| 174 | &p.type); |
| 175 | if (rv) |
| 176 | goto out; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 177 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 178 | rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 179 | if (rv) |
| 180 | goto out; |
| 181 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 182 | #else |
| 183 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 184 | goto out; |
| 185 | #endif |
| 186 | |
| 187 | out: |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 188 | /* *INDENT-OFF* */ |
| 189 | REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY, |
| 190 | ({ |
| 191 | rmp->stat_index = ntohl(stat_index); |
| 192 | })); |
| 193 | /* *INDENT-ON* */ |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 196 | static int |
| 197 | ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out) |
| 198 | { |
| 199 | in = clib_net_to_host_u32 (in); |
| 200 | |
| 201 | switch (in) |
| 202 | { |
| 203 | case IPSEC_API_PROTO_ESP: |
| 204 | *out = IPSEC_PROTOCOL_ESP; |
| 205 | return (0); |
| 206 | case IPSEC_API_PROTO_AH: |
| 207 | *out = IPSEC_PROTOCOL_AH; |
| 208 | return (0); |
| 209 | } |
Neale Ranns | af3f078 | 2019-03-26 08:21:25 +0000 | [diff] [blame] | 210 | return (VNET_API_ERROR_INVALID_PROTOCOL); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 213 | static vl_api_ipsec_proto_t |
| 214 | ipsec_proto_encode (ipsec_protocol_t p) |
| 215 | { |
| 216 | switch (p) |
| 217 | { |
| 218 | case IPSEC_PROTOCOL_ESP: |
| 219 | return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP); |
| 220 | case IPSEC_PROTOCOL_AH: |
| 221 | return clib_host_to_net_u32 (IPSEC_API_PROTO_AH); |
| 222 | } |
| 223 | return (VNET_API_ERROR_UNIMPLEMENTED); |
| 224 | } |
| 225 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 226 | static int |
| 227 | ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in, |
| 228 | ipsec_crypto_alg_t * out) |
| 229 | { |
| 230 | in = clib_net_to_host_u32 (in); |
| 231 | |
| 232 | switch (in) |
| 233 | { |
| 234 | #define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \ |
| 235 | *out = IPSEC_CRYPTO_ALG_##f; \ |
| 236 | return (0); |
| 237 | foreach_ipsec_crypto_alg |
| 238 | #undef _ |
| 239 | } |
Neale Ranns | af3f078 | 2019-03-26 08:21:25 +0000 | [diff] [blame] | 240 | return (VNET_API_ERROR_INVALID_ALGORITHM); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 243 | static vl_api_ipsec_crypto_alg_t |
| 244 | ipsec_crypto_algo_encode (ipsec_crypto_alg_t c) |
| 245 | { |
| 246 | switch (c) |
| 247 | { |
| 248 | #define _(v,f,s) case IPSEC_CRYPTO_ALG_##f: \ |
| 249 | return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f); |
| 250 | foreach_ipsec_crypto_alg |
| 251 | #undef _ |
| 252 | case IPSEC_CRYPTO_N_ALG: |
| 253 | break; |
| 254 | } |
| 255 | ASSERT (0); |
| 256 | return (VNET_API_ERROR_UNIMPLEMENTED); |
| 257 | } |
| 258 | |
| 259 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 260 | static int |
| 261 | ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out) |
| 262 | { |
| 263 | in = clib_net_to_host_u32 (in); |
| 264 | |
| 265 | switch (in) |
| 266 | { |
| 267 | #define _(v,f,s) case IPSEC_API_INTEG_ALG_##f: \ |
| 268 | *out = IPSEC_INTEG_ALG_##f; \ |
| 269 | return (0); |
| 270 | foreach_ipsec_integ_alg |
| 271 | #undef _ |
| 272 | } |
Neale Ranns | af3f078 | 2019-03-26 08:21:25 +0000 | [diff] [blame] | 273 | return (VNET_API_ERROR_INVALID_ALGORITHM); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 276 | static vl_api_ipsec_integ_alg_t |
| 277 | ipsec_integ_algo_encode (ipsec_integ_alg_t i) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 278 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 279 | switch (i) |
| 280 | { |
| 281 | #define _(v,f,s) case IPSEC_INTEG_ALG_##f: \ |
| 282 | return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f)); |
| 283 | foreach_ipsec_integ_alg |
| 284 | #undef _ |
| 285 | case IPSEC_INTEG_N_ALG: |
| 286 | break; |
| 287 | } |
| 288 | ASSERT (0); |
| 289 | return (VNET_API_ERROR_UNIMPLEMENTED); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 293 | ipsec_key_decode (const vl_api_key_t * key, ipsec_key_t * out) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 294 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 295 | ipsec_mk_key (out, key->data, key->length); |
| 296 | } |
| 297 | |
| 298 | static void |
| 299 | ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out) |
| 300 | { |
| 301 | out->length = in->len; |
| 302 | clib_memcpy (out->data, in->data, out->length); |
| 303 | } |
| 304 | |
| 305 | static ipsec_sa_flags_t |
| 306 | ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in) |
| 307 | { |
| 308 | ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE; |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 309 | in = clib_net_to_host_u32 (in); |
| 310 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 311 | #define _(v,f,s) if (in & IPSEC_API_SAD_FLAG_##f) \ |
| 312 | flags |= IPSEC_SA_FLAG_##f; |
| 313 | foreach_ipsec_sa_flags |
| 314 | #undef _ |
| 315 | return (flags); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 318 | static vl_api_ipsec_sad_flags_t |
| 319 | ipsec_sad_flags_encode (const ipsec_sa_t * sa) |
| 320 | { |
| 321 | vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE; |
| 322 | |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame^] | 323 | if (ipsec_sa_is_set_USE_ESN (sa)) |
| 324 | flags |= IPSEC_API_SAD_FLAG_USE_ESN; |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 325 | if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa)) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 326 | flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY; |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 327 | if (ipsec_sa_is_set_IS_TUNNEL (sa)) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 328 | flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL; |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 329 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa)) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 330 | flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6; |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 331 | if (ipsec_sa_is_set_UDP_ENCAP (sa)) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 332 | flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP; |
| 333 | |
| 334 | return clib_host_to_net_u32 (flags); |
| 335 | } |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 336 | |
| 337 | static void vl_api_ipsec_sad_entry_add_del_t_handler |
| 338 | (vl_api_ipsec_sad_entry_add_del_t * mp) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 339 | { |
| 340 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 341 | vl_api_ipsec_sad_entry_add_del_reply_t *rmp; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 342 | ip46_address_t tun_src = { }, tun_dst = |
| 343 | { |
| 344 | }; |
| 345 | ipsec_key_t crypto_key, integ_key; |
| 346 | ipsec_crypto_alg_t crypto_alg; |
| 347 | ipsec_integ_alg_t integ_alg; |
| 348 | ipsec_protocol_t proto; |
| 349 | ipsec_sa_flags_t flags; |
Dave Barach | a5160d7 | 2019-03-13 15:29:15 -0400 | [diff] [blame] | 350 | u32 id, spi, sa_index = ~0; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 351 | int rv; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 352 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 353 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 354 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 355 | id = ntohl (mp->entry.sad_id); |
| 356 | spi = ntohl (mp->entry.spi); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 357 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 358 | rv = ipsec_proto_decode (mp->entry.protocol, &proto); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 359 | |
| 360 | if (rv) |
| 361 | goto out; |
| 362 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 363 | rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 364 | |
| 365 | if (rv) |
| 366 | goto out; |
| 367 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 368 | rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 369 | |
| 370 | if (rv) |
| 371 | goto out; |
| 372 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 373 | ipsec_key_decode (&mp->entry.crypto_key, &crypto_key); |
| 374 | ipsec_key_decode (&mp->entry.integrity_key, &integ_key); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 375 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 376 | flags = ipsec_sa_flags_decode (mp->entry.flags); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 377 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 378 | ip_address_decode (&mp->entry.tunnel_src, &tun_src); |
| 379 | ip_address_decode (&mp->entry.tunnel_dst, &tun_dst); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 380 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 381 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 382 | if (mp->is_add) |
| 383 | rv = ipsec_sa_add (id, spi, proto, |
| 384 | crypto_alg, &crypto_key, |
| 385 | integ_alg, &integ_key, flags, |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 386 | 0, &tun_src, &tun_dst, &sa_index); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 387 | else |
| 388 | rv = ipsec_sa_del (id); |
| 389 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 390 | #else |
| 391 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 392 | #endif |
| 393 | |
| 394 | out: |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 395 | /* *INDENT-OFF* */ |
| 396 | REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY, |
| 397 | { |
| 398 | rmp->stat_index = htonl (sa_index); |
| 399 | }); |
| 400 | /* *INDENT-ON* */ |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static void |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 404 | send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg, |
| 405 | u32 context) |
| 406 | { |
| 407 | vl_api_ipsec_spds_details_t *mp; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 408 | u32 n_policies = 0; |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 409 | |
| 410 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 411 | clib_memset (mp, 0, sizeof (*mp)); |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 412 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS); |
| 413 | mp->context = context; |
| 414 | |
| 415 | mp->spd_id = htonl (spd->id); |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 416 | #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]); |
| 417 | foreach_ipsec_spd_policy_type |
| 418 | #undef _ |
| 419 | mp->npolicies = htonl (n_policies); |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 420 | |
| 421 | vl_api_send_msg (reg, (u8 *) mp); |
| 422 | } |
| 423 | |
| 424 | static void |
| 425 | vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp) |
| 426 | { |
| 427 | vl_api_registration_t *reg; |
| 428 | ipsec_main_t *im = &ipsec_main; |
| 429 | ipsec_spd_t *spd; |
| 430 | #if WITH_LIBSSL > 0 |
| 431 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 432 | if (!reg) |
| 433 | return; |
| 434 | |
| 435 | /* *INDENT-OFF* */ |
| 436 | pool_foreach (spd, im->spds, ({ |
| 437 | send_ipsec_spds_details (spd, reg, mp->context); |
| 438 | })); |
| 439 | /* *INDENT-ON* */ |
| 440 | #else |
| 441 | clib_warning ("unimplemented"); |
| 442 | #endif |
| 443 | } |
| 444 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 445 | vl_api_ipsec_spd_action_t |
| 446 | ipsec_spd_action_encode (ipsec_policy_action_t in) |
| 447 | { |
| 448 | vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS; |
| 449 | |
| 450 | switch (in) |
| 451 | { |
| 452 | #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \ |
| 453 | out = IPSEC_API_SPD_ACTION_##f; \ |
| 454 | break; |
| 455 | foreach_ipsec_policy_action |
| 456 | #undef _ |
| 457 | } |
| 458 | return (clib_host_to_net_u32 (out)); |
| 459 | } |
| 460 | |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 461 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 462 | send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg, |
| 463 | u32 context) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 464 | { |
| 465 | vl_api_ipsec_spd_details_t *mp; |
| 466 | |
| 467 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 468 | clib_memset (mp, 0, sizeof (*mp)); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 469 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS); |
| 470 | mp->context = context; |
| 471 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 472 | mp->entry.spd_id = htonl (p->id); |
| 473 | mp->entry.priority = htonl (p->priority); |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 474 | mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) || |
| 475 | (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND)); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 476 | |
| 477 | ip_address_encode (&p->laddr.start, IP46_TYPE_ANY, |
| 478 | &mp->entry.local_address_start); |
| 479 | ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY, |
| 480 | &mp->entry.local_address_stop); |
| 481 | ip_address_encode (&p->raddr.start, IP46_TYPE_ANY, |
| 482 | &mp->entry.remote_address_start); |
| 483 | ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY, |
| 484 | &mp->entry.remote_address_stop); |
Neale Ranns | 231c469 | 2019-03-18 17:11:28 +0000 | [diff] [blame] | 485 | mp->entry.local_port_start = p->lport.start; |
| 486 | mp->entry.local_port_stop = p->lport.stop; |
| 487 | mp->entry.remote_port_start = p->rport.start; |
| 488 | mp->entry.remote_port_stop = p->rport.stop; |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 489 | mp->entry.protocol = p->protocol; |
| 490 | mp->entry.policy = ipsec_spd_action_encode (p->policy); |
| 491 | mp->entry.sa_id = htonl (p->sa_id); |
| 492 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 493 | vl_api_send_msg (reg, (u8 *) mp); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static void |
| 497 | vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp) |
| 498 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 499 | vl_api_registration_t *reg; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 500 | ipsec_main_t *im = &ipsec_main; |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 501 | ipsec_spd_policy_type_t ptype; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 502 | ipsec_policy_t *policy; |
| 503 | ipsec_spd_t *spd; |
| 504 | uword *p; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 505 | u32 spd_index, *ii; |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 506 | #if WITH_LIBSSL > 0 |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 507 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 508 | if (!reg) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 509 | return; |
| 510 | |
| 511 | p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id)); |
| 512 | if (!p) |
| 513 | return; |
| 514 | |
| 515 | spd_index = p[0]; |
| 516 | spd = pool_elt_at_index (im->spds, spd_index); |
| 517 | |
| 518 | /* *INDENT-OFF* */ |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 519 | FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) { |
| 520 | vec_foreach(ii, spd->policies[ptype]) |
| 521 | { |
| 522 | policy = pool_elt_at_index(im->policies, *ii); |
| 523 | |
| 524 | if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id) |
| 525 | send_ipsec_spd_details (policy, reg, mp->context); |
| 526 | } |
| 527 | } |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 528 | /* *INDENT-ON* */ |
| 529 | #else |
| 530 | clib_warning ("unimplemented"); |
| 531 | #endif |
| 532 | } |
| 533 | |
| 534 | static void |
Filip Varga | 871bca9 | 2018-11-02 13:51:44 +0100 | [diff] [blame] | 535 | send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index, |
| 536 | u32 sw_if_index, u32 context) |
| 537 | { |
| 538 | vl_api_ipsec_spd_interface_details_t *mp; |
| 539 | |
| 540 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 541 | clib_memset (mp, 0, sizeof (*mp)); |
| 542 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS); |
| 543 | mp->context = context; |
| 544 | |
| 545 | mp->spd_index = htonl (spd_index); |
| 546 | mp->sw_if_index = htonl (sw_if_index); |
| 547 | |
| 548 | vl_api_send_msg (reg, (u8 *) mp); |
| 549 | } |
| 550 | |
| 551 | static void |
| 552 | vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t * |
| 553 | mp) |
| 554 | { |
| 555 | ipsec_main_t *im = &ipsec_main; |
| 556 | vl_api_registration_t *reg; |
| 557 | u32 k, v, spd_index; |
| 558 | |
| 559 | #if WITH_LIBSSL > 0 |
| 560 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 561 | if (!reg) |
| 562 | return; |
| 563 | |
| 564 | if (mp->spd_index_valid) |
| 565 | { |
| 566 | spd_index = ntohl (mp->spd_index); |
| 567 | /* *INDENT-OFF* */ |
| 568 | hash_foreach(k, v, im->spd_index_by_sw_if_index, ({ |
| 569 | if (v == spd_index) |
| 570 | send_ipsec_spd_interface_details(reg, v, k, mp->context); |
| 571 | })); |
| 572 | /* *INDENT-ON* */ |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | /* *INDENT-OFF* */ |
| 577 | hash_foreach(k, v, im->spd_index_by_sw_if_index, ({ |
| 578 | send_ipsec_spd_interface_details(reg, v, k, mp->context); |
| 579 | })); |
| 580 | /* *INDENT-ON* */ |
| 581 | } |
| 582 | |
| 583 | #else |
| 584 | clib_warning ("unimplemented"); |
| 585 | #endif |
| 586 | } |
| 587 | |
| 588 | static void |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 589 | vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp) |
| 590 | { |
| 591 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 592 | vl_api_ipsec_sa_set_key_reply_t *rmp; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 593 | ipsec_key_t ck, ik; |
| 594 | u32 id; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 595 | int rv; |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 596 | #if WITH_LIBSSL > 0 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 597 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 598 | id = ntohl (mp->sa_id); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 599 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 600 | ipsec_key_decode (&mp->crypto_key, &ck); |
| 601 | ipsec_key_decode (&mp->integrity_key, &ik); |
| 602 | |
| 603 | rv = ipsec_set_sa_key (id, &ck, &ik); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 604 | #else |
| 605 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 606 | #endif |
| 607 | |
| 608 | REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY); |
| 609 | } |
| 610 | |
| 611 | static void |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 612 | vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t * |
| 613 | mp) |
| 614 | { |
| 615 | vl_api_ipsec_tunnel_if_add_del_reply_t *rmp; |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 616 | ipsec_main_t *im = &ipsec_main; |
| 617 | vnet_main_t *vnm = im->vnet_main; |
| 618 | u32 sw_if_index = ~0; |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 619 | ip46_type_t itype; |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 620 | int rv; |
| 621 | |
| 622 | #if WITH_LIBSSL > 0 |
| 623 | ipsec_add_del_tunnel_args_t tun; |
| 624 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 625 | clib_memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t)); |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 626 | |
| 627 | tun.is_add = mp->is_add; |
| 628 | tun.esn = mp->esn; |
| 629 | tun.anti_replay = mp->anti_replay; |
| 630 | tun.local_spi = ntohl (mp->local_spi); |
| 631 | tun.remote_spi = ntohl (mp->remote_spi); |
| 632 | tun.crypto_alg = mp->crypto_alg; |
| 633 | tun.local_crypto_key_len = mp->local_crypto_key_len; |
| 634 | tun.remote_crypto_key_len = mp->remote_crypto_key_len; |
| 635 | tun.integ_alg = mp->integ_alg; |
| 636 | tun.local_integ_key_len = mp->local_integ_key_len; |
| 637 | tun.remote_integ_key_len = mp->remote_integ_key_len; |
Filip Tehlar | b4a7a7d | 2018-11-30 07:27:27 -0800 | [diff] [blame] | 638 | tun.udp_encap = mp->udp_encap; |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 639 | tun.tx_table_id = ntohl (mp->tx_table_id); |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 640 | itype = ip_address_decode (&mp->local_ip, &tun.local_ip); |
| 641 | itype = ip_address_decode (&mp->remote_ip, &tun.remote_ip); |
| 642 | tun.is_ip6 = (IP46_TYPE_IP6 == itype); |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 643 | memcpy (&tun.local_crypto_key, &mp->local_crypto_key, |
| 644 | mp->local_crypto_key_len); |
| 645 | memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key, |
| 646 | mp->remote_crypto_key_len); |
| 647 | memcpy (&tun.local_integ_key, &mp->local_integ_key, |
| 648 | mp->local_integ_key_len); |
| 649 | memcpy (&tun.remote_integ_key, &mp->remote_integ_key, |
| 650 | mp->remote_integ_key_len); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 651 | tun.renumber = mp->renumber; |
| 652 | tun.show_instance = ntohl (mp->show_instance); |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 653 | |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 654 | rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index); |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 655 | |
| 656 | #else |
| 657 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 658 | #endif |
| 659 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 660 | /* *INDENT-OFF* */ |
| 661 | REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY, |
| 662 | ({ |
| 663 | rmp->sw_if_index = htonl (sw_if_index); |
| 664 | })); |
| 665 | /* *INDENT-ON* */ |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 666 | } |
| 667 | |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 668 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 669 | send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg, |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 670 | u32 context, u32 sw_if_index) |
| 671 | { |
| 672 | vl_api_ipsec_sa_details_t *mp; |
| 673 | |
| 674 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 675 | clib_memset (mp, 0, sizeof (*mp)); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 676 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS); |
| 677 | mp->context = context; |
| 678 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 679 | mp->entry.sad_id = htonl (sa->id); |
| 680 | mp->entry.spi = htonl (sa->spi); |
| 681 | mp->entry.protocol = ipsec_proto_encode (sa->protocol); |
| 682 | mp->entry.tx_table_id = |
| 683 | htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4)); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 684 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 685 | mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg); |
| 686 | ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 687 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 688 | mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg); |
| 689 | ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 690 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 691 | mp->entry.flags = ipsec_sad_flags_encode (sa); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 692 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 693 | if (ipsec_sa_is_set_IS_TUNNEL (sa)) |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 694 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 695 | ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY, |
| 696 | &mp->entry.tunnel_src); |
| 697 | ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY, |
| 698 | &mp->entry.tunnel_dst); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 699 | } |
| 700 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 701 | mp->sw_if_index = htonl (sw_if_index); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 702 | mp->salt = clib_host_to_net_u32 (sa->salt); |
| 703 | mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq)); |
| 704 | mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq)); |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame^] | 705 | if (ipsec_sa_is_set_USE_ESN (sa)) |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 706 | { |
| 707 | mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi)); |
| 708 | mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi)); |
| 709 | } |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 710 | if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa)) |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 711 | mp->replay_window = clib_host_to_net_u64 (sa->replay_window); |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 712 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 713 | vl_api_send_msg (reg, (u8 *) mp); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | |
| 717 | static void |
| 718 | vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp) |
| 719 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 720 | vl_api_registration_t *reg; |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 721 | ipsec_main_t *im = &ipsec_main; |
| 722 | vnet_main_t *vnm = im->vnet_main; |
| 723 | ipsec_sa_t *sa; |
| 724 | ipsec_tunnel_if_t *t; |
| 725 | u32 *sa_index_to_tun_if_index = 0; |
| 726 | |
| 727 | #if WITH_LIBSSL > 0 |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 728 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 729 | if (!reg || pool_elts (im->sad) == 0) |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 730 | return; |
| 731 | |
| 732 | vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1, |
| 733 | ~0); |
| 734 | |
| 735 | /* *INDENT-OFF* */ |
| 736 | pool_foreach (t, im->tunnel_interfaces, |
| 737 | ({ |
| 738 | vnet_hw_interface_t *hi; |
| 739 | u32 sw_if_index = ~0; |
| 740 | |
| 741 | hi = vnet_get_hw_interface (vnm, t->hw_if_index); |
| 742 | sw_if_index = hi->sw_if_index; |
| 743 | sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index; |
| 744 | sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index; |
| 745 | })); |
| 746 | |
| 747 | pool_foreach (sa, im->sad, |
| 748 | ({ |
| 749 | if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id) |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 750 | send_ipsec_sa_details (sa, reg, mp->context, |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 751 | sa_index_to_tun_if_index[sa - im->sad]); |
| 752 | })); |
| 753 | /* *INDENT-ON* */ |
| 754 | |
| 755 | vec_free (sa_index_to_tun_if_index); |
| 756 | #else |
| 757 | clib_warning ("unimplemented"); |
| 758 | #endif |
| 759 | } |
| 760 | |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 761 | |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 762 | static void |
Matthew Smith | 75d8560 | 2017-10-05 19:03:05 -0500 | [diff] [blame] | 763 | vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t * |
| 764 | mp) |
| 765 | { |
| 766 | vl_api_ipsec_tunnel_if_set_key_reply_t *rmp; |
| 767 | ipsec_main_t *im = &ipsec_main; |
| 768 | vnet_main_t *vnm = im->vnet_main; |
| 769 | vnet_sw_interface_t *sw; |
| 770 | u8 *key = 0; |
| 771 | int rv; |
| 772 | |
| 773 | #if WITH_LIBSSL > 0 |
| 774 | sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index)); |
| 775 | |
| 776 | switch (mp->key_type) |
| 777 | { |
| 778 | case IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO: |
| 779 | case IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO: |
| 780 | if (mp->alg < IPSEC_CRYPTO_ALG_AES_CBC_128 || |
Dave Barach | a8d4764 | 2018-07-13 11:22:23 -0400 | [diff] [blame] | 781 | mp->alg >= IPSEC_CRYPTO_N_ALG) |
Matthew Smith | 75d8560 | 2017-10-05 19:03:05 -0500 | [diff] [blame] | 782 | { |
Neale Ranns | af3f078 | 2019-03-26 08:21:25 +0000 | [diff] [blame] | 783 | rv = VNET_API_ERROR_INVALID_ALGORITHM; |
Matthew Smith | 75d8560 | 2017-10-05 19:03:05 -0500 | [diff] [blame] | 784 | goto out; |
| 785 | } |
| 786 | break; |
| 787 | case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG: |
| 788 | case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG: |
Dave Barach | a8d4764 | 2018-07-13 11:22:23 -0400 | [diff] [blame] | 789 | if (mp->alg >= IPSEC_INTEG_N_ALG) |
Matthew Smith | 75d8560 | 2017-10-05 19:03:05 -0500 | [diff] [blame] | 790 | { |
Neale Ranns | af3f078 | 2019-03-26 08:21:25 +0000 | [diff] [blame] | 791 | rv = VNET_API_ERROR_INVALID_ALGORITHM; |
Matthew Smith | 75d8560 | 2017-10-05 19:03:05 -0500 | [diff] [blame] | 792 | goto out; |
| 793 | } |
| 794 | break; |
| 795 | case IPSEC_IF_SET_KEY_TYPE_NONE: |
| 796 | default: |
| 797 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 798 | goto out; |
| 799 | break; |
| 800 | } |
| 801 | |
| 802 | key = vec_new (u8, mp->key_len); |
| 803 | clib_memcpy (key, mp->key, mp->key_len); |
| 804 | |
| 805 | rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg, |
| 806 | key); |
| 807 | vec_free (key); |
| 808 | #else |
| 809 | clib_warning ("unimplemented"); |
| 810 | #endif |
| 811 | |
| 812 | out: |
| 813 | REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY); |
| 814 | } |
| 815 | |
| 816 | |
| 817 | static void |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 818 | vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp) |
| 819 | { |
| 820 | vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp; |
| 821 | ipsec_main_t *im = &ipsec_main; |
| 822 | vnet_main_t *vnm = im->vnet_main; |
| 823 | vnet_sw_interface_t *sw; |
| 824 | int rv; |
| 825 | |
| 826 | #if WITH_LIBSSL > 0 |
| 827 | sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index)); |
| 828 | |
| 829 | rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id), |
| 830 | mp->is_outbound); |
| 831 | #else |
| 832 | clib_warning ("unimplemented"); |
| 833 | #endif |
| 834 | |
| 835 | REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY); |
| 836 | } |
| 837 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 838 | static void |
| 839 | vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp) |
| 840 | { |
| 841 | vl_api_registration_t *rp; |
| 842 | ipsec_main_t *im = &ipsec_main; |
| 843 | u32 context = mp->context; |
| 844 | |
| 845 | rp = vl_api_client_index_to_registration (mp->client_index); |
| 846 | |
| 847 | if (rp == 0) |
| 848 | { |
| 849 | clib_warning ("Client %d AWOL", mp->client_index); |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | ipsec_ah_backend_t *ab; |
| 854 | ipsec_esp_backend_t *eb; |
| 855 | /* *INDENT-OFF* */ |
| 856 | pool_foreach (ab, im->ah_backends, { |
| 857 | vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp)); |
| 858 | clib_memset (mp, 0, sizeof (*mp)); |
| 859 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS); |
| 860 | mp->context = context; |
| 861 | snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name), |
| 862 | ab->name); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 863 | mp->protocol = ntohl (IPSEC_API_PROTO_AH); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 864 | mp->index = ab - im->ah_backends; |
| 865 | mp->active = mp->index == im->ah_current_backend ? 1 : 0; |
| 866 | vl_api_send_msg (rp, (u8 *)mp); |
| 867 | }); |
| 868 | pool_foreach (eb, im->esp_backends, { |
| 869 | vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp)); |
| 870 | clib_memset (mp, 0, sizeof (*mp)); |
| 871 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS); |
| 872 | mp->context = context; |
| 873 | snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name), |
| 874 | eb->name); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 875 | mp->protocol = ntohl (IPSEC_API_PROTO_ESP); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 876 | mp->index = eb - im->esp_backends; |
| 877 | mp->active = mp->index == im->esp_current_backend ? 1 : 0; |
| 878 | vl_api_send_msg (rp, (u8 *)mp); |
| 879 | }); |
| 880 | /* *INDENT-ON* */ |
| 881 | } |
| 882 | |
| 883 | static void |
| 884 | vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp) |
| 885 | { |
| 886 | ipsec_main_t *im = &ipsec_main; |
| 887 | vl_api_ipsec_select_backend_reply_t *rmp; |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 888 | ipsec_protocol_t protocol; |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 889 | int rv = 0; |
| 890 | if (pool_elts (im->sad) > 0) |
| 891 | { |
| 892 | rv = VNET_API_ERROR_INSTANCE_IN_USE; |
| 893 | goto done; |
| 894 | } |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 895 | |
| 896 | rv = ipsec_proto_decode (mp->protocol, &protocol); |
| 897 | |
| 898 | if (rv) |
| 899 | goto done; |
| 900 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 901 | #if WITH_LIBSSL > 0 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 902 | switch (protocol) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 903 | { |
| 904 | case IPSEC_PROTOCOL_ESP: |
| 905 | if (pool_is_free_index (im->esp_backends, mp->index)) |
| 906 | { |
| 907 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 908 | break; |
| 909 | } |
| 910 | ipsec_select_esp_backend (im, mp->index); |
| 911 | break; |
| 912 | case IPSEC_PROTOCOL_AH: |
| 913 | if (pool_is_free_index (im->ah_backends, mp->index)) |
| 914 | { |
| 915 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 916 | break; |
| 917 | } |
| 918 | ipsec_select_ah_backend (im, mp->index); |
| 919 | break; |
| 920 | default: |
| 921 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 922 | break; |
| 923 | } |
| 924 | #else |
| 925 | clib_warning ("unimplemented"); /* FIXME */ |
| 926 | #endif |
| 927 | done: |
| 928 | REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY); |
| 929 | } |
| 930 | |
Neale Ranns | 7c44d78 | 2019-02-25 10:28:29 +0000 | [diff] [blame] | 931 | /* |
| 932 | * ipsec_api_hookup |
| 933 | * Add vpe's API message handlers to the table. |
| 934 | * vlib has already mapped shared memory and |
| 935 | * added the client registration handlers. |
| 936 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 937 | */ |
| 938 | #define vl_msg_name_crc_list |
| 939 | #include <vnet/vnet_all_api_h.h> |
| 940 | #undef vl_msg_name_crc_list |
| 941 | |
| 942 | static void |
| 943 | setup_message_id_table (api_main_t * am) |
| 944 | { |
| 945 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 946 | foreach_vl_msg_name_crc_ipsec; |
| 947 | #undef _ |
| 948 | } |
| 949 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 950 | static clib_error_t * |
| 951 | ipsec_api_hookup (vlib_main_t * vm) |
| 952 | { |
| 953 | api_main_t *am = &api_main; |
| 954 | |
| 955 | #define _(N,n) \ |
| 956 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 957 | vl_api_##n##_t_handler, \ |
| 958 | vl_noop_handler, \ |
| 959 | vl_api_##n##_t_endian, \ |
| 960 | vl_api_##n##_t_print, \ |
| 961 | sizeof(vl_api_##n##_t), 1); |
| 962 | foreach_vpe_api_msg; |
| 963 | #undef _ |
| 964 | |
| 965 | /* |
| 966 | * Set up the (msg_name, crc, message-id) table |
| 967 | */ |
| 968 | setup_message_id_table (am); |
| 969 | |
| 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | VLIB_API_INIT_FUNCTION (ipsec_api_hookup); |
| 974 | |
| 975 | /* |
| 976 | * fd.io coding-style-patch-verification: ON |
| 977 | * |
| 978 | * Local Variables: |
| 979 | * eval: (c-set-style "gnu") |
| 980 | * End: |
| 981 | */ |