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> |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 28 | #include <vnet/tunnel/tunnel_types_api.h> |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 29 | #include <vnet/fib/fib.h> |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 30 | #include <vnet/ipip/ipip.h> |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 31 | #include <vnet/tunnel/tunnel_types_api.h> |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 32 | #include <vnet/ipsec/ipsec.h> |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 33 | #include <vnet/ipsec/ipsec_tun.h> |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 34 | #include <vnet/ipsec/ipsec_itf.h> |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 35 | |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 36 | #include <vnet/format_fns.h> |
| 37 | #include <vnet/ipsec/ipsec.api_enum.h> |
| 38 | #include <vnet/ipsec/ipsec.api_types.h> |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 39 | |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 40 | #define REPLY_MSG_ID_BASE ipsec_main.msg_id_base |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 41 | #include <vlibapi/api_helper_macros.h> |
| 42 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 43 | static void |
| 44 | 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] | 45 | { |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 46 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 47 | vl_api_ipsec_spd_add_del_reply_t *rmp; |
| 48 | int rv; |
| 49 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 50 | 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] | 51 | |
| 52 | REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static void vl_api_ipsec_interface_add_del_spd_t_handler |
| 56 | (vl_api_ipsec_interface_add_del_spd_t * mp) |
| 57 | { |
| 58 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 59 | vl_api_ipsec_interface_add_del_spd_reply_t *rmp; |
| 60 | int rv; |
| 61 | u32 sw_if_index __attribute__ ((unused)); |
| 62 | u32 spd_id __attribute__ ((unused)); |
| 63 | |
| 64 | sw_if_index = ntohl (mp->sw_if_index); |
| 65 | spd_id = ntohl (mp->spd_id); |
| 66 | |
| 67 | VALIDATE_SW_IF_INDEX (mp); |
| 68 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 69 | rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 70 | |
| 71 | BAD_SW_IF_INDEX_LABEL; |
| 72 | |
| 73 | REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY); |
| 74 | } |
| 75 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 76 | static void vl_api_ipsec_tunnel_protect_update_t_handler |
| 77 | (vl_api_ipsec_tunnel_protect_update_t * mp) |
| 78 | { |
| 79 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 80 | vl_api_ipsec_tunnel_protect_update_reply_t *rmp; |
| 81 | u32 sw_if_index, ii, *sa_ins = NULL; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 82 | ip_address_t nh; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 83 | int rv; |
| 84 | |
| 85 | sw_if_index = ntohl (mp->tunnel.sw_if_index); |
| 86 | |
| 87 | VALIDATE_SW_IF_INDEX (&(mp->tunnel)); |
| 88 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 89 | for (ii = 0; ii < mp->tunnel.n_sa_in; ii++) |
| 90 | vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii])); |
| 91 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 92 | ip_address_decode2 (&mp->tunnel.nh, &nh); |
| 93 | |
| 94 | rv = ipsec_tun_protect_update (sw_if_index, &nh, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 95 | ntohl (mp->tunnel.sa_out), sa_ins); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 96 | |
| 97 | BAD_SW_IF_INDEX_LABEL; |
| 98 | |
| 99 | REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY); |
| 100 | } |
| 101 | |
| 102 | static void vl_api_ipsec_tunnel_protect_del_t_handler |
| 103 | (vl_api_ipsec_tunnel_protect_del_t * mp) |
| 104 | { |
| 105 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 106 | vl_api_ipsec_tunnel_protect_del_reply_t *rmp; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 107 | ip_address_t nh; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 108 | u32 sw_if_index; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 109 | int rv; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 110 | |
| 111 | sw_if_index = ntohl (mp->sw_if_index); |
| 112 | |
| 113 | VALIDATE_SW_IF_INDEX (mp); |
| 114 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 115 | ip_address_decode2 (&mp->nh, &nh); |
| 116 | rv = ipsec_tun_protect_del (sw_if_index, &nh); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 117 | |
| 118 | BAD_SW_IF_INDEX_LABEL; |
| 119 | |
| 120 | REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY); |
| 121 | } |
| 122 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 123 | typedef struct ipsec_dump_walk_ctx_t_ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 124 | { |
| 125 | vl_api_registration_t *reg; |
| 126 | u32 context; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 127 | } ipsec_dump_walk_ctx_t; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 128 | |
| 129 | static walk_rc_t |
| 130 | send_ipsec_tunnel_protect_details (index_t itpi, void *arg) |
| 131 | { |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 132 | ipsec_dump_walk_ctx_t *ctx = arg; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 133 | vl_api_ipsec_tunnel_protect_details_t *mp; |
| 134 | ipsec_tun_protect_t *itp; |
Matthew Smith | 5cee0bc | 2020-03-31 09:52:17 -0500 | [diff] [blame] | 135 | u32 ii = 0; |
| 136 | ipsec_sa_t *sa; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 137 | |
| 138 | itp = ipsec_tun_protect_get (itpi); |
| 139 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 140 | mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in)); |
| 141 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 142 | mp->_vl_msg_id = |
| 143 | ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_TUNNEL_PROTECT_DETAILS); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 144 | mp->context = ctx->context; |
| 145 | |
| 146 | mp->tun.sw_if_index = htonl (itp->itp_sw_if_index); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 147 | ip_address_encode2 (itp->itp_key, &mp->tun.nh); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 148 | |
Matthew Smith | 5cee0bc | 2020-03-31 09:52:17 -0500 | [diff] [blame] | 149 | sa = ipsec_sa_get (itp->itp_out_sa); |
| 150 | mp->tun.sa_out = htonl (sa->id); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 151 | mp->tun.n_sa_in = itp->itp_n_sa_in; |
| 152 | /* *INDENT-OFF* */ |
Matthew Smith | 5cee0bc | 2020-03-31 09:52:17 -0500 | [diff] [blame] | 153 | FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 154 | ({ |
Matthew Smith | 5cee0bc | 2020-03-31 09:52:17 -0500 | [diff] [blame] | 155 | mp->tun.sa_in[ii++] = htonl (sa->id); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 156 | })); |
| 157 | /* *INDENT-ON* */ |
| 158 | |
| 159 | vl_api_send_msg (ctx->reg, (u8 *) mp); |
| 160 | |
| 161 | return (WALK_CONTINUE); |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t |
| 166 | * mp) |
| 167 | { |
| 168 | vl_api_registration_t *reg; |
| 169 | u32 sw_if_index; |
| 170 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 171 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 172 | if (!reg) |
| 173 | return; |
| 174 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 175 | ipsec_dump_walk_ctx_t ctx = { |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 176 | .reg = reg, |
| 177 | .context = mp->context, |
| 178 | }; |
| 179 | |
| 180 | sw_if_index = ntohl (mp->sw_if_index); |
| 181 | |
| 182 | if (~0 == sw_if_index) |
| 183 | { |
| 184 | ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx); |
| 185 | } |
| 186 | else |
| 187 | { |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 188 | ipsec_tun_protect_walk_itf (sw_if_index, |
| 189 | send_ipsec_tunnel_protect_details, &ctx); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 190 | } |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 193 | static int |
| 194 | ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in, |
| 195 | ipsec_policy_action_t * out) |
| 196 | { |
| 197 | in = clib_net_to_host_u32 (in); |
| 198 | |
| 199 | switch (in) |
| 200 | { |
| 201 | #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \ |
| 202 | *out = IPSEC_POLICY_ACTION_##f; \ |
| 203 | return (0); |
| 204 | foreach_ipsec_policy_action |
| 205 | #undef _ |
| 206 | } |
| 207 | return (VNET_API_ERROR_UNIMPLEMENTED); |
| 208 | } |
| 209 | |
| 210 | static void vl_api_ipsec_spd_entry_add_del_t_handler |
| 211 | (vl_api_ipsec_spd_entry_add_del_t * mp) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 212 | { |
| 213 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 214 | vl_api_ipsec_spd_entry_add_del_reply_t *rmp; |
| 215 | ip46_type_t itype; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 216 | u32 stat_index; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 217 | int rv; |
| 218 | |
Neale Ranns | 8c2dd1b | 2019-02-19 08:27:34 -0800 | [diff] [blame] | 219 | stat_index = ~0; |
| 220 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 221 | ipsec_policy_t p; |
| 222 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 223 | clib_memset (&p, 0, sizeof (p)); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 224 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 225 | p.id = ntohl (mp->entry.spd_id); |
| 226 | p.priority = ntohl (mp->entry.priority); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 227 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 228 | itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start); |
| 229 | ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop); |
| 230 | ip_address_decode (&mp->entry.local_address_start, &p.laddr.start); |
| 231 | ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop); |
| 232 | |
| 233 | p.is_ipv6 = (itype == IP46_TYPE_IP6); |
| 234 | |
| 235 | p.protocol = mp->entry.protocol; |
Neale Ranns | a4d2431 | 2019-07-10 13:46:21 +0000 | [diff] [blame] | 236 | p.rport.start = ntohs (mp->entry.remote_port_start); |
| 237 | p.rport.stop = ntohs (mp->entry.remote_port_stop); |
| 238 | p.lport.start = ntohs (mp->entry.local_port_start); |
| 239 | p.lport.stop = ntohs (mp->entry.local_port_stop); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 240 | |
| 241 | rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy); |
| 242 | |
| 243 | if (rv) |
| 244 | goto out; |
| 245 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 246 | /* policy action resolve unsupported */ |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 247 | if (p.policy == IPSEC_POLICY_ACTION_RESOLVE) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 248 | { |
| 249 | clib_warning ("unsupported action: 'resolve'"); |
| 250 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 251 | goto out; |
| 252 | } |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 253 | p.sa_id = ntohl (mp->entry.sa_id); |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 254 | rv = |
| 255 | ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy, |
| 256 | &p.type); |
| 257 | if (rv) |
| 258 | goto out; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 259 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 260 | rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 261 | if (rv) |
| 262 | goto out; |
| 263 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 264 | out: |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 265 | /* *INDENT-OFF* */ |
| 266 | REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY, |
| 267 | ({ |
| 268 | rmp->stat_index = ntohl(stat_index); |
| 269 | })); |
| 270 | /* *INDENT-ON* */ |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 273 | static void vl_api_ipsec_sad_entry_add_del_t_handler |
| 274 | (vl_api_ipsec_sad_entry_add_del_t * mp) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 275 | { |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 276 | vl_api_ipsec_sad_entry_add_del_reply_t *rmp; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 277 | ipsec_key_t crypto_key, integ_key; |
| 278 | ipsec_crypto_alg_t crypto_alg; |
| 279 | ipsec_integ_alg_t integ_alg; |
| 280 | ipsec_protocol_t proto; |
| 281 | ipsec_sa_flags_t flags; |
Dave Barach | a5160d7 | 2019-03-13 15:29:15 -0400 | [diff] [blame] | 282 | u32 id, spi, sa_index = ~0; |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 283 | tunnel_t tun = { |
| 284 | .t_flags = TUNNEL_FLAG_NONE, |
| 285 | .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE, |
| 286 | .t_dscp = 0, |
| 287 | .t_mode = TUNNEL_MODE_P2P, |
| 288 | .t_table_id = 0, |
| 289 | .t_hop_limit = 255, |
| 290 | }; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 291 | int rv; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 292 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 293 | id = ntohl (mp->entry.sad_id); |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 294 | if (!mp->is_add) |
| 295 | { |
| 296 | rv = ipsec_sa_unlock_id (id); |
| 297 | goto out; |
| 298 | } |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 299 | spi = ntohl (mp->entry.spi); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 300 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 301 | rv = ipsec_proto_decode (mp->entry.protocol, &proto); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 302 | |
| 303 | if (rv) |
| 304 | goto out; |
| 305 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 306 | rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 307 | |
| 308 | if (rv) |
| 309 | goto out; |
| 310 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 311 | rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 312 | |
| 313 | if (rv) |
| 314 | goto out; |
| 315 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 316 | ipsec_key_decode (&mp->entry.crypto_key, &crypto_key); |
| 317 | ipsec_key_decode (&mp->entry.integrity_key, &integ_key); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 318 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 319 | flags = ipsec_sa_flags_decode (mp->entry.flags); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 320 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 321 | ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src); |
| 322 | ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 323 | |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 324 | rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &crypto_key, |
| 325 | integ_alg, &integ_key, flags, mp->entry.salt, |
| 326 | htons (mp->entry.udp_src_port), |
| 327 | htons (mp->entry.udp_dst_port), &tun, &sa_index); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 328 | |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 329 | out: |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 330 | /* *INDENT-OFF* */ |
| 331 | REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY, |
| 332 | { |
| 333 | rmp->stat_index = htonl (sa_index); |
| 334 | }); |
| 335 | /* *INDENT-ON* */ |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 338 | static void vl_api_ipsec_sad_entry_add_del_v2_t_handler |
| 339 | (vl_api_ipsec_sad_entry_add_del_v2_t * mp) |
| 340 | { |
| 341 | vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main (); |
| 342 | vl_api_ipsec_sad_entry_add_del_v2_reply_t *rmp; |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 343 | ipsec_key_t crypto_key, integ_key; |
| 344 | ipsec_crypto_alg_t crypto_alg; |
| 345 | ipsec_integ_alg_t integ_alg; |
| 346 | ipsec_protocol_t proto; |
| 347 | ipsec_sa_flags_t flags; |
| 348 | u32 id, spi, sa_index = ~0; |
| 349 | int rv; |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 350 | tunnel_t tun = { |
| 351 | .t_flags = TUNNEL_FLAG_NONE, |
| 352 | .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE, |
| 353 | .t_dscp = 0, |
| 354 | .t_mode = TUNNEL_MODE_P2P, |
| 355 | .t_table_id = htonl (mp->entry.tx_table_id), |
| 356 | .t_hop_limit = 255, |
| 357 | }; |
| 358 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 359 | id = ntohl (mp->entry.sad_id); |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 360 | if (!mp->is_add) |
| 361 | { |
| 362 | rv = ipsec_sa_unlock_id (id); |
| 363 | goto out; |
| 364 | } |
| 365 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 366 | spi = ntohl (mp->entry.spi); |
| 367 | |
| 368 | rv = ipsec_proto_decode (mp->entry.protocol, &proto); |
| 369 | |
| 370 | if (rv) |
| 371 | goto out; |
| 372 | |
| 373 | rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg); |
| 374 | |
| 375 | if (rv) |
| 376 | goto out; |
| 377 | |
| 378 | rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg); |
| 379 | |
| 380 | if (rv) |
| 381 | goto out; |
| 382 | |
| 383 | rv = tunnel_encap_decap_flags_decode (mp->entry.tunnel_flags, |
| 384 | &tun.t_encap_decap_flags); |
| 385 | |
| 386 | if (rv) |
| 387 | goto out; |
| 388 | |
| 389 | ipsec_key_decode (&mp->entry.crypto_key, &crypto_key); |
| 390 | ipsec_key_decode (&mp->entry.integrity_key, &integ_key); |
| 391 | |
| 392 | flags = ipsec_sa_flags_decode (mp->entry.flags); |
| 393 | tun.t_dscp = ip_dscp_decode (mp->entry.dscp); |
| 394 | |
| 395 | ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src); |
| 396 | ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst); |
| 397 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 398 | rv = ipsec_sa_add_and_lock ( |
| 399 | id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags, |
| 400 | mp->entry.salt, htons (mp->entry.udp_src_port), |
| 401 | htons (mp->entry.udp_dst_port), &tun, &sa_index); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 402 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 403 | out: |
| 404 | /* *INDENT-OFF* */ |
| 405 | REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V2_REPLY, |
| 406 | { |
| 407 | rmp->stat_index = htonl (sa_index); |
| 408 | }); |
| 409 | /* *INDENT-ON* */ |
| 410 | } |
| 411 | |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 412 | static int |
| 413 | ipsec_sad_entry_add_v3 (const vl_api_ipsec_sad_entry_v3_t *entry, |
| 414 | u32 *sa_index) |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 415 | { |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 416 | ipsec_key_t crypto_key, integ_key; |
| 417 | ipsec_crypto_alg_t crypto_alg; |
| 418 | ipsec_integ_alg_t integ_alg; |
| 419 | ipsec_protocol_t proto; |
| 420 | ipsec_sa_flags_t flags; |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 421 | u32 id, spi; |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 422 | tunnel_t tun; |
| 423 | int rv; |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 424 | |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 425 | id = ntohl (entry->sad_id); |
| 426 | spi = ntohl (entry->spi); |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 427 | |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 428 | rv = ipsec_proto_decode (entry->protocol, &proto); |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 429 | |
| 430 | if (rv) |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 431 | return (rv); |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 432 | |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 433 | rv = ipsec_crypto_algo_decode (entry->crypto_algorithm, &crypto_alg); |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 434 | |
| 435 | if (rv) |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 436 | return (rv); |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 437 | |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 438 | rv = ipsec_integ_algo_decode (entry->integrity_algorithm, &integ_alg); |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 439 | |
| 440 | if (rv) |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 441 | return (rv); |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 442 | |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 443 | flags = ipsec_sa_flags_decode (entry->flags); |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 444 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 445 | if (flags & IPSEC_SA_FLAG_IS_TUNNEL) |
| 446 | { |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 447 | rv = tunnel_decode (&entry->tunnel, &tun); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 448 | |
| 449 | if (rv) |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 450 | return (rv); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 451 | } |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 452 | |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 453 | ipsec_key_decode (&entry->crypto_key, &crypto_key); |
| 454 | ipsec_key_decode (&entry->integrity_key, &integ_key); |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 455 | |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 456 | return ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &crypto_key, |
| 457 | integ_alg, &integ_key, flags, entry->salt, |
| 458 | htons (entry->udp_src_port), |
| 459 | htons (entry->udp_dst_port), &tun, sa_index); |
| 460 | } |
| 461 | |
| 462 | static void |
| 463 | vl_api_ipsec_sad_entry_add_del_v3_t_handler ( |
| 464 | vl_api_ipsec_sad_entry_add_del_v3_t *mp) |
| 465 | { |
| 466 | vl_api_ipsec_sad_entry_add_del_v3_reply_t *rmp; |
| 467 | u32 id, sa_index = ~0; |
| 468 | int rv; |
| 469 | |
| 470 | id = ntohl (mp->entry.sad_id); |
| 471 | |
| 472 | if (!mp->is_add) |
| 473 | { |
| 474 | rv = ipsec_sa_unlock_id (id); |
| 475 | } |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 476 | else |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 477 | { |
| 478 | rv = ipsec_sad_entry_add_v3 (&mp->entry, &sa_index); |
| 479 | } |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 480 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 481 | REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V3_REPLY, |
| 482 | { rmp->stat_index = htonl (sa_index); }); |
Neale Ranns | c7eaa71 | 2021-02-04 11:09:33 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static void |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 486 | vl_api_ipsec_sad_entry_del_t_handler (vl_api_ipsec_sad_entry_del_t *mp) |
| 487 | { |
| 488 | vl_api_ipsec_sad_entry_del_reply_t *rmp; |
| 489 | int rv; |
| 490 | |
| 491 | rv = ipsec_sa_unlock_id (ntohl (mp->id)); |
| 492 | |
| 493 | REPLY_MACRO (VL_API_IPSEC_SAD_ENTRY_DEL_REPLY); |
| 494 | } |
| 495 | |
| 496 | static void |
| 497 | vl_api_ipsec_sad_entry_add_t_handler (vl_api_ipsec_sad_entry_add_t *mp) |
| 498 | { |
| 499 | vl_api_ipsec_sad_entry_add_reply_t *rmp; |
| 500 | u32 sa_index = ~0; |
| 501 | int rv; |
| 502 | |
| 503 | rv = ipsec_sad_entry_add_v3 (&mp->entry, &sa_index); |
| 504 | |
| 505 | REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_REPLY, |
| 506 | { rmp->stat_index = htonl (sa_index); }); |
| 507 | } |
| 508 | |
| 509 | static void |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 510 | send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg, |
| 511 | u32 context) |
| 512 | { |
| 513 | vl_api_ipsec_spds_details_t *mp; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 514 | u32 n_policies = 0; |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 515 | |
| 516 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 517 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 518 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPDS_DETAILS); |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 519 | mp->context = context; |
| 520 | |
| 521 | mp->spd_id = htonl (spd->id); |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 522 | #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]); |
| 523 | foreach_ipsec_spd_policy_type |
| 524 | #undef _ |
| 525 | mp->npolicies = htonl (n_policies); |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 526 | |
| 527 | vl_api_send_msg (reg, (u8 *) mp); |
| 528 | } |
| 529 | |
| 530 | static void |
| 531 | vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp) |
| 532 | { |
| 533 | vl_api_registration_t *reg; |
| 534 | ipsec_main_t *im = &ipsec_main; |
| 535 | ipsec_spd_t *spd; |
Damjan Marion | 9789898 | 2021-04-19 18:15:31 +0200 | [diff] [blame] | 536 | |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 537 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 538 | if (!reg) |
| 539 | return; |
| 540 | |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 541 | pool_foreach (spd, im->spds) { |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 542 | send_ipsec_spds_details (spd, reg, mp->context); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 543 | } |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 546 | vl_api_ipsec_spd_action_t |
| 547 | ipsec_spd_action_encode (ipsec_policy_action_t in) |
| 548 | { |
| 549 | vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS; |
| 550 | |
| 551 | switch (in) |
| 552 | { |
| 553 | #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \ |
| 554 | out = IPSEC_API_SPD_ACTION_##f; \ |
| 555 | break; |
| 556 | foreach_ipsec_policy_action |
| 557 | #undef _ |
| 558 | } |
| 559 | return (clib_host_to_net_u32 (out)); |
| 560 | } |
| 561 | |
Matus Fabian | a9a0b2c | 2018-10-02 01:13:25 -0700 | [diff] [blame] | 562 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 563 | send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg, |
| 564 | u32 context) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 565 | { |
| 566 | vl_api_ipsec_spd_details_t *mp; |
| 567 | |
| 568 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 569 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 570 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_DETAILS); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 571 | mp->context = context; |
| 572 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 573 | mp->entry.spd_id = htonl (p->id); |
| 574 | mp->entry.priority = htonl (p->priority); |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 575 | mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) || |
| 576 | (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND)); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 577 | |
| 578 | ip_address_encode (&p->laddr.start, IP46_TYPE_ANY, |
| 579 | &mp->entry.local_address_start); |
| 580 | ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY, |
| 581 | &mp->entry.local_address_stop); |
| 582 | ip_address_encode (&p->raddr.start, IP46_TYPE_ANY, |
| 583 | &mp->entry.remote_address_start); |
| 584 | ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY, |
| 585 | &mp->entry.remote_address_stop); |
Neale Ranns | a4d2431 | 2019-07-10 13:46:21 +0000 | [diff] [blame] | 586 | mp->entry.local_port_start = htons (p->lport.start); |
| 587 | mp->entry.local_port_stop = htons (p->lport.stop); |
| 588 | mp->entry.remote_port_start = htons (p->rport.start); |
| 589 | mp->entry.remote_port_stop = htons (p->rport.stop); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 590 | mp->entry.protocol = p->protocol; |
| 591 | mp->entry.policy = ipsec_spd_action_encode (p->policy); |
| 592 | mp->entry.sa_id = htonl (p->sa_id); |
| 593 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 594 | vl_api_send_msg (reg, (u8 *) mp); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static void |
| 598 | vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp) |
| 599 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 600 | vl_api_registration_t *reg; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 601 | ipsec_main_t *im = &ipsec_main; |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 602 | ipsec_spd_policy_type_t ptype; |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 603 | ipsec_policy_t *policy; |
| 604 | ipsec_spd_t *spd; |
| 605 | uword *p; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 606 | u32 spd_index, *ii; |
Damjan Marion | 9789898 | 2021-04-19 18:15:31 +0200 | [diff] [blame] | 607 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 608 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 609 | if (!reg) |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 610 | return; |
| 611 | |
| 612 | p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id)); |
| 613 | if (!p) |
| 614 | return; |
| 615 | |
| 616 | spd_index = p[0]; |
| 617 | spd = pool_elt_at_index (im->spds, spd_index); |
| 618 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 619 | FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) { |
| 620 | vec_foreach(ii, spd->policies[ptype]) |
| 621 | { |
| 622 | policy = pool_elt_at_index(im->policies, *ii); |
| 623 | |
| 624 | if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id) |
| 625 | send_ipsec_spd_details (policy, reg, mp->context); |
| 626 | } |
| 627 | } |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static void |
Filip Varga | 871bca9 | 2018-11-02 13:51:44 +0100 | [diff] [blame] | 631 | send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index, |
| 632 | u32 sw_if_index, u32 context) |
| 633 | { |
| 634 | vl_api_ipsec_spd_interface_details_t *mp; |
| 635 | |
| 636 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 637 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 638 | mp->_vl_msg_id = |
| 639 | ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_INTERFACE_DETAILS); |
Filip Varga | 871bca9 | 2018-11-02 13:51:44 +0100 | [diff] [blame] | 640 | mp->context = context; |
| 641 | |
| 642 | mp->spd_index = htonl (spd_index); |
| 643 | mp->sw_if_index = htonl (sw_if_index); |
| 644 | |
| 645 | vl_api_send_msg (reg, (u8 *) mp); |
| 646 | } |
| 647 | |
| 648 | static void |
| 649 | vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t * |
| 650 | mp) |
| 651 | { |
| 652 | ipsec_main_t *im = &ipsec_main; |
| 653 | vl_api_registration_t *reg; |
| 654 | u32 k, v, spd_index; |
| 655 | |
Filip Varga | 871bca9 | 2018-11-02 13:51:44 +0100 | [diff] [blame] | 656 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 657 | if (!reg) |
| 658 | return; |
| 659 | |
| 660 | if (mp->spd_index_valid) |
| 661 | { |
| 662 | spd_index = ntohl (mp->spd_index); |
| 663 | /* *INDENT-OFF* */ |
| 664 | hash_foreach(k, v, im->spd_index_by_sw_if_index, ({ |
| 665 | if (v == spd_index) |
| 666 | send_ipsec_spd_interface_details(reg, v, k, mp->context); |
| 667 | })); |
| 668 | /* *INDENT-ON* */ |
| 669 | } |
| 670 | else |
| 671 | { |
Filip Varga | 871bca9 | 2018-11-02 13:51:44 +0100 | [diff] [blame] | 672 | hash_foreach(k, v, im->spd_index_by_sw_if_index, ({ |
| 673 | send_ipsec_spd_interface_details(reg, v, k, mp->context); |
| 674 | })); |
Filip Varga | 871bca9 | 2018-11-02 13:51:44 +0100 | [diff] [blame] | 675 | } |
Filip Varga | 871bca9 | 2018-11-02 13:51:44 +0100 | [diff] [blame] | 676 | } |
| 677 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 678 | static void |
| 679 | vl_api_ipsec_itf_create_t_handler (vl_api_ipsec_itf_create_t * mp) |
| 680 | { |
| 681 | vl_api_ipsec_itf_create_reply_t *rmp; |
| 682 | tunnel_mode_t mode; |
| 683 | u32 sw_if_index = ~0; |
| 684 | int rv; |
| 685 | |
| 686 | rv = tunnel_mode_decode (mp->itf.mode, &mode); |
| 687 | |
| 688 | if (!rv) |
| 689 | rv = ipsec_itf_create (ntohl (mp->itf.user_instance), mode, &sw_if_index); |
| 690 | |
| 691 | /* *INDENT-OFF* */ |
| 692 | REPLY_MACRO2 (VL_API_IPSEC_ITF_CREATE_REPLY, |
| 693 | ({ |
| 694 | rmp->sw_if_index = htonl (sw_if_index); |
| 695 | })); |
| 696 | /* *INDENT-ON* */ |
| 697 | } |
| 698 | |
| 699 | static void |
| 700 | vl_api_ipsec_itf_delete_t_handler (vl_api_ipsec_itf_delete_t * mp) |
| 701 | { |
| 702 | vl_api_ipsec_itf_delete_reply_t *rmp; |
| 703 | int rv; |
| 704 | |
| 705 | rv = ipsec_itf_delete (ntohl (mp->sw_if_index)); |
| 706 | |
| 707 | REPLY_MACRO (VL_API_IPSEC_ITF_DELETE_REPLY); |
| 708 | } |
| 709 | |
Neale Ranns | 89d939e | 2021-06-07 09:34:07 +0000 | [diff] [blame] | 710 | static walk_rc_t |
| 711 | send_ipsec_itf_details (ipsec_itf_t *itf, void *arg) |
| 712 | { |
| 713 | ipsec_dump_walk_ctx_t *ctx = arg; |
| 714 | vl_api_ipsec_itf_details_t *mp; |
| 715 | |
| 716 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 717 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 718 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_ITF_DETAILS); |
Neale Ranns | 89d939e | 2021-06-07 09:34:07 +0000 | [diff] [blame] | 719 | mp->context = ctx->context; |
| 720 | |
| 721 | mp->itf.mode = tunnel_mode_encode (itf->ii_mode); |
| 722 | mp->itf.user_instance = htonl (itf->ii_user_instance); |
| 723 | mp->itf.sw_if_index = htonl (itf->ii_sw_if_index); |
| 724 | vl_api_send_msg (ctx->reg, (u8 *) mp); |
| 725 | |
| 726 | return (WALK_CONTINUE); |
| 727 | } |
| 728 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 729 | static void |
| 730 | vl_api_ipsec_itf_dump_t_handler (vl_api_ipsec_itf_dump_t * mp) |
| 731 | { |
Neale Ranns | 89d939e | 2021-06-07 09:34:07 +0000 | [diff] [blame] | 732 | vl_api_registration_t *reg; |
| 733 | |
| 734 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 735 | if (!reg) |
| 736 | return; |
| 737 | |
| 738 | ipsec_dump_walk_ctx_t ctx = { |
| 739 | .reg = reg, |
| 740 | .context = mp->context, |
| 741 | }; |
| 742 | |
| 743 | ipsec_itf_walk (send_ipsec_itf_details, &ctx); |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 746 | typedef struct ipsec_sa_dump_match_ctx_t_ |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 747 | { |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 748 | index_t sai; |
| 749 | u32 sw_if_index; |
| 750 | } ipsec_sa_dump_match_ctx_t; |
| 751 | |
| 752 | static walk_rc_t |
| 753 | ipsec_sa_dump_match_sa (index_t itpi, void *arg) |
| 754 | { |
| 755 | ipsec_sa_dump_match_ctx_t *ctx = arg; |
| 756 | ipsec_tun_protect_t *itp; |
| 757 | index_t sai; |
| 758 | |
| 759 | itp = ipsec_tun_protect_get (itpi); |
| 760 | |
| 761 | if (itp->itp_out_sa == ctx->sai) |
| 762 | { |
| 763 | ctx->sw_if_index = itp->itp_sw_if_index; |
| 764 | return (WALK_STOP); |
| 765 | } |
Damjan Marion | 9789898 | 2021-04-19 18:15:31 +0200 | [diff] [blame] | 766 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 767 | FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai, |
| 768 | ({ |
| 769 | if (sai == ctx->sai) |
| 770 | { |
| 771 | ctx->sw_if_index = itp->itp_sw_if_index; |
| 772 | return (WALK_STOP); |
| 773 | } |
| 774 | })); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 775 | |
| 776 | return (WALK_CONTINUE); |
| 777 | } |
| 778 | |
| 779 | static walk_rc_t |
| 780 | send_ipsec_sa_details (ipsec_sa_t * sa, void *arg) |
| 781 | { |
| 782 | ipsec_dump_walk_ctx_t *ctx = arg; |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 783 | vl_api_ipsec_sa_details_t *mp; |
| 784 | |
| 785 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 786 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 787 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_DETAILS); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 788 | mp->context = ctx->context; |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 789 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 790 | mp->entry.sad_id = htonl (sa->id); |
| 791 | mp->entry.spi = htonl (sa->spi); |
| 792 | mp->entry.protocol = ipsec_proto_encode (sa->protocol); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 793 | mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 794 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 795 | mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg); |
| 796 | ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 797 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 798 | mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg); |
| 799 | ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 800 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 801 | mp->entry.flags = ipsec_sad_flags_encode (sa); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 802 | mp->entry.salt = clib_host_to_net_u32 (sa->salt); |
| 803 | |
| 804 | if (ipsec_sa_is_set_IS_PROTECT (sa)) |
| 805 | { |
| 806 | ipsec_sa_dump_match_ctx_t ctx = { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 807 | .sai = sa - ipsec_sa_pool, |
| 808 | .sw_if_index = ~0, |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 809 | }; |
| 810 | ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx); |
| 811 | |
| 812 | mp->sw_if_index = htonl (ctx.sw_if_index); |
| 813 | } |
| 814 | else |
| 815 | mp->sw_if_index = ~0; |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 816 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 817 | if (ipsec_sa_is_set_IS_TUNNEL (sa)) |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 818 | { |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 819 | ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src); |
| 820 | ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 821 | } |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 822 | if (ipsec_sa_is_set_UDP_ENCAP (sa)) |
| 823 | { |
| 824 | mp->entry.udp_src_port = sa->udp_hdr.src_port; |
| 825 | mp->entry.udp_dst_port = sa->udp_hdr.dst_port; |
| 826 | } |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 827 | |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 828 | mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq)); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 829 | mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq)); |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 830 | if (ipsec_sa_is_set_USE_ESN (sa)) |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 831 | { |
| 832 | mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi)); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 833 | mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi)); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 834 | } |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 835 | if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa)) |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 836 | mp->replay_window = clib_host_to_net_u64 (sa->replay_window); |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 837 | |
Matthew Smith | 48d32b4 | 2020-04-02 07:45:49 -0500 | [diff] [blame] | 838 | mp->stat_index = clib_host_to_net_u32 (sa->stat_index); |
| 839 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 840 | vl_api_send_msg (ctx->reg, (u8 *) mp); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 841 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 842 | return (WALK_CONTINUE); |
| 843 | } |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 844 | |
| 845 | static void |
| 846 | vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp) |
| 847 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 848 | vl_api_registration_t *reg; |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 849 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 850 | reg = vl_api_client_index_to_registration (mp->client_index); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 851 | if (!reg) |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 852 | return; |
| 853 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 854 | ipsec_dump_walk_ctx_t ctx = { |
| 855 | .reg = reg, |
| 856 | .context = mp->context, |
| 857 | }; |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 858 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 859 | ipsec_sa_walk (send_ipsec_sa_details, &ctx); |
Matthew Smith | 2802953 | 2017-09-26 13:33:44 -0500 | [diff] [blame] | 860 | } |
| 861 | |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 862 | static walk_rc_t |
| 863 | send_ipsec_sa_v2_details (ipsec_sa_t * sa, void *arg) |
| 864 | { |
| 865 | ipsec_dump_walk_ctx_t *ctx = arg; |
| 866 | vl_api_ipsec_sa_v2_details_t *mp; |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 867 | |
| 868 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 869 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 870 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V2_DETAILS); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 871 | mp->context = ctx->context; |
| 872 | |
| 873 | mp->entry.sad_id = htonl (sa->id); |
| 874 | mp->entry.spi = htonl (sa->spi); |
| 875 | mp->entry.protocol = ipsec_proto_encode (sa->protocol); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 876 | mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 877 | |
| 878 | mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg); |
| 879 | ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key); |
| 880 | |
| 881 | mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg); |
| 882 | ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key); |
| 883 | |
| 884 | mp->entry.flags = ipsec_sad_flags_encode (sa); |
| 885 | mp->entry.salt = clib_host_to_net_u32 (sa->salt); |
| 886 | |
| 887 | if (ipsec_sa_is_set_IS_PROTECT (sa)) |
| 888 | { |
| 889 | ipsec_sa_dump_match_ctx_t ctx = { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 890 | .sai = sa - ipsec_sa_pool, |
| 891 | .sw_if_index = ~0, |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 892 | }; |
| 893 | ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx); |
| 894 | |
| 895 | mp->sw_if_index = htonl (ctx.sw_if_index); |
| 896 | } |
| 897 | else |
| 898 | mp->sw_if_index = ~0; |
| 899 | |
| 900 | if (ipsec_sa_is_set_IS_TUNNEL (sa)) |
| 901 | { |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 902 | ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src); |
| 903 | ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 904 | } |
| 905 | if (ipsec_sa_is_set_UDP_ENCAP (sa)) |
| 906 | { |
| 907 | mp->entry.udp_src_port = sa->udp_hdr.src_port; |
| 908 | mp->entry.udp_dst_port = sa->udp_hdr.dst_port; |
| 909 | } |
| 910 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 911 | mp->entry.tunnel_flags = |
| 912 | tunnel_encap_decap_flags_encode (sa->tunnel.t_encap_decap_flags); |
| 913 | mp->entry.dscp = ip_dscp_encode (sa->tunnel.t_dscp); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 914 | |
| 915 | mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq)); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 916 | mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq)); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 917 | if (ipsec_sa_is_set_USE_ESN (sa)) |
| 918 | { |
| 919 | mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi)); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 920 | mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi)); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 921 | } |
| 922 | if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa)) |
| 923 | mp->replay_window = clib_host_to_net_u64 (sa->replay_window); |
| 924 | |
| 925 | mp->stat_index = clib_host_to_net_u32 (sa->stat_index); |
| 926 | |
| 927 | vl_api_send_msg (ctx->reg, (u8 *) mp); |
| 928 | |
| 929 | return (WALK_CONTINUE); |
| 930 | } |
| 931 | |
| 932 | static void |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 933 | vl_api_ipsec_sa_v2_dump_t_handler (vl_api_ipsec_sa_v2_dump_t *mp) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 934 | { |
| 935 | vl_api_registration_t *reg; |
| 936 | |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 937 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 938 | if (!reg) |
| 939 | return; |
| 940 | |
| 941 | ipsec_dump_walk_ctx_t ctx = { |
| 942 | .reg = reg, |
| 943 | .context = mp->context, |
| 944 | }; |
| 945 | |
| 946 | ipsec_sa_walk (send_ipsec_sa_v2_details, &ctx); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 949 | static walk_rc_t |
| 950 | send_ipsec_sa_v3_details (ipsec_sa_t *sa, void *arg) |
| 951 | { |
| 952 | ipsec_dump_walk_ctx_t *ctx = arg; |
| 953 | vl_api_ipsec_sa_v3_details_t *mp; |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 954 | |
| 955 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 956 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 957 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V3_DETAILS); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 958 | mp->context = ctx->context; |
| 959 | |
| 960 | mp->entry.sad_id = htonl (sa->id); |
| 961 | mp->entry.spi = htonl (sa->spi); |
| 962 | mp->entry.protocol = ipsec_proto_encode (sa->protocol); |
| 963 | |
| 964 | mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg); |
| 965 | ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key); |
| 966 | |
| 967 | mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg); |
| 968 | ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key); |
| 969 | |
| 970 | mp->entry.flags = ipsec_sad_flags_encode (sa); |
| 971 | mp->entry.salt = clib_host_to_net_u32 (sa->salt); |
| 972 | |
| 973 | if (ipsec_sa_is_set_IS_PROTECT (sa)) |
| 974 | { |
| 975 | ipsec_sa_dump_match_ctx_t ctx = { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 976 | .sai = sa - ipsec_sa_pool, |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 977 | .sw_if_index = ~0, |
| 978 | }; |
| 979 | ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx); |
| 980 | |
| 981 | mp->sw_if_index = htonl (ctx.sw_if_index); |
| 982 | } |
| 983 | else |
| 984 | mp->sw_if_index = ~0; |
| 985 | |
| 986 | if (ipsec_sa_is_set_IS_TUNNEL (sa)) |
| 987 | tunnel_encode (&sa->tunnel, &mp->entry.tunnel); |
| 988 | |
| 989 | if (ipsec_sa_is_set_UDP_ENCAP (sa)) |
| 990 | { |
| 991 | mp->entry.udp_src_port = sa->udp_hdr.src_port; |
| 992 | mp->entry.udp_dst_port = sa->udp_hdr.dst_port; |
| 993 | } |
| 994 | |
| 995 | mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq)); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 996 | mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq)); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 997 | if (ipsec_sa_is_set_USE_ESN (sa)) |
| 998 | { |
| 999 | mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi)); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 1000 | mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi)); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 1001 | } |
| 1002 | if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa)) |
| 1003 | mp->replay_window = clib_host_to_net_u64 (sa->replay_window); |
| 1004 | |
| 1005 | mp->stat_index = clib_host_to_net_u32 (sa->stat_index); |
| 1006 | |
| 1007 | vl_api_send_msg (ctx->reg, (u8 *) mp); |
| 1008 | |
| 1009 | return (WALK_CONTINUE); |
| 1010 | } |
| 1011 | |
| 1012 | static void |
| 1013 | vl_api_ipsec_sa_v3_dump_t_handler (vl_api_ipsec_sa_v3_dump_t *mp) |
| 1014 | { |
| 1015 | vl_api_registration_t *reg; |
| 1016 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 1017 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 1018 | if (!reg) |
| 1019 | return; |
| 1020 | |
| 1021 | ipsec_dump_walk_ctx_t ctx = { |
| 1022 | .reg = reg, |
| 1023 | .context = mp->context, |
| 1024 | }; |
| 1025 | |
| 1026 | ipsec_sa_walk (send_ipsec_sa_v3_details, &ctx); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Matthew Smith | 75d8560 | 2017-10-05 19:03:05 -0500 | [diff] [blame] | 1029 | static void |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1030 | vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp) |
| 1031 | { |
| 1032 | vl_api_registration_t *rp; |
| 1033 | ipsec_main_t *im = &ipsec_main; |
| 1034 | u32 context = mp->context; |
| 1035 | |
| 1036 | rp = vl_api_client_index_to_registration (mp->client_index); |
| 1037 | |
| 1038 | if (rp == 0) |
| 1039 | { |
| 1040 | clib_warning ("Client %d AWOL", mp->client_index); |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | ipsec_ah_backend_t *ab; |
| 1045 | ipsec_esp_backend_t *eb; |
| 1046 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1047 | pool_foreach (ab, im->ah_backends) { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1048 | vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp)); |
| 1049 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 1050 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1051 | mp->context = context; |
| 1052 | snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name), |
| 1053 | ab->name); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 1054 | mp->protocol = ntohl (IPSEC_API_PROTO_AH); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1055 | mp->index = ab - im->ah_backends; |
| 1056 | mp->active = mp->index == im->ah_current_backend ? 1 : 0; |
| 1057 | vl_api_send_msg (rp, (u8 *)mp); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1058 | } |
| 1059 | pool_foreach (eb, im->esp_backends) { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1060 | vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp)); |
| 1061 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 1062 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1063 | mp->context = context; |
| 1064 | snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name), |
| 1065 | eb->name); |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 1066 | mp->protocol = ntohl (IPSEC_API_PROTO_ESP); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1067 | mp->index = eb - im->esp_backends; |
| 1068 | mp->active = mp->index == im->esp_current_backend ? 1 : 0; |
| 1069 | vl_api_send_msg (rp, (u8 *)mp); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1070 | } |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1071 | /* *INDENT-ON* */ |
| 1072 | } |
| 1073 | |
| 1074 | static void |
| 1075 | vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp) |
| 1076 | { |
| 1077 | ipsec_main_t *im = &ipsec_main; |
| 1078 | vl_api_ipsec_select_backend_reply_t *rmp; |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 1079 | ipsec_protocol_t protocol; |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1080 | int rv = 0; |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 1081 | if (pool_elts (ipsec_sa_pool) > 0) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1082 | { |
| 1083 | rv = VNET_API_ERROR_INSTANCE_IN_USE; |
| 1084 | goto done; |
| 1085 | } |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 1086 | |
| 1087 | rv = ipsec_proto_decode (mp->protocol, &protocol); |
| 1088 | |
| 1089 | if (rv) |
| 1090 | goto done; |
| 1091 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 1092 | switch (protocol) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1093 | { |
| 1094 | case IPSEC_PROTOCOL_ESP: |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 1095 | rv = ipsec_select_esp_backend (im, mp->index); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1096 | break; |
| 1097 | case IPSEC_PROTOCOL_AH: |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 1098 | rv = ipsec_select_ah_backend (im, mp->index); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1099 | break; |
| 1100 | default: |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 1101 | rv = VNET_API_ERROR_INVALID_PROTOCOL; |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1102 | break; |
| 1103 | } |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1104 | done: |
| 1105 | REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY); |
| 1106 | } |
| 1107 | |
Yulong Pei | 2e84d66 | 2020-08-14 18:21:08 +0800 | [diff] [blame] | 1108 | static void |
| 1109 | vl_api_ipsec_set_async_mode_t_handler (vl_api_ipsec_set_async_mode_t * mp) |
| 1110 | { |
| 1111 | vl_api_ipsec_set_async_mode_reply_t *rmp; |
| 1112 | int rv = 0; |
| 1113 | |
Yulong Pei | 2e84d66 | 2020-08-14 18:21:08 +0800 | [diff] [blame] | 1114 | ipsec_set_async_mode (mp->async_enable); |
| 1115 | |
| 1116 | REPLY_MACRO (VL_API_IPSEC_SET_ASYNC_MODE_REPLY); |
| 1117 | } |
| 1118 | |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 1119 | #include <vnet/ipsec/ipsec.api.c> |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 1120 | static clib_error_t * |
| 1121 | ipsec_api_hookup (vlib_main_t * vm) |
| 1122 | { |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 1123 | /* |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 1124 | * Set up the (msg_name, crc, message-id) table |
| 1125 | */ |
Filip Tehlar | c73f329 | 2021-06-22 08:21:31 +0000 | [diff] [blame] | 1126 | REPLY_MSG_ID_BASE = setup_message_id_table (); |
Pavel Kotucek | 9c7ef03 | 2016-12-21 07:46:45 +0100 | [diff] [blame] | 1127 | |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | VLIB_API_INIT_FUNCTION (ipsec_api_hookup); |
| 1132 | |
| 1133 | /* |
| 1134 | * fd.io coding-style-patch-verification: ON |
| 1135 | * |
| 1136 | * Local Variables: |
| 1137 | * eval: (c-set-style "gnu") |
| 1138 | * End: |
| 1139 | */ |