blob: 335996ee07797d088df899ee54800d785de7b9e5 [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
33#include <vnet/vnet_msg_enum.h>
34
Damjan Mariona9a951f2017-01-16 22:06:10 +010035#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010036#include <vnet/ipsec/ipsec.h>
Neale Rannsc87b66c2019-02-07 07:26:12 -080037#include <vnet/ipsec/ipsec_tun.h>
Neale Rannsdd4ccf22020-06-30 07:47:14 +000038#include <vnet/ipsec/ipsec_itf.h>
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010039#endif /* IPSEC */
40
41#define vl_typedefs /* define message structures */
42#include <vnet/vnet_all_api_h.h>
43#undef vl_typedefs
44
45#define vl_endianfun /* define message structures */
46#include <vnet/vnet_all_api_h.h>
47#undef vl_endianfun
48
49/* instantiate all the print functions we know about */
50#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
51#define vl_printfun
52#include <vnet/vnet_all_api_h.h>
53#undef vl_printfun
54
55#include <vlibapi/api_helper_macros.h>
56
Klement Sekerab4d30532018-11-08 13:00:02 +010057#define foreach_vpe_api_msg \
58_(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
59_(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
Neale Ranns17dcec02019-01-09 21:22:20 -080060_(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \
61_(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \
Neale Ranns041add72020-01-02 04:06:10 +000062_(IPSEC_SAD_ENTRY_ADD_DEL_V2, ipsec_sad_entry_add_del_v2) \
Klement Sekerab4d30532018-11-08 13:00:02 +010063_(IPSEC_SA_DUMP, ipsec_sa_dump) \
Neale Ranns041add72020-01-02 04:06:10 +000064_(IPSEC_SA_V2_DUMP, ipsec_sa_v2_dump) \
Klement Sekerab4d30532018-11-08 13:00:02 +010065_(IPSEC_SPDS_DUMP, ipsec_spds_dump) \
66_(IPSEC_SPD_DUMP, ipsec_spd_dump) \
67_(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \
Neale Rannsdd4ccf22020-06-30 07:47:14 +000068_(IPSEC_ITF_CREATE, ipsec_itf_create) \
69_(IPSEC_ITF_DELETE, ipsec_itf_delete) \
70_(IPSEC_ITF_DUMP, ipsec_itf_dump) \
Klement Sekerab4d30532018-11-08 13:00:02 +010071_(IPSEC_SELECT_BACKEND, ipsec_select_backend) \
Neale Rannsc87b66c2019-02-07 07:26:12 -080072_(IPSEC_BACKEND_DUMP, ipsec_backend_dump) \
73_(IPSEC_TUNNEL_PROTECT_UPDATE, ipsec_tunnel_protect_update) \
74_(IPSEC_TUNNEL_PROTECT_DEL, ipsec_tunnel_protect_del) \
Yulong Pei2e84d662020-08-14 18:21:08 +080075_(IPSEC_TUNNEL_PROTECT_DUMP, ipsec_tunnel_protect_dump) \
76_(IPSEC_SET_ASYNC_MODE, ipsec_set_async_mode)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010077
Klement Sekerab4d30532018-11-08 13:00:02 +010078static void
79vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010080{
Damjan Mariona9a951f2017-01-16 22:06:10 +010081#if WITH_LIBSSL == 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010082 clib_warning ("unimplemented");
83#else
84
85 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
86 vl_api_ipsec_spd_add_del_reply_t *rmp;
87 int rv;
88
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010089 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010090
91 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
92#endif
93}
94
95static void vl_api_ipsec_interface_add_del_spd_t_handler
96 (vl_api_ipsec_interface_add_del_spd_t * mp)
97{
98 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
99 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
100 int rv;
101 u32 sw_if_index __attribute__ ((unused));
102 u32 spd_id __attribute__ ((unused));
103
104 sw_if_index = ntohl (mp->sw_if_index);
105 spd_id = ntohl (mp->spd_id);
106
107 VALIDATE_SW_IF_INDEX (mp);
108
Damjan Mariona9a951f2017-01-16 22:06:10 +0100109#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100110 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
111#else
112 rv = VNET_API_ERROR_UNIMPLEMENTED;
113#endif
114
115 BAD_SW_IF_INDEX_LABEL;
116
117 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
118}
119
Neale Rannsc87b66c2019-02-07 07:26:12 -0800120static void vl_api_ipsec_tunnel_protect_update_t_handler
121 (vl_api_ipsec_tunnel_protect_update_t * mp)
122{
123 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
124 vl_api_ipsec_tunnel_protect_update_reply_t *rmp;
125 u32 sw_if_index, ii, *sa_ins = NULL;
Neale Ranns28287212019-12-16 00:53:11 +0000126 ip_address_t nh;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800127 int rv;
128
129 sw_if_index = ntohl (mp->tunnel.sw_if_index);
130
131 VALIDATE_SW_IF_INDEX (&(mp->tunnel));
132
133#if WITH_LIBSSL > 0
134
135 for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
136 vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
137
Neale Ranns28287212019-12-16 00:53:11 +0000138 ip_address_decode2 (&mp->tunnel.nh, &nh);
139
140 rv = ipsec_tun_protect_update (sw_if_index, &nh,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800141 ntohl (mp->tunnel.sa_out), sa_ins);
142#else
143 rv = VNET_API_ERROR_UNIMPLEMENTED;
144#endif
145
146 BAD_SW_IF_INDEX_LABEL;
147
148 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
149}
150
151static void vl_api_ipsec_tunnel_protect_del_t_handler
152 (vl_api_ipsec_tunnel_protect_del_t * mp)
153{
154 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
155 vl_api_ipsec_tunnel_protect_del_reply_t *rmp;
Neale Ranns28287212019-12-16 00:53:11 +0000156 ip_address_t nh;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800157 u32 sw_if_index;
Neale Ranns28287212019-12-16 00:53:11 +0000158 int rv;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800159
160 sw_if_index = ntohl (mp->sw_if_index);
161
162 VALIDATE_SW_IF_INDEX (mp);
163
164#if WITH_LIBSSL > 0
Neale Ranns28287212019-12-16 00:53:11 +0000165 ip_address_decode2 (&mp->nh, &nh);
166 rv = ipsec_tun_protect_del (sw_if_index, &nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800167#else
168 rv = VNET_API_ERROR_UNIMPLEMENTED;
169#endif
170
171 BAD_SW_IF_INDEX_LABEL;
172
173 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
174}
175
Neale Ranns12989b52019-09-26 16:20:19 +0000176typedef struct ipsec_dump_walk_ctx_t_
Neale Rannsc87b66c2019-02-07 07:26:12 -0800177{
178 vl_api_registration_t *reg;
179 u32 context;
Neale Ranns12989b52019-09-26 16:20:19 +0000180} ipsec_dump_walk_ctx_t;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800181
182static walk_rc_t
183send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
184{
Neale Ranns12989b52019-09-26 16:20:19 +0000185 ipsec_dump_walk_ctx_t *ctx = arg;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800186 vl_api_ipsec_tunnel_protect_details_t *mp;
187 ipsec_tun_protect_t *itp;
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500188 u32 ii = 0;
189 ipsec_sa_t *sa;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800190
191 itp = ipsec_tun_protect_get (itpi);
192
Neale Rannsc87b66c2019-02-07 07:26:12 -0800193 mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
194 clib_memset (mp, 0, sizeof (*mp));
195 mp->_vl_msg_id = ntohs (VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
196 mp->context = ctx->context;
197
198 mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
Neale Ranns28287212019-12-16 00:53:11 +0000199 ip_address_encode2 (itp->itp_key, &mp->tun.nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800200
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500201 sa = ipsec_sa_get (itp->itp_out_sa);
202 mp->tun.sa_out = htonl (sa->id);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800203 mp->tun.n_sa_in = itp->itp_n_sa_in;
204 /* *INDENT-OFF* */
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500205 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800206 ({
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500207 mp->tun.sa_in[ii++] = htonl (sa->id);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800208 }));
209 /* *INDENT-ON* */
210
211 vl_api_send_msg (ctx->reg, (u8 *) mp);
212
213 return (WALK_CONTINUE);
214}
215
216static void
217vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
218 * mp)
219{
220 vl_api_registration_t *reg;
221 u32 sw_if_index;
222
223#if WITH_LIBSSL > 0
224 reg = vl_api_client_index_to_registration (mp->client_index);
225 if (!reg)
226 return;
227
Neale Ranns12989b52019-09-26 16:20:19 +0000228 ipsec_dump_walk_ctx_t ctx = {
Neale Rannsc87b66c2019-02-07 07:26:12 -0800229 .reg = reg,
230 .context = mp->context,
231 };
232
233 sw_if_index = ntohl (mp->sw_if_index);
234
235 if (~0 == sw_if_index)
236 {
237 ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
238 }
239 else
240 {
Neale Ranns28287212019-12-16 00:53:11 +0000241 ipsec_tun_protect_walk_itf (sw_if_index,
242 send_ipsec_tunnel_protect_details, &ctx);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800243 }
244#else
245 clib_warning ("unimplemented");
246#endif
247}
248
Neale Ranns17dcec02019-01-09 21:22:20 -0800249static int
250ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
251 ipsec_policy_action_t * out)
252{
253 in = clib_net_to_host_u32 (in);
254
255 switch (in)
256 {
257#define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
258 *out = IPSEC_POLICY_ACTION_##f; \
259 return (0);
260 foreach_ipsec_policy_action
261#undef _
262 }
263 return (VNET_API_ERROR_UNIMPLEMENTED);
264}
265
266static void vl_api_ipsec_spd_entry_add_del_t_handler
267 (vl_api_ipsec_spd_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100268{
269 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
Neale Ranns17dcec02019-01-09 21:22:20 -0800270 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
271 ip46_type_t itype;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800272 u32 stat_index;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100273 int rv;
274
Neale Ranns8c2dd1b2019-02-19 08:27:34 -0800275 stat_index = ~0;
276
Damjan Mariona9a951f2017-01-16 22:06:10 +0100277#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100278 ipsec_policy_t p;
279
Dave Barachb7b92992018-10-17 10:38:51 -0400280 clib_memset (&p, 0, sizeof (p));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100281
Neale Ranns17dcec02019-01-09 21:22:20 -0800282 p.id = ntohl (mp->entry.spd_id);
283 p.priority = ntohl (mp->entry.priority);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100284
Neale Ranns17dcec02019-01-09 21:22:20 -0800285 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
286 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
287 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
288 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
289
290 p.is_ipv6 = (itype == IP46_TYPE_IP6);
291
292 p.protocol = mp->entry.protocol;
Neale Rannsa4d24312019-07-10 13:46:21 +0000293 p.rport.start = ntohs (mp->entry.remote_port_start);
294 p.rport.stop = ntohs (mp->entry.remote_port_stop);
295 p.lport.start = ntohs (mp->entry.local_port_start);
296 p.lport.stop = ntohs (mp->entry.local_port_stop);
Neale Ranns17dcec02019-01-09 21:22:20 -0800297
298 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
299
300 if (rv)
301 goto out;
302
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100303 /* policy action resolve unsupported */
Neale Ranns17dcec02019-01-09 21:22:20 -0800304 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100305 {
306 clib_warning ("unsupported action: 'resolve'");
307 rv = VNET_API_ERROR_UNIMPLEMENTED;
308 goto out;
309 }
Neale Ranns17dcec02019-01-09 21:22:20 -0800310 p.sa_id = ntohl (mp->entry.sa_id);
Neale Ranns9f231d42019-03-19 10:06:00 +0000311 rv =
312 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
313 &p.type);
314 if (rv)
315 goto out;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100316
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800317 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100318 if (rv)
319 goto out;
320
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100321#else
322 rv = VNET_API_ERROR_UNIMPLEMENTED;
323 goto out;
324#endif
325
326out:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800327 /* *INDENT-OFF* */
328 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
329 ({
330 rmp->stat_index = ntohl(stat_index);
331 }));
332 /* *INDENT-ON* */
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100333}
334
Neale Ranns17dcec02019-01-09 21:22:20 -0800335static void vl_api_ipsec_sad_entry_add_del_t_handler
336 (vl_api_ipsec_sad_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100337{
338 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
Neale Ranns17dcec02019-01-09 21:22:20 -0800339 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800340 ip46_address_t tun_src = { }, tun_dst =
341 {
342 };
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;
Dave Baracha5160d72019-03-13 15:29:15 -0400348 u32 id, spi, sa_index = ~0;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100349 int rv;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800350
Damjan Mariona9a951f2017-01-16 22:06:10 +0100351#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100352
Neale Ranns8d7c5022019-02-06 01:41:05 -0800353 id = ntohl (mp->entry.sad_id);
354 spi = ntohl (mp->entry.spi);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100355
Neale Ranns8d7c5022019-02-06 01:41:05 -0800356 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
Neale Ranns17dcec02019-01-09 21:22:20 -0800357
358 if (rv)
359 goto out;
360
Neale Ranns8d7c5022019-02-06 01:41:05 -0800361 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800362
363 if (rv)
364 goto out;
365
Neale Ranns8d7c5022019-02-06 01:41:05 -0800366 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800367
368 if (rv)
369 goto out;
370
Neale Ranns8d7c5022019-02-06 01:41:05 -0800371 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
372 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
Neale Ranns17dcec02019-01-09 21:22:20 -0800373
Neale Ranns8d7c5022019-02-06 01:41:05 -0800374 flags = ipsec_sa_flags_decode (mp->entry.flags);
Neale Ranns17dcec02019-01-09 21:22:20 -0800375
Neale Ranns8d7c5022019-02-06 01:41:05 -0800376 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
377 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100378
Neale Ranns8d7c5022019-02-06 01:41:05 -0800379 if (mp->is_add)
Neale Ranns495d7ff2019-07-12 09:15:26 +0000380 rv = ipsec_sa_add_and_lock (id, spi, proto,
381 crypto_alg, &crypto_key,
382 integ_alg, &integ_key, flags,
383 0, mp->entry.salt, &tun_src, &tun_dst,
Neale Ranns041add72020-01-02 04:06:10 +0000384 TUNNEL_ENCAP_DECAP_FLAG_NONE,
385 IP_DSCP_CS0,
386 &sa_index,
387 htons (mp->entry.udp_src_port),
Neale Rannsabc56602020-04-01 09:45:23 +0000388 htons (mp->entry.udp_dst_port));
Neale Ranns8d7c5022019-02-06 01:41:05 -0800389 else
Neale Ranns495d7ff2019-07-12 09:15:26 +0000390 rv = ipsec_sa_unlock_id (id);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800391
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100392#else
393 rv = VNET_API_ERROR_UNIMPLEMENTED;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100394#endif
395
396out:
Neale Rannseba31ec2019-02-17 18:04:27 +0000397 /* *INDENT-OFF* */
398 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
399 {
400 rmp->stat_index = htonl (sa_index);
401 });
402 /* *INDENT-ON* */
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100403}
404
Neale Ranns041add72020-01-02 04:06:10 +0000405static void vl_api_ipsec_sad_entry_add_del_v2_t_handler
406 (vl_api_ipsec_sad_entry_add_del_v2_t * mp)
407{
408 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
409 vl_api_ipsec_sad_entry_add_del_v2_reply_t *rmp;
410 ip46_address_t tun_src = { }, tun_dst =
411 {
412 };
413 tunnel_encap_decap_flags_t tunnel_flags;
414 ipsec_key_t crypto_key, integ_key;
415 ipsec_crypto_alg_t crypto_alg;
416 ipsec_integ_alg_t integ_alg;
417 ipsec_protocol_t proto;
418 ipsec_sa_flags_t flags;
419 u32 id, spi, sa_index = ~0;
420 int rv;
421
422#if WITH_LIBSSL > 0
423
424 id = ntohl (mp->entry.sad_id);
425 spi = ntohl (mp->entry.spi);
426
427 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
428
429 if (rv)
430 goto out;
431
432 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
433
434 if (rv)
435 goto out;
436
437 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
438
439 if (rv)
440 goto out;
441
442 rv =
443 tunnel_encap_decap_flags_decode (mp->entry.tunnel_flags, &tunnel_flags);
444
445 if (rv)
446 goto out;
447
448 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
449 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
450
451 flags = ipsec_sa_flags_decode (mp->entry.flags);
452
453 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
454 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
455
456 if (mp->is_add)
457 rv = ipsec_sa_add_and_lock (id, spi, proto,
458 crypto_alg, &crypto_key,
459 integ_alg, &integ_key, flags,
460 0, mp->entry.salt, &tun_src, &tun_dst,
461 tunnel_flags,
462 ip_dscp_decode (mp->entry.dscp),
463 &sa_index,
464 htons (mp->entry.udp_src_port),
465 htons (mp->entry.udp_dst_port));
466 else
467 rv = ipsec_sa_unlock_id (id);
468
469#else
470 rv = VNET_API_ERROR_UNIMPLEMENTED;
471#endif
472
473out:
474 /* *INDENT-OFF* */
475 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V2_REPLY,
476 {
477 rmp->stat_index = htonl (sa_index);
478 });
479 /* *INDENT-ON* */
480}
481
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100482static void
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700483send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
484 u32 context)
485{
486 vl_api_ipsec_spds_details_t *mp;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800487 u32 n_policies = 0;
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700488
489 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400490 clib_memset (mp, 0, sizeof (*mp));
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700491 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
492 mp->context = context;
493
494 mp->spd_id = htonl (spd->id);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800495#define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
496 foreach_ipsec_spd_policy_type
497#undef _
498 mp->npolicies = htonl (n_policies);
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700499
500 vl_api_send_msg (reg, (u8 *) mp);
501}
502
503static void
504vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
505{
506 vl_api_registration_t *reg;
507 ipsec_main_t *im = &ipsec_main;
508 ipsec_spd_t *spd;
509#if WITH_LIBSSL > 0
510 reg = vl_api_client_index_to_registration (mp->client_index);
511 if (!reg)
512 return;
513
514 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100515 pool_foreach (spd, im->spds) {
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700516 send_ipsec_spds_details (spd, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100517 }
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700518 /* *INDENT-ON* */
519#else
520 clib_warning ("unimplemented");
521#endif
522}
523
Neale Ranns17dcec02019-01-09 21:22:20 -0800524vl_api_ipsec_spd_action_t
525ipsec_spd_action_encode (ipsec_policy_action_t in)
526{
527 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
528
529 switch (in)
530 {
531#define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
532 out = IPSEC_API_SPD_ACTION_##f; \
533 break;
534 foreach_ipsec_policy_action
535#undef _
536 }
537 return (clib_host_to_net_u32 (out));
538}
539
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700540static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800541send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
542 u32 context)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100543{
544 vl_api_ipsec_spd_details_t *mp;
545
546 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400547 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100548 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
549 mp->context = context;
550
Neale Ranns17dcec02019-01-09 21:22:20 -0800551 mp->entry.spd_id = htonl (p->id);
552 mp->entry.priority = htonl (p->priority);
Neale Ranns9f231d42019-03-19 10:06:00 +0000553 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
554 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
Neale Ranns17dcec02019-01-09 21:22:20 -0800555
556 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
557 &mp->entry.local_address_start);
558 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
559 &mp->entry.local_address_stop);
560 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
561 &mp->entry.remote_address_start);
562 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
563 &mp->entry.remote_address_stop);
Neale Rannsa4d24312019-07-10 13:46:21 +0000564 mp->entry.local_port_start = htons (p->lport.start);
565 mp->entry.local_port_stop = htons (p->lport.stop);
566 mp->entry.remote_port_start = htons (p->rport.start);
567 mp->entry.remote_port_stop = htons (p->rport.stop);
Neale Ranns17dcec02019-01-09 21:22:20 -0800568 mp->entry.protocol = p->protocol;
569 mp->entry.policy = ipsec_spd_action_encode (p->policy);
570 mp->entry.sa_id = htonl (p->sa_id);
571
Florin Coras6c4dae22018-01-09 06:39:23 -0800572 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100573}
574
575static void
576vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
577{
Florin Coras6c4dae22018-01-09 06:39:23 -0800578 vl_api_registration_t *reg;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100579 ipsec_main_t *im = &ipsec_main;
Neale Ranns9f231d42019-03-19 10:06:00 +0000580 ipsec_spd_policy_type_t ptype;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100581 ipsec_policy_t *policy;
582 ipsec_spd_t *spd;
583 uword *p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800584 u32 spd_index, *ii;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100585#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800586 reg = vl_api_client_index_to_registration (mp->client_index);
587 if (!reg)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100588 return;
589
590 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
591 if (!p)
592 return;
593
594 spd_index = p[0];
595 spd = pool_elt_at_index (im->spds, spd_index);
596
597 /* *INDENT-OFF* */
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800598 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
599 vec_foreach(ii, spd->policies[ptype])
600 {
601 policy = pool_elt_at_index(im->policies, *ii);
602
603 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
604 send_ipsec_spd_details (policy, reg, mp->context);
605 }
606 }
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100607 /* *INDENT-ON* */
608#else
609 clib_warning ("unimplemented");
610#endif
611}
612
613static void
Filip Varga871bca92018-11-02 13:51:44 +0100614send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
615 u32 sw_if_index, u32 context)
616{
617 vl_api_ipsec_spd_interface_details_t *mp;
618
619 mp = vl_msg_api_alloc (sizeof (*mp));
620 clib_memset (mp, 0, sizeof (*mp));
621 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
622 mp->context = context;
623
624 mp->spd_index = htonl (spd_index);
625 mp->sw_if_index = htonl (sw_if_index);
626
627 vl_api_send_msg (reg, (u8 *) mp);
628}
629
630static void
631vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
632 mp)
633{
634 ipsec_main_t *im = &ipsec_main;
635 vl_api_registration_t *reg;
636 u32 k, v, spd_index;
637
638#if WITH_LIBSSL > 0
639 reg = vl_api_client_index_to_registration (mp->client_index);
640 if (!reg)
641 return;
642
643 if (mp->spd_index_valid)
644 {
645 spd_index = ntohl (mp->spd_index);
646 /* *INDENT-OFF* */
647 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
648 if (v == spd_index)
649 send_ipsec_spd_interface_details(reg, v, k, mp->context);
650 }));
651 /* *INDENT-ON* */
652 }
653 else
654 {
655 /* *INDENT-OFF* */
656 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
657 send_ipsec_spd_interface_details(reg, v, k, mp->context);
658 }));
659 /* *INDENT-ON* */
660 }
661
662#else
663 clib_warning ("unimplemented");
664#endif
665}
666
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000667static void
668vl_api_ipsec_itf_create_t_handler (vl_api_ipsec_itf_create_t * mp)
669{
670 vl_api_ipsec_itf_create_reply_t *rmp;
671 tunnel_mode_t mode;
672 u32 sw_if_index = ~0;
673 int rv;
674
675 rv = tunnel_mode_decode (mp->itf.mode, &mode);
676
677 if (!rv)
678 rv = ipsec_itf_create (ntohl (mp->itf.user_instance), mode, &sw_if_index);
679
680 /* *INDENT-OFF* */
681 REPLY_MACRO2 (VL_API_IPSEC_ITF_CREATE_REPLY,
682 ({
683 rmp->sw_if_index = htonl (sw_if_index);
684 }));
685 /* *INDENT-ON* */
686}
687
688static void
689vl_api_ipsec_itf_delete_t_handler (vl_api_ipsec_itf_delete_t * mp)
690{
691 vl_api_ipsec_itf_delete_reply_t *rmp;
692 int rv;
693
694 rv = ipsec_itf_delete (ntohl (mp->sw_if_index));
695
696 REPLY_MACRO (VL_API_IPSEC_ITF_DELETE_REPLY);
697}
698
699static void
700vl_api_ipsec_itf_dump_t_handler (vl_api_ipsec_itf_dump_t * mp)
701{
702}
703
Neale Ranns12989b52019-09-26 16:20:19 +0000704typedef struct ipsec_sa_dump_match_ctx_t_
Matthew Smith28029532017-09-26 13:33:44 -0500705{
Neale Ranns12989b52019-09-26 16:20:19 +0000706 index_t sai;
707 u32 sw_if_index;
708} ipsec_sa_dump_match_ctx_t;
709
710static walk_rc_t
711ipsec_sa_dump_match_sa (index_t itpi, void *arg)
712{
713 ipsec_sa_dump_match_ctx_t *ctx = arg;
714 ipsec_tun_protect_t *itp;
715 index_t sai;
716
717 itp = ipsec_tun_protect_get (itpi);
718
719 if (itp->itp_out_sa == ctx->sai)
720 {
721 ctx->sw_if_index = itp->itp_sw_if_index;
722 return (WALK_STOP);
723 }
724 /* *INDENT-OFF* */
725 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
726 ({
727 if (sai == ctx->sai)
728 {
729 ctx->sw_if_index = itp->itp_sw_if_index;
730 return (WALK_STOP);
731 }
732 }));
733 /* *INDENT-OFF* */
734
735 return (WALK_CONTINUE);
736}
737
738static walk_rc_t
739send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
740{
741 ipsec_dump_walk_ctx_t *ctx = arg;
Matthew Smith28029532017-09-26 13:33:44 -0500742 vl_api_ipsec_sa_details_t *mp;
Neale Ranns12989b52019-09-26 16:20:19 +0000743 ipsec_main_t *im = &ipsec_main;
Matthew Smith28029532017-09-26 13:33:44 -0500744
745 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400746 clib_memset (mp, 0, sizeof (*mp));
Matthew Smith28029532017-09-26 13:33:44 -0500747 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
Neale Ranns12989b52019-09-26 16:20:19 +0000748 mp->context = ctx->context;
Matthew Smith28029532017-09-26 13:33:44 -0500749
Neale Ranns8d7c5022019-02-06 01:41:05 -0800750 mp->entry.sad_id = htonl (sa->id);
751 mp->entry.spi = htonl (sa->spi);
752 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
753 mp->entry.tx_table_id =
754 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
Matthew Smith28029532017-09-26 13:33:44 -0500755
Neale Ranns8d7c5022019-02-06 01:41:05 -0800756 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
757 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
Matthew Smith28029532017-09-26 13:33:44 -0500758
Neale Ranns8d7c5022019-02-06 01:41:05 -0800759 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
760 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
Matthew Smith28029532017-09-26 13:33:44 -0500761
Neale Ranns8d7c5022019-02-06 01:41:05 -0800762 mp->entry.flags = ipsec_sad_flags_encode (sa);
Neale Ranns12989b52019-09-26 16:20:19 +0000763 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
764
765 if (ipsec_sa_is_set_IS_PROTECT (sa))
766 {
767 ipsec_sa_dump_match_ctx_t ctx = {
768 .sai = sa - im->sad,
769 .sw_if_index = ~0,
770 };
771 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
772
773 mp->sw_if_index = htonl (ctx.sw_if_index);
774 }
775 else
776 mp->sw_if_index = ~0;
Matthew Smith28029532017-09-26 13:33:44 -0500777
Damjan Mariond709cbc2019-03-26 13:16:42 +0100778 if (ipsec_sa_is_set_IS_TUNNEL (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500779 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800780 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
781 &mp->entry.tunnel_src);
782 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
783 &mp->entry.tunnel_dst);
Matthew Smith28029532017-09-26 13:33:44 -0500784 }
Neale Rannsabc56602020-04-01 09:45:23 +0000785 if (ipsec_sa_is_set_UDP_ENCAP (sa))
786 {
787 mp->entry.udp_src_port = sa->udp_hdr.src_port;
788 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
789 }
Matthew Smith28029532017-09-26 13:33:44 -0500790
Matthew Smith28029532017-09-26 13:33:44 -0500791 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
792 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100793 if (ipsec_sa_is_set_USE_ESN (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500794 {
795 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
796 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
797 }
Damjan Mariond709cbc2019-03-26 13:16:42 +0100798 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500799 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
Pierre Pfister4c422f92018-12-10 11:19:08 +0100800
Matthew Smith48d32b42020-04-02 07:45:49 -0500801 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
802
Neale Ranns12989b52019-09-26 16:20:19 +0000803 vl_api_send_msg (ctx->reg, (u8 *) mp);
Matthew Smith28029532017-09-26 13:33:44 -0500804
Neale Ranns12989b52019-09-26 16:20:19 +0000805 return (WALK_CONTINUE);
806}
Matthew Smith28029532017-09-26 13:33:44 -0500807
808static void
809vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
810{
Florin Coras6c4dae22018-01-09 06:39:23 -0800811 vl_api_registration_t *reg;
Matthew Smith28029532017-09-26 13:33:44 -0500812
813#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800814 reg = vl_api_client_index_to_registration (mp->client_index);
Neale Ranns12989b52019-09-26 16:20:19 +0000815 if (!reg)
Matthew Smith28029532017-09-26 13:33:44 -0500816 return;
817
Neale Ranns12989b52019-09-26 16:20:19 +0000818 ipsec_dump_walk_ctx_t ctx = {
819 .reg = reg,
820 .context = mp->context,
821 };
Matthew Smith28029532017-09-26 13:33:44 -0500822
Neale Ranns12989b52019-09-26 16:20:19 +0000823 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
Matthew Smith28029532017-09-26 13:33:44 -0500824
Matthew Smith28029532017-09-26 13:33:44 -0500825#else
826 clib_warning ("unimplemented");
827#endif
828}
829
Neale Ranns041add72020-01-02 04:06:10 +0000830static walk_rc_t
831send_ipsec_sa_v2_details (ipsec_sa_t * sa, void *arg)
832{
833 ipsec_dump_walk_ctx_t *ctx = arg;
834 vl_api_ipsec_sa_v2_details_t *mp;
835 ipsec_main_t *im = &ipsec_main;
836
837 mp = vl_msg_api_alloc (sizeof (*mp));
838 clib_memset (mp, 0, sizeof (*mp));
839 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_V2_DETAILS);
840 mp->context = ctx->context;
841
842 mp->entry.sad_id = htonl (sa->id);
843 mp->entry.spi = htonl (sa->spi);
844 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
845 mp->entry.tx_table_id =
846 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
847
848 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
849 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
850
851 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
852 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
853
854 mp->entry.flags = ipsec_sad_flags_encode (sa);
855 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
856
857 if (ipsec_sa_is_set_IS_PROTECT (sa))
858 {
859 ipsec_sa_dump_match_ctx_t ctx = {
860 .sai = sa - im->sad,
861 .sw_if_index = ~0,
862 };
863 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
864
865 mp->sw_if_index = htonl (ctx.sw_if_index);
866 }
867 else
868 mp->sw_if_index = ~0;
869
870 if (ipsec_sa_is_set_IS_TUNNEL (sa))
871 {
872 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
873 &mp->entry.tunnel_src);
874 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
875 &mp->entry.tunnel_dst);
876 }
877 if (ipsec_sa_is_set_UDP_ENCAP (sa))
878 {
879 mp->entry.udp_src_port = sa->udp_hdr.src_port;
880 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
881 }
882
883 mp->entry.tunnel_flags = tunnel_encap_decap_flags_encode (sa->tunnel_flags);
884 mp->entry.dscp = ip_dscp_encode (sa->dscp);
885
886 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
887 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
888 if (ipsec_sa_is_set_USE_ESN (sa))
889 {
890 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
891 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
892 }
893 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
894 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
895
896 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
897
898 vl_api_send_msg (ctx->reg, (u8 *) mp);
899
900 return (WALK_CONTINUE);
901}
902
903static void
904vl_api_ipsec_sa_v2_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
905{
906 vl_api_registration_t *reg;
907
908#if WITH_LIBSSL > 0
909 reg = vl_api_client_index_to_registration (mp->client_index);
910 if (!reg)
911 return;
912
913 ipsec_dump_walk_ctx_t ctx = {
914 .reg = reg,
915 .context = mp->context,
916 };
917
918 ipsec_sa_walk (send_ipsec_sa_v2_details, &ctx);
919
920#else
921 clib_warning ("unimplemented");
922#endif
923}
924
Matthew Smith75d85602017-10-05 19:03:05 -0500925static void
Klement Sekerab4d30532018-11-08 13:00:02 +0100926vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
927{
928 vl_api_registration_t *rp;
929 ipsec_main_t *im = &ipsec_main;
930 u32 context = mp->context;
931
932 rp = vl_api_client_index_to_registration (mp->client_index);
933
934 if (rp == 0)
935 {
936 clib_warning ("Client %d AWOL", mp->client_index);
937 return;
938 }
939
940 ipsec_ah_backend_t *ab;
941 ipsec_esp_backend_t *eb;
942 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100943 pool_foreach (ab, im->ah_backends) {
Klement Sekerab4d30532018-11-08 13:00:02 +0100944 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
945 clib_memset (mp, 0, sizeof (*mp));
946 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
947 mp->context = context;
948 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
949 ab->name);
Neale Ranns17dcec02019-01-09 21:22:20 -0800950 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
Klement Sekerab4d30532018-11-08 13:00:02 +0100951 mp->index = ab - im->ah_backends;
952 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
953 vl_api_send_msg (rp, (u8 *)mp);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100954 }
955 pool_foreach (eb, im->esp_backends) {
Klement Sekerab4d30532018-11-08 13:00:02 +0100956 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
957 clib_memset (mp, 0, sizeof (*mp));
958 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
959 mp->context = context;
960 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
961 eb->name);
Neale Ranns17dcec02019-01-09 21:22:20 -0800962 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
Klement Sekerab4d30532018-11-08 13:00:02 +0100963 mp->index = eb - im->esp_backends;
964 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
965 vl_api_send_msg (rp, (u8 *)mp);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100966 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100967 /* *INDENT-ON* */
968}
969
970static void
971vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
972{
973 ipsec_main_t *im = &ipsec_main;
974 vl_api_ipsec_select_backend_reply_t *rmp;
Neale Ranns17dcec02019-01-09 21:22:20 -0800975 ipsec_protocol_t protocol;
Klement Sekerab4d30532018-11-08 13:00:02 +0100976 int rv = 0;
977 if (pool_elts (im->sad) > 0)
978 {
979 rv = VNET_API_ERROR_INSTANCE_IN_USE;
980 goto done;
981 }
Neale Ranns17dcec02019-01-09 21:22:20 -0800982
983 rv = ipsec_proto_decode (mp->protocol, &protocol);
984
985 if (rv)
986 goto done;
987
Klement Sekerab4d30532018-11-08 13:00:02 +0100988#if WITH_LIBSSL > 0
Neale Ranns17dcec02019-01-09 21:22:20 -0800989 switch (protocol)
Klement Sekerab4d30532018-11-08 13:00:02 +0100990 {
991 case IPSEC_PROTOCOL_ESP:
Neale Rannse8915fc2019-04-23 20:57:55 -0400992 rv = ipsec_select_esp_backend (im, mp->index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100993 break;
994 case IPSEC_PROTOCOL_AH:
Neale Rannse8915fc2019-04-23 20:57:55 -0400995 rv = ipsec_select_ah_backend (im, mp->index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100996 break;
997 default:
Neale Rannse8915fc2019-04-23 20:57:55 -0400998 rv = VNET_API_ERROR_INVALID_PROTOCOL;
Klement Sekerab4d30532018-11-08 13:00:02 +0100999 break;
1000 }
1001#else
1002 clib_warning ("unimplemented"); /* FIXME */
1003#endif
1004done:
1005 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1006}
1007
Yulong Pei2e84d662020-08-14 18:21:08 +08001008static void
1009vl_api_ipsec_set_async_mode_t_handler (vl_api_ipsec_set_async_mode_t * mp)
1010{
1011 vl_api_ipsec_set_async_mode_reply_t *rmp;
1012 int rv = 0;
1013
1014 vnet_crypto_request_async_mode (mp->async_enable);
1015 ipsec_set_async_mode (mp->async_enable);
1016
1017 REPLY_MACRO (VL_API_IPSEC_SET_ASYNC_MODE_REPLY);
1018}
1019
Neale Ranns7c44d782019-02-25 10:28:29 +00001020/*
1021 * ipsec_api_hookup
1022 * Add vpe's API message handlers to the table.
1023 * vlib has already mapped shared memory and
1024 * added the client registration handlers.
1025 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1026 */
1027#define vl_msg_name_crc_list
1028#include <vnet/vnet_all_api_h.h>
1029#undef vl_msg_name_crc_list
1030
1031static void
1032setup_message_id_table (api_main_t * am)
1033{
1034#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1035 foreach_vl_msg_name_crc_ipsec;
1036#undef _
1037}
1038
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001039static clib_error_t *
1040ipsec_api_hookup (vlib_main_t * vm)
1041{
Dave Barach39d69112019-11-27 11:42:13 -05001042 api_main_t *am = vlibapi_get_main ();
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001043
1044#define _(N,n) \
1045 vl_msg_api_set_handlers(VL_API_##N, #n, \
1046 vl_api_##n##_t_handler, \
1047 vl_noop_handler, \
1048 vl_api_##n##_t_endian, \
1049 vl_api_##n##_t_print, \
1050 sizeof(vl_api_##n##_t), 1);
1051 foreach_vpe_api_msg;
1052#undef _
1053
1054 /*
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001055 * Set up the (msg_name, crc, message-id) table
1056 */
1057 setup_message_id_table (am);
1058
1059 return 0;
1060}
1061
1062VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1063
1064/*
1065 * fd.io coding-style-patch-verification: ON
1066 *
1067 * Local Variables:
1068 * eval: (c-set-style "gnu")
1069 * End:
1070 */