blob: 4a55a29c288dc2294577e8eddab35bfba0b0485c [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>
Pierre Pfister4c422f92018-12-10 11:19:08 +010028#include <vnet/fib/fib.h>
Neale Ranns12989b52019-09-26 16:20:19 +000029#include <vnet/ipip/ipip.h>
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010030
31#include <vnet/vnet_msg_enum.h>
32
Damjan Mariona9a951f2017-01-16 22:06:10 +010033#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010034#include <vnet/ipsec/ipsec.h>
Neale Rannsc87b66c2019-02-07 07:26:12 -080035#include <vnet/ipsec/ipsec_tun.h>
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010036#endif /* IPSEC */
37
38#define vl_typedefs /* define message structures */
39#include <vnet/vnet_all_api_h.h>
40#undef vl_typedefs
41
42#define vl_endianfun /* define message structures */
43#include <vnet/vnet_all_api_h.h>
44#undef vl_endianfun
45
46/* instantiate all the print functions we know about */
47#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
48#define vl_printfun
49#include <vnet/vnet_all_api_h.h>
50#undef vl_printfun
51
52#include <vlibapi/api_helper_macros.h>
53
Klement Sekerab4d30532018-11-08 13:00:02 +010054#define foreach_vpe_api_msg \
55_(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
56_(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
Neale Ranns17dcec02019-01-09 21:22:20 -080057_(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \
58_(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \
Klement Sekerab4d30532018-11-08 13:00:02 +010059_(IPSEC_SA_DUMP, ipsec_sa_dump) \
60_(IPSEC_SPDS_DUMP, ipsec_spds_dump) \
61_(IPSEC_SPD_DUMP, ipsec_spd_dump) \
62_(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \
63_(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \
Klement Sekerab4d30532018-11-08 13:00:02 +010064_(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \
Klement Sekerab4d30532018-11-08 13:00:02 +010065_(IPSEC_SELECT_BACKEND, ipsec_select_backend) \
Neale Rannsc87b66c2019-02-07 07:26:12 -080066_(IPSEC_BACKEND_DUMP, ipsec_backend_dump) \
67_(IPSEC_TUNNEL_PROTECT_UPDATE, ipsec_tunnel_protect_update) \
68_(IPSEC_TUNNEL_PROTECT_DEL, ipsec_tunnel_protect_del) \
69_(IPSEC_TUNNEL_PROTECT_DUMP, ipsec_tunnel_protect_dump)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010070
Klement Sekerab4d30532018-11-08 13:00:02 +010071static void
72vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010073{
Damjan Mariona9a951f2017-01-16 22:06:10 +010074#if WITH_LIBSSL == 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010075 clib_warning ("unimplemented");
76#else
77
78 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
79 vl_api_ipsec_spd_add_del_reply_t *rmp;
80 int rv;
81
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010082 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010083
84 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
85#endif
86}
87
88static void vl_api_ipsec_interface_add_del_spd_t_handler
89 (vl_api_ipsec_interface_add_del_spd_t * mp)
90{
91 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
92 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
93 int rv;
94 u32 sw_if_index __attribute__ ((unused));
95 u32 spd_id __attribute__ ((unused));
96
97 sw_if_index = ntohl (mp->sw_if_index);
98 spd_id = ntohl (mp->spd_id);
99
100 VALIDATE_SW_IF_INDEX (mp);
101
Damjan Mariona9a951f2017-01-16 22:06:10 +0100102#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100103 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
104#else
105 rv = VNET_API_ERROR_UNIMPLEMENTED;
106#endif
107
108 BAD_SW_IF_INDEX_LABEL;
109
110 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
111}
112
Neale Rannsc87b66c2019-02-07 07:26:12 -0800113static void vl_api_ipsec_tunnel_protect_update_t_handler
114 (vl_api_ipsec_tunnel_protect_update_t * mp)
115{
116 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
117 vl_api_ipsec_tunnel_protect_update_reply_t *rmp;
118 u32 sw_if_index, ii, *sa_ins = NULL;
Neale Ranns28287212019-12-16 00:53:11 +0000119 ip_address_t nh;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800120 int rv;
121
122 sw_if_index = ntohl (mp->tunnel.sw_if_index);
123
124 VALIDATE_SW_IF_INDEX (&(mp->tunnel));
125
126#if WITH_LIBSSL > 0
127
128 for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
129 vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
130
Neale Ranns28287212019-12-16 00:53:11 +0000131 ip_address_decode2 (&mp->tunnel.nh, &nh);
132
133 rv = ipsec_tun_protect_update (sw_if_index, &nh,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800134 ntohl (mp->tunnel.sa_out), sa_ins);
135#else
136 rv = VNET_API_ERROR_UNIMPLEMENTED;
137#endif
138
139 BAD_SW_IF_INDEX_LABEL;
140
141 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
142}
143
144static void vl_api_ipsec_tunnel_protect_del_t_handler
145 (vl_api_ipsec_tunnel_protect_del_t * mp)
146{
147 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
148 vl_api_ipsec_tunnel_protect_del_reply_t *rmp;
Neale Ranns28287212019-12-16 00:53:11 +0000149 ip_address_t nh;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800150 u32 sw_if_index;
Neale Ranns28287212019-12-16 00:53:11 +0000151 int rv;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800152
153 sw_if_index = ntohl (mp->sw_if_index);
154
155 VALIDATE_SW_IF_INDEX (mp);
156
157#if WITH_LIBSSL > 0
Neale Ranns28287212019-12-16 00:53:11 +0000158 ip_address_decode2 (&mp->nh, &nh);
159 rv = ipsec_tun_protect_del (sw_if_index, &nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800160#else
161 rv = VNET_API_ERROR_UNIMPLEMENTED;
162#endif
163
164 BAD_SW_IF_INDEX_LABEL;
165
166 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
167}
168
Neale Ranns12989b52019-09-26 16:20:19 +0000169typedef struct ipsec_dump_walk_ctx_t_
Neale Rannsc87b66c2019-02-07 07:26:12 -0800170{
171 vl_api_registration_t *reg;
172 u32 context;
Neale Ranns12989b52019-09-26 16:20:19 +0000173} ipsec_dump_walk_ctx_t;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800174
175static walk_rc_t
176send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
177{
Neale Ranns12989b52019-09-26 16:20:19 +0000178 ipsec_dump_walk_ctx_t *ctx = arg;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800179 vl_api_ipsec_tunnel_protect_details_t *mp;
180 ipsec_tun_protect_t *itp;
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500181 u32 ii = 0;
182 ipsec_sa_t *sa;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800183
184 itp = ipsec_tun_protect_get (itpi);
185
Neale Rannsc87b66c2019-02-07 07:26:12 -0800186 mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
187 clib_memset (mp, 0, sizeof (*mp));
188 mp->_vl_msg_id = ntohs (VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
189 mp->context = ctx->context;
190
191 mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
Neale Ranns28287212019-12-16 00:53:11 +0000192 ip_address_encode2 (itp->itp_key, &mp->tun.nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800193
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500194 sa = ipsec_sa_get (itp->itp_out_sa);
195 mp->tun.sa_out = htonl (sa->id);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800196 mp->tun.n_sa_in = itp->itp_n_sa_in;
197 /* *INDENT-OFF* */
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500198 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800199 ({
Matthew Smith5cee0bc2020-03-31 09:52:17 -0500200 mp->tun.sa_in[ii++] = htonl (sa->id);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800201 }));
202 /* *INDENT-ON* */
203
204 vl_api_send_msg (ctx->reg, (u8 *) mp);
205
206 return (WALK_CONTINUE);
207}
208
209static void
210vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
211 * mp)
212{
213 vl_api_registration_t *reg;
214 u32 sw_if_index;
215
216#if WITH_LIBSSL > 0
217 reg = vl_api_client_index_to_registration (mp->client_index);
218 if (!reg)
219 return;
220
Neale Ranns12989b52019-09-26 16:20:19 +0000221 ipsec_dump_walk_ctx_t ctx = {
Neale Rannsc87b66c2019-02-07 07:26:12 -0800222 .reg = reg,
223 .context = mp->context,
224 };
225
226 sw_if_index = ntohl (mp->sw_if_index);
227
228 if (~0 == sw_if_index)
229 {
230 ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
231 }
232 else
233 {
Neale Ranns28287212019-12-16 00:53:11 +0000234 ipsec_tun_protect_walk_itf (sw_if_index,
235 send_ipsec_tunnel_protect_details, &ctx);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800236 }
237#else
238 clib_warning ("unimplemented");
239#endif
240}
241
Neale Ranns17dcec02019-01-09 21:22:20 -0800242static int
243ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
244 ipsec_policy_action_t * out)
245{
246 in = clib_net_to_host_u32 (in);
247
248 switch (in)
249 {
250#define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
251 *out = IPSEC_POLICY_ACTION_##f; \
252 return (0);
253 foreach_ipsec_policy_action
254#undef _
255 }
256 return (VNET_API_ERROR_UNIMPLEMENTED);
257}
258
259static void vl_api_ipsec_spd_entry_add_del_t_handler
260 (vl_api_ipsec_spd_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100261{
262 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
Neale Ranns17dcec02019-01-09 21:22:20 -0800263 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
264 ip46_type_t itype;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800265 u32 stat_index;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100266 int rv;
267
Neale Ranns8c2dd1b2019-02-19 08:27:34 -0800268 stat_index = ~0;
269
Damjan Mariona9a951f2017-01-16 22:06:10 +0100270#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100271 ipsec_policy_t p;
272
Dave Barachb7b92992018-10-17 10:38:51 -0400273 clib_memset (&p, 0, sizeof (p));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100274
Neale Ranns17dcec02019-01-09 21:22:20 -0800275 p.id = ntohl (mp->entry.spd_id);
276 p.priority = ntohl (mp->entry.priority);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100277
Neale Ranns17dcec02019-01-09 21:22:20 -0800278 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
279 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
280 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
281 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
282
283 p.is_ipv6 = (itype == IP46_TYPE_IP6);
284
285 p.protocol = mp->entry.protocol;
Neale Rannsa4d24312019-07-10 13:46:21 +0000286 p.rport.start = ntohs (mp->entry.remote_port_start);
287 p.rport.stop = ntohs (mp->entry.remote_port_stop);
288 p.lport.start = ntohs (mp->entry.local_port_start);
289 p.lport.stop = ntohs (mp->entry.local_port_stop);
Neale Ranns17dcec02019-01-09 21:22:20 -0800290
291 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
292
293 if (rv)
294 goto out;
295
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100296 /* policy action resolve unsupported */
Neale Ranns17dcec02019-01-09 21:22:20 -0800297 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100298 {
299 clib_warning ("unsupported action: 'resolve'");
300 rv = VNET_API_ERROR_UNIMPLEMENTED;
301 goto out;
302 }
Neale Ranns17dcec02019-01-09 21:22:20 -0800303 p.sa_id = ntohl (mp->entry.sa_id);
Neale Ranns9f231d42019-03-19 10:06:00 +0000304 rv =
305 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
306 &p.type);
307 if (rv)
308 goto out;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100309
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800310 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100311 if (rv)
312 goto out;
313
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100314#else
315 rv = VNET_API_ERROR_UNIMPLEMENTED;
316 goto out;
317#endif
318
319out:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800320 /* *INDENT-OFF* */
321 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
322 ({
323 rmp->stat_index = ntohl(stat_index);
324 }));
325 /* *INDENT-ON* */
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100326}
327
Neale Ranns17dcec02019-01-09 21:22:20 -0800328static void vl_api_ipsec_sad_entry_add_del_t_handler
329 (vl_api_ipsec_sad_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100330{
331 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
Neale Ranns17dcec02019-01-09 21:22:20 -0800332 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800333 ip46_address_t tun_src = { }, tun_dst =
334 {
335 };
336 ipsec_key_t crypto_key, integ_key;
337 ipsec_crypto_alg_t crypto_alg;
338 ipsec_integ_alg_t integ_alg;
339 ipsec_protocol_t proto;
340 ipsec_sa_flags_t flags;
Dave Baracha5160d72019-03-13 15:29:15 -0400341 u32 id, spi, sa_index = ~0;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100342 int rv;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800343
Damjan Mariona9a951f2017-01-16 22:06:10 +0100344#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100345
Neale Ranns8d7c5022019-02-06 01:41:05 -0800346 id = ntohl (mp->entry.sad_id);
347 spi = ntohl (mp->entry.spi);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100348
Neale Ranns8d7c5022019-02-06 01:41:05 -0800349 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
Neale Ranns17dcec02019-01-09 21:22:20 -0800350
351 if (rv)
352 goto out;
353
Neale Ranns8d7c5022019-02-06 01:41:05 -0800354 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800355
356 if (rv)
357 goto out;
358
Neale Ranns8d7c5022019-02-06 01:41:05 -0800359 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800360
361 if (rv)
362 goto out;
363
Neale Ranns8d7c5022019-02-06 01:41:05 -0800364 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
365 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
Neale Ranns17dcec02019-01-09 21:22:20 -0800366
Neale Ranns8d7c5022019-02-06 01:41:05 -0800367 flags = ipsec_sa_flags_decode (mp->entry.flags);
Neale Ranns17dcec02019-01-09 21:22:20 -0800368
Neale Ranns8d7c5022019-02-06 01:41:05 -0800369 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
370 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100371
Neale Ranns8d7c5022019-02-06 01:41:05 -0800372 if (mp->is_add)
Neale Ranns495d7ff2019-07-12 09:15:26 +0000373 rv = ipsec_sa_add_and_lock (id, spi, proto,
374 crypto_alg, &crypto_key,
375 integ_alg, &integ_key, flags,
376 0, mp->entry.salt, &tun_src, &tun_dst,
Filip Tehlare5d34912020-03-02 15:17:37 +0000377 &sa_index, IPSEC_UDP_PORT_NONE);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800378 else
Neale Ranns495d7ff2019-07-12 09:15:26 +0000379 rv = ipsec_sa_unlock_id (id);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800380
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100381#else
382 rv = VNET_API_ERROR_UNIMPLEMENTED;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100383#endif
384
385out:
Neale Rannseba31ec2019-02-17 18:04:27 +0000386 /* *INDENT-OFF* */
387 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
388 {
389 rmp->stat_index = htonl (sa_index);
390 });
391 /* *INDENT-ON* */
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100392}
393
394static void
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700395send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
396 u32 context)
397{
398 vl_api_ipsec_spds_details_t *mp;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800399 u32 n_policies = 0;
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700400
401 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400402 clib_memset (mp, 0, sizeof (*mp));
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700403 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
404 mp->context = context;
405
406 mp->spd_id = htonl (spd->id);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800407#define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
408 foreach_ipsec_spd_policy_type
409#undef _
410 mp->npolicies = htonl (n_policies);
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700411
412 vl_api_send_msg (reg, (u8 *) mp);
413}
414
415static void
416vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
417{
418 vl_api_registration_t *reg;
419 ipsec_main_t *im = &ipsec_main;
420 ipsec_spd_t *spd;
421#if WITH_LIBSSL > 0
422 reg = vl_api_client_index_to_registration (mp->client_index);
423 if (!reg)
424 return;
425
426 /* *INDENT-OFF* */
427 pool_foreach (spd, im->spds, ({
428 send_ipsec_spds_details (spd, reg, mp->context);
429 }));
430 /* *INDENT-ON* */
431#else
432 clib_warning ("unimplemented");
433#endif
434}
435
Neale Ranns17dcec02019-01-09 21:22:20 -0800436vl_api_ipsec_spd_action_t
437ipsec_spd_action_encode (ipsec_policy_action_t in)
438{
439 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
440
441 switch (in)
442 {
443#define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
444 out = IPSEC_API_SPD_ACTION_##f; \
445 break;
446 foreach_ipsec_policy_action
447#undef _
448 }
449 return (clib_host_to_net_u32 (out));
450}
451
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700452static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800453send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
454 u32 context)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100455{
456 vl_api_ipsec_spd_details_t *mp;
457
458 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400459 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100460 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
461 mp->context = context;
462
Neale Ranns17dcec02019-01-09 21:22:20 -0800463 mp->entry.spd_id = htonl (p->id);
464 mp->entry.priority = htonl (p->priority);
Neale Ranns9f231d42019-03-19 10:06:00 +0000465 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
466 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
Neale Ranns17dcec02019-01-09 21:22:20 -0800467
468 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
469 &mp->entry.local_address_start);
470 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
471 &mp->entry.local_address_stop);
472 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
473 &mp->entry.remote_address_start);
474 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
475 &mp->entry.remote_address_stop);
Neale Rannsa4d24312019-07-10 13:46:21 +0000476 mp->entry.local_port_start = htons (p->lport.start);
477 mp->entry.local_port_stop = htons (p->lport.stop);
478 mp->entry.remote_port_start = htons (p->rport.start);
479 mp->entry.remote_port_stop = htons (p->rport.stop);
Neale Ranns17dcec02019-01-09 21:22:20 -0800480 mp->entry.protocol = p->protocol;
481 mp->entry.policy = ipsec_spd_action_encode (p->policy);
482 mp->entry.sa_id = htonl (p->sa_id);
483
Florin Coras6c4dae22018-01-09 06:39:23 -0800484 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100485}
486
487static void
488vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
489{
Florin Coras6c4dae22018-01-09 06:39:23 -0800490 vl_api_registration_t *reg;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100491 ipsec_main_t *im = &ipsec_main;
Neale Ranns9f231d42019-03-19 10:06:00 +0000492 ipsec_spd_policy_type_t ptype;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100493 ipsec_policy_t *policy;
494 ipsec_spd_t *spd;
495 uword *p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800496 u32 spd_index, *ii;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100497#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800498 reg = vl_api_client_index_to_registration (mp->client_index);
499 if (!reg)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100500 return;
501
502 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
503 if (!p)
504 return;
505
506 spd_index = p[0];
507 spd = pool_elt_at_index (im->spds, spd_index);
508
509 /* *INDENT-OFF* */
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800510 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
511 vec_foreach(ii, spd->policies[ptype])
512 {
513 policy = pool_elt_at_index(im->policies, *ii);
514
515 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
516 send_ipsec_spd_details (policy, reg, mp->context);
517 }
518 }
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100519 /* *INDENT-ON* */
520#else
521 clib_warning ("unimplemented");
522#endif
523}
524
525static void
Filip Varga871bca92018-11-02 13:51:44 +0100526send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
527 u32 sw_if_index, u32 context)
528{
529 vl_api_ipsec_spd_interface_details_t *mp;
530
531 mp = vl_msg_api_alloc (sizeof (*mp));
532 clib_memset (mp, 0, sizeof (*mp));
533 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
534 mp->context = context;
535
536 mp->spd_index = htonl (spd_index);
537 mp->sw_if_index = htonl (sw_if_index);
538
539 vl_api_send_msg (reg, (u8 *) mp);
540}
541
542static void
543vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
544 mp)
545{
546 ipsec_main_t *im = &ipsec_main;
547 vl_api_registration_t *reg;
548 u32 k, v, spd_index;
549
550#if WITH_LIBSSL > 0
551 reg = vl_api_client_index_to_registration (mp->client_index);
552 if (!reg)
553 return;
554
555 if (mp->spd_index_valid)
556 {
557 spd_index = ntohl (mp->spd_index);
558 /* *INDENT-OFF* */
559 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
560 if (v == spd_index)
561 send_ipsec_spd_interface_details(reg, v, k, mp->context);
562 }));
563 /* *INDENT-ON* */
564 }
565 else
566 {
567 /* *INDENT-OFF* */
568 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
569 send_ipsec_spd_interface_details(reg, v, k, mp->context);
570 }));
571 /* *INDENT-ON* */
572 }
573
574#else
575 clib_warning ("unimplemented");
576#endif
577}
578
Neale Ranns12989b52019-09-26 16:20:19 +0000579static u32
580ipsec_tun_mk_input_sa_id (u32 ti)
581{
582 return (0x80000000 | ti);
583}
584
585static u32
586ipsec_tun_mk_output_sa_id (u32 ti)
587{
588 return (0xc0000000 | ti);
589}
590
Filip Varga871bca92018-11-02 13:51:44 +0100591static void
Matthew Smithb0972cb2017-05-02 16:20:41 -0500592vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
593 mp)
594{
595 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500596 u32 sw_if_index = ~0;
Matthew Smithb0972cb2017-05-02 16:20:41 -0500597 int rv;
598
599#if WITH_LIBSSL > 0
Neale Ranns12989b52019-09-26 16:20:19 +0000600 ip46_address_t local_ip = ip46_address_initializer;
601 ip46_address_t remote_ip = ip46_address_initializer;
602 ipsec_key_t crypto_key, integ_key;
603 ipsec_sa_flags_t flags;
604 ip46_type_t local_ip_type, remote_ip_type;
605 ipip_transport_t transport;
606 u32 fib_index;
Matthew Smithb0972cb2017-05-02 16:20:41 -0500607
Neale Ranns12989b52019-09-26 16:20:19 +0000608 local_ip_type = ip_address_decode (&mp->local_ip, &local_ip);
609 remote_ip_type = ip_address_decode (&mp->remote_ip, &remote_ip);
610 transport = (IP46_TYPE_IP6 == local_ip_type ?
611 IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500612
Neale Ranns12989b52019-09-26 16:20:19 +0000613 if (local_ip_type != remote_ip_type)
614 {
615 rv = VNET_API_ERROR_INVALID_VALUE;
616 goto done;
617 }
Matthew Smithb0972cb2017-05-02 16:20:41 -0500618
Neale Ranns12989b52019-09-26 16:20:19 +0000619 flags = IPSEC_SA_FLAG_NONE;
620
621 if (mp->udp_encap)
622 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
623 if (mp->esn)
624 flags |= IPSEC_SA_FLAG_USE_ESN;
625 if (mp->anti_replay)
626 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
627
628 ipsec_mk_key (&crypto_key, mp->remote_crypto_key,
629 mp->remote_crypto_key_len);
630 ipsec_mk_key (&integ_key, mp->remote_integ_key, mp->remote_integ_key_len);
631 ipsec_mk_key (&crypto_key, mp->local_crypto_key, mp->local_crypto_key_len);
632 ipsec_mk_key (&integ_key, mp->local_integ_key, mp->local_integ_key_len);
633
634 fib_index =
635 fib_table_find (fib_proto_from_ip46 (local_ip_type),
636 ntohl (mp->tx_table_id));
637
638 if (~0 == fib_index)
639 {
640 rv = VNET_API_ERROR_NO_SUCH_FIB;
641 goto done;
642 }
643
644 if (mp->is_add)
645 {
646 // remote = input, local = output
647 /* create an ip-ip tunnel, then the two SA, then bind them */
648 rv = ipip_add_tunnel (transport,
Neale Rannsa548d132019-11-20 08:34:58 +0000649 (mp->renumber ? ntohl (mp->show_instance) : ~0),
Neale Ranns12989b52019-09-26 16:20:19 +0000650 &local_ip,
Neale Ranns95346962019-11-25 13:04:44 +0000651 &remote_ip, fib_index,
Neale Ranns59ff9182019-12-29 23:55:18 +0000652 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
Neale Ranns14053c92019-12-29 23:55:18 +0000653 TUNNEL_MODE_P2P, &sw_if_index);
Neale Ranns12989b52019-09-26 16:20:19 +0000654
655 if (rv)
656 goto done;
657
658 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_input_sa_id (sw_if_index),
659 ntohl (mp->remote_spi),
660 IPSEC_PROTOCOL_ESP,
661 mp->crypto_alg,
662 &crypto_key,
663 mp->integ_alg,
664 &integ_key,
665 (flags | IPSEC_SA_FLAG_IS_INBOUND),
666 ntohl (mp->tx_table_id),
Filip Tehlare5d34912020-03-02 15:17:37 +0000667 mp->salt, &remote_ip, &local_ip, NULL,
668 IPSEC_UDP_PORT_NONE);
Neale Ranns12989b52019-09-26 16:20:19 +0000669
670 if (rv)
671 goto done;
672
673 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_output_sa_id (sw_if_index),
674 ntohl (mp->local_spi),
675 IPSEC_PROTOCOL_ESP,
676 mp->crypto_alg,
677 &crypto_key,
678 mp->integ_alg,
679 &integ_key,
680 flags,
681 ntohl (mp->tx_table_id),
Filip Tehlare5d34912020-03-02 15:17:37 +0000682 mp->salt, &local_ip, &remote_ip, NULL,
683 IPSEC_UDP_PORT_NONE);
Neale Ranns12989b52019-09-26 16:20:19 +0000684
685 if (rv)
686 goto done;
687
Neale Ranns28287212019-12-16 00:53:11 +0000688 rv = ipsec_tun_protect_update_one (sw_if_index, NULL,
Neale Ranns12989b52019-09-26 16:20:19 +0000689 ipsec_tun_mk_output_sa_id
690 (sw_if_index),
691 ipsec_tun_mk_input_sa_id
692 (sw_if_index));
693 if (rv)
694 goto done;
695
696 /* the SAs are locked as a result of being used for proection,
697 * they cannot be removed from the API, since they cannot be refered
698 * to by the API. unlock them now, so that if the tunnel is rekeyed
699 * they-ll disapper
700 */
701 ipsec_sa_unlock_id (ipsec_tun_mk_input_sa_id (sw_if_index));
702 ipsec_sa_unlock_id (ipsec_tun_mk_output_sa_id (sw_if_index));
703 }
704 else
705 {
Neale Rannsd14fccd2019-11-15 15:03:27 +0000706 /* *INDENT-OFF* */
Neale Ranns12989b52019-09-26 16:20:19 +0000707 ipip_tunnel_key_t key = {
708 .transport = transport,
709 .fib_index = fib_index,
710 .src = local_ip,
711 .dst = remote_ip
712 };
Neale Rannsd14fccd2019-11-15 15:03:27 +0000713 /* *INDENT-ON* */
714
Neale Ranns12989b52019-09-26 16:20:19 +0000715 ipip_tunnel_t *t = ipip_tunnel_db_find (&key);
716
Neale Rannsd14fccd2019-11-15 15:03:27 +0000717 if (NULL != t)
718 {
Neale Ranns28287212019-12-16 00:53:11 +0000719 rv = ipsec_tun_protect_del (t->sw_if_index, NULL);
Neale Rannsd14fccd2019-11-15 15:03:27 +0000720 ipip_del_tunnel (t->sw_if_index);
721 }
722 else
723 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
Neale Ranns12989b52019-09-26 16:20:19 +0000724 }
Matthew Smithb0972cb2017-05-02 16:20:41 -0500725
726#else
727 rv = VNET_API_ERROR_UNIMPLEMENTED;
728#endif
Neale Ranns12989b52019-09-26 16:20:19 +0000729done:
Neale Ranns8d7c5022019-02-06 01:41:05 -0800730 /* *INDENT-OFF* */
731 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
732 ({
733 rmp->sw_if_index = htonl (sw_if_index);
734 }));
735 /* *INDENT-ON* */
Matthew Smithb0972cb2017-05-02 16:20:41 -0500736}
737
Neale Ranns12989b52019-09-26 16:20:19 +0000738typedef struct ipsec_sa_dump_match_ctx_t_
Matthew Smith28029532017-09-26 13:33:44 -0500739{
Neale Ranns12989b52019-09-26 16:20:19 +0000740 index_t sai;
741 u32 sw_if_index;
742} ipsec_sa_dump_match_ctx_t;
743
744static walk_rc_t
745ipsec_sa_dump_match_sa (index_t itpi, void *arg)
746{
747 ipsec_sa_dump_match_ctx_t *ctx = arg;
748 ipsec_tun_protect_t *itp;
749 index_t sai;
750
751 itp = ipsec_tun_protect_get (itpi);
752
753 if (itp->itp_out_sa == ctx->sai)
754 {
755 ctx->sw_if_index = itp->itp_sw_if_index;
756 return (WALK_STOP);
757 }
758 /* *INDENT-OFF* */
759 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
760 ({
761 if (sai == ctx->sai)
762 {
763 ctx->sw_if_index = itp->itp_sw_if_index;
764 return (WALK_STOP);
765 }
766 }));
767 /* *INDENT-OFF* */
768
769 return (WALK_CONTINUE);
770}
771
772static walk_rc_t
773send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
774{
775 ipsec_dump_walk_ctx_t *ctx = arg;
Matthew Smith28029532017-09-26 13:33:44 -0500776 vl_api_ipsec_sa_details_t *mp;
Neale Ranns12989b52019-09-26 16:20:19 +0000777 ipsec_main_t *im = &ipsec_main;
Matthew Smith28029532017-09-26 13:33:44 -0500778
779 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400780 clib_memset (mp, 0, sizeof (*mp));
Matthew Smith28029532017-09-26 13:33:44 -0500781 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
Neale Ranns12989b52019-09-26 16:20:19 +0000782 mp->context = ctx->context;
Matthew Smith28029532017-09-26 13:33:44 -0500783
Neale Ranns8d7c5022019-02-06 01:41:05 -0800784 mp->entry.sad_id = htonl (sa->id);
785 mp->entry.spi = htonl (sa->spi);
786 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
787 mp->entry.tx_table_id =
788 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
Matthew Smith28029532017-09-26 13:33:44 -0500789
Neale Ranns8d7c5022019-02-06 01:41:05 -0800790 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
791 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
Matthew Smith28029532017-09-26 13:33:44 -0500792
Neale Ranns8d7c5022019-02-06 01:41:05 -0800793 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
794 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
Matthew Smith28029532017-09-26 13:33:44 -0500795
Neale Ranns8d7c5022019-02-06 01:41:05 -0800796 mp->entry.flags = ipsec_sad_flags_encode (sa);
Neale Ranns12989b52019-09-26 16:20:19 +0000797 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
798
799 if (ipsec_sa_is_set_IS_PROTECT (sa))
800 {
801 ipsec_sa_dump_match_ctx_t ctx = {
802 .sai = sa - im->sad,
803 .sw_if_index = ~0,
804 };
805 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
806
807 mp->sw_if_index = htonl (ctx.sw_if_index);
808 }
809 else
810 mp->sw_if_index = ~0;
Matthew Smith28029532017-09-26 13:33:44 -0500811
Damjan Mariond709cbc2019-03-26 13:16:42 +0100812 if (ipsec_sa_is_set_IS_TUNNEL (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500813 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800814 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
815 &mp->entry.tunnel_src);
816 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
817 &mp->entry.tunnel_dst);
Matthew Smith28029532017-09-26 13:33:44 -0500818 }
819
Matthew Smith28029532017-09-26 13:33:44 -0500820 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
821 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100822 if (ipsec_sa_is_set_USE_ESN (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500823 {
824 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
825 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
826 }
Damjan Mariond709cbc2019-03-26 13:16:42 +0100827 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500828 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
Pierre Pfister4c422f92018-12-10 11:19:08 +0100829
Matthew Smith48d32b42020-04-02 07:45:49 -0500830 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
831
Neale Ranns12989b52019-09-26 16:20:19 +0000832 vl_api_send_msg (ctx->reg, (u8 *) mp);
Matthew Smith28029532017-09-26 13:33:44 -0500833
Neale Ranns12989b52019-09-26 16:20:19 +0000834 return (WALK_CONTINUE);
835}
Matthew Smith28029532017-09-26 13:33:44 -0500836
837static void
838vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
839{
Florin Coras6c4dae22018-01-09 06:39:23 -0800840 vl_api_registration_t *reg;
Matthew Smith28029532017-09-26 13:33:44 -0500841
842#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800843 reg = vl_api_client_index_to_registration (mp->client_index);
Neale Ranns12989b52019-09-26 16:20:19 +0000844 if (!reg)
Matthew Smith28029532017-09-26 13:33:44 -0500845 return;
846
Neale Ranns12989b52019-09-26 16:20:19 +0000847 ipsec_dump_walk_ctx_t ctx = {
848 .reg = reg,
849 .context = mp->context,
850 };
Matthew Smith28029532017-09-26 13:33:44 -0500851
Neale Ranns12989b52019-09-26 16:20:19 +0000852 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
Matthew Smith28029532017-09-26 13:33:44 -0500853
Matthew Smith28029532017-09-26 13:33:44 -0500854#else
855 clib_warning ("unimplemented");
856#endif
857}
858
Matthew Smith75d85602017-10-05 19:03:05 -0500859static void
Matthew Smithca514fd2017-10-12 12:06:59 -0500860vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
861{
862 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
Matthew Smithca514fd2017-10-12 12:06:59 -0500863 int rv;
864
865#if WITH_LIBSSL > 0
Neale Ranns12989b52019-09-26 16:20:19 +0000866 VALIDATE_SW_IF_INDEX(mp);
Matthew Smithca514fd2017-10-12 12:06:59 -0500867
Neale Ranns12989b52019-09-26 16:20:19 +0000868 if (mp->is_outbound)
Neale Ranns28287212019-12-16 00:53:11 +0000869 rv = ipsec_tun_protect_update_out (ntohl (mp->sw_if_index), NULL,
Neale Ranns12989b52019-09-26 16:20:19 +0000870 ntohl (mp->sa_id));
871 else
Neale Ranns28287212019-12-16 00:53:11 +0000872 rv = ipsec_tun_protect_update_in (ntohl (mp->sw_if_index), NULL,
Neale Ranns12989b52019-09-26 16:20:19 +0000873 ntohl (mp->sa_id));
874
Matthew Smithca514fd2017-10-12 12:06:59 -0500875#else
876 clib_warning ("unimplemented");
877#endif
878
Neale Ranns12989b52019-09-26 16:20:19 +0000879 BAD_SW_IF_INDEX_LABEL;
880
Matthew Smithca514fd2017-10-12 12:06:59 -0500881 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
882}
883
Klement Sekerab4d30532018-11-08 13:00:02 +0100884static void
885vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
886{
887 vl_api_registration_t *rp;
888 ipsec_main_t *im = &ipsec_main;
889 u32 context = mp->context;
890
891 rp = vl_api_client_index_to_registration (mp->client_index);
892
893 if (rp == 0)
894 {
895 clib_warning ("Client %d AWOL", mp->client_index);
896 return;
897 }
898
899 ipsec_ah_backend_t *ab;
900 ipsec_esp_backend_t *eb;
901 /* *INDENT-OFF* */
902 pool_foreach (ab, im->ah_backends, {
903 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
904 clib_memset (mp, 0, sizeof (*mp));
905 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
906 mp->context = context;
907 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
908 ab->name);
Neale Ranns17dcec02019-01-09 21:22:20 -0800909 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
Klement Sekerab4d30532018-11-08 13:00:02 +0100910 mp->index = ab - im->ah_backends;
911 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
912 vl_api_send_msg (rp, (u8 *)mp);
913 });
914 pool_foreach (eb, im->esp_backends, {
915 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
916 clib_memset (mp, 0, sizeof (*mp));
917 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
918 mp->context = context;
919 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
920 eb->name);
Neale Ranns17dcec02019-01-09 21:22:20 -0800921 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
Klement Sekerab4d30532018-11-08 13:00:02 +0100922 mp->index = eb - im->esp_backends;
923 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
924 vl_api_send_msg (rp, (u8 *)mp);
925 });
926 /* *INDENT-ON* */
927}
928
929static void
930vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
931{
932 ipsec_main_t *im = &ipsec_main;
933 vl_api_ipsec_select_backend_reply_t *rmp;
Neale Ranns17dcec02019-01-09 21:22:20 -0800934 ipsec_protocol_t protocol;
Klement Sekerab4d30532018-11-08 13:00:02 +0100935 int rv = 0;
936 if (pool_elts (im->sad) > 0)
937 {
938 rv = VNET_API_ERROR_INSTANCE_IN_USE;
939 goto done;
940 }
Neale Ranns17dcec02019-01-09 21:22:20 -0800941
942 rv = ipsec_proto_decode (mp->protocol, &protocol);
943
944 if (rv)
945 goto done;
946
Klement Sekerab4d30532018-11-08 13:00:02 +0100947#if WITH_LIBSSL > 0
Neale Ranns17dcec02019-01-09 21:22:20 -0800948 switch (protocol)
Klement Sekerab4d30532018-11-08 13:00:02 +0100949 {
950 case IPSEC_PROTOCOL_ESP:
Neale Rannse8915fc2019-04-23 20:57:55 -0400951 rv = ipsec_select_esp_backend (im, mp->index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100952 break;
953 case IPSEC_PROTOCOL_AH:
Neale Rannse8915fc2019-04-23 20:57:55 -0400954 rv = ipsec_select_ah_backend (im, mp->index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100955 break;
956 default:
Neale Rannse8915fc2019-04-23 20:57:55 -0400957 rv = VNET_API_ERROR_INVALID_PROTOCOL;
Klement Sekerab4d30532018-11-08 13:00:02 +0100958 break;
959 }
960#else
961 clib_warning ("unimplemented"); /* FIXME */
962#endif
963done:
964 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
965}
966
Neale Ranns7c44d782019-02-25 10:28:29 +0000967/*
968 * ipsec_api_hookup
969 * Add vpe's API message handlers to the table.
970 * vlib has already mapped shared memory and
971 * added the client registration handlers.
972 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
973 */
974#define vl_msg_name_crc_list
975#include <vnet/vnet_all_api_h.h>
976#undef vl_msg_name_crc_list
977
978static void
979setup_message_id_table (api_main_t * am)
980{
981#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
982 foreach_vl_msg_name_crc_ipsec;
983#undef _
984}
985
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100986static clib_error_t *
987ipsec_api_hookup (vlib_main_t * vm)
988{
Dave Barach39d69112019-11-27 11:42:13 -0500989 api_main_t *am = vlibapi_get_main ();
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100990
991#define _(N,n) \
992 vl_msg_api_set_handlers(VL_API_##N, #n, \
993 vl_api_##n##_t_handler, \
994 vl_noop_handler, \
995 vl_api_##n##_t_endian, \
996 vl_api_##n##_t_print, \
997 sizeof(vl_api_##n##_t), 1);
998 foreach_vpe_api_msg;
999#undef _
1000
1001 /*
Neale Rannsc87b66c2019-02-07 07:26:12 -08001002 * Adding and deleting SAs is MP safe since when they are added/delete
1003 * no traffic is using them
1004 */
1005 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL] = 1;
1006 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY] = 1;
1007
1008 /*
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001009 * Set up the (msg_name, crc, message-id) table
1010 */
1011 setup_message_id_table (am);
1012
1013 return 0;
1014}
1015
1016VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1017
1018/*
1019 * fd.io coding-style-patch-verification: ON
1020 *
1021 * Local Variables:
1022 * eval: (c-set-style "gnu")
1023 * End:
1024 */