blob: 99e25f1b17a3e2da3b0993d8249565529f1aa818 [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>
Pierre Pfister4c422f92018-12-10 11:19:08 +010027#include <vnet/fib/fib.h>
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010028
29#include <vnet/vnet_msg_enum.h>
30
Damjan Mariona9a951f2017-01-16 22:06:10 +010031#if WITH_LIBSSL > 0
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>
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010034#endif /* IPSEC */
35
36#define vl_typedefs /* define message structures */
37#include <vnet/vnet_all_api_h.h>
38#undef vl_typedefs
39
40#define vl_endianfun /* define message structures */
41#include <vnet/vnet_all_api_h.h>
42#undef vl_endianfun
43
44/* instantiate all the print functions we know about */
45#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46#define vl_printfun
47#include <vnet/vnet_all_api_h.h>
48#undef vl_printfun
49
50#include <vlibapi/api_helper_macros.h>
51
Klement Sekerab4d30532018-11-08 13:00:02 +010052#define foreach_vpe_api_msg \
53_(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
54_(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
Neale Ranns17dcec02019-01-09 21:22:20 -080055_(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \
56_(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \
Klement Sekerab4d30532018-11-08 13:00:02 +010057_(IPSEC_SA_DUMP, ipsec_sa_dump) \
58_(IPSEC_SPDS_DUMP, ipsec_spds_dump) \
59_(IPSEC_SPD_DUMP, ipsec_spd_dump) \
60_(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \
61_(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \
Klement Sekerab4d30532018-11-08 13:00:02 +010062_(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \
Klement Sekerab4d30532018-11-08 13:00:02 +010063_(IPSEC_SELECT_BACKEND, ipsec_select_backend) \
Neale Rannsc87b66c2019-02-07 07:26:12 -080064_(IPSEC_BACKEND_DUMP, ipsec_backend_dump) \
65_(IPSEC_TUNNEL_PROTECT_UPDATE, ipsec_tunnel_protect_update) \
66_(IPSEC_TUNNEL_PROTECT_DEL, ipsec_tunnel_protect_del) \
67_(IPSEC_TUNNEL_PROTECT_DUMP, ipsec_tunnel_protect_dump)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010068
Klement Sekerab4d30532018-11-08 13:00:02 +010069static void
70vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010071{
Damjan Mariona9a951f2017-01-16 22:06:10 +010072#if WITH_LIBSSL == 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010073 clib_warning ("unimplemented");
74#else
75
76 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
77 vl_api_ipsec_spd_add_del_reply_t *rmp;
78 int rv;
79
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010080 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010081
82 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
83#endif
84}
85
86static void vl_api_ipsec_interface_add_del_spd_t_handler
87 (vl_api_ipsec_interface_add_del_spd_t * mp)
88{
89 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
90 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
91 int rv;
92 u32 sw_if_index __attribute__ ((unused));
93 u32 spd_id __attribute__ ((unused));
94
95 sw_if_index = ntohl (mp->sw_if_index);
96 spd_id = ntohl (mp->spd_id);
97
98 VALIDATE_SW_IF_INDEX (mp);
99
Damjan Mariona9a951f2017-01-16 22:06:10 +0100100#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100101 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
102#else
103 rv = VNET_API_ERROR_UNIMPLEMENTED;
104#endif
105
106 BAD_SW_IF_INDEX_LABEL;
107
108 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
109}
110
Neale Rannsc87b66c2019-02-07 07:26:12 -0800111static void vl_api_ipsec_tunnel_protect_update_t_handler
112 (vl_api_ipsec_tunnel_protect_update_t * mp)
113{
114 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
115 vl_api_ipsec_tunnel_protect_update_reply_t *rmp;
116 u32 sw_if_index, ii, *sa_ins = NULL;
117 int rv;
118
119 sw_if_index = ntohl (mp->tunnel.sw_if_index);
120
121 VALIDATE_SW_IF_INDEX (&(mp->tunnel));
122
123#if WITH_LIBSSL > 0
124
125 for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
126 vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
127
128 rv = ipsec_tun_protect_update (sw_if_index,
129 ntohl (mp->tunnel.sa_out), sa_ins);
130#else
131 rv = VNET_API_ERROR_UNIMPLEMENTED;
132#endif
133
134 BAD_SW_IF_INDEX_LABEL;
135
136 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
137}
138
139static void vl_api_ipsec_tunnel_protect_del_t_handler
140 (vl_api_ipsec_tunnel_protect_del_t * mp)
141{
142 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
143 vl_api_ipsec_tunnel_protect_del_reply_t *rmp;
144 int rv;
145 u32 sw_if_index;
146
147 sw_if_index = ntohl (mp->sw_if_index);
148
149 VALIDATE_SW_IF_INDEX (mp);
150
151#if WITH_LIBSSL > 0
152 rv = ipsec_tun_protect_del (sw_if_index);
153#else
154 rv = VNET_API_ERROR_UNIMPLEMENTED;
155#endif
156
157 BAD_SW_IF_INDEX_LABEL;
158
159 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
160}
161
162typedef struct ipsec_tunnel_protect_walk_ctx_t_
163{
164 vl_api_registration_t *reg;
165 u32 context;
166} ipsec_tunnel_protect_walk_ctx_t;
167
168static walk_rc_t
169send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
170{
171 ipsec_tunnel_protect_walk_ctx_t *ctx = arg;
172 vl_api_ipsec_tunnel_protect_details_t *mp;
173 ipsec_tun_protect_t *itp;
174 u32 sai, ii = 0;
175
176 itp = ipsec_tun_protect_get (itpi);
177
178
179 mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
180 clib_memset (mp, 0, sizeof (*mp));
181 mp->_vl_msg_id = ntohs (VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
182 mp->context = ctx->context;
183
184 mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
185
186 mp->tun.sa_out = htonl (itp->itp_out_sa);
187 mp->tun.n_sa_in = itp->itp_n_sa_in;
188 /* *INDENT-OFF* */
189 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
190 ({
191 mp->tun.sa_in[ii++] = htonl (sai);
192 }));
193 /* *INDENT-ON* */
194
195 vl_api_send_msg (ctx->reg, (u8 *) mp);
196
197 return (WALK_CONTINUE);
198}
199
200static void
201vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
202 * mp)
203{
204 vl_api_registration_t *reg;
205 u32 sw_if_index;
206
207#if WITH_LIBSSL > 0
208 reg = vl_api_client_index_to_registration (mp->client_index);
209 if (!reg)
210 return;
211
212 ipsec_tunnel_protect_walk_ctx_t ctx = {
213 .reg = reg,
214 .context = mp->context,
215 };
216
217 sw_if_index = ntohl (mp->sw_if_index);
218
219 if (~0 == sw_if_index)
220 {
221 ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
222 }
223 else
224 {
225 index_t itpi;
226
227 itpi = ipsec_tun_protect_find (sw_if_index);
228
229 if (INDEX_INVALID != itpi)
230 send_ipsec_tunnel_protect_details (itpi, &ctx);
231 }
232#else
233 clib_warning ("unimplemented");
234#endif
235}
236
Neale Ranns17dcec02019-01-09 21:22:20 -0800237static int
238ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
239 ipsec_policy_action_t * out)
240{
241 in = clib_net_to_host_u32 (in);
242
243 switch (in)
244 {
245#define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
246 *out = IPSEC_POLICY_ACTION_##f; \
247 return (0);
248 foreach_ipsec_policy_action
249#undef _
250 }
251 return (VNET_API_ERROR_UNIMPLEMENTED);
252}
253
254static void vl_api_ipsec_spd_entry_add_del_t_handler
255 (vl_api_ipsec_spd_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100256{
257 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
Neale Ranns17dcec02019-01-09 21:22:20 -0800258 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
259 ip46_type_t itype;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800260 u32 stat_index;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100261 int rv;
262
Neale Ranns8c2dd1b2019-02-19 08:27:34 -0800263 stat_index = ~0;
264
Damjan Mariona9a951f2017-01-16 22:06:10 +0100265#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100266 ipsec_policy_t p;
267
Dave Barachb7b92992018-10-17 10:38:51 -0400268 clib_memset (&p, 0, sizeof (p));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100269
Neale Ranns17dcec02019-01-09 21:22:20 -0800270 p.id = ntohl (mp->entry.spd_id);
271 p.priority = ntohl (mp->entry.priority);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100272
Neale Ranns17dcec02019-01-09 21:22:20 -0800273 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
274 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
275 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
276 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
277
278 p.is_ipv6 = (itype == IP46_TYPE_IP6);
279
280 p.protocol = mp->entry.protocol;
Neale Ranns231c4692019-03-18 17:11:28 +0000281 /* leave the ports in network order */
282 p.rport.start = mp->entry.remote_port_start;
283 p.rport.stop = mp->entry.remote_port_stop;
284 p.lport.start = mp->entry.local_port_start;
285 p.lport.stop = mp->entry.local_port_stop;
Neale Ranns17dcec02019-01-09 21:22:20 -0800286
287 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
288
289 if (rv)
290 goto out;
291
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100292 /* policy action resolve unsupported */
Neale Ranns17dcec02019-01-09 21:22:20 -0800293 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100294 {
295 clib_warning ("unsupported action: 'resolve'");
296 rv = VNET_API_ERROR_UNIMPLEMENTED;
297 goto out;
298 }
Neale Ranns17dcec02019-01-09 21:22:20 -0800299 p.sa_id = ntohl (mp->entry.sa_id);
Neale Ranns9f231d42019-03-19 10:06:00 +0000300 rv =
301 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
302 &p.type);
303 if (rv)
304 goto out;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100305
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800306 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100307 if (rv)
308 goto out;
309
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100310#else
311 rv = VNET_API_ERROR_UNIMPLEMENTED;
312 goto out;
313#endif
314
315out:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800316 /* *INDENT-OFF* */
317 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
318 ({
319 rmp->stat_index = ntohl(stat_index);
320 }));
321 /* *INDENT-ON* */
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100322}
323
Neale Ranns17dcec02019-01-09 21:22:20 -0800324static int
325ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
326{
327 in = clib_net_to_host_u32 (in);
328
329 switch (in)
330 {
331 case IPSEC_API_PROTO_ESP:
332 *out = IPSEC_PROTOCOL_ESP;
333 return (0);
334 case IPSEC_API_PROTO_AH:
335 *out = IPSEC_PROTOCOL_AH;
336 return (0);
337 }
Neale Rannsaf3f0782019-03-26 08:21:25 +0000338 return (VNET_API_ERROR_INVALID_PROTOCOL);
Neale Ranns17dcec02019-01-09 21:22:20 -0800339}
340
Neale Ranns8d7c5022019-02-06 01:41:05 -0800341static vl_api_ipsec_proto_t
342ipsec_proto_encode (ipsec_protocol_t p)
343{
344 switch (p)
345 {
346 case IPSEC_PROTOCOL_ESP:
347 return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP);
348 case IPSEC_PROTOCOL_AH:
349 return clib_host_to_net_u32 (IPSEC_API_PROTO_AH);
350 }
351 return (VNET_API_ERROR_UNIMPLEMENTED);
352}
353
Neale Ranns17dcec02019-01-09 21:22:20 -0800354static int
355ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
356 ipsec_crypto_alg_t * out)
357{
358 in = clib_net_to_host_u32 (in);
359
360 switch (in)
361 {
362#define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
363 *out = IPSEC_CRYPTO_ALG_##f; \
364 return (0);
365 foreach_ipsec_crypto_alg
366#undef _
367 }
Neale Rannsaf3f0782019-03-26 08:21:25 +0000368 return (VNET_API_ERROR_INVALID_ALGORITHM);
Neale Ranns17dcec02019-01-09 21:22:20 -0800369}
370
Neale Ranns8d7c5022019-02-06 01:41:05 -0800371static vl_api_ipsec_crypto_alg_t
372ipsec_crypto_algo_encode (ipsec_crypto_alg_t c)
373{
374 switch (c)
375 {
376#define _(v,f,s) case IPSEC_CRYPTO_ALG_##f: \
377 return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f);
378 foreach_ipsec_crypto_alg
379#undef _
380 case IPSEC_CRYPTO_N_ALG:
381 break;
382 }
383 ASSERT (0);
384 return (VNET_API_ERROR_UNIMPLEMENTED);
385}
386
387
Neale Ranns17dcec02019-01-09 21:22:20 -0800388static int
389ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
390{
391 in = clib_net_to_host_u32 (in);
392
393 switch (in)
394 {
395#define _(v,f,s) case IPSEC_API_INTEG_ALG_##f: \
396 *out = IPSEC_INTEG_ALG_##f; \
397 return (0);
398 foreach_ipsec_integ_alg
399#undef _
400 }
Neale Rannsaf3f0782019-03-26 08:21:25 +0000401 return (VNET_API_ERROR_INVALID_ALGORITHM);
Neale Ranns17dcec02019-01-09 21:22:20 -0800402}
403
Neale Ranns8d7c5022019-02-06 01:41:05 -0800404static vl_api_ipsec_integ_alg_t
405ipsec_integ_algo_encode (ipsec_integ_alg_t i)
Neale Ranns17dcec02019-01-09 21:22:20 -0800406{
Neale Ranns8d7c5022019-02-06 01:41:05 -0800407 switch (i)
408 {
409#define _(v,f,s) case IPSEC_INTEG_ALG_##f: \
410 return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f));
411 foreach_ipsec_integ_alg
412#undef _
413 case IPSEC_INTEG_N_ALG:
414 break;
415 }
416 ASSERT (0);
417 return (VNET_API_ERROR_UNIMPLEMENTED);
Neale Ranns17dcec02019-01-09 21:22:20 -0800418}
419
420static void
Neale Ranns8d7c5022019-02-06 01:41:05 -0800421ipsec_key_decode (const vl_api_key_t * key, ipsec_key_t * out)
Neale Ranns17dcec02019-01-09 21:22:20 -0800422{
Neale Ranns8d7c5022019-02-06 01:41:05 -0800423 ipsec_mk_key (out, key->data, key->length);
424}
425
426static void
427ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out)
428{
429 out->length = in->len;
430 clib_memcpy (out->data, in->data, out->length);
431}
432
433static ipsec_sa_flags_t
434ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in)
435{
436 ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE;
Neale Ranns17dcec02019-01-09 21:22:20 -0800437 in = clib_net_to_host_u32 (in);
438
Neale Rannse524d452019-02-19 15:22:46 +0000439 if (in & IPSEC_API_SAD_FLAG_USE_ESN)
440 flags |= IPSEC_SA_FLAG_USE_ESN;
441 if (in & IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY)
442 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
443 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL)
444 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
445 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL_V6)
446 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
447 if (in & IPSEC_API_SAD_FLAG_UDP_ENCAP)
448 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
449
450 return (flags);
Neale Ranns17dcec02019-01-09 21:22:20 -0800451}
452
Neale Ranns8d7c5022019-02-06 01:41:05 -0800453static vl_api_ipsec_sad_flags_t
454ipsec_sad_flags_encode (const ipsec_sa_t * sa)
455{
456 vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE;
457
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100458 if (ipsec_sa_is_set_USE_ESN (sa))
459 flags |= IPSEC_API_SAD_FLAG_USE_ESN;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100460 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800461 flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100462 if (ipsec_sa_is_set_IS_TUNNEL (sa))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800463 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100464 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800465 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100466 if (ipsec_sa_is_set_UDP_ENCAP (sa))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800467 flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
468
469 return clib_host_to_net_u32 (flags);
470}
Neale Ranns17dcec02019-01-09 21:22:20 -0800471
472static void vl_api_ipsec_sad_entry_add_del_t_handler
473 (vl_api_ipsec_sad_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100474{
475 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
Neale Ranns17dcec02019-01-09 21:22:20 -0800476 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800477 ip46_address_t tun_src = { }, tun_dst =
478 {
479 };
480 ipsec_key_t crypto_key, integ_key;
481 ipsec_crypto_alg_t crypto_alg;
482 ipsec_integ_alg_t integ_alg;
483 ipsec_protocol_t proto;
484 ipsec_sa_flags_t flags;
Dave Baracha5160d72019-03-13 15:29:15 -0400485 u32 id, spi, sa_index = ~0;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100486 int rv;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800487
Damjan Mariona9a951f2017-01-16 22:06:10 +0100488#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100489
Neale Ranns8d7c5022019-02-06 01:41:05 -0800490 id = ntohl (mp->entry.sad_id);
491 spi = ntohl (mp->entry.spi);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100492
Neale Ranns8d7c5022019-02-06 01:41:05 -0800493 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
Neale Ranns17dcec02019-01-09 21:22:20 -0800494
495 if (rv)
496 goto out;
497
Neale Ranns8d7c5022019-02-06 01:41:05 -0800498 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800499
500 if (rv)
501 goto out;
502
Neale Ranns8d7c5022019-02-06 01:41:05 -0800503 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800504
505 if (rv)
506 goto out;
507
Neale Ranns8d7c5022019-02-06 01:41:05 -0800508 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
509 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
Neale Ranns17dcec02019-01-09 21:22:20 -0800510
Neale Ranns8d7c5022019-02-06 01:41:05 -0800511 flags = ipsec_sa_flags_decode (mp->entry.flags);
Neale Ranns17dcec02019-01-09 21:22:20 -0800512
Neale Ranns8d7c5022019-02-06 01:41:05 -0800513 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
514 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100515
Neale Ranns8d7c5022019-02-06 01:41:05 -0800516 if (mp->is_add)
517 rv = ipsec_sa_add (id, spi, proto,
518 crypto_alg, &crypto_key,
519 integ_alg, &integ_key, flags,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000520 0, mp->entry.salt, &tun_src, &tun_dst, &sa_index);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800521 else
522 rv = ipsec_sa_del (id);
523
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100524#else
525 rv = VNET_API_ERROR_UNIMPLEMENTED;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100526#endif
527
528out:
Neale Rannseba31ec2019-02-17 18:04:27 +0000529 /* *INDENT-OFF* */
530 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
531 {
532 rmp->stat_index = htonl (sa_index);
533 });
534 /* *INDENT-ON* */
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100535}
536
537static void
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700538send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
539 u32 context)
540{
541 vl_api_ipsec_spds_details_t *mp;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800542 u32 n_policies = 0;
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700543
544 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400545 clib_memset (mp, 0, sizeof (*mp));
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700546 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
547 mp->context = context;
548
549 mp->spd_id = htonl (spd->id);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800550#define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
551 foreach_ipsec_spd_policy_type
552#undef _
553 mp->npolicies = htonl (n_policies);
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700554
555 vl_api_send_msg (reg, (u8 *) mp);
556}
557
558static void
559vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
560{
561 vl_api_registration_t *reg;
562 ipsec_main_t *im = &ipsec_main;
563 ipsec_spd_t *spd;
564#if WITH_LIBSSL > 0
565 reg = vl_api_client_index_to_registration (mp->client_index);
566 if (!reg)
567 return;
568
569 /* *INDENT-OFF* */
570 pool_foreach (spd, im->spds, ({
571 send_ipsec_spds_details (spd, reg, mp->context);
572 }));
573 /* *INDENT-ON* */
574#else
575 clib_warning ("unimplemented");
576#endif
577}
578
Neale Ranns17dcec02019-01-09 21:22:20 -0800579vl_api_ipsec_spd_action_t
580ipsec_spd_action_encode (ipsec_policy_action_t in)
581{
582 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
583
584 switch (in)
585 {
586#define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
587 out = IPSEC_API_SPD_ACTION_##f; \
588 break;
589 foreach_ipsec_policy_action
590#undef _
591 }
592 return (clib_host_to_net_u32 (out));
593}
594
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700595static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800596send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
597 u32 context)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100598{
599 vl_api_ipsec_spd_details_t *mp;
600
601 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400602 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100603 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
604 mp->context = context;
605
Neale Ranns17dcec02019-01-09 21:22:20 -0800606 mp->entry.spd_id = htonl (p->id);
607 mp->entry.priority = htonl (p->priority);
Neale Ranns9f231d42019-03-19 10:06:00 +0000608 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
609 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
Neale Ranns17dcec02019-01-09 21:22:20 -0800610
611 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
612 &mp->entry.local_address_start);
613 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
614 &mp->entry.local_address_stop);
615 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
616 &mp->entry.remote_address_start);
617 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
618 &mp->entry.remote_address_stop);
Neale Ranns231c4692019-03-18 17:11:28 +0000619 mp->entry.local_port_start = p->lport.start;
620 mp->entry.local_port_stop = p->lport.stop;
621 mp->entry.remote_port_start = p->rport.start;
622 mp->entry.remote_port_stop = p->rport.stop;
Neale Ranns17dcec02019-01-09 21:22:20 -0800623 mp->entry.protocol = p->protocol;
624 mp->entry.policy = ipsec_spd_action_encode (p->policy);
625 mp->entry.sa_id = htonl (p->sa_id);
626
Florin Coras6c4dae22018-01-09 06:39:23 -0800627 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100628}
629
630static void
631vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
632{
Florin Coras6c4dae22018-01-09 06:39:23 -0800633 vl_api_registration_t *reg;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100634 ipsec_main_t *im = &ipsec_main;
Neale Ranns9f231d42019-03-19 10:06:00 +0000635 ipsec_spd_policy_type_t ptype;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100636 ipsec_policy_t *policy;
637 ipsec_spd_t *spd;
638 uword *p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800639 u32 spd_index, *ii;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100640#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800641 reg = vl_api_client_index_to_registration (mp->client_index);
642 if (!reg)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100643 return;
644
645 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
646 if (!p)
647 return;
648
649 spd_index = p[0];
650 spd = pool_elt_at_index (im->spds, spd_index);
651
652 /* *INDENT-OFF* */
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800653 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
654 vec_foreach(ii, spd->policies[ptype])
655 {
656 policy = pool_elt_at_index(im->policies, *ii);
657
658 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
659 send_ipsec_spd_details (policy, reg, mp->context);
660 }
661 }
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100662 /* *INDENT-ON* */
663#else
664 clib_warning ("unimplemented");
665#endif
666}
667
668static void
Filip Varga871bca92018-11-02 13:51:44 +0100669send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
670 u32 sw_if_index, u32 context)
671{
672 vl_api_ipsec_spd_interface_details_t *mp;
673
674 mp = vl_msg_api_alloc (sizeof (*mp));
675 clib_memset (mp, 0, sizeof (*mp));
676 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
677 mp->context = context;
678
679 mp->spd_index = htonl (spd_index);
680 mp->sw_if_index = htonl (sw_if_index);
681
682 vl_api_send_msg (reg, (u8 *) mp);
683}
684
685static void
686vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
687 mp)
688{
689 ipsec_main_t *im = &ipsec_main;
690 vl_api_registration_t *reg;
691 u32 k, v, spd_index;
692
693#if WITH_LIBSSL > 0
694 reg = vl_api_client_index_to_registration (mp->client_index);
695 if (!reg)
696 return;
697
698 if (mp->spd_index_valid)
699 {
700 spd_index = ntohl (mp->spd_index);
701 /* *INDENT-OFF* */
702 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
703 if (v == spd_index)
704 send_ipsec_spd_interface_details(reg, v, k, mp->context);
705 }));
706 /* *INDENT-ON* */
707 }
708 else
709 {
710 /* *INDENT-OFF* */
711 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
712 send_ipsec_spd_interface_details(reg, v, k, mp->context);
713 }));
714 /* *INDENT-ON* */
715 }
716
717#else
718 clib_warning ("unimplemented");
719#endif
720}
721
722static void
Matthew Smithb0972cb2017-05-02 16:20:41 -0500723vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
724 mp)
725{
726 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500727 ipsec_main_t *im = &ipsec_main;
728 vnet_main_t *vnm = im->vnet_main;
729 u32 sw_if_index = ~0;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400730 ip46_type_t itype;
Matthew Smithb0972cb2017-05-02 16:20:41 -0500731 int rv;
732
733#if WITH_LIBSSL > 0
734 ipsec_add_del_tunnel_args_t tun;
735
Dave Barachb7b92992018-10-17 10:38:51 -0400736 clib_memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
Matthew Smithb0972cb2017-05-02 16:20:41 -0500737
738 tun.is_add = mp->is_add;
739 tun.esn = mp->esn;
740 tun.anti_replay = mp->anti_replay;
741 tun.local_spi = ntohl (mp->local_spi);
742 tun.remote_spi = ntohl (mp->remote_spi);
743 tun.crypto_alg = mp->crypto_alg;
744 tun.local_crypto_key_len = mp->local_crypto_key_len;
745 tun.remote_crypto_key_len = mp->remote_crypto_key_len;
746 tun.integ_alg = mp->integ_alg;
747 tun.local_integ_key_len = mp->local_integ_key_len;
748 tun.remote_integ_key_len = mp->remote_integ_key_len;
Filip Tehlarb4a7a7d2018-11-30 07:27:27 -0800749 tun.udp_encap = mp->udp_encap;
Pierre Pfister4c422f92018-12-10 11:19:08 +0100750 tun.tx_table_id = ntohl (mp->tx_table_id);
Neale Ranns80f6fd52019-04-16 02:41:34 +0000751 tun.salt = mp->salt;
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400752 itype = ip_address_decode (&mp->local_ip, &tun.local_ip);
753 itype = ip_address_decode (&mp->remote_ip, &tun.remote_ip);
754 tun.is_ip6 = (IP46_TYPE_IP6 == itype);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500755 memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
756 mp->local_crypto_key_len);
757 memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
758 mp->remote_crypto_key_len);
759 memcpy (&tun.local_integ_key, &mp->local_integ_key,
760 mp->local_integ_key_len);
761 memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
762 mp->remote_integ_key_len);
Matthew Smith8e1039a2018-04-12 07:32:56 -0500763 tun.renumber = mp->renumber;
764 tun.show_instance = ntohl (mp->show_instance);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500765
Matthew Smithe04d09d2017-05-14 21:47:18 -0500766 rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500767
768#else
769 rv = VNET_API_ERROR_UNIMPLEMENTED;
770#endif
771
Neale Ranns8d7c5022019-02-06 01:41:05 -0800772 /* *INDENT-OFF* */
773 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
774 ({
775 rmp->sw_if_index = htonl (sw_if_index);
776 }));
777 /* *INDENT-ON* */
Matthew Smithb0972cb2017-05-02 16:20:41 -0500778}
779
Matthew Smith28029532017-09-26 13:33:44 -0500780static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800781send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
Matthew Smith28029532017-09-26 13:33:44 -0500782 u32 context, u32 sw_if_index)
783{
784 vl_api_ipsec_sa_details_t *mp;
785
786 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400787 clib_memset (mp, 0, sizeof (*mp));
Matthew Smith28029532017-09-26 13:33:44 -0500788 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
789 mp->context = context;
790
Neale Ranns8d7c5022019-02-06 01:41:05 -0800791 mp->entry.sad_id = htonl (sa->id);
792 mp->entry.spi = htonl (sa->spi);
793 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
794 mp->entry.tx_table_id =
795 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
Matthew Smith28029532017-09-26 13:33:44 -0500796
Neale Ranns8d7c5022019-02-06 01:41:05 -0800797 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
798 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
Matthew Smith28029532017-09-26 13:33:44 -0500799
Neale Ranns8d7c5022019-02-06 01:41:05 -0800800 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
801 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
Matthew Smith28029532017-09-26 13:33:44 -0500802
Neale Ranns8d7c5022019-02-06 01:41:05 -0800803 mp->entry.flags = ipsec_sad_flags_encode (sa);
Matthew Smith28029532017-09-26 13:33:44 -0500804
Damjan Mariond709cbc2019-03-26 13:16:42 +0100805 if (ipsec_sa_is_set_IS_TUNNEL (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500806 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800807 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
808 &mp->entry.tunnel_src);
809 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
810 &mp->entry.tunnel_dst);
Matthew Smith28029532017-09-26 13:33:44 -0500811 }
812
Neale Ranns8d7c5022019-02-06 01:41:05 -0800813 mp->sw_if_index = htonl (sw_if_index);
Matthew Smith28029532017-09-26 13:33:44 -0500814 mp->salt = clib_host_to_net_u32 (sa->salt);
815 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
816 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100817 if (ipsec_sa_is_set_USE_ESN (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500818 {
819 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
820 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
821 }
Damjan Mariond709cbc2019-03-26 13:16:42 +0100822 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
Matthew Smith28029532017-09-26 13:33:44 -0500823 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
Pierre Pfister4c422f92018-12-10 11:19:08 +0100824
Florin Coras6c4dae22018-01-09 06:39:23 -0800825 vl_api_send_msg (reg, (u8 *) mp);
Matthew Smith28029532017-09-26 13:33:44 -0500826}
827
828
829static void
830vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
831{
Florin Coras6c4dae22018-01-09 06:39:23 -0800832 vl_api_registration_t *reg;
Matthew Smith28029532017-09-26 13:33:44 -0500833 ipsec_main_t *im = &ipsec_main;
834 vnet_main_t *vnm = im->vnet_main;
835 ipsec_sa_t *sa;
836 ipsec_tunnel_if_t *t;
837 u32 *sa_index_to_tun_if_index = 0;
838
839#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800840 reg = vl_api_client_index_to_registration (mp->client_index);
841 if (!reg || pool_elts (im->sad) == 0)
Matthew Smith28029532017-09-26 13:33:44 -0500842 return;
843
844 vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
845 ~0);
846
847 /* *INDENT-OFF* */
848 pool_foreach (t, im->tunnel_interfaces,
849 ({
850 vnet_hw_interface_t *hi;
851 u32 sw_if_index = ~0;
852
853 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
854 sw_if_index = hi->sw_if_index;
855 sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
856 sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
857 }));
858
859 pool_foreach (sa, im->sad,
860 ({
861 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
Florin Coras6c4dae22018-01-09 06:39:23 -0800862 send_ipsec_sa_details (sa, reg, mp->context,
Matthew Smith28029532017-09-26 13:33:44 -0500863 sa_index_to_tun_if_index[sa - im->sad]);
864 }));
865 /* *INDENT-ON* */
866
867 vec_free (sa_index_to_tun_if_index);
868#else
869 clib_warning ("unimplemented");
870#endif
871}
872
Matthew Smith75d85602017-10-05 19:03:05 -0500873static void
Matthew Smithca514fd2017-10-12 12:06:59 -0500874vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
875{
876 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
877 ipsec_main_t *im = &ipsec_main;
878 vnet_main_t *vnm = im->vnet_main;
879 vnet_sw_interface_t *sw;
880 int rv;
881
882#if WITH_LIBSSL > 0
883 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
884
885 rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
886 mp->is_outbound);
887#else
888 clib_warning ("unimplemented");
889#endif
890
891 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
892}
893
Klement Sekerab4d30532018-11-08 13:00:02 +0100894static void
895vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
896{
897 vl_api_registration_t *rp;
898 ipsec_main_t *im = &ipsec_main;
899 u32 context = mp->context;
900
901 rp = vl_api_client_index_to_registration (mp->client_index);
902
903 if (rp == 0)
904 {
905 clib_warning ("Client %d AWOL", mp->client_index);
906 return;
907 }
908
909 ipsec_ah_backend_t *ab;
910 ipsec_esp_backend_t *eb;
911 /* *INDENT-OFF* */
912 pool_foreach (ab, im->ah_backends, {
913 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
914 clib_memset (mp, 0, sizeof (*mp));
915 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
916 mp->context = context;
917 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
918 ab->name);
Neale Ranns17dcec02019-01-09 21:22:20 -0800919 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
Klement Sekerab4d30532018-11-08 13:00:02 +0100920 mp->index = ab - im->ah_backends;
921 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
922 vl_api_send_msg (rp, (u8 *)mp);
923 });
924 pool_foreach (eb, im->esp_backends, {
925 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
926 clib_memset (mp, 0, sizeof (*mp));
927 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
928 mp->context = context;
929 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
930 eb->name);
Neale Ranns17dcec02019-01-09 21:22:20 -0800931 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
Klement Sekerab4d30532018-11-08 13:00:02 +0100932 mp->index = eb - im->esp_backends;
933 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
934 vl_api_send_msg (rp, (u8 *)mp);
935 });
936 /* *INDENT-ON* */
937}
938
939static void
940vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
941{
942 ipsec_main_t *im = &ipsec_main;
943 vl_api_ipsec_select_backend_reply_t *rmp;
Neale Ranns17dcec02019-01-09 21:22:20 -0800944 ipsec_protocol_t protocol;
Klement Sekerab4d30532018-11-08 13:00:02 +0100945 int rv = 0;
946 if (pool_elts (im->sad) > 0)
947 {
948 rv = VNET_API_ERROR_INSTANCE_IN_USE;
949 goto done;
950 }
Neale Ranns17dcec02019-01-09 21:22:20 -0800951
952 rv = ipsec_proto_decode (mp->protocol, &protocol);
953
954 if (rv)
955 goto done;
956
Klement Sekerab4d30532018-11-08 13:00:02 +0100957#if WITH_LIBSSL > 0
Neale Ranns17dcec02019-01-09 21:22:20 -0800958 switch (protocol)
Klement Sekerab4d30532018-11-08 13:00:02 +0100959 {
960 case IPSEC_PROTOCOL_ESP:
Neale Rannse8915fc2019-04-23 20:57:55 -0400961 rv = ipsec_select_esp_backend (im, mp->index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100962 break;
963 case IPSEC_PROTOCOL_AH:
Neale Rannse8915fc2019-04-23 20:57:55 -0400964 rv = ipsec_select_ah_backend (im, mp->index);
Klement Sekerab4d30532018-11-08 13:00:02 +0100965 break;
966 default:
Neale Rannse8915fc2019-04-23 20:57:55 -0400967 rv = VNET_API_ERROR_INVALID_PROTOCOL;
Klement Sekerab4d30532018-11-08 13:00:02 +0100968 break;
969 }
970#else
971 clib_warning ("unimplemented"); /* FIXME */
972#endif
973done:
974 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
975}
976
Neale Ranns7c44d782019-02-25 10:28:29 +0000977/*
978 * ipsec_api_hookup
979 * Add vpe's API message handlers to the table.
980 * vlib has already mapped shared memory and
981 * added the client registration handlers.
982 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
983 */
984#define vl_msg_name_crc_list
985#include <vnet/vnet_all_api_h.h>
986#undef vl_msg_name_crc_list
987
988static void
989setup_message_id_table (api_main_t * am)
990{
991#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
992 foreach_vl_msg_name_crc_ipsec;
993#undef _
994}
995
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100996static clib_error_t *
997ipsec_api_hookup (vlib_main_t * vm)
998{
999 api_main_t *am = &api_main;
1000
1001#define _(N,n) \
1002 vl_msg_api_set_handlers(VL_API_##N, #n, \
1003 vl_api_##n##_t_handler, \
1004 vl_noop_handler, \
1005 vl_api_##n##_t_endian, \
1006 vl_api_##n##_t_print, \
1007 sizeof(vl_api_##n##_t), 1);
1008 foreach_vpe_api_msg;
1009#undef _
1010
1011 /*
Neale Rannsc87b66c2019-02-07 07:26:12 -08001012 * Adding and deleting SAs is MP safe since when they are added/delete
1013 * no traffic is using them
1014 */
1015 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL] = 1;
1016 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY] = 1;
1017
1018 /*
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001019 * Set up the (msg_name, crc, message-id) table
1020 */
1021 setup_message_id_table (am);
1022
1023 return 0;
1024}
1025
1026VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1027
1028/*
1029 * fd.io coding-style-patch-verification: ON
1030 *
1031 * Local Variables:
1032 * eval: (c-set-style "gnu")
1033 * End:
1034 */