blob: 11bfa41b4f14c5fa8cd8ee420c75a31af1d4781f [file] [log] [blame]
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001/*
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 Ranns17dcec02019-01-09 21:22:20 -080026#include <vnet/ip/ip_types_api.h>
Prashant Maheshwaridbf68c92019-11-14 12:42:59 +053027#include <vnet/ipsec/ipsec_types_api.h>
Neale Rannsdd4ccf22020-06-30 07:47:14 +000028#include <vnet/tunnel/tunnel_types_api.h>
Pierre Pfister4c422f92018-12-10 11:19:08 +010029#include <vnet/fib/fib.h>
Neale Ranns12989b52019-09-26 16:20:19 +000030#include <vnet/ipip/ipip.h>
Neale Ranns041add72020-01-02 04:06:10 +000031#include <vnet/tunnel/tunnel_types_api.h>
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010032#include <vnet/ipsec/ipsec.h>
Neale Rannsc87b66c2019-02-07 07:26:12 -080033#include <vnet/ipsec/ipsec_tun.h>
Neale Rannsdd4ccf22020-06-30 07:47:14 +000034#include <vnet/ipsec/ipsec_itf.h>
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010035
Filip Tehlarc73f3292021-06-22 08:21:31 +000036#include <vnet/format_fns.h>
37#include <vnet/ipsec/ipsec.api_enum.h>
38#include <vnet/ipsec/ipsec.api_types.h>
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010039
Filip Tehlarc73f3292021-06-22 08:21:31 +000040#define REPLY_MSG_ID_BASE ipsec_main.msg_id_base
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010041#include <vlibapi/api_helper_macros.h>
42
Klement Sekerab4d30532018-11-08 13:00:02 +010043static void
44vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010045{
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010046 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
47 vl_api_ipsec_spd_add_del_reply_t *rmp;
48 int rv;
49
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010050 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010051
52 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010053}
54
55static 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 Kotucek9c7ef032016-12-21 07:46:45 +010069 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010070
71 BAD_SW_IF_INDEX_LABEL;
72
73 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
74}
75
Neale Rannsc87b66c2019-02-07 07:26:12 -080076static 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 Ranns28287212019-12-16 00:53:11 +000082 ip_address_t nh;
Neale Rannsc87b66c2019-02-07 07:26:12 -080083 int rv;
84
85 sw_if_index = ntohl (mp->tunnel.sw_if_index);
86
87 VALIDATE_SW_IF_INDEX (&(mp->tunnel));
88
Neale Rannsc87b66c2019-02-07 07:26:12 -080089 for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
90 vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
91
Neale Ranns28287212019-12-16 00:53:11 +000092 ip_address_decode2 (&mp->tunnel.nh, &nh);
93
94 rv = ipsec_tun_protect_update (sw_if_index, &nh,
Neale Rannsc87b66c2019-02-07 07:26:12 -080095 ntohl (mp->tunnel.sa_out), sa_ins);
Neale Rannsc87b66c2019-02-07 07:26:12 -080096
97 BAD_SW_IF_INDEX_LABEL;
98
99 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
100}
101
102static 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 Ranns28287212019-12-16 00:53:11 +0000107 ip_address_t nh;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800108 u32 sw_if_index;
Neale Ranns28287212019-12-16 00:53:11 +0000109 int rv;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800110
111 sw_if_index = ntohl (mp->sw_if_index);
112
113 VALIDATE_SW_IF_INDEX (mp);
114
Neale Ranns28287212019-12-16 00:53:11 +0000115 ip_address_decode2 (&mp->nh, &nh);
116 rv = ipsec_tun_protect_del (sw_if_index, &nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800117
118 BAD_SW_IF_INDEX_LABEL;
119
120 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
121}
122
Neale Ranns12989b52019-09-26 16:20:19 +0000123typedef struct ipsec_dump_walk_ctx_t_
Neale Rannsc87b66c2019-02-07 07:26:12 -0800124{
125 vl_api_registration_t *reg;
126 u32 context;
Neale Ranns12989b52019-09-26 16:20:19 +0000127} ipsec_dump_walk_ctx_t;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800128
129static walk_rc_t
130send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
131{
Neale Ranns12989b52019-09-26 16:20:19 +0000132 ipsec_dump_walk_ctx_t *ctx = arg;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800133 vl_api_ipsec_tunnel_protect_details_t *mp;
134 ipsec_tun_protect_t *itp;
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500135 u32 ii = 0;
136 ipsec_sa_t *sa;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800137
138 itp = ipsec_tun_protect_get (itpi);
139
Neale Rannsc87b66c2019-02-07 07:26:12 -0800140 mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
141 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000142 mp->_vl_msg_id =
143 ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800144 mp->context = ctx->context;
145
146 mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
Neale Ranns28287212019-12-16 00:53:11 +0000147 ip_address_encode2 (itp->itp_key, &mp->tun.nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800148
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500149 sa = ipsec_sa_get (itp->itp_out_sa);
150 mp->tun.sa_out = htonl (sa->id);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800151 mp->tun.n_sa_in = itp->itp_n_sa_in;
152 /* *INDENT-OFF* */
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500153 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800154 ({
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500155 mp->tun.sa_in[ii++] = htonl (sa->id);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800156 }));
157 /* *INDENT-ON* */
158
159 vl_api_send_msg (ctx->reg, (u8 *) mp);
160
161 return (WALK_CONTINUE);
162}
163
164static void
165vl_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 Rannsc87b66c2019-02-07 07:26:12 -0800171 reg = vl_api_client_index_to_registration (mp->client_index);
172 if (!reg)
173 return;
174
Neale Ranns12989b52019-09-26 16:20:19 +0000175 ipsec_dump_walk_ctx_t ctx = {
Neale Rannsc87b66c2019-02-07 07:26:12 -0800176 .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 Ranns28287212019-12-16 00:53:11 +0000188 ipsec_tun_protect_walk_itf (sw_if_index,
189 send_ipsec_tunnel_protect_details, &ctx);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800190 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800191}
192
Neale Ranns17dcec02019-01-09 21:22:20 -0800193static int
194ipsec_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
210static void vl_api_ipsec_spd_entry_add_del_t_handler
211 (vl_api_ipsec_spd_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100212{
213 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
Neale Ranns17dcec02019-01-09 21:22:20 -0800214 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
215 ip46_type_t itype;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800216 u32 stat_index;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100217 int rv;
218
Neale Ranns8c2dd1b2019-02-19 08:27:34 -0800219 stat_index = ~0;
220
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100221 ipsec_policy_t p;
222
Dave Barachb7b92992018-10-17 10:38:51 -0400223 clib_memset (&p, 0, sizeof (p));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100224
Neale Ranns17dcec02019-01-09 21:22:20 -0800225 p.id = ntohl (mp->entry.spd_id);
226 p.priority = ntohl (mp->entry.priority);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100227
Neale Ranns17dcec02019-01-09 21:22:20 -0800228 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 Rannsa4d24312019-07-10 13:46:21 +0000236 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 Ranns17dcec02019-01-09 21:22:20 -0800240
241 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
242
243 if (rv)
244 goto out;
245
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100246 /* policy action resolve unsupported */
Neale Ranns17dcec02019-01-09 21:22:20 -0800247 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100248 {
249 clib_warning ("unsupported action: 'resolve'");
250 rv = VNET_API_ERROR_UNIMPLEMENTED;
251 goto out;
252 }
Neale Ranns17dcec02019-01-09 21:22:20 -0800253 p.sa_id = ntohl (mp->entry.sa_id);
Neale Ranns9f231d42019-03-19 10:06:00 +0000254 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 Kotucek9c7ef032016-12-21 07:46:45 +0100259
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800260 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100261 if (rv)
262 goto out;
263
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100264out:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800265 /* *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 Kotucek9c7ef032016-12-21 07:46:45 +0100271}
272
Neale Ranns17dcec02019-01-09 21:22:20 -0800273static void vl_api_ipsec_sad_entry_add_del_t_handler
274 (vl_api_ipsec_sad_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100275{
Neale Ranns17dcec02019-01-09 21:22:20 -0800276 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800277 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 Baracha5160d72019-03-13 15:29:15 -0400282 u32 id, spi, sa_index = ~0;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000283 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 Kotucek9c7ef032016-12-21 07:46:45 +0100291 int rv;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800292
Neale Ranns8d7c5022019-02-06 01:41:05 -0800293 id = ntohl (mp->entry.sad_id);
Neale Rannsff2e4132021-06-24 14:57:56 +0000294 if (!mp->is_add)
295 {
296 rv = ipsec_sa_unlock_id (id);
297 goto out;
298 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800299 spi = ntohl (mp->entry.spi);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100300
Neale Ranns8d7c5022019-02-06 01:41:05 -0800301 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
Neale Ranns17dcec02019-01-09 21:22:20 -0800302
303 if (rv)
304 goto out;
305
Neale Ranns8d7c5022019-02-06 01:41:05 -0800306 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800307
308 if (rv)
309 goto out;
310
Neale Ranns8d7c5022019-02-06 01:41:05 -0800311 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800312
313 if (rv)
314 goto out;
315
Neale Ranns8d7c5022019-02-06 01:41:05 -0800316 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
317 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
Neale Ranns17dcec02019-01-09 21:22:20 -0800318
Neale Ranns8d7c5022019-02-06 01:41:05 -0800319 flags = ipsec_sa_flags_decode (mp->entry.flags);
Neale Ranns17dcec02019-01-09 21:22:20 -0800320
Neale Ranns9ec846c2021-02-09 14:04:02 +0000321 ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
322 ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100323
Neale Rannsff2e4132021-06-24 14:57:56 +0000324 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 Ranns8d7c5022019-02-06 01:41:05 -0800328
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100329out:
Neale Rannseba31ec2019-02-17 18:04:27 +0000330 /* *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 Kotucek9c7ef032016-12-21 07:46:45 +0100336}
337
Neale Ranns041add72020-01-02 04:06:10 +0000338static 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 Ranns041add72020-01-02 04:06:10 +0000343 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 Ranns9ec846c2021-02-09 14:04:02 +0000350 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 Ranns9ec846c2021-02-09 14:04:02 +0000359 id = ntohl (mp->entry.sad_id);
Neale Rannsff2e4132021-06-24 14:57:56 +0000360 if (!mp->is_add)
361 {
362 rv = ipsec_sa_unlock_id (id);
363 goto out;
364 }
365
Neale Ranns9ec846c2021-02-09 14:04:02 +0000366 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 Ranns9ec846c2021-02-09 14:04:02 +0000398 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 Ranns9ec846c2021-02-09 14:04:02 +0000402
Neale Ranns9ec846c2021-02-09 14:04:02 +0000403out:
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 Rannsff2e4132021-06-24 14:57:56 +0000412static int
413ipsec_sad_entry_add_v3 (const vl_api_ipsec_sad_entry_v3_t *entry,
414 u32 *sa_index)
Neale Ranns9ec846c2021-02-09 14:04:02 +0000415{
Neale Ranns9ec846c2021-02-09 14:04:02 +0000416 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 Rannsff2e4132021-06-24 14:57:56 +0000421 u32 id, spi;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000422 tunnel_t tun;
423 int rv;
Neale Rannsc7eaa712021-02-04 11:09:33 +0000424
Neale Rannsff2e4132021-06-24 14:57:56 +0000425 id = ntohl (entry->sad_id);
426 spi = ntohl (entry->spi);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000427
Neale Rannsff2e4132021-06-24 14:57:56 +0000428 rv = ipsec_proto_decode (entry->protocol, &proto);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000429
430 if (rv)
Neale Rannsff2e4132021-06-24 14:57:56 +0000431 return (rv);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000432
Neale Rannsff2e4132021-06-24 14:57:56 +0000433 rv = ipsec_crypto_algo_decode (entry->crypto_algorithm, &crypto_alg);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000434
435 if (rv)
Neale Rannsff2e4132021-06-24 14:57:56 +0000436 return (rv);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000437
Neale Rannsff2e4132021-06-24 14:57:56 +0000438 rv = ipsec_integ_algo_decode (entry->integrity_algorithm, &integ_alg);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000439
440 if (rv)
Neale Rannsff2e4132021-06-24 14:57:56 +0000441 return (rv);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000442
Neale Rannsff2e4132021-06-24 14:57:56 +0000443 flags = ipsec_sa_flags_decode (entry->flags);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000444
Neale Ranns9ec846c2021-02-09 14:04:02 +0000445 if (flags & IPSEC_SA_FLAG_IS_TUNNEL)
446 {
Neale Rannsff2e4132021-06-24 14:57:56 +0000447 rv = tunnel_decode (&entry->tunnel, &tun);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000448
449 if (rv)
Neale Rannsff2e4132021-06-24 14:57:56 +0000450 return (rv);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000451 }
Neale Rannsc7eaa712021-02-04 11:09:33 +0000452
Neale Rannsff2e4132021-06-24 14:57:56 +0000453 ipsec_key_decode (&entry->crypto_key, &crypto_key);
454 ipsec_key_decode (&entry->integrity_key, &integ_key);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000455
Neale Rannsff2e4132021-06-24 14:57:56 +0000456 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
462static void
463vl_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 Rannsc7eaa712021-02-04 11:09:33 +0000476 else
Neale Rannsff2e4132021-06-24 14:57:56 +0000477 {
478 rv = ipsec_sad_entry_add_v3 (&mp->entry, &sa_index);
479 }
Neale Rannsc7eaa712021-02-04 11:09:33 +0000480
Neale Ranns9ec846c2021-02-09 14:04:02 +0000481 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V3_REPLY,
482 { rmp->stat_index = htonl (sa_index); });
Neale Rannsc7eaa712021-02-04 11:09:33 +0000483}
484
485static void
Neale Rannsff2e4132021-06-24 14:57:56 +0000486vl_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
496static void
497vl_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
509static void
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700510send_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 Rannsa09c1ff2019-02-04 01:10:30 -0800514 u32 n_policies = 0;
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700515
516 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400517 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000518 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPDS_DETAILS);
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700519 mp->context = context;
520
521 mp->spd_id = htonl (spd->id);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800522#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 Fabiana9a0b2c2018-10-02 01:13:25 -0700526
527 vl_api_send_msg (reg, (u8 *) mp);
528}
529
530static void
531vl_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 Marion97898982021-04-19 18:15:31 +0200536
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700537 reg = vl_api_client_index_to_registration (mp->client_index);
538 if (!reg)
539 return;
540
Damjan Marionb2c31b62020-12-13 21:47:40 +0100541 pool_foreach (spd, im->spds) {
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700542 send_ipsec_spds_details (spd, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100543 }
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700544}
545
Neale Ranns17dcec02019-01-09 21:22:20 -0800546vl_api_ipsec_spd_action_t
547ipsec_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 Fabiana9a0b2c2018-10-02 01:13:25 -0700562static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800563send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
564 u32 context)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100565{
566 vl_api_ipsec_spd_details_t *mp;
567
568 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400569 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000570 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_DETAILS);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100571 mp->context = context;
572
Neale Ranns17dcec02019-01-09 21:22:20 -0800573 mp->entry.spd_id = htonl (p->id);
574 mp->entry.priority = htonl (p->priority);
Neale Ranns9f231d42019-03-19 10:06:00 +0000575 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
576 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
Neale Ranns17dcec02019-01-09 21:22:20 -0800577
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 Rannsa4d24312019-07-10 13:46:21 +0000586 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 Ranns17dcec02019-01-09 21:22:20 -0800590 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 Coras6c4dae22018-01-09 06:39:23 -0800594 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100595}
596
597static void
598vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
599{
Florin Coras6c4dae22018-01-09 06:39:23 -0800600 vl_api_registration_t *reg;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100601 ipsec_main_t *im = &ipsec_main;
Neale Ranns9f231d42019-03-19 10:06:00 +0000602 ipsec_spd_policy_type_t ptype;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100603 ipsec_policy_t *policy;
604 ipsec_spd_t *spd;
605 uword *p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800606 u32 spd_index, *ii;
Damjan Marion97898982021-04-19 18:15:31 +0200607
Florin Coras6c4dae22018-01-09 06:39:23 -0800608 reg = vl_api_client_index_to_registration (mp->client_index);
609 if (!reg)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100610 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 Rannsa09c1ff2019-02-04 01:10:30 -0800619 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 Kotucek9c7ef032016-12-21 07:46:45 +0100628}
629
630static void
Filip Varga871bca92018-11-02 13:51:44 +0100631send_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 Tehlarc73f3292021-06-22 08:21:31 +0000638 mp->_vl_msg_id =
639 ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_INTERFACE_DETAILS);
Filip Varga871bca92018-11-02 13:51:44 +0100640 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
648static void
649vl_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 Varga871bca92018-11-02 13:51:44 +0100656 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 Varga871bca92018-11-02 13:51:44 +0100672 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
673 send_ipsec_spd_interface_details(reg, v, k, mp->context);
674 }));
Filip Varga871bca92018-11-02 13:51:44 +0100675 }
Filip Varga871bca92018-11-02 13:51:44 +0100676}
677
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000678static void
679vl_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
699static void
700vl_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 Ranns89d939e2021-06-07 09:34:07 +0000710static walk_rc_t
711send_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 Tehlarc73f3292021-06-22 08:21:31 +0000718 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_ITF_DETAILS);
Neale Ranns89d939e2021-06-07 09:34:07 +0000719 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 Rannsdd4ccf22020-06-30 07:47:14 +0000729static void
730vl_api_ipsec_itf_dump_t_handler (vl_api_ipsec_itf_dump_t * mp)
731{
Neale Ranns89d939e2021-06-07 09:34:07 +0000732 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 Rannsdd4ccf22020-06-30 07:47:14 +0000744}
745
Neale Ranns12989b52019-09-26 16:20:19 +0000746typedef struct ipsec_sa_dump_match_ctx_t_
Matthew Smith28029532017-09-26 13:33:44 -0500747{
Neale Ranns12989b52019-09-26 16:20:19 +0000748 index_t sai;
749 u32 sw_if_index;
750} ipsec_sa_dump_match_ctx_t;
751
752static walk_rc_t
753ipsec_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 Marion97898982021-04-19 18:15:31 +0200766
Neale Ranns12989b52019-09-26 16:20:19 +0000767 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 Ranns12989b52019-09-26 16:20:19 +0000775
776 return (WALK_CONTINUE);
777}
778
779static walk_rc_t
780send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
781{
782 ipsec_dump_walk_ctx_t *ctx = arg;
Matthew Smith28029532017-09-26 13:33:44 -0500783 vl_api_ipsec_sa_details_t *mp;
784
785 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400786 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000787 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_DETAILS);
Neale Ranns12989b52019-09-26 16:20:19 +0000788 mp->context = ctx->context;
Matthew Smith28029532017-09-26 13:33:44 -0500789
Neale Ranns8d7c5022019-02-06 01:41:05 -0800790 mp->entry.sad_id = htonl (sa->id);
791 mp->entry.spi = htonl (sa->spi);
792 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000793 mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
Matthew Smith28029532017-09-26 13:33:44 -0500794
Neale Ranns8d7c5022019-02-06 01:41:05 -0800795 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
796 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
Matthew Smith28029532017-09-26 13:33:44 -0500797
Neale Ranns8d7c5022019-02-06 01:41:05 -0800798 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
799 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
Matthew Smith28029532017-09-26 13:33:44 -0500800
Neale Ranns8d7c5022019-02-06 01:41:05 -0800801 mp->entry.flags = ipsec_sad_flags_encode (sa);
Neale Ranns12989b52019-09-26 16:20:19 +0000802 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 Rannsc5fe57d2021-02-25 16:01:28 +0000807 .sai = sa - ipsec_sa_pool,
808 .sw_if_index = ~0,
Neale Ranns12989b52019-09-26 16:20:19 +0000809 };
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 Smith28029532017-09-26 13:33:44 -0500816
Damjan Mariond709cbc2019-03-26 13:16:42 +0100817 if (ipsec_sa_is_set_IS_TUNNEL (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500818 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000819 ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
820 ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
Matthew Smith28029532017-09-26 13:33:44 -0500821 }
Neale Rannsabc56602020-04-01 09:45:23 +0000822 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 Smith28029532017-09-26 13:33:44 -0500827
Matthew Smith28029532017-09-26 13:33:44 -0500828 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
Neale Ranns5b891102021-06-28 13:31:28 +0000829 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq));
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100830 if (ipsec_sa_is_set_USE_ESN (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500831 {
832 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
Neale Ranns5b891102021-06-28 13:31:28 +0000833 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
Matthew Smith28029532017-09-26 13:33:44 -0500834 }
Damjan Mariond709cbc2019-03-26 13:16:42 +0100835 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500836 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
Pierre Pfister4c422f92018-12-10 11:19:08 +0100837
Matthew Smith48d32b42020-04-02 07:45:49 -0500838 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
839
Neale Ranns12989b52019-09-26 16:20:19 +0000840 vl_api_send_msg (ctx->reg, (u8 *) mp);
Matthew Smith28029532017-09-26 13:33:44 -0500841
Neale Ranns12989b52019-09-26 16:20:19 +0000842 return (WALK_CONTINUE);
843}
Matthew Smith28029532017-09-26 13:33:44 -0500844
845static void
846vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
847{
Florin Coras6c4dae22018-01-09 06:39:23 -0800848 vl_api_registration_t *reg;
Matthew Smith28029532017-09-26 13:33:44 -0500849
Florin Coras6c4dae22018-01-09 06:39:23 -0800850 reg = vl_api_client_index_to_registration (mp->client_index);
Neale Ranns12989b52019-09-26 16:20:19 +0000851 if (!reg)
Matthew Smith28029532017-09-26 13:33:44 -0500852 return;
853
Neale Ranns12989b52019-09-26 16:20:19 +0000854 ipsec_dump_walk_ctx_t ctx = {
855 .reg = reg,
856 .context = mp->context,
857 };
Matthew Smith28029532017-09-26 13:33:44 -0500858
Neale Ranns12989b52019-09-26 16:20:19 +0000859 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
Matthew Smith28029532017-09-26 13:33:44 -0500860}
861
Neale Ranns041add72020-01-02 04:06:10 +0000862static walk_rc_t
863send_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 Ranns041add72020-01-02 04:06:10 +0000867
868 mp = vl_msg_api_alloc (sizeof (*mp));
869 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000870 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V2_DETAILS);
Neale Ranns041add72020-01-02 04:06:10 +0000871 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 Ranns9ec846c2021-02-09 14:04:02 +0000876 mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
Neale Ranns041add72020-01-02 04:06:10 +0000877
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 Rannsc5fe57d2021-02-25 16:01:28 +0000890 .sai = sa - ipsec_sa_pool,
891 .sw_if_index = ~0,
Neale Ranns041add72020-01-02 04:06:10 +0000892 };
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 Ranns9ec846c2021-02-09 14:04:02 +0000902 ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
903 ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
Neale Ranns041add72020-01-02 04:06:10 +0000904 }
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 Ranns9ec846c2021-02-09 14:04:02 +0000911 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 Ranns041add72020-01-02 04:06:10 +0000914
915 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
Neale Ranns5b891102021-06-28 13:31:28 +0000916 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq));
Neale Ranns041add72020-01-02 04:06:10 +0000917 if (ipsec_sa_is_set_USE_ESN (sa))
918 {
919 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
Neale Ranns5b891102021-06-28 13:31:28 +0000920 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
Neale Ranns041add72020-01-02 04:06:10 +0000921 }
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
932static void
Neale Ranns9ec846c2021-02-09 14:04:02 +0000933vl_api_ipsec_sa_v2_dump_t_handler (vl_api_ipsec_sa_v2_dump_t *mp)
Neale Ranns041add72020-01-02 04:06:10 +0000934{
935 vl_api_registration_t *reg;
936
Neale Ranns041add72020-01-02 04:06:10 +0000937 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 Ranns041add72020-01-02 04:06:10 +0000947}
948
Neale Ranns9ec846c2021-02-09 14:04:02 +0000949static walk_rc_t
950send_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 Ranns9ec846c2021-02-09 14:04:02 +0000954
955 mp = vl_msg_api_alloc (sizeof (*mp));
956 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000957 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V3_DETAILS);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000958 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 Rannsc5fe57d2021-02-25 16:01:28 +0000976 .sai = sa - ipsec_sa_pool,
Neale Ranns9ec846c2021-02-09 14:04:02 +0000977 .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 Ranns5b891102021-06-28 13:31:28 +0000996 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq));
Neale Ranns9ec846c2021-02-09 14:04:02 +0000997 if (ipsec_sa_is_set_USE_ESN (sa))
998 {
999 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
Neale Ranns5b891102021-06-28 13:31:28 +00001000 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
Neale Ranns9ec846c2021-02-09 14:04:02 +00001001 }
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
1012static void
1013vl_api_ipsec_sa_v3_dump_t_handler (vl_api_ipsec_sa_v3_dump_t *mp)
1014{
1015 vl_api_registration_t *reg;
1016
Neale Ranns9ec846c2021-02-09 14:04:02 +00001017 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 Ranns9ec846c2021-02-09 14:04:02 +00001027}
1028
Matthew Smith75d85602017-10-05 19:03:05 -05001029static void
Klement Sekerab4d30532018-11-08 13:00:02 +01001030vl_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 Marionb2c31b62020-12-13 21:47:40 +01001047 pool_foreach (ab, im->ah_backends) {
Klement Sekerab4d30532018-11-08 13:00:02 +01001048 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1049 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +00001050 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
Klement Sekerab4d30532018-11-08 13:00:02 +01001051 mp->context = context;
1052 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1053 ab->name);
Neale Ranns17dcec02019-01-09 21:22:20 -08001054 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
Klement Sekerab4d30532018-11-08 13:00:02 +01001055 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 Marionb2c31b62020-12-13 21:47:40 +01001058 }
1059 pool_foreach (eb, im->esp_backends) {
Klement Sekerab4d30532018-11-08 13:00:02 +01001060 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1061 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +00001062 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
Klement Sekerab4d30532018-11-08 13:00:02 +01001063 mp->context = context;
1064 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1065 eb->name);
Neale Ranns17dcec02019-01-09 21:22:20 -08001066 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
Klement Sekerab4d30532018-11-08 13:00:02 +01001067 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 Marionb2c31b62020-12-13 21:47:40 +01001070 }
Klement Sekerab4d30532018-11-08 13:00:02 +01001071 /* *INDENT-ON* */
1072}
1073
1074static void
1075vl_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 Ranns17dcec02019-01-09 21:22:20 -08001079 ipsec_protocol_t protocol;
Klement Sekerab4d30532018-11-08 13:00:02 +01001080 int rv = 0;
Neale Rannsc5fe57d2021-02-25 16:01:28 +00001081 if (pool_elts (ipsec_sa_pool) > 0)
Klement Sekerab4d30532018-11-08 13:00:02 +01001082 {
1083 rv = VNET_API_ERROR_INSTANCE_IN_USE;
1084 goto done;
1085 }
Neale Ranns17dcec02019-01-09 21:22:20 -08001086
1087 rv = ipsec_proto_decode (mp->protocol, &protocol);
1088
1089 if (rv)
1090 goto done;
1091
Neale Ranns17dcec02019-01-09 21:22:20 -08001092 switch (protocol)
Klement Sekerab4d30532018-11-08 13:00:02 +01001093 {
1094 case IPSEC_PROTOCOL_ESP:
Neale Rannse8915fc2019-04-23 20:57:55 -04001095 rv = ipsec_select_esp_backend (im, mp->index);
Klement Sekerab4d30532018-11-08 13:00:02 +01001096 break;
1097 case IPSEC_PROTOCOL_AH:
Neale Rannse8915fc2019-04-23 20:57:55 -04001098 rv = ipsec_select_ah_backend (im, mp->index);
Klement Sekerab4d30532018-11-08 13:00:02 +01001099 break;
1100 default:
Neale Rannse8915fc2019-04-23 20:57:55 -04001101 rv = VNET_API_ERROR_INVALID_PROTOCOL;
Klement Sekerab4d30532018-11-08 13:00:02 +01001102 break;
1103 }
Klement Sekerab4d30532018-11-08 13:00:02 +01001104done:
1105 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1106}
1107
Yulong Pei2e84d662020-08-14 18:21:08 +08001108static void
1109vl_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 Pei2e84d662020-08-14 18:21:08 +08001114 ipsec_set_async_mode (mp->async_enable);
1115
1116 REPLY_MACRO (VL_API_IPSEC_SET_ASYNC_MODE_REPLY);
1117}
1118
Filip Tehlarc73f3292021-06-22 08:21:31 +00001119#include <vnet/ipsec/ipsec.api.c>
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001120static clib_error_t *
1121ipsec_api_hookup (vlib_main_t * vm)
1122{
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001123 /*
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001124 * Set up the (msg_name, crc, message-id) table
1125 */
Filip Tehlarc73f3292021-06-22 08:21:31 +00001126 REPLY_MSG_ID_BASE = setup_message_id_table ();
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001127
1128 return 0;
1129}
1130
1131VLIB_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 */