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