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> |
| 33 | #include <vnet/ipsec/ikev2.h> |
| 34 | #endif /* IPSEC */ |
| 35 | |
| 36 | #define vl_typedefs /* define message structures */ |
| 37 | #include <vnet/vnet_all_api_h.h> |
| 38 | #undef vl_typedefs |
| 39 | |
| 40 | #define vl_endianfun /* define message structures */ |
| 41 | #include <vnet/vnet_all_api_h.h> |
| 42 | #undef vl_endianfun |
| 43 | |
| 44 | /* instantiate all the print functions we know about */ |
| 45 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 46 | #define vl_printfun |
| 47 | #include <vnet/vnet_all_api_h.h> |
| 48 | #undef vl_printfun |
| 49 | |
| 50 | #include <vlibapi/api_helper_macros.h> |
| 51 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 52 | #define foreach_vpe_api_msg \ |
| 53 | _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \ |
| 54 | _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \ |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 55 | _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \ |
| 56 | _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \ |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 57 | _(IPSEC_SA_SET_KEY, ipsec_sa_set_key) \ |
| 58 | _(IPSEC_SA_DUMP, ipsec_sa_dump) \ |
| 59 | _(IPSEC_SPDS_DUMP, ipsec_spds_dump) \ |
| 60 | _(IPSEC_SPD_DUMP, ipsec_spd_dump) \ |
| 61 | _(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \ |
| 62 | _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \ |
| 63 | _(IPSEC_TUNNEL_IF_SET_KEY, ipsec_tunnel_if_set_key) \ |
| 64 | _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \ |
| 65 | _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del) \ |
| 66 | _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth) \ |
| 67 | _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id) \ |
| 68 | _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts) \ |
| 69 | _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key) \ |
| 70 | _(IKEV2_SET_RESPONDER, ikev2_set_responder) \ |
| 71 | _(IKEV2_SET_IKE_TRANSFORMS, ikev2_set_ike_transforms) \ |
| 72 | _(IKEV2_SET_ESP_TRANSFORMS, ikev2_set_esp_transforms) \ |
| 73 | _(IKEV2_SET_SA_LIFETIME, ikev2_set_sa_lifetime) \ |
| 74 | _(IKEV2_INITIATE_SA_INIT, ikev2_initiate_sa_init) \ |
| 75 | _(IKEV2_INITIATE_DEL_IKE_SA, ikev2_initiate_del_ike_sa) \ |
| 76 | _(IKEV2_INITIATE_DEL_CHILD_SA, ikev2_initiate_del_child_sa) \ |
| 77 | _(IKEV2_INITIATE_REKEY_CHILD_SA, ikev2_initiate_rekey_child_sa) \ |
| 78 | _(IPSEC_SELECT_BACKEND, ipsec_select_backend) \ |
| 79 | _(IPSEC_BACKEND_DUMP, ipsec_backend_dump) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 80 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 81 | static void |
| 82 | 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] | 83 | { |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 84 | #if WITH_LIBSSL == 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 85 | clib_warning ("unimplemented"); |
| 86 | #else |
| 87 | |
| 88 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 89 | vl_api_ipsec_spd_add_del_reply_t *rmp; |
| 90 | int rv; |
| 91 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 92 | 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] | 93 | |
| 94 | REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY); |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | static void vl_api_ipsec_interface_add_del_spd_t_handler |
| 99 | (vl_api_ipsec_interface_add_del_spd_t * mp) |
| 100 | { |
| 101 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 102 | vl_api_ipsec_interface_add_del_spd_reply_t *rmp; |
| 103 | int rv; |
| 104 | u32 sw_if_index __attribute__ ((unused)); |
| 105 | u32 spd_id __attribute__ ((unused)); |
| 106 | |
| 107 | sw_if_index = ntohl (mp->sw_if_index); |
| 108 | spd_id = ntohl (mp->spd_id); |
| 109 | |
| 110 | VALIDATE_SW_IF_INDEX (mp); |
| 111 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 112 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 113 | rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add); |
| 114 | #else |
| 115 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 116 | #endif |
| 117 | |
| 118 | BAD_SW_IF_INDEX_LABEL; |
| 119 | |
| 120 | REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY); |
| 121 | } |
| 122 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 123 | static int |
| 124 | ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in, |
| 125 | ipsec_policy_action_t * out) |
| 126 | { |
| 127 | in = clib_net_to_host_u32 (in); |
| 128 | |
| 129 | switch (in) |
| 130 | { |
| 131 | #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \ |
| 132 | *out = IPSEC_POLICY_ACTION_##f; \ |
| 133 | return (0); |
| 134 | foreach_ipsec_policy_action |
| 135 | #undef _ |
| 136 | } |
| 137 | return (VNET_API_ERROR_UNIMPLEMENTED); |
| 138 | } |
| 139 | |
| 140 | static void vl_api_ipsec_spd_entry_add_del_t_handler |
| 141 | (vl_api_ipsec_spd_entry_add_del_t * mp) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 142 | { |
| 143 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 144 | vl_api_ipsec_spd_entry_add_del_reply_t *rmp; |
| 145 | ip46_type_t itype; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 146 | u32 stat_index; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 147 | int rv; |
| 148 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 149 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 150 | ipsec_policy_t p; |
| 151 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 152 | clib_memset (&p, 0, sizeof (p)); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 153 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 154 | p.id = ntohl (mp->entry.spd_id); |
| 155 | p.priority = ntohl (mp->entry.priority); |
| 156 | p.is_outbound = mp->entry.is_outbound; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 157 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 158 | itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start); |
| 159 | ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop); |
| 160 | ip_address_decode (&mp->entry.local_address_start, &p.laddr.start); |
| 161 | ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop); |
| 162 | |
| 163 | p.is_ipv6 = (itype == IP46_TYPE_IP6); |
| 164 | |
| 165 | p.protocol = mp->entry.protocol; |
| 166 | p.rport.start = ntohs (mp->entry.remote_port_start); |
| 167 | p.rport.stop = ntohs (mp->entry.remote_port_stop); |
| 168 | p.lport.start = ntohs (mp->entry.local_port_start); |
| 169 | p.lport.stop = ntohs (mp->entry.local_port_stop); |
| 170 | |
| 171 | rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy); |
| 172 | |
| 173 | if (rv) |
| 174 | goto out; |
| 175 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 176 | /* policy action resolve unsupported */ |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 177 | if (p.policy == IPSEC_POLICY_ACTION_RESOLVE) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 178 | { |
| 179 | clib_warning ("unsupported action: 'resolve'"); |
| 180 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 181 | goto out; |
| 182 | } |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 183 | p.sa_id = ntohl (mp->entry.sa_id); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 184 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 185 | rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 186 | if (rv) |
| 187 | goto out; |
| 188 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 189 | #else |
| 190 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 191 | goto out; |
| 192 | #endif |
| 193 | |
| 194 | out: |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 195 | /* *INDENT-OFF* */ |
| 196 | REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY, |
| 197 | ({ |
| 198 | rmp->stat_index = ntohl(stat_index); |
| 199 | })); |
| 200 | /* *INDENT-ON* */ |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 203 | static int |
| 204 | ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out) |
| 205 | { |
| 206 | in = clib_net_to_host_u32 (in); |
| 207 | |
| 208 | switch (in) |
| 209 | { |
| 210 | case IPSEC_API_PROTO_ESP: |
| 211 | *out = IPSEC_PROTOCOL_ESP; |
| 212 | return (0); |
| 213 | case IPSEC_API_PROTO_AH: |
| 214 | *out = IPSEC_PROTOCOL_AH; |
| 215 | return (0); |
| 216 | } |
| 217 | return (VNET_API_ERROR_UNIMPLEMENTED); |
| 218 | } |
| 219 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 220 | static vl_api_ipsec_proto_t |
| 221 | ipsec_proto_encode (ipsec_protocol_t p) |
| 222 | { |
| 223 | switch (p) |
| 224 | { |
| 225 | case IPSEC_PROTOCOL_ESP: |
| 226 | return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP); |
| 227 | case IPSEC_PROTOCOL_AH: |
| 228 | return clib_host_to_net_u32 (IPSEC_API_PROTO_AH); |
| 229 | } |
| 230 | return (VNET_API_ERROR_UNIMPLEMENTED); |
| 231 | } |
| 232 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 233 | static int |
| 234 | ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in, |
| 235 | ipsec_crypto_alg_t * out) |
| 236 | { |
| 237 | in = clib_net_to_host_u32 (in); |
| 238 | |
| 239 | switch (in) |
| 240 | { |
| 241 | #define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \ |
| 242 | *out = IPSEC_CRYPTO_ALG_##f; \ |
| 243 | return (0); |
| 244 | foreach_ipsec_crypto_alg |
| 245 | #undef _ |
| 246 | } |
| 247 | return (VNET_API_ERROR_UNIMPLEMENTED); |
| 248 | } |
| 249 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 250 | static vl_api_ipsec_crypto_alg_t |
| 251 | ipsec_crypto_algo_encode (ipsec_crypto_alg_t c) |
| 252 | { |
| 253 | switch (c) |
| 254 | { |
| 255 | #define _(v,f,s) case IPSEC_CRYPTO_ALG_##f: \ |
| 256 | return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f); |
| 257 | foreach_ipsec_crypto_alg |
| 258 | #undef _ |
| 259 | case IPSEC_CRYPTO_N_ALG: |
| 260 | break; |
| 261 | } |
| 262 | ASSERT (0); |
| 263 | return (VNET_API_ERROR_UNIMPLEMENTED); |
| 264 | } |
| 265 | |
| 266 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 267 | static int |
| 268 | ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out) |
| 269 | { |
| 270 | in = clib_net_to_host_u32 (in); |
| 271 | |
| 272 | switch (in) |
| 273 | { |
| 274 | #define _(v,f,s) case IPSEC_API_INTEG_ALG_##f: \ |
| 275 | *out = IPSEC_INTEG_ALG_##f; \ |
| 276 | return (0); |
| 277 | foreach_ipsec_integ_alg |
| 278 | #undef _ |
| 279 | } |
| 280 | return (VNET_API_ERROR_UNIMPLEMENTED); |
| 281 | } |
| 282 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 283 | static vl_api_ipsec_integ_alg_t |
| 284 | ipsec_integ_algo_encode (ipsec_integ_alg_t i) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 285 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 286 | switch (i) |
| 287 | { |
| 288 | #define _(v,f,s) case IPSEC_INTEG_ALG_##f: \ |
| 289 | return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f)); |
| 290 | foreach_ipsec_integ_alg |
| 291 | #undef _ |
| 292 | case IPSEC_INTEG_N_ALG: |
| 293 | break; |
| 294 | } |
| 295 | ASSERT (0); |
| 296 | return (VNET_API_ERROR_UNIMPLEMENTED); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static void |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 300 | 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] | 301 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 302 | ipsec_mk_key (out, key->data, key->length); |
| 303 | } |
| 304 | |
| 305 | static void |
| 306 | ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out) |
| 307 | { |
| 308 | out->length = in->len; |
| 309 | clib_memcpy (out->data, in->data, out->length); |
| 310 | } |
| 311 | |
| 312 | static ipsec_sa_flags_t |
| 313 | ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in) |
| 314 | { |
| 315 | ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE; |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 316 | in = clib_net_to_host_u32 (in); |
| 317 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 318 | #define _(v,f,s) if (in & IPSEC_API_SAD_FLAG_##f) \ |
| 319 | flags |= IPSEC_SA_FLAG_##f; |
| 320 | foreach_ipsec_sa_flags |
| 321 | #undef _ |
| 322 | return (flags); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 325 | static vl_api_ipsec_sad_flags_t |
| 326 | ipsec_sad_flags_encode (const ipsec_sa_t * sa) |
| 327 | { |
| 328 | vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE; |
| 329 | |
| 330 | if (sa->use_esn) |
| 331 | flags |= IPSEC_API_SAD_FLAG_USE_EXTENDED_SEQ_NUM; |
| 332 | if (sa->use_anti_replay) |
| 333 | flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY; |
| 334 | if (sa->is_tunnel) |
| 335 | flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL; |
| 336 | if (sa->is_tunnel_ip6) |
| 337 | flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6; |
| 338 | if (sa->udp_encap) |
| 339 | flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP; |
| 340 | |
| 341 | return clib_host_to_net_u32 (flags); |
| 342 | } |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 343 | |
| 344 | static void vl_api_ipsec_sad_entry_add_del_t_handler |
| 345 | (vl_api_ipsec_sad_entry_add_del_t * mp) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 346 | { |
| 347 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 348 | vl_api_ipsec_sad_entry_add_del_reply_t *rmp; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 349 | ip46_address_t tun_src = { }, tun_dst = |
| 350 | { |
| 351 | }; |
| 352 | ipsec_key_t crypto_key, integ_key; |
| 353 | ipsec_crypto_alg_t crypto_alg; |
| 354 | ipsec_integ_alg_t integ_alg; |
| 355 | ipsec_protocol_t proto; |
| 356 | ipsec_sa_flags_t flags; |
| 357 | u32 id, spi; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 358 | int rv; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 359 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 360 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 361 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 362 | id = ntohl (mp->entry.sad_id); |
| 363 | spi = ntohl (mp->entry.spi); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 364 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 365 | rv = ipsec_proto_decode (mp->entry.protocol, &proto); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 366 | |
| 367 | if (rv) |
| 368 | goto out; |
| 369 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 370 | rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 371 | |
| 372 | if (rv) |
| 373 | goto out; |
| 374 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 375 | rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 376 | |
| 377 | if (rv) |
| 378 | goto out; |
| 379 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 380 | ipsec_key_decode (&mp->entry.crypto_key, &crypto_key); |
| 381 | ipsec_key_decode (&mp->entry.integrity_key, &integ_key); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 382 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 383 | flags = ipsec_sa_flags_decode (mp->entry.flags); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 384 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 385 | ip_address_decode (&mp->entry.tunnel_src, &tun_src); |
| 386 | ip_address_decode (&mp->entry.tunnel_dst, &tun_dst); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 387 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 388 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 389 | if (mp->is_add) |
| 390 | rv = ipsec_sa_add (id, spi, proto, |
| 391 | crypto_alg, &crypto_key, |
| 392 | integ_alg, &integ_key, flags, |
| 393 | 0, &tun_src, &tun_dst, NULL); |
| 394 | else |
| 395 | rv = ipsec_sa_del (id); |
| 396 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 397 | #else |
| 398 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 399 | #endif |
| 400 | |
| 401 | out: |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 402 | REPLY_MACRO (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static void |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 406 | send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg, |
| 407 | u32 context) |
| 408 | { |
| 409 | vl_api_ipsec_spds_details_t *mp; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 410 | u32 n_policies = 0; |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 411 | |
| 412 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 413 | clib_memset (mp, 0, sizeof (*mp)); |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 414 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS); |
| 415 | mp->context = context; |
| 416 | |
| 417 | mp->spd_id = htonl (spd->id); |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 418 | #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]); |
| 419 | foreach_ipsec_spd_policy_type |
| 420 | #undef _ |
| 421 | mp->npolicies = htonl (n_policies); |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 422 | |
| 423 | vl_api_send_msg (reg, (u8 *) mp); |
| 424 | } |
| 425 | |
| 426 | static void |
| 427 | vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp) |
| 428 | { |
| 429 | vl_api_registration_t *reg; |
| 430 | ipsec_main_t *im = &ipsec_main; |
| 431 | ipsec_spd_t *spd; |
| 432 | #if WITH_LIBSSL > 0 |
| 433 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 434 | if (!reg) |
| 435 | return; |
| 436 | |
| 437 | /* *INDENT-OFF* */ |
| 438 | pool_foreach (spd, im->spds, ({ |
| 439 | send_ipsec_spds_details (spd, reg, mp->context); |
| 440 | })); |
| 441 | /* *INDENT-ON* */ |
| 442 | #else |
| 443 | clib_warning ("unimplemented"); |
| 444 | #endif |
| 445 | } |
| 446 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 447 | vl_api_ipsec_spd_action_t |
| 448 | ipsec_spd_action_encode (ipsec_policy_action_t in) |
| 449 | { |
| 450 | vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS; |
| 451 | |
| 452 | switch (in) |
| 453 | { |
| 454 | #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \ |
| 455 | out = IPSEC_API_SPD_ACTION_##f; \ |
| 456 | break; |
| 457 | foreach_ipsec_policy_action |
| 458 | #undef _ |
| 459 | } |
| 460 | return (clib_host_to_net_u32 (out)); |
| 461 | } |
| 462 | |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 463 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 464 | send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg, |
| 465 | u32 context) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 466 | { |
| 467 | vl_api_ipsec_spd_details_t *mp; |
| 468 | |
| 469 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 470 | clib_memset (mp, 0, sizeof (*mp)); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 471 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS); |
| 472 | mp->context = context; |
| 473 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 474 | mp->entry.spd_id = htonl (p->id); |
| 475 | mp->entry.priority = htonl (p->priority); |
| 476 | mp->entry.is_outbound = p->is_outbound; |
| 477 | |
| 478 | ip_address_encode (&p->laddr.start, IP46_TYPE_ANY, |
| 479 | &mp->entry.local_address_start); |
| 480 | ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY, |
| 481 | &mp->entry.local_address_stop); |
| 482 | ip_address_encode (&p->raddr.start, IP46_TYPE_ANY, |
| 483 | &mp->entry.remote_address_start); |
| 484 | ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY, |
| 485 | &mp->entry.remote_address_stop); |
| 486 | mp->entry.local_port_start = htons (p->lport.start); |
| 487 | mp->entry.local_port_stop = htons (p->lport.stop); |
| 488 | mp->entry.remote_port_start = htons (p->rport.start); |
| 489 | mp->entry.remote_port_stop = htons (p->rport.stop); |
| 490 | mp->entry.protocol = p->protocol; |
| 491 | mp->entry.policy = ipsec_spd_action_encode (p->policy); |
| 492 | mp->entry.sa_id = htonl (p->sa_id); |
| 493 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 494 | vl_api_send_msg (reg, (u8 *) mp); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static void |
| 498 | vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp) |
| 499 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 500 | vl_api_registration_t *reg; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 501 | ipsec_main_t *im = &ipsec_main; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 502 | ipsec_spd_policy_t ptype; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 503 | ipsec_policy_t *policy; |
| 504 | ipsec_spd_t *spd; |
| 505 | uword *p; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 506 | u32 spd_index, *ii; |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 507 | #if WITH_LIBSSL > 0 |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 508 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 509 | if (!reg) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 510 | return; |
| 511 | |
| 512 | p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id)); |
| 513 | if (!p) |
| 514 | return; |
| 515 | |
| 516 | spd_index = p[0]; |
| 517 | spd = pool_elt_at_index (im->spds, spd_index); |
| 518 | |
| 519 | /* *INDENT-OFF* */ |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 520 | FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) { |
| 521 | vec_foreach(ii, spd->policies[ptype]) |
| 522 | { |
| 523 | policy = pool_elt_at_index(im->policies, *ii); |
| 524 | |
| 525 | if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id) |
| 526 | send_ipsec_spd_details (policy, reg, mp->context); |
| 527 | } |
| 528 | } |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 529 | /* *INDENT-ON* */ |
| 530 | #else |
| 531 | clib_warning ("unimplemented"); |
| 532 | #endif |
| 533 | } |
| 534 | |
| 535 | static void |
Filip Varga | 871bca9 | 2018-11-02 13:51:44 +0100 | [diff] [blame] | 536 | send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index, |
| 537 | u32 sw_if_index, u32 context) |
| 538 | { |
| 539 | vl_api_ipsec_spd_interface_details_t *mp; |
| 540 | |
| 541 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 542 | clib_memset (mp, 0, sizeof (*mp)); |
| 543 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS); |
| 544 | mp->context = context; |
| 545 | |
| 546 | mp->spd_index = htonl (spd_index); |
| 547 | mp->sw_if_index = htonl (sw_if_index); |
| 548 | |
| 549 | vl_api_send_msg (reg, (u8 *) mp); |
| 550 | } |
| 551 | |
| 552 | static void |
| 553 | vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t * |
| 554 | mp) |
| 555 | { |
| 556 | ipsec_main_t *im = &ipsec_main; |
| 557 | vl_api_registration_t *reg; |
| 558 | u32 k, v, spd_index; |
| 559 | |
| 560 | #if WITH_LIBSSL > 0 |
| 561 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 562 | if (!reg) |
| 563 | return; |
| 564 | |
| 565 | if (mp->spd_index_valid) |
| 566 | { |
| 567 | spd_index = ntohl (mp->spd_index); |
| 568 | /* *INDENT-OFF* */ |
| 569 | hash_foreach(k, v, im->spd_index_by_sw_if_index, ({ |
| 570 | if (v == spd_index) |
| 571 | send_ipsec_spd_interface_details(reg, v, k, mp->context); |
| 572 | })); |
| 573 | /* *INDENT-ON* */ |
| 574 | } |
| 575 | else |
| 576 | { |
| 577 | /* *INDENT-OFF* */ |
| 578 | hash_foreach(k, v, im->spd_index_by_sw_if_index, ({ |
| 579 | send_ipsec_spd_interface_details(reg, v, k, mp->context); |
| 580 | })); |
| 581 | /* *INDENT-ON* */ |
| 582 | } |
| 583 | |
| 584 | #else |
| 585 | clib_warning ("unimplemented"); |
| 586 | #endif |
| 587 | } |
| 588 | |
| 589 | static void |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 590 | vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp) |
| 591 | { |
| 592 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 593 | vl_api_ipsec_sa_set_key_reply_t *rmp; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 594 | ipsec_key_t ck, ik; |
| 595 | u32 id; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 596 | int rv; |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 597 | #if WITH_LIBSSL > 0 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 598 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 599 | id = ntohl (mp->sa_id); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 600 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 601 | ipsec_key_decode (&mp->crypto_key, &ck); |
| 602 | ipsec_key_decode (&mp->integrity_key, &ik); |
| 603 | |
| 604 | rv = ipsec_set_sa_key (id, &ck, &ik); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 605 | #else |
| 606 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 607 | #endif |
| 608 | |
| 609 | REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY); |
| 610 | } |
| 611 | |
| 612 | static void |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 613 | vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t * |
| 614 | mp) |
| 615 | { |
| 616 | vl_api_ipsec_tunnel_if_add_del_reply_t *rmp; |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 617 | ipsec_main_t *im = &ipsec_main; |
| 618 | vnet_main_t *vnm = im->vnet_main; |
| 619 | u32 sw_if_index = ~0; |
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); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 640 | memcpy (&tun.local_ip.ip4, mp->local_ip, 4); |
| 641 | memcpy (&tun.remote_ip.ip4, mp->remote_ip, 4); |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 642 | memcpy (&tun.local_crypto_key, &mp->local_crypto_key, |
| 643 | mp->local_crypto_key_len); |
| 644 | memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key, |
| 645 | mp->remote_crypto_key_len); |
| 646 | memcpy (&tun.local_integ_key, &mp->local_integ_key, |
| 647 | mp->local_integ_key_len); |
| 648 | memcpy (&tun.remote_integ_key, &mp->remote_integ_key, |
| 649 | mp->remote_integ_key_len); |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 650 | tun.renumber = mp->renumber; |
| 651 | tun.show_instance = ntohl (mp->show_instance); |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 652 | |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 653 | rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index); |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 654 | |
| 655 | #else |
| 656 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 657 | #endif |
| 658 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 659 | /* *INDENT-OFF* */ |
| 660 | REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY, |
| 661 | ({ |
| 662 | rmp->sw_if_index = htonl (sw_if_index); |
| 663 | })); |
| 664 | /* *INDENT-ON* */ |
Matthew Smith | b0972cb | 2017-05-02 16:20:41 -0500 | [diff] [blame] | 665 | } |
| 666 | |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 667 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 668 | 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] | 669 | u32 context, u32 sw_if_index) |
| 670 | { |
| 671 | vl_api_ipsec_sa_details_t *mp; |
| 672 | |
| 673 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 674 | clib_memset (mp, 0, sizeof (*mp)); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 675 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS); |
| 676 | mp->context = context; |
| 677 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 678 | mp->entry.sad_id = htonl (sa->id); |
| 679 | mp->entry.spi = htonl (sa->spi); |
| 680 | mp->entry.protocol = ipsec_proto_encode (sa->protocol); |
| 681 | mp->entry.tx_table_id = |
| 682 | 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] | 683 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 684 | mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg); |
| 685 | ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 686 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 687 | mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg); |
| 688 | ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 689 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 690 | mp->entry.flags = ipsec_sad_flags_encode (sa); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 691 | |
| 692 | if (sa->is_tunnel) |
| 693 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 694 | ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY, |
| 695 | &mp->entry.tunnel_src); |
| 696 | ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY, |
| 697 | &mp->entry.tunnel_dst); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 698 | } |
| 699 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 700 | mp->sw_if_index = htonl (sw_if_index); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 701 | mp->salt = clib_host_to_net_u32 (sa->salt); |
| 702 | mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq)); |
| 703 | mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq)); |
| 704 | if (sa->use_esn) |
| 705 | { |
| 706 | mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi)); |
| 707 | mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi)); |
| 708 | } |
| 709 | if (sa->use_anti_replay) |
| 710 | mp->replay_window = clib_host_to_net_u64 (sa->replay_window); |
| 711 | mp->total_data_size = clib_host_to_net_u64 (sa->total_data_size); |
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 | { |
| 783 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 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 | { |
| 791 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 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 | |
| 838 | |
| 839 | static void |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 840 | vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp) |
| 841 | { |
| 842 | vl_api_ikev2_profile_add_del_reply_t *rmp; |
| 843 | int rv = 0; |
| 844 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 845 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 846 | vlib_main_t *vm = vlib_get_main (); |
| 847 | clib_error_t *error; |
| 848 | u8 *tmp = format (0, "%s", mp->name); |
| 849 | error = ikev2_add_del_profile (vm, tmp, mp->is_add); |
| 850 | vec_free (tmp); |
| 851 | if (error) |
| 852 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 853 | #else |
| 854 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 855 | #endif |
| 856 | |
| 857 | REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY); |
| 858 | } |
| 859 | |
| 860 | static void |
| 861 | vl_api_ikev2_profile_set_auth_t_handler |
| 862 | (vl_api_ikev2_profile_set_auth_t * mp) |
| 863 | { |
| 864 | vl_api_ikev2_profile_set_auth_reply_t *rmp; |
| 865 | int rv = 0; |
| 866 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 867 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 868 | vlib_main_t *vm = vlib_get_main (); |
| 869 | clib_error_t *error; |
Michal Cmarada | e67c1d8 | 2019-01-18 12:51:08 +0100 | [diff] [blame] | 870 | int data_len = ntohl (mp->data_len); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 871 | u8 *tmp = format (0, "%s", mp->name); |
Michal Cmarada | e67c1d8 | 2019-01-18 12:51:08 +0100 | [diff] [blame] | 872 | u8 *data = vec_new (u8, data_len); |
| 873 | clib_memcpy (data, mp->data, data_len); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 874 | error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex); |
| 875 | vec_free (tmp); |
| 876 | vec_free (data); |
| 877 | if (error) |
| 878 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 879 | #else |
| 880 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 881 | #endif |
| 882 | |
| 883 | REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY); |
| 884 | } |
| 885 | |
| 886 | static void |
| 887 | vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp) |
| 888 | { |
| 889 | vl_api_ikev2_profile_add_del_reply_t *rmp; |
| 890 | int rv = 0; |
| 891 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 892 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 893 | vlib_main_t *vm = vlib_get_main (); |
| 894 | clib_error_t *error; |
| 895 | u8 *tmp = format (0, "%s", mp->name); |
Michal Cmarada | e67c1d8 | 2019-01-18 12:51:08 +0100 | [diff] [blame] | 896 | int data_len = ntohl (mp->data_len); |
| 897 | u8 *data = vec_new (u8, data_len); |
| 898 | clib_memcpy (data, mp->data, data_len); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 899 | error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local); |
| 900 | vec_free (tmp); |
| 901 | vec_free (data); |
| 902 | if (error) |
| 903 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 904 | #else |
| 905 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 906 | #endif |
| 907 | |
| 908 | REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY); |
| 909 | } |
| 910 | |
| 911 | static void |
| 912 | vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp) |
| 913 | { |
| 914 | vl_api_ikev2_profile_set_ts_reply_t *rmp; |
| 915 | int rv = 0; |
| 916 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 917 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 918 | vlib_main_t *vm = vlib_get_main (); |
| 919 | clib_error_t *error; |
| 920 | u8 *tmp = format (0, "%s", mp->name); |
| 921 | error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port, |
| 922 | mp->end_port, (ip4_address_t) mp->start_addr, |
| 923 | (ip4_address_t) mp->end_addr, mp->is_local); |
| 924 | vec_free (tmp); |
| 925 | if (error) |
| 926 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 927 | #else |
| 928 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 929 | #endif |
| 930 | |
| 931 | REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY); |
| 932 | } |
| 933 | |
| 934 | static void |
| 935 | vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp) |
| 936 | { |
| 937 | vl_api_ikev2_profile_set_ts_reply_t *rmp; |
| 938 | int rv = 0; |
| 939 | |
Damjan Marion | a9a951f | 2017-01-16 22:06:10 +0100 | [diff] [blame] | 940 | #if WITH_LIBSSL > 0 |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 941 | vlib_main_t *vm = vlib_get_main (); |
| 942 | clib_error_t *error; |
| 943 | |
| 944 | error = ikev2_set_local_key (vm, mp->key_file); |
| 945 | if (error) |
| 946 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 947 | #else |
| 948 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 949 | #endif |
| 950 | |
| 951 | REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY); |
| 952 | } |
| 953 | |
Radu Nicolau | cb33dc2 | 2017-02-16 16:49:46 +0000 | [diff] [blame] | 954 | static void |
| 955 | vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp) |
| 956 | { |
| 957 | vl_api_ikev2_set_responder_reply_t *rmp; |
| 958 | int rv = 0; |
| 959 | |
| 960 | #if WITH_LIBSSL > 0 |
| 961 | vlib_main_t *vm = vlib_get_main (); |
| 962 | clib_error_t *error; |
| 963 | |
| 964 | u8 *tmp = format (0, "%s", mp->name); |
| 965 | ip4_address_t ip4; |
| 966 | clib_memcpy (&ip4, mp->address, sizeof (ip4)); |
| 967 | |
| 968 | error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4); |
| 969 | vec_free (tmp); |
| 970 | if (error) |
| 971 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 972 | #else |
| 973 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 974 | #endif |
| 975 | |
| 976 | REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY); |
| 977 | } |
| 978 | |
| 979 | static void |
| 980 | vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t * |
| 981 | mp) |
| 982 | { |
| 983 | vl_api_ikev2_set_ike_transforms_reply_t *rmp; |
| 984 | int rv = 0; |
| 985 | |
| 986 | #if WITH_LIBSSL > 0 |
| 987 | vlib_main_t *vm = vlib_get_main (); |
| 988 | clib_error_t *error; |
| 989 | |
| 990 | u8 *tmp = format (0, "%s", mp->name); |
| 991 | |
| 992 | error = |
| 993 | ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg, |
| 994 | mp->dh_group, mp->crypto_key_size); |
| 995 | vec_free (tmp); |
| 996 | if (error) |
| 997 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 998 | #else |
| 999 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 1000 | #endif |
| 1001 | |
| 1002 | REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY); |
| 1003 | } |
| 1004 | |
| 1005 | static void |
| 1006 | vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t * |
| 1007 | mp) |
| 1008 | { |
| 1009 | vl_api_ikev2_set_esp_transforms_reply_t *rmp; |
| 1010 | int rv = 0; |
| 1011 | |
| 1012 | #if WITH_LIBSSL > 0 |
| 1013 | vlib_main_t *vm = vlib_get_main (); |
| 1014 | clib_error_t *error; |
| 1015 | |
| 1016 | u8 *tmp = format (0, "%s", mp->name); |
| 1017 | |
| 1018 | error = |
| 1019 | ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg, |
| 1020 | mp->dh_group, mp->crypto_key_size); |
| 1021 | vec_free (tmp); |
| 1022 | if (error) |
| 1023 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 1024 | #else |
| 1025 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 1026 | #endif |
| 1027 | |
| 1028 | REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY); |
| 1029 | } |
| 1030 | |
| 1031 | static void |
| 1032 | vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp) |
| 1033 | { |
| 1034 | vl_api_ikev2_set_sa_lifetime_reply_t *rmp; |
| 1035 | int rv = 0; |
| 1036 | |
| 1037 | #if WITH_LIBSSL > 0 |
| 1038 | vlib_main_t *vm = vlib_get_main (); |
| 1039 | clib_error_t *error; |
| 1040 | |
| 1041 | u8 *tmp = format (0, "%s", mp->name); |
| 1042 | |
| 1043 | error = |
| 1044 | ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter, |
| 1045 | mp->handover, mp->lifetime_maxdata); |
| 1046 | vec_free (tmp); |
| 1047 | if (error) |
| 1048 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 1049 | #else |
| 1050 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 1051 | #endif |
| 1052 | |
| 1053 | REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY); |
| 1054 | } |
| 1055 | |
| 1056 | static void |
| 1057 | vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp) |
| 1058 | { |
| 1059 | vl_api_ikev2_initiate_sa_init_reply_t *rmp; |
| 1060 | int rv = 0; |
| 1061 | |
| 1062 | #if WITH_LIBSSL > 0 |
| 1063 | vlib_main_t *vm = vlib_get_main (); |
| 1064 | clib_error_t *error; |
| 1065 | |
| 1066 | u8 *tmp = format (0, "%s", mp->name); |
| 1067 | |
| 1068 | error = ikev2_initiate_sa_init (vm, tmp); |
| 1069 | vec_free (tmp); |
| 1070 | if (error) |
| 1071 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 1072 | #else |
| 1073 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 1074 | #endif |
| 1075 | |
| 1076 | REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY); |
| 1077 | } |
| 1078 | |
| 1079 | static void |
| 1080 | vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t |
| 1081 | * mp) |
| 1082 | { |
| 1083 | vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp; |
| 1084 | int rv = 0; |
| 1085 | |
| 1086 | #if WITH_LIBSSL > 0 |
| 1087 | vlib_main_t *vm = vlib_get_main (); |
| 1088 | clib_error_t *error; |
| 1089 | |
| 1090 | error = ikev2_initiate_delete_ike_sa (vm, mp->ispi); |
| 1091 | if (error) |
| 1092 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 1093 | #else |
| 1094 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 1095 | #endif |
| 1096 | |
| 1097 | REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY); |
| 1098 | } |
| 1099 | |
| 1100 | static void |
| 1101 | vl_api_ikev2_initiate_del_child_sa_t_handler |
| 1102 | (vl_api_ikev2_initiate_del_child_sa_t * mp) |
| 1103 | { |
| 1104 | vl_api_ikev2_initiate_del_child_sa_reply_t *rmp; |
| 1105 | int rv = 0; |
| 1106 | |
| 1107 | #if WITH_LIBSSL > 0 |
| 1108 | vlib_main_t *vm = vlib_get_main (); |
| 1109 | clib_error_t *error; |
| 1110 | |
| 1111 | error = ikev2_initiate_delete_child_sa (vm, mp->ispi); |
| 1112 | if (error) |
| 1113 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 1114 | #else |
| 1115 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 1116 | #endif |
| 1117 | |
| 1118 | REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY); |
| 1119 | } |
| 1120 | |
| 1121 | static void |
| 1122 | vl_api_ikev2_initiate_rekey_child_sa_t_handler |
| 1123 | (vl_api_ikev2_initiate_rekey_child_sa_t * mp) |
| 1124 | { |
| 1125 | vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp; |
| 1126 | int rv = 0; |
| 1127 | |
| 1128 | #if WITH_LIBSSL > 0 |
| 1129 | vlib_main_t *vm = vlib_get_main (); |
| 1130 | clib_error_t *error; |
| 1131 | |
| 1132 | error = ikev2_initiate_rekey_child_sa (vm, mp->ispi); |
| 1133 | if (error) |
| 1134 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 1135 | #else |
| 1136 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 1137 | #endif |
| 1138 | |
| 1139 | REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY); |
| 1140 | } |
| 1141 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 1142 | /* |
| 1143 | * ipsec_api_hookup |
| 1144 | * Add vpe's API message handlers to the table. |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 1145 | * vlib has already mapped shared memory and |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 1146 | * added the client registration handlers. |
| 1147 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 1148 | */ |
| 1149 | #define vl_msg_name_crc_list |
| 1150 | #include <vnet/vnet_all_api_h.h> |
| 1151 | #undef vl_msg_name_crc_list |
| 1152 | |
| 1153 | static void |
| 1154 | setup_message_id_table (api_main_t * am) |
| 1155 | { |
| 1156 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 1157 | foreach_vl_msg_name_crc_ipsec; |
| 1158 | #undef _ |
| 1159 | } |
| 1160 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1161 | static void |
| 1162 | vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp) |
| 1163 | { |
| 1164 | vl_api_registration_t *rp; |
| 1165 | ipsec_main_t *im = &ipsec_main; |
| 1166 | u32 context = mp->context; |
| 1167 | |
| 1168 | rp = vl_api_client_index_to_registration (mp->client_index); |
| 1169 | |
| 1170 | if (rp == 0) |
| 1171 | { |
| 1172 | clib_warning ("Client %d AWOL", mp->client_index); |
| 1173 | return; |
| 1174 | } |
| 1175 | |
| 1176 | ipsec_ah_backend_t *ab; |
| 1177 | ipsec_esp_backend_t *eb; |
| 1178 | /* *INDENT-OFF* */ |
| 1179 | pool_foreach (ab, im->ah_backends, { |
| 1180 | vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp)); |
| 1181 | clib_memset (mp, 0, sizeof (*mp)); |
| 1182 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS); |
| 1183 | mp->context = context; |
| 1184 | snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name), |
| 1185 | ab->name); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 1186 | mp->protocol = ntohl (IPSEC_API_PROTO_AH); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1187 | mp->index = ab - im->ah_backends; |
| 1188 | mp->active = mp->index == im->ah_current_backend ? 1 : 0; |
| 1189 | vl_api_send_msg (rp, (u8 *)mp); |
| 1190 | }); |
| 1191 | pool_foreach (eb, im->esp_backends, { |
| 1192 | vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp)); |
| 1193 | clib_memset (mp, 0, sizeof (*mp)); |
| 1194 | mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS); |
| 1195 | mp->context = context; |
| 1196 | snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name), |
| 1197 | eb->name); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 1198 | mp->protocol = ntohl (IPSEC_API_PROTO_ESP); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1199 | mp->index = eb - im->esp_backends; |
| 1200 | mp->active = mp->index == im->esp_current_backend ? 1 : 0; |
| 1201 | vl_api_send_msg (rp, (u8 *)mp); |
| 1202 | }); |
| 1203 | /* *INDENT-ON* */ |
| 1204 | } |
| 1205 | |
| 1206 | static void |
| 1207 | vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp) |
| 1208 | { |
| 1209 | ipsec_main_t *im = &ipsec_main; |
| 1210 | vl_api_ipsec_select_backend_reply_t *rmp; |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 1211 | ipsec_protocol_t protocol; |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1212 | int rv = 0; |
| 1213 | if (pool_elts (im->sad) > 0) |
| 1214 | { |
| 1215 | rv = VNET_API_ERROR_INSTANCE_IN_USE; |
| 1216 | goto done; |
| 1217 | } |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 1218 | |
| 1219 | rv = ipsec_proto_decode (mp->protocol, &protocol); |
| 1220 | |
| 1221 | if (rv) |
| 1222 | goto done; |
| 1223 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1224 | #if WITH_LIBSSL > 0 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 1225 | switch (protocol) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1226 | { |
| 1227 | case IPSEC_PROTOCOL_ESP: |
| 1228 | if (pool_is_free_index (im->esp_backends, mp->index)) |
| 1229 | { |
| 1230 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1231 | break; |
| 1232 | } |
| 1233 | ipsec_select_esp_backend (im, mp->index); |
| 1234 | break; |
| 1235 | case IPSEC_PROTOCOL_AH: |
| 1236 | if (pool_is_free_index (im->ah_backends, mp->index)) |
| 1237 | { |
| 1238 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1239 | break; |
| 1240 | } |
| 1241 | ipsec_select_ah_backend (im, mp->index); |
| 1242 | break; |
| 1243 | default: |
| 1244 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1245 | break; |
| 1246 | } |
| 1247 | #else |
| 1248 | clib_warning ("unimplemented"); /* FIXME */ |
| 1249 | #endif |
| 1250 | done: |
| 1251 | REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY); |
| 1252 | } |
| 1253 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 1254 | static clib_error_t * |
| 1255 | ipsec_api_hookup (vlib_main_t * vm) |
| 1256 | { |
| 1257 | api_main_t *am = &api_main; |
| 1258 | |
| 1259 | #define _(N,n) \ |
| 1260 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1261 | vl_api_##n##_t_handler, \ |
| 1262 | vl_noop_handler, \ |
| 1263 | vl_api_##n##_t_endian, \ |
| 1264 | vl_api_##n##_t_print, \ |
| 1265 | sizeof(vl_api_##n##_t), 1); |
| 1266 | foreach_vpe_api_msg; |
| 1267 | #undef _ |
| 1268 | |
| 1269 | /* |
| 1270 | * Set up the (msg_name, crc, message-id) table |
| 1271 | */ |
| 1272 | setup_message_id_table (am); |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | VLIB_API_INIT_FUNCTION (ipsec_api_hookup); |
| 1278 | |
| 1279 | /* |
| 1280 | * fd.io coding-style-patch-verification: ON |
| 1281 | * |
| 1282 | * Local Variables: |
| 1283 | * eval: (c-set-style "gnu") |
| 1284 | * End: |
| 1285 | */ |