blob: a0c2768318f61f2bae461d3550319c85c88d87d1 [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);
294 spi = ntohl (mp->entry.spi);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100295
Neale Ranns8d7c5022019-02-06 01:41:05 -0800296 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
Neale Ranns17dcec02019-01-09 21:22:20 -0800297
298 if (rv)
299 goto out;
300
Neale Ranns8d7c5022019-02-06 01:41:05 -0800301 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
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_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800307
308 if (rv)
309 goto out;
310
Neale Ranns8d7c5022019-02-06 01:41:05 -0800311 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
312 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
Neale Ranns17dcec02019-01-09 21:22:20 -0800313
Neale Ranns8d7c5022019-02-06 01:41:05 -0800314 flags = ipsec_sa_flags_decode (mp->entry.flags);
Neale Ranns17dcec02019-01-09 21:22:20 -0800315
Neale Ranns9ec846c2021-02-09 14:04:02 +0000316 ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
317 ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100318
Neale Ranns8d7c5022019-02-06 01:41:05 -0800319 if (mp->is_add)
Neale Ranns9ec846c2021-02-09 14:04:02 +0000320 rv = ipsec_sa_add_and_lock (
321 id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags,
322 mp->entry.salt, htons (mp->entry.udp_src_port),
323 htons (mp->entry.udp_dst_port), &tun, &sa_index);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800324 else
Neale Ranns495d7ff2019-07-12 09:15:26 +0000325 rv = ipsec_sa_unlock_id (id);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800326
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100327out:
Neale Rannseba31ec2019-02-17 18:04:27 +0000328 /* *INDENT-OFF* */
329 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
330 {
331 rmp->stat_index = htonl (sa_index);
332 });
333 /* *INDENT-ON* */
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100334}
335
Neale Ranns041add72020-01-02 04:06:10 +0000336static void vl_api_ipsec_sad_entry_add_del_v2_t_handler
337 (vl_api_ipsec_sad_entry_add_del_v2_t * mp)
338{
339 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
340 vl_api_ipsec_sad_entry_add_del_v2_reply_t *rmp;
Neale Ranns041add72020-01-02 04:06:10 +0000341 ipsec_key_t crypto_key, integ_key;
342 ipsec_crypto_alg_t crypto_alg;
343 ipsec_integ_alg_t integ_alg;
344 ipsec_protocol_t proto;
345 ipsec_sa_flags_t flags;
346 u32 id, spi, sa_index = ~0;
347 int rv;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000348 tunnel_t tun = {
349 .t_flags = TUNNEL_FLAG_NONE,
350 .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
351 .t_dscp = 0,
352 .t_mode = TUNNEL_MODE_P2P,
353 .t_table_id = htonl (mp->entry.tx_table_id),
354 .t_hop_limit = 255,
355 };
356
Neale Ranns9ec846c2021-02-09 14:04:02 +0000357 id = ntohl (mp->entry.sad_id);
358 spi = ntohl (mp->entry.spi);
359
360 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
361
362 if (rv)
363 goto out;
364
365 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
366
367 if (rv)
368 goto out;
369
370 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
371
372 if (rv)
373 goto out;
374
375 rv = tunnel_encap_decap_flags_decode (mp->entry.tunnel_flags,
376 &tun.t_encap_decap_flags);
377
378 if (rv)
379 goto out;
380
381 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
382 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
383
384 flags = ipsec_sa_flags_decode (mp->entry.flags);
385 tun.t_dscp = ip_dscp_decode (mp->entry.dscp);
386
387 ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
388 ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
389
390 if (mp->is_add)
391 rv = ipsec_sa_add_and_lock (
392 id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags,
393 mp->entry.salt, htons (mp->entry.udp_src_port),
394 htons (mp->entry.udp_dst_port), &tun, &sa_index);
395 else
396 rv = ipsec_sa_unlock_id (id);
397
Neale Ranns9ec846c2021-02-09 14:04:02 +0000398out:
399 /* *INDENT-OFF* */
400 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V2_REPLY,
401 {
402 rmp->stat_index = htonl (sa_index);
403 });
404 /* *INDENT-ON* */
405}
406
407static void
408vl_api_ipsec_sad_entry_add_del_v3_t_handler (
409 vl_api_ipsec_sad_entry_add_del_v3_t *mp)
410{
411 vl_api_ipsec_sad_entry_add_del_v3_reply_t *rmp;
412 ipsec_key_t crypto_key, integ_key;
413 ipsec_crypto_alg_t crypto_alg;
414 ipsec_integ_alg_t integ_alg;
415 ipsec_protocol_t proto;
416 ipsec_sa_flags_t flags;
417 u32 id, spi, sa_index = ~0;
418 tunnel_t tun;
419 int rv;
Neale Rannsc7eaa712021-02-04 11:09:33 +0000420
Neale Rannsc7eaa712021-02-04 11:09:33 +0000421 id = ntohl (mp->entry.sad_id);
422 spi = ntohl (mp->entry.spi);
423
424 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
425
426 if (rv)
427 goto out;
428
429 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
430
431 if (rv)
432 goto out;
433
434 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
435
436 if (rv)
437 goto out;
438
Neale Ranns9ec846c2021-02-09 14:04:02 +0000439 flags = ipsec_sa_flags_decode (mp->entry.flags);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000440
Neale Ranns9ec846c2021-02-09 14:04:02 +0000441 if (flags & IPSEC_SA_FLAG_IS_TUNNEL)
442 {
443 rv = tunnel_decode (&mp->entry.tunnel, &tun);
444
445 if (rv)
446 goto out;
447 }
Neale Rannsc7eaa712021-02-04 11:09:33 +0000448
449 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
450 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
451
Neale Rannsc7eaa712021-02-04 11:09:33 +0000452 if (mp->is_add)
453 rv = ipsec_sa_add_and_lock (
454 id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags,
Neale Ranns9ec846c2021-02-09 14:04:02 +0000455 mp->entry.salt, htons (mp->entry.udp_src_port),
456 htons (mp->entry.udp_dst_port), &tun, &sa_index);
Neale Rannsc7eaa712021-02-04 11:09:33 +0000457 else
458 rv = ipsec_sa_unlock_id (id);
459
Neale Rannsc7eaa712021-02-04 11:09:33 +0000460out:
Neale Ranns9ec846c2021-02-09 14:04:02 +0000461 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V3_REPLY,
462 { rmp->stat_index = htonl (sa_index); });
Neale Rannsc7eaa712021-02-04 11:09:33 +0000463}
464
465static void
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700466send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
467 u32 context)
468{
469 vl_api_ipsec_spds_details_t *mp;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800470 u32 n_policies = 0;
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700471
472 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400473 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000474 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPDS_DETAILS);
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700475 mp->context = context;
476
477 mp->spd_id = htonl (spd->id);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800478#define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
479 foreach_ipsec_spd_policy_type
480#undef _
481 mp->npolicies = htonl (n_policies);
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700482
483 vl_api_send_msg (reg, (u8 *) mp);
484}
485
486static void
487vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
488{
489 vl_api_registration_t *reg;
490 ipsec_main_t *im = &ipsec_main;
491 ipsec_spd_t *spd;
Damjan Marion97898982021-04-19 18:15:31 +0200492
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700493 reg = vl_api_client_index_to_registration (mp->client_index);
494 if (!reg)
495 return;
496
Damjan Marionb2c31b62020-12-13 21:47:40 +0100497 pool_foreach (spd, im->spds) {
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700498 send_ipsec_spds_details (spd, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100499 }
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700500}
501
Neale Ranns17dcec02019-01-09 21:22:20 -0800502vl_api_ipsec_spd_action_t
503ipsec_spd_action_encode (ipsec_policy_action_t in)
504{
505 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
506
507 switch (in)
508 {
509#define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
510 out = IPSEC_API_SPD_ACTION_##f; \
511 break;
512 foreach_ipsec_policy_action
513#undef _
514 }
515 return (clib_host_to_net_u32 (out));
516}
517
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700518static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800519send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
520 u32 context)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100521{
522 vl_api_ipsec_spd_details_t *mp;
523
524 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400525 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000526 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_DETAILS);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100527 mp->context = context;
528
Neale Ranns17dcec02019-01-09 21:22:20 -0800529 mp->entry.spd_id = htonl (p->id);
530 mp->entry.priority = htonl (p->priority);
Neale Ranns9f231d42019-03-19 10:06:00 +0000531 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
532 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
Neale Ranns17dcec02019-01-09 21:22:20 -0800533
534 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
535 &mp->entry.local_address_start);
536 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
537 &mp->entry.local_address_stop);
538 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
539 &mp->entry.remote_address_start);
540 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
541 &mp->entry.remote_address_stop);
Neale Rannsa4d24312019-07-10 13:46:21 +0000542 mp->entry.local_port_start = htons (p->lport.start);
543 mp->entry.local_port_stop = htons (p->lport.stop);
544 mp->entry.remote_port_start = htons (p->rport.start);
545 mp->entry.remote_port_stop = htons (p->rport.stop);
Neale Ranns17dcec02019-01-09 21:22:20 -0800546 mp->entry.protocol = p->protocol;
547 mp->entry.policy = ipsec_spd_action_encode (p->policy);
548 mp->entry.sa_id = htonl (p->sa_id);
549
Florin Coras6c4dae22018-01-09 06:39:23 -0800550 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100551}
552
553static void
554vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
555{
Florin Coras6c4dae22018-01-09 06:39:23 -0800556 vl_api_registration_t *reg;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100557 ipsec_main_t *im = &ipsec_main;
Neale Ranns9f231d42019-03-19 10:06:00 +0000558 ipsec_spd_policy_type_t ptype;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100559 ipsec_policy_t *policy;
560 ipsec_spd_t *spd;
561 uword *p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800562 u32 spd_index, *ii;
Damjan Marion97898982021-04-19 18:15:31 +0200563
Florin Coras6c4dae22018-01-09 06:39:23 -0800564 reg = vl_api_client_index_to_registration (mp->client_index);
565 if (!reg)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100566 return;
567
568 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
569 if (!p)
570 return;
571
572 spd_index = p[0];
573 spd = pool_elt_at_index (im->spds, spd_index);
574
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800575 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
576 vec_foreach(ii, spd->policies[ptype])
577 {
578 policy = pool_elt_at_index(im->policies, *ii);
579
580 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
581 send_ipsec_spd_details (policy, reg, mp->context);
582 }
583 }
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100584}
585
586static void
Filip Varga871bca92018-11-02 13:51:44 +0100587send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
588 u32 sw_if_index, u32 context)
589{
590 vl_api_ipsec_spd_interface_details_t *mp;
591
592 mp = vl_msg_api_alloc (sizeof (*mp));
593 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000594 mp->_vl_msg_id =
595 ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_INTERFACE_DETAILS);
Filip Varga871bca92018-11-02 13:51:44 +0100596 mp->context = context;
597
598 mp->spd_index = htonl (spd_index);
599 mp->sw_if_index = htonl (sw_if_index);
600
601 vl_api_send_msg (reg, (u8 *) mp);
602}
603
604static void
605vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
606 mp)
607{
608 ipsec_main_t *im = &ipsec_main;
609 vl_api_registration_t *reg;
610 u32 k, v, spd_index;
611
Filip Varga871bca92018-11-02 13:51:44 +0100612 reg = vl_api_client_index_to_registration (mp->client_index);
613 if (!reg)
614 return;
615
616 if (mp->spd_index_valid)
617 {
618 spd_index = ntohl (mp->spd_index);
619 /* *INDENT-OFF* */
620 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
621 if (v == spd_index)
622 send_ipsec_spd_interface_details(reg, v, k, mp->context);
623 }));
624 /* *INDENT-ON* */
625 }
626 else
627 {
Filip Varga871bca92018-11-02 13:51:44 +0100628 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
629 send_ipsec_spd_interface_details(reg, v, k, mp->context);
630 }));
Filip Varga871bca92018-11-02 13:51:44 +0100631 }
Filip Varga871bca92018-11-02 13:51:44 +0100632}
633
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000634static void
635vl_api_ipsec_itf_create_t_handler (vl_api_ipsec_itf_create_t * mp)
636{
637 vl_api_ipsec_itf_create_reply_t *rmp;
638 tunnel_mode_t mode;
639 u32 sw_if_index = ~0;
640 int rv;
641
642 rv = tunnel_mode_decode (mp->itf.mode, &mode);
643
644 if (!rv)
645 rv = ipsec_itf_create (ntohl (mp->itf.user_instance), mode, &sw_if_index);
646
647 /* *INDENT-OFF* */
648 REPLY_MACRO2 (VL_API_IPSEC_ITF_CREATE_REPLY,
649 ({
650 rmp->sw_if_index = htonl (sw_if_index);
651 }));
652 /* *INDENT-ON* */
653}
654
655static void
656vl_api_ipsec_itf_delete_t_handler (vl_api_ipsec_itf_delete_t * mp)
657{
658 vl_api_ipsec_itf_delete_reply_t *rmp;
659 int rv;
660
661 rv = ipsec_itf_delete (ntohl (mp->sw_if_index));
662
663 REPLY_MACRO (VL_API_IPSEC_ITF_DELETE_REPLY);
664}
665
Neale Ranns89d939e2021-06-07 09:34:07 +0000666static walk_rc_t
667send_ipsec_itf_details (ipsec_itf_t *itf, void *arg)
668{
669 ipsec_dump_walk_ctx_t *ctx = arg;
670 vl_api_ipsec_itf_details_t *mp;
671
672 mp = vl_msg_api_alloc (sizeof (*mp));
673 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000674 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_ITF_DETAILS);
Neale Ranns89d939e2021-06-07 09:34:07 +0000675 mp->context = ctx->context;
676
677 mp->itf.mode = tunnel_mode_encode (itf->ii_mode);
678 mp->itf.user_instance = htonl (itf->ii_user_instance);
679 mp->itf.sw_if_index = htonl (itf->ii_sw_if_index);
680 vl_api_send_msg (ctx->reg, (u8 *) mp);
681
682 return (WALK_CONTINUE);
683}
684
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000685static void
686vl_api_ipsec_itf_dump_t_handler (vl_api_ipsec_itf_dump_t * mp)
687{
Neale Ranns89d939e2021-06-07 09:34:07 +0000688 vl_api_registration_t *reg;
689
690 reg = vl_api_client_index_to_registration (mp->client_index);
691 if (!reg)
692 return;
693
694 ipsec_dump_walk_ctx_t ctx = {
695 .reg = reg,
696 .context = mp->context,
697 };
698
699 ipsec_itf_walk (send_ipsec_itf_details, &ctx);
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000700}
701
Neale Ranns12989b52019-09-26 16:20:19 +0000702typedef struct ipsec_sa_dump_match_ctx_t_
Matthew Smith28029532017-09-26 13:33:44 -0500703{
Neale Ranns12989b52019-09-26 16:20:19 +0000704 index_t sai;
705 u32 sw_if_index;
706} ipsec_sa_dump_match_ctx_t;
707
708static walk_rc_t
709ipsec_sa_dump_match_sa (index_t itpi, void *arg)
710{
711 ipsec_sa_dump_match_ctx_t *ctx = arg;
712 ipsec_tun_protect_t *itp;
713 index_t sai;
714
715 itp = ipsec_tun_protect_get (itpi);
716
717 if (itp->itp_out_sa == ctx->sai)
718 {
719 ctx->sw_if_index = itp->itp_sw_if_index;
720 return (WALK_STOP);
721 }
Damjan Marion97898982021-04-19 18:15:31 +0200722
Neale Ranns12989b52019-09-26 16:20:19 +0000723 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
724 ({
725 if (sai == ctx->sai)
726 {
727 ctx->sw_if_index = itp->itp_sw_if_index;
728 return (WALK_STOP);
729 }
730 }));
Neale Ranns12989b52019-09-26 16:20:19 +0000731
732 return (WALK_CONTINUE);
733}
734
735static walk_rc_t
736send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
737{
738 ipsec_dump_walk_ctx_t *ctx = arg;
Matthew Smith28029532017-09-26 13:33:44 -0500739 vl_api_ipsec_sa_details_t *mp;
740
741 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400742 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000743 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_DETAILS);
Neale Ranns12989b52019-09-26 16:20:19 +0000744 mp->context = ctx->context;
Matthew Smith28029532017-09-26 13:33:44 -0500745
Neale Ranns8d7c5022019-02-06 01:41:05 -0800746 mp->entry.sad_id = htonl (sa->id);
747 mp->entry.spi = htonl (sa->spi);
748 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000749 mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
Matthew Smith28029532017-09-26 13:33:44 -0500750
Neale Ranns8d7c5022019-02-06 01:41:05 -0800751 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
752 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
Matthew Smith28029532017-09-26 13:33:44 -0500753
Neale Ranns8d7c5022019-02-06 01:41:05 -0800754 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
755 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
Matthew Smith28029532017-09-26 13:33:44 -0500756
Neale Ranns8d7c5022019-02-06 01:41:05 -0800757 mp->entry.flags = ipsec_sad_flags_encode (sa);
Neale Ranns12989b52019-09-26 16:20:19 +0000758 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
759
760 if (ipsec_sa_is_set_IS_PROTECT (sa))
761 {
762 ipsec_sa_dump_match_ctx_t ctx = {
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000763 .sai = sa - ipsec_sa_pool,
764 .sw_if_index = ~0,
Neale Ranns12989b52019-09-26 16:20:19 +0000765 };
766 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
767
768 mp->sw_if_index = htonl (ctx.sw_if_index);
769 }
770 else
771 mp->sw_if_index = ~0;
Matthew Smith28029532017-09-26 13:33:44 -0500772
Damjan Mariond709cbc2019-03-26 13:16:42 +0100773 if (ipsec_sa_is_set_IS_TUNNEL (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500774 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000775 ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
776 ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
Matthew Smith28029532017-09-26 13:33:44 -0500777 }
Neale Rannsabc56602020-04-01 09:45:23 +0000778 if (ipsec_sa_is_set_UDP_ENCAP (sa))
779 {
780 mp->entry.udp_src_port = sa->udp_hdr.src_port;
781 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
782 }
Matthew Smith28029532017-09-26 13:33:44 -0500783
Matthew Smith28029532017-09-26 13:33:44 -0500784 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
785 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100786 if (ipsec_sa_is_set_USE_ESN (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500787 {
788 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
789 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
790 }
Damjan Mariond709cbc2019-03-26 13:16:42 +0100791 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500792 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
Pierre Pfister4c422f92018-12-10 11:19:08 +0100793
Matthew Smith48d32b42020-04-02 07:45:49 -0500794 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
795
Neale Ranns12989b52019-09-26 16:20:19 +0000796 vl_api_send_msg (ctx->reg, (u8 *) mp);
Matthew Smith28029532017-09-26 13:33:44 -0500797
Neale Ranns12989b52019-09-26 16:20:19 +0000798 return (WALK_CONTINUE);
799}
Matthew Smith28029532017-09-26 13:33:44 -0500800
801static void
802vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
803{
Florin Coras6c4dae22018-01-09 06:39:23 -0800804 vl_api_registration_t *reg;
Matthew Smith28029532017-09-26 13:33:44 -0500805
Florin Coras6c4dae22018-01-09 06:39:23 -0800806 reg = vl_api_client_index_to_registration (mp->client_index);
Neale Ranns12989b52019-09-26 16:20:19 +0000807 if (!reg)
Matthew Smith28029532017-09-26 13:33:44 -0500808 return;
809
Neale Ranns12989b52019-09-26 16:20:19 +0000810 ipsec_dump_walk_ctx_t ctx = {
811 .reg = reg,
812 .context = mp->context,
813 };
Matthew Smith28029532017-09-26 13:33:44 -0500814
Neale Ranns12989b52019-09-26 16:20:19 +0000815 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
Matthew Smith28029532017-09-26 13:33:44 -0500816}
817
Neale Ranns041add72020-01-02 04:06:10 +0000818static walk_rc_t
819send_ipsec_sa_v2_details (ipsec_sa_t * sa, void *arg)
820{
821 ipsec_dump_walk_ctx_t *ctx = arg;
822 vl_api_ipsec_sa_v2_details_t *mp;
Neale Ranns041add72020-01-02 04:06:10 +0000823
824 mp = vl_msg_api_alloc (sizeof (*mp));
825 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000826 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V2_DETAILS);
Neale Ranns041add72020-01-02 04:06:10 +0000827 mp->context = ctx->context;
828
829 mp->entry.sad_id = htonl (sa->id);
830 mp->entry.spi = htonl (sa->spi);
831 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000832 mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
Neale Ranns041add72020-01-02 04:06:10 +0000833
834 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
835 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
836
837 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
838 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
839
840 mp->entry.flags = ipsec_sad_flags_encode (sa);
841 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
842
843 if (ipsec_sa_is_set_IS_PROTECT (sa))
844 {
845 ipsec_sa_dump_match_ctx_t ctx = {
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000846 .sai = sa - ipsec_sa_pool,
847 .sw_if_index = ~0,
Neale Ranns041add72020-01-02 04:06:10 +0000848 };
849 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
850
851 mp->sw_if_index = htonl (ctx.sw_if_index);
852 }
853 else
854 mp->sw_if_index = ~0;
855
856 if (ipsec_sa_is_set_IS_TUNNEL (sa))
857 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000858 ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
859 ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
Neale Ranns041add72020-01-02 04:06:10 +0000860 }
861 if (ipsec_sa_is_set_UDP_ENCAP (sa))
862 {
863 mp->entry.udp_src_port = sa->udp_hdr.src_port;
864 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
865 }
866
Neale Ranns9ec846c2021-02-09 14:04:02 +0000867 mp->entry.tunnel_flags =
868 tunnel_encap_decap_flags_encode (sa->tunnel.t_encap_decap_flags);
869 mp->entry.dscp = ip_dscp_encode (sa->tunnel.t_dscp);
Neale Ranns041add72020-01-02 04:06:10 +0000870
871 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
872 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
873 if (ipsec_sa_is_set_USE_ESN (sa))
874 {
875 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
876 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
877 }
878 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
879 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
880
881 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
882
883 vl_api_send_msg (ctx->reg, (u8 *) mp);
884
885 return (WALK_CONTINUE);
886}
887
888static void
Neale Ranns9ec846c2021-02-09 14:04:02 +0000889vl_api_ipsec_sa_v2_dump_t_handler (vl_api_ipsec_sa_v2_dump_t *mp)
Neale Ranns041add72020-01-02 04:06:10 +0000890{
891 vl_api_registration_t *reg;
892
Neale Ranns041add72020-01-02 04:06:10 +0000893 reg = vl_api_client_index_to_registration (mp->client_index);
894 if (!reg)
895 return;
896
897 ipsec_dump_walk_ctx_t ctx = {
898 .reg = reg,
899 .context = mp->context,
900 };
901
902 ipsec_sa_walk (send_ipsec_sa_v2_details, &ctx);
Neale Ranns041add72020-01-02 04:06:10 +0000903}
904
Neale Ranns9ec846c2021-02-09 14:04:02 +0000905static walk_rc_t
906send_ipsec_sa_v3_details (ipsec_sa_t *sa, void *arg)
907{
908 ipsec_dump_walk_ctx_t *ctx = arg;
909 vl_api_ipsec_sa_v3_details_t *mp;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000910
911 mp = vl_msg_api_alloc (sizeof (*mp));
912 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +0000913 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V3_DETAILS);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000914 mp->context = ctx->context;
915
916 mp->entry.sad_id = htonl (sa->id);
917 mp->entry.spi = htonl (sa->spi);
918 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
919
920 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
921 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
922
923 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
924 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
925
926 mp->entry.flags = ipsec_sad_flags_encode (sa);
927 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
928
929 if (ipsec_sa_is_set_IS_PROTECT (sa))
930 {
931 ipsec_sa_dump_match_ctx_t ctx = {
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000932 .sai = sa - ipsec_sa_pool,
Neale Ranns9ec846c2021-02-09 14:04:02 +0000933 .sw_if_index = ~0,
934 };
935 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
936
937 mp->sw_if_index = htonl (ctx.sw_if_index);
938 }
939 else
940 mp->sw_if_index = ~0;
941
942 if (ipsec_sa_is_set_IS_TUNNEL (sa))
943 tunnel_encode (&sa->tunnel, &mp->entry.tunnel);
944
945 if (ipsec_sa_is_set_UDP_ENCAP (sa))
946 {
947 mp->entry.udp_src_port = sa->udp_hdr.src_port;
948 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
949 }
950
951 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
952 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
953 if (ipsec_sa_is_set_USE_ESN (sa))
954 {
955 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
956 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
957 }
958 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
959 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
960
961 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
962
963 vl_api_send_msg (ctx->reg, (u8 *) mp);
964
965 return (WALK_CONTINUE);
966}
967
968static void
969vl_api_ipsec_sa_v3_dump_t_handler (vl_api_ipsec_sa_v3_dump_t *mp)
970{
971 vl_api_registration_t *reg;
972
Neale Ranns9ec846c2021-02-09 14:04:02 +0000973 reg = vl_api_client_index_to_registration (mp->client_index);
974 if (!reg)
975 return;
976
977 ipsec_dump_walk_ctx_t ctx = {
978 .reg = reg,
979 .context = mp->context,
980 };
981
982 ipsec_sa_walk (send_ipsec_sa_v3_details, &ctx);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000983}
984
Matthew Smith75d85602017-10-05 19:03:05 -0500985static void
Klement Sekerab4d30532018-11-08 13:00:02 +0100986vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
987{
988 vl_api_registration_t *rp;
989 ipsec_main_t *im = &ipsec_main;
990 u32 context = mp->context;
991
992 rp = vl_api_client_index_to_registration (mp->client_index);
993
994 if (rp == 0)
995 {
996 clib_warning ("Client %d AWOL", mp->client_index);
997 return;
998 }
999
1000 ipsec_ah_backend_t *ab;
1001 ipsec_esp_backend_t *eb;
1002 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001003 pool_foreach (ab, im->ah_backends) {
Klement Sekerab4d30532018-11-08 13:00:02 +01001004 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1005 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +00001006 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
Klement Sekerab4d30532018-11-08 13:00:02 +01001007 mp->context = context;
1008 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1009 ab->name);
Neale Ranns17dcec02019-01-09 21:22:20 -08001010 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
Klement Sekerab4d30532018-11-08 13:00:02 +01001011 mp->index = ab - im->ah_backends;
1012 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1013 vl_api_send_msg (rp, (u8 *)mp);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001014 }
1015 pool_foreach (eb, im->esp_backends) {
Klement Sekerab4d30532018-11-08 13:00:02 +01001016 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1017 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlarc73f3292021-06-22 08:21:31 +00001018 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
Klement Sekerab4d30532018-11-08 13:00:02 +01001019 mp->context = context;
1020 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1021 eb->name);
Neale Ranns17dcec02019-01-09 21:22:20 -08001022 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
Klement Sekerab4d30532018-11-08 13:00:02 +01001023 mp->index = eb - im->esp_backends;
1024 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1025 vl_api_send_msg (rp, (u8 *)mp);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001026 }
Klement Sekerab4d30532018-11-08 13:00:02 +01001027 /* *INDENT-ON* */
1028}
1029
1030static void
1031vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1032{
1033 ipsec_main_t *im = &ipsec_main;
1034 vl_api_ipsec_select_backend_reply_t *rmp;
Neale Ranns17dcec02019-01-09 21:22:20 -08001035 ipsec_protocol_t protocol;
Klement Sekerab4d30532018-11-08 13:00:02 +01001036 int rv = 0;
Neale Rannsc5fe57d2021-02-25 16:01:28 +00001037 if (pool_elts (ipsec_sa_pool) > 0)
Klement Sekerab4d30532018-11-08 13:00:02 +01001038 {
1039 rv = VNET_API_ERROR_INSTANCE_IN_USE;
1040 goto done;
1041 }
Neale Ranns17dcec02019-01-09 21:22:20 -08001042
1043 rv = ipsec_proto_decode (mp->protocol, &protocol);
1044
1045 if (rv)
1046 goto done;
1047
Neale Ranns17dcec02019-01-09 21:22:20 -08001048 switch (protocol)
Klement Sekerab4d30532018-11-08 13:00:02 +01001049 {
1050 case IPSEC_PROTOCOL_ESP:
Neale Rannse8915fc2019-04-23 20:57:55 -04001051 rv = ipsec_select_esp_backend (im, mp->index);
Klement Sekerab4d30532018-11-08 13:00:02 +01001052 break;
1053 case IPSEC_PROTOCOL_AH:
Neale Rannse8915fc2019-04-23 20:57:55 -04001054 rv = ipsec_select_ah_backend (im, mp->index);
Klement Sekerab4d30532018-11-08 13:00:02 +01001055 break;
1056 default:
Neale Rannse8915fc2019-04-23 20:57:55 -04001057 rv = VNET_API_ERROR_INVALID_PROTOCOL;
Klement Sekerab4d30532018-11-08 13:00:02 +01001058 break;
1059 }
Klement Sekerab4d30532018-11-08 13:00:02 +01001060done:
1061 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1062}
1063
Yulong Pei2e84d662020-08-14 18:21:08 +08001064static void
1065vl_api_ipsec_set_async_mode_t_handler (vl_api_ipsec_set_async_mode_t * mp)
1066{
1067 vl_api_ipsec_set_async_mode_reply_t *rmp;
1068 int rv = 0;
1069
Yulong Pei2e84d662020-08-14 18:21:08 +08001070 ipsec_set_async_mode (mp->async_enable);
1071
1072 REPLY_MACRO (VL_API_IPSEC_SET_ASYNC_MODE_REPLY);
1073}
1074
Filip Tehlarc73f3292021-06-22 08:21:31 +00001075#include <vnet/ipsec/ipsec.api.c>
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001076static clib_error_t *
1077ipsec_api_hookup (vlib_main_t * vm)
1078{
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001079 /*
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001080 * Set up the (msg_name, crc, message-id) table
1081 */
Filip Tehlarc73f3292021-06-22 08:21:31 +00001082 REPLY_MSG_ID_BASE = setup_message_id_table ();
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001083
1084 return 0;
1085}
1086
1087VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1088
1089/*
1090 * fd.io coding-style-patch-verification: ON
1091 *
1092 * Local Variables:
1093 * eval: (c-set-style "gnu")
1094 * End:
1095 */