blob: 06f954622c70f1accdf5c6190a6a17f91c8e36a7 [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>
33#include <vnet/ipsec/ikev2.h>
34#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_SET_KEY, ipsec_sa_set_key) \
58_(IPSEC_SA_DUMP, ipsec_sa_dump) \
59_(IPSEC_SPDS_DUMP, ipsec_spds_dump) \
60_(IPSEC_SPD_DUMP, ipsec_spd_dump) \
61_(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \
62_(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \
63_(IPSEC_TUNNEL_IF_SET_KEY, ipsec_tunnel_if_set_key) \
64_(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \
65_(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del) \
66_(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth) \
67_(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id) \
68_(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts) \
69_(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key) \
70_(IKEV2_SET_RESPONDER, ikev2_set_responder) \
71_(IKEV2_SET_IKE_TRANSFORMS, ikev2_set_ike_transforms) \
72_(IKEV2_SET_ESP_TRANSFORMS, ikev2_set_esp_transforms) \
73_(IKEV2_SET_SA_LIFETIME, ikev2_set_sa_lifetime) \
74_(IKEV2_INITIATE_SA_INIT, ikev2_initiate_sa_init) \
75_(IKEV2_INITIATE_DEL_IKE_SA, ikev2_initiate_del_ike_sa) \
76_(IKEV2_INITIATE_DEL_CHILD_SA, ikev2_initiate_del_child_sa) \
77_(IKEV2_INITIATE_REKEY_CHILD_SA, ikev2_initiate_rekey_child_sa) \
78_(IPSEC_SELECT_BACKEND, ipsec_select_backend) \
79_(IPSEC_BACKEND_DUMP, ipsec_backend_dump)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010080
Klement Sekerab4d30532018-11-08 13:00:02 +010081static void
82vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010083{
Damjan Mariona9a951f2017-01-16 22:06:10 +010084#if WITH_LIBSSL == 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010085 clib_warning ("unimplemented");
86#else
87
88 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
89 vl_api_ipsec_spd_add_del_reply_t *rmp;
90 int rv;
91
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010092 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010093
94 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
95#endif
96}
97
98static void vl_api_ipsec_interface_add_del_spd_t_handler
99 (vl_api_ipsec_interface_add_del_spd_t * mp)
100{
101 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
102 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
103 int rv;
104 u32 sw_if_index __attribute__ ((unused));
105 u32 spd_id __attribute__ ((unused));
106
107 sw_if_index = ntohl (mp->sw_if_index);
108 spd_id = ntohl (mp->spd_id);
109
110 VALIDATE_SW_IF_INDEX (mp);
111
Damjan Mariona9a951f2017-01-16 22:06:10 +0100112#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100113 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
114#else
115 rv = VNET_API_ERROR_UNIMPLEMENTED;
116#endif
117
118 BAD_SW_IF_INDEX_LABEL;
119
120 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
121}
122
Neale Ranns17dcec02019-01-09 21:22:20 -0800123static int
124ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
125 ipsec_policy_action_t * out)
126{
127 in = clib_net_to_host_u32 (in);
128
129 switch (in)
130 {
131#define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
132 *out = IPSEC_POLICY_ACTION_##f; \
133 return (0);
134 foreach_ipsec_policy_action
135#undef _
136 }
137 return (VNET_API_ERROR_UNIMPLEMENTED);
138}
139
140static void vl_api_ipsec_spd_entry_add_del_t_handler
141 (vl_api_ipsec_spd_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100142{
143 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
Neale Ranns17dcec02019-01-09 21:22:20 -0800144 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
145 ip46_type_t itype;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800146 u32 stat_index;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100147 int rv;
148
Damjan Mariona9a951f2017-01-16 22:06:10 +0100149#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100150 ipsec_policy_t p;
151
Dave Barachb7b92992018-10-17 10:38:51 -0400152 clib_memset (&p, 0, sizeof (p));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100153
Neale Ranns17dcec02019-01-09 21:22:20 -0800154 p.id = ntohl (mp->entry.spd_id);
155 p.priority = ntohl (mp->entry.priority);
156 p.is_outbound = mp->entry.is_outbound;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100157
Neale Ranns17dcec02019-01-09 21:22:20 -0800158 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
159 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
160 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
161 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
162
163 p.is_ipv6 = (itype == IP46_TYPE_IP6);
164
165 p.protocol = mp->entry.protocol;
166 p.rport.start = ntohs (mp->entry.remote_port_start);
167 p.rport.stop = ntohs (mp->entry.remote_port_stop);
168 p.lport.start = ntohs (mp->entry.local_port_start);
169 p.lport.stop = ntohs (mp->entry.local_port_stop);
170
171 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
172
173 if (rv)
174 goto out;
175
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100176 /* policy action resolve unsupported */
Neale Ranns17dcec02019-01-09 21:22:20 -0800177 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100178 {
179 clib_warning ("unsupported action: 'resolve'");
180 rv = VNET_API_ERROR_UNIMPLEMENTED;
181 goto out;
182 }
Neale Ranns17dcec02019-01-09 21:22:20 -0800183 p.sa_id = ntohl (mp->entry.sa_id);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100184
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800185 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100186 if (rv)
187 goto out;
188
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100189#else
190 rv = VNET_API_ERROR_UNIMPLEMENTED;
191 goto out;
192#endif
193
194out:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800195 /* *INDENT-OFF* */
196 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
197 ({
198 rmp->stat_index = ntohl(stat_index);
199 }));
200 /* *INDENT-ON* */
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100201}
202
Neale Ranns17dcec02019-01-09 21:22:20 -0800203static int
204ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
205{
206 in = clib_net_to_host_u32 (in);
207
208 switch (in)
209 {
210 case IPSEC_API_PROTO_ESP:
211 *out = IPSEC_PROTOCOL_ESP;
212 return (0);
213 case IPSEC_API_PROTO_AH:
214 *out = IPSEC_PROTOCOL_AH;
215 return (0);
216 }
217 return (VNET_API_ERROR_UNIMPLEMENTED);
218}
219
220static int
221ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
222 ipsec_crypto_alg_t * out)
223{
224 in = clib_net_to_host_u32 (in);
225
226 switch (in)
227 {
228#define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
229 *out = IPSEC_CRYPTO_ALG_##f; \
230 return (0);
231 foreach_ipsec_crypto_alg
232#undef _
233 }
234 return (VNET_API_ERROR_UNIMPLEMENTED);
235}
236
237static int
238ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
239{
240 in = clib_net_to_host_u32 (in);
241
242 switch (in)
243 {
244#define _(v,f,s) case IPSEC_API_INTEG_ALG_##f: \
245 *out = IPSEC_INTEG_ALG_##f; \
246 return (0);
247 foreach_ipsec_integ_alg
248#undef _
249 }
250 return (VNET_API_ERROR_UNIMPLEMENTED);
251}
252
253static void
254vl_api_ipsec_key_decode (const vl_api_key_t * key, u8 * len, u8 out[128])
255{
256 *len = key->length;
257 clib_memcpy (out, key->data, key->length);
258}
259
260static void
261vl_api_ipsec_sad_flags_decode (vl_api_ipsec_sad_flags_t in, ipsec_sa_t * sa)
262{
263 in = clib_net_to_host_u32 (in);
264
265 if (in & IPSEC_API_SAD_FLAG_USE_EXTENDED_SEQ_NUM)
266 sa->use_esn = 1;
267 if (in & IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY)
268 sa->use_anti_replay = 1;
269 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL)
270 sa->is_tunnel = 1;
271 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL_V6)
272 sa->is_tunnel_ip6 = 1;
273 if (in & IPSEC_API_SAD_FLAG_UDP_ENCAP)
274 sa->udp_encap = 1;
275}
276
277
278static void vl_api_ipsec_sad_entry_add_del_t_handler
279 (vl_api_ipsec_sad_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100280{
281 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
Neale Ranns17dcec02019-01-09 21:22:20 -0800282 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100283 int rv;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100284#if WITH_LIBSSL > 0
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000285 ipsec_main_t *im = &ipsec_main;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100286 ipsec_sa_t sa;
287
Dave Barachb7b92992018-10-17 10:38:51 -0400288 clib_memset (&sa, 0, sizeof (sa));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100289
Neale Ranns17dcec02019-01-09 21:22:20 -0800290 sa.id = ntohl (mp->entry.sad_id);
291 sa.spi = ntohl (mp->entry.spi);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100292
Neale Ranns17dcec02019-01-09 21:22:20 -0800293 rv = ipsec_proto_decode (mp->entry.protocol, &sa.protocol);
294
295 if (rv)
296 goto out;
297
298 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &sa.crypto_alg);
299
300 if (rv)
301 goto out;
302
303 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &sa.integ_alg);
304
305 if (rv)
306 goto out;
307
308 vl_api_ipsec_key_decode (&mp->entry.crypto_key,
309 &sa.crypto_key_len, sa.crypto_key);
310 vl_api_ipsec_key_decode (&mp->entry.integrity_key,
311 &sa.integ_key_len, sa.integ_key);
312
313 vl_api_ipsec_sad_flags_decode (mp->entry.flags, &sa);
314
315 if (sa.is_tunnel_ip6 || sa.is_tunnel)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100316 {
Neale Ranns17dcec02019-01-09 21:22:20 -0800317 ip_address_decode (&mp->entry.tunnel_src, &sa.tunnel_src_addr);
318 ip_address_decode (&mp->entry.tunnel_dst, &sa.tunnel_dst_addr);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100319 }
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100320
Klement Sekerab4d30532018-11-08 13:00:02 +0100321 clib_error_t *err = ipsec_check_support_cb (im, &sa);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000322 if (err)
323 {
324 clib_warning ("%s", err->what);
325 rv = VNET_API_ERROR_UNIMPLEMENTED;
326 goto out;
327 }
328
Radu Nicolau717de092018-08-03 10:37:24 +0100329 rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100330#else
331 rv = VNET_API_ERROR_UNIMPLEMENTED;
332 goto out;
333#endif
334
335out:
Neale Ranns17dcec02019-01-09 21:22:20 -0800336 REPLY_MACRO (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100337}
338
339static void
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700340send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
341 u32 context)
342{
343 vl_api_ipsec_spds_details_t *mp;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800344 u32 n_policies = 0;
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700345
346 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400347 clib_memset (mp, 0, sizeof (*mp));
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700348 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
349 mp->context = context;
350
351 mp->spd_id = htonl (spd->id);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800352#define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
353 foreach_ipsec_spd_policy_type
354#undef _
355 mp->npolicies = htonl (n_policies);
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700356
357 vl_api_send_msg (reg, (u8 *) mp);
358}
359
360static void
361vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
362{
363 vl_api_registration_t *reg;
364 ipsec_main_t *im = &ipsec_main;
365 ipsec_spd_t *spd;
366#if WITH_LIBSSL > 0
367 reg = vl_api_client_index_to_registration (mp->client_index);
368 if (!reg)
369 return;
370
371 /* *INDENT-OFF* */
372 pool_foreach (spd, im->spds, ({
373 send_ipsec_spds_details (spd, reg, mp->context);
374 }));
375 /* *INDENT-ON* */
376#else
377 clib_warning ("unimplemented");
378#endif
379}
380
Neale Ranns17dcec02019-01-09 21:22:20 -0800381vl_api_ipsec_spd_action_t
382ipsec_spd_action_encode (ipsec_policy_action_t in)
383{
384 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
385
386 switch (in)
387 {
388#define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
389 out = IPSEC_API_SPD_ACTION_##f; \
390 break;
391 foreach_ipsec_policy_action
392#undef _
393 }
394 return (clib_host_to_net_u32 (out));
395}
396
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700397static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800398send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
399 u32 context)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100400{
401 vl_api_ipsec_spd_details_t *mp;
402
403 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400404 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100405 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
406 mp->context = context;
407
Neale Ranns17dcec02019-01-09 21:22:20 -0800408 mp->entry.spd_id = htonl (p->id);
409 mp->entry.priority = htonl (p->priority);
410 mp->entry.is_outbound = p->is_outbound;
411
412 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
413 &mp->entry.local_address_start);
414 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
415 &mp->entry.local_address_stop);
416 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
417 &mp->entry.remote_address_start);
418 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
419 &mp->entry.remote_address_stop);
420 mp->entry.local_port_start = htons (p->lport.start);
421 mp->entry.local_port_stop = htons (p->lport.stop);
422 mp->entry.remote_port_start = htons (p->rport.start);
423 mp->entry.remote_port_stop = htons (p->rport.stop);
424 mp->entry.protocol = p->protocol;
425 mp->entry.policy = ipsec_spd_action_encode (p->policy);
426 mp->entry.sa_id = htonl (p->sa_id);
427
Florin Coras6c4dae22018-01-09 06:39:23 -0800428 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100429}
430
431static void
432vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
433{
Florin Coras6c4dae22018-01-09 06:39:23 -0800434 vl_api_registration_t *reg;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100435 ipsec_main_t *im = &ipsec_main;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800436 ipsec_spd_policy_t ptype;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100437 ipsec_policy_t *policy;
438 ipsec_spd_t *spd;
439 uword *p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800440 u32 spd_index, *ii;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100441#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800442 reg = vl_api_client_index_to_registration (mp->client_index);
443 if (!reg)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100444 return;
445
446 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
447 if (!p)
448 return;
449
450 spd_index = p[0];
451 spd = pool_elt_at_index (im->spds, spd_index);
452
453 /* *INDENT-OFF* */
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800454 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
455 vec_foreach(ii, spd->policies[ptype])
456 {
457 policy = pool_elt_at_index(im->policies, *ii);
458
459 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
460 send_ipsec_spd_details (policy, reg, mp->context);
461 }
462 }
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100463 /* *INDENT-ON* */
464#else
465 clib_warning ("unimplemented");
466#endif
467}
468
469static void
Filip Varga871bca92018-11-02 13:51:44 +0100470send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
471 u32 sw_if_index, u32 context)
472{
473 vl_api_ipsec_spd_interface_details_t *mp;
474
475 mp = vl_msg_api_alloc (sizeof (*mp));
476 clib_memset (mp, 0, sizeof (*mp));
477 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
478 mp->context = context;
479
480 mp->spd_index = htonl (spd_index);
481 mp->sw_if_index = htonl (sw_if_index);
482
483 vl_api_send_msg (reg, (u8 *) mp);
484}
485
486static void
487vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
488 mp)
489{
490 ipsec_main_t *im = &ipsec_main;
491 vl_api_registration_t *reg;
492 u32 k, v, spd_index;
493
494#if WITH_LIBSSL > 0
495 reg = vl_api_client_index_to_registration (mp->client_index);
496 if (!reg)
497 return;
498
499 if (mp->spd_index_valid)
500 {
501 spd_index = ntohl (mp->spd_index);
502 /* *INDENT-OFF* */
503 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
504 if (v == spd_index)
505 send_ipsec_spd_interface_details(reg, v, k, mp->context);
506 }));
507 /* *INDENT-ON* */
508 }
509 else
510 {
511 /* *INDENT-OFF* */
512 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
513 send_ipsec_spd_interface_details(reg, v, k, mp->context);
514 }));
515 /* *INDENT-ON* */
516 }
517
518#else
519 clib_warning ("unimplemented");
520#endif
521}
522
523static void
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100524vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
525{
526 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
527 vl_api_ipsec_sa_set_key_reply_t *rmp;
528 int rv;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100529#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100530 ipsec_sa_t sa;
531 sa.id = ntohl (mp->sa_id);
Neale Ranns17dcec02019-01-09 21:22:20 -0800532
533 vl_api_ipsec_key_decode (&mp->crypto_key,
534 &sa.crypto_key_len, sa.crypto_key);
535 vl_api_ipsec_key_decode (&mp->integrity_key,
536 &sa.integ_key_len, sa.integ_key);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100537
538 rv = ipsec_set_sa_key (vm, &sa);
539#else
540 rv = VNET_API_ERROR_UNIMPLEMENTED;
541#endif
542
543 REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
544}
545
546static void
Matthew Smithb0972cb2017-05-02 16:20:41 -0500547vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
548 mp)
549{
550 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500551 ipsec_main_t *im = &ipsec_main;
552 vnet_main_t *vnm = im->vnet_main;
553 u32 sw_if_index = ~0;
Matthew Smithb0972cb2017-05-02 16:20:41 -0500554 int rv;
555
556#if WITH_LIBSSL > 0
557 ipsec_add_del_tunnel_args_t tun;
558
Dave Barachb7b92992018-10-17 10:38:51 -0400559 clib_memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
Matthew Smithb0972cb2017-05-02 16:20:41 -0500560
561 tun.is_add = mp->is_add;
562 tun.esn = mp->esn;
563 tun.anti_replay = mp->anti_replay;
564 tun.local_spi = ntohl (mp->local_spi);
565 tun.remote_spi = ntohl (mp->remote_spi);
566 tun.crypto_alg = mp->crypto_alg;
567 tun.local_crypto_key_len = mp->local_crypto_key_len;
568 tun.remote_crypto_key_len = mp->remote_crypto_key_len;
569 tun.integ_alg = mp->integ_alg;
570 tun.local_integ_key_len = mp->local_integ_key_len;
571 tun.remote_integ_key_len = mp->remote_integ_key_len;
Filip Tehlarb4a7a7d2018-11-30 07:27:27 -0800572 tun.udp_encap = mp->udp_encap;
Pierre Pfister4c422f92018-12-10 11:19:08 +0100573 tun.tx_table_id = ntohl (mp->tx_table_id);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500574 memcpy (&tun.local_ip, mp->local_ip, 4);
575 memcpy (&tun.remote_ip, mp->remote_ip, 4);
576 memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
577 mp->local_crypto_key_len);
578 memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
579 mp->remote_crypto_key_len);
580 memcpy (&tun.local_integ_key, &mp->local_integ_key,
581 mp->local_integ_key_len);
582 memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
583 mp->remote_integ_key_len);
Matthew Smith8e1039a2018-04-12 07:32:56 -0500584 tun.renumber = mp->renumber;
585 tun.show_instance = ntohl (mp->show_instance);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500586
Matthew Smithe04d09d2017-05-14 21:47:18 -0500587 rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500588
589#else
590 rv = VNET_API_ERROR_UNIMPLEMENTED;
591#endif
592
Matthew Smithe04d09d2017-05-14 21:47:18 -0500593 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY, (
594 {
595 rmp->sw_if_index =
596 htonl (sw_if_index);
597 }));
Matthew Smithb0972cb2017-05-02 16:20:41 -0500598}
599
Matthew Smith28029532017-09-26 13:33:44 -0500600static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800601send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
Matthew Smith28029532017-09-26 13:33:44 -0500602 u32 context, u32 sw_if_index)
603{
604 vl_api_ipsec_sa_details_t *mp;
605
606 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400607 clib_memset (mp, 0, sizeof (*mp));
Matthew Smith28029532017-09-26 13:33:44 -0500608 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
609 mp->context = context;
610
611 mp->sa_id = htonl (sa->id);
612 mp->sw_if_index = htonl (sw_if_index);
613
614 mp->spi = htonl (sa->spi);
615 mp->protocol = sa->protocol;
616
617 mp->crypto_alg = sa->crypto_alg;
618 mp->crypto_key_len = sa->crypto_key_len;
619 memcpy (mp->crypto_key, sa->crypto_key, sa->crypto_key_len);
620
621 mp->integ_alg = sa->integ_alg;
622 mp->integ_key_len = sa->integ_key_len;
623 memcpy (mp->integ_key, sa->integ_key, sa->integ_key_len);
624
625 mp->use_esn = sa->use_esn;
626 mp->use_anti_replay = sa->use_anti_replay;
627
628 mp->is_tunnel = sa->is_tunnel;
629 mp->is_tunnel_ip6 = sa->is_tunnel_ip6;
630
631 if (sa->is_tunnel)
632 {
633 if (sa->is_tunnel_ip6)
634 {
635 memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip6, 16);
636 memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip6, 16);
637 }
638 else
639 {
640 memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip4, 4);
641 memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip4, 4);
642 }
643 }
644
645 mp->salt = clib_host_to_net_u32 (sa->salt);
646 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
647 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
648 if (sa->use_esn)
649 {
650 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
651 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
652 }
653 if (sa->use_anti_replay)
654 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
655 mp->total_data_size = clib_host_to_net_u64 (sa->total_data_size);
Klement Sekera4b089f22018-04-17 18:04:57 +0200656 mp->udp_encap = sa->udp_encap;
Matthew Smith28029532017-09-26 13:33:44 -0500657
Pierre Pfister4c422f92018-12-10 11:19:08 +0100658 mp->tx_table_id =
659 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
660
Florin Coras6c4dae22018-01-09 06:39:23 -0800661 vl_api_send_msg (reg, (u8 *) mp);
Matthew Smith28029532017-09-26 13:33:44 -0500662}
663
664
665static void
666vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
667{
Florin Coras6c4dae22018-01-09 06:39:23 -0800668 vl_api_registration_t *reg;
Matthew Smith28029532017-09-26 13:33:44 -0500669 ipsec_main_t *im = &ipsec_main;
670 vnet_main_t *vnm = im->vnet_main;
671 ipsec_sa_t *sa;
672 ipsec_tunnel_if_t *t;
673 u32 *sa_index_to_tun_if_index = 0;
674
675#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800676 reg = vl_api_client_index_to_registration (mp->client_index);
677 if (!reg || pool_elts (im->sad) == 0)
Matthew Smith28029532017-09-26 13:33:44 -0500678 return;
679
680 vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
681 ~0);
682
683 /* *INDENT-OFF* */
684 pool_foreach (t, im->tunnel_interfaces,
685 ({
686 vnet_hw_interface_t *hi;
687 u32 sw_if_index = ~0;
688
689 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
690 sw_if_index = hi->sw_if_index;
691 sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
692 sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
693 }));
694
695 pool_foreach (sa, im->sad,
696 ({
697 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
Florin Coras6c4dae22018-01-09 06:39:23 -0800698 send_ipsec_sa_details (sa, reg, mp->context,
Matthew Smith28029532017-09-26 13:33:44 -0500699 sa_index_to_tun_if_index[sa - im->sad]);
700 }));
701 /* *INDENT-ON* */
702
703 vec_free (sa_index_to_tun_if_index);
704#else
705 clib_warning ("unimplemented");
706#endif
707}
708
Matthew Smithe04d09d2017-05-14 21:47:18 -0500709
Matthew Smithb0972cb2017-05-02 16:20:41 -0500710static void
Matthew Smith75d85602017-10-05 19:03:05 -0500711vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t *
712 mp)
713{
714 vl_api_ipsec_tunnel_if_set_key_reply_t *rmp;
715 ipsec_main_t *im = &ipsec_main;
716 vnet_main_t *vnm = im->vnet_main;
717 vnet_sw_interface_t *sw;
718 u8 *key = 0;
719 int rv;
720
721#if WITH_LIBSSL > 0
722 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
723
724 switch (mp->key_type)
725 {
726 case IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO:
727 case IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO:
728 if (mp->alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
Dave Baracha8d47642018-07-13 11:22:23 -0400729 mp->alg >= IPSEC_CRYPTO_N_ALG)
Matthew Smith75d85602017-10-05 19:03:05 -0500730 {
731 rv = VNET_API_ERROR_UNIMPLEMENTED;
732 goto out;
733 }
734 break;
735 case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG:
736 case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG:
Dave Baracha8d47642018-07-13 11:22:23 -0400737 if (mp->alg >= IPSEC_INTEG_N_ALG)
Matthew Smith75d85602017-10-05 19:03:05 -0500738 {
739 rv = VNET_API_ERROR_UNIMPLEMENTED;
740 goto out;
741 }
742 break;
743 case IPSEC_IF_SET_KEY_TYPE_NONE:
744 default:
745 rv = VNET_API_ERROR_UNIMPLEMENTED;
746 goto out;
747 break;
748 }
749
750 key = vec_new (u8, mp->key_len);
751 clib_memcpy (key, mp->key, mp->key_len);
752
753 rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg,
754 key);
755 vec_free (key);
756#else
757 clib_warning ("unimplemented");
758#endif
759
760out:
761 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY);
762}
763
764
765static void
Matthew Smithca514fd2017-10-12 12:06:59 -0500766vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
767{
768 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
769 ipsec_main_t *im = &ipsec_main;
770 vnet_main_t *vnm = im->vnet_main;
771 vnet_sw_interface_t *sw;
772 int rv;
773
774#if WITH_LIBSSL > 0
775 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
776
777 rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
778 mp->is_outbound);
779#else
780 clib_warning ("unimplemented");
781#endif
782
783 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
784}
785
786
787static void
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100788vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
789{
790 vl_api_ikev2_profile_add_del_reply_t *rmp;
791 int rv = 0;
792
Damjan Mariona9a951f2017-01-16 22:06:10 +0100793#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100794 vlib_main_t *vm = vlib_get_main ();
795 clib_error_t *error;
796 u8 *tmp = format (0, "%s", mp->name);
797 error = ikev2_add_del_profile (vm, tmp, mp->is_add);
798 vec_free (tmp);
799 if (error)
800 rv = VNET_API_ERROR_UNSPECIFIED;
801#else
802 rv = VNET_API_ERROR_UNIMPLEMENTED;
803#endif
804
805 REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
806}
807
808static void
809 vl_api_ikev2_profile_set_auth_t_handler
810 (vl_api_ikev2_profile_set_auth_t * mp)
811{
812 vl_api_ikev2_profile_set_auth_reply_t *rmp;
813 int rv = 0;
814
Damjan Mariona9a951f2017-01-16 22:06:10 +0100815#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100816 vlib_main_t *vm = vlib_get_main ();
817 clib_error_t *error;
Michal Cmaradae67c1d82019-01-18 12:51:08 +0100818 int data_len = ntohl (mp->data_len);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100819 u8 *tmp = format (0, "%s", mp->name);
Michal Cmaradae67c1d82019-01-18 12:51:08 +0100820 u8 *data = vec_new (u8, data_len);
821 clib_memcpy (data, mp->data, data_len);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100822 error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
823 vec_free (tmp);
824 vec_free (data);
825 if (error)
826 rv = VNET_API_ERROR_UNSPECIFIED;
827#else
828 rv = VNET_API_ERROR_UNIMPLEMENTED;
829#endif
830
831 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
832}
833
834static void
835vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
836{
837 vl_api_ikev2_profile_add_del_reply_t *rmp;
838 int rv = 0;
839
Damjan Mariona9a951f2017-01-16 22:06:10 +0100840#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100841 vlib_main_t *vm = vlib_get_main ();
842 clib_error_t *error;
843 u8 *tmp = format (0, "%s", mp->name);
Michal Cmaradae67c1d82019-01-18 12:51:08 +0100844 int data_len = ntohl (mp->data_len);
845 u8 *data = vec_new (u8, data_len);
846 clib_memcpy (data, mp->data, data_len);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100847 error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
848 vec_free (tmp);
849 vec_free (data);
850 if (error)
851 rv = VNET_API_ERROR_UNSPECIFIED;
852#else
853 rv = VNET_API_ERROR_UNIMPLEMENTED;
854#endif
855
856 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
857}
858
859static void
860vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
861{
862 vl_api_ikev2_profile_set_ts_reply_t *rmp;
863 int rv = 0;
864
Damjan Mariona9a951f2017-01-16 22:06:10 +0100865#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100866 vlib_main_t *vm = vlib_get_main ();
867 clib_error_t *error;
868 u8 *tmp = format (0, "%s", mp->name);
869 error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
870 mp->end_port, (ip4_address_t) mp->start_addr,
871 (ip4_address_t) mp->end_addr, mp->is_local);
872 vec_free (tmp);
873 if (error)
874 rv = VNET_API_ERROR_UNSPECIFIED;
875#else
876 rv = VNET_API_ERROR_UNIMPLEMENTED;
877#endif
878
879 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
880}
881
882static void
883vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
884{
885 vl_api_ikev2_profile_set_ts_reply_t *rmp;
886 int rv = 0;
887
Damjan Mariona9a951f2017-01-16 22:06:10 +0100888#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100889 vlib_main_t *vm = vlib_get_main ();
890 clib_error_t *error;
891
892 error = ikev2_set_local_key (vm, mp->key_file);
893 if (error)
894 rv = VNET_API_ERROR_UNSPECIFIED;
895#else
896 rv = VNET_API_ERROR_UNIMPLEMENTED;
897#endif
898
899 REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
900}
901
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000902static void
903vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
904{
905 vl_api_ikev2_set_responder_reply_t *rmp;
906 int rv = 0;
907
908#if WITH_LIBSSL > 0
909 vlib_main_t *vm = vlib_get_main ();
910 clib_error_t *error;
911
912 u8 *tmp = format (0, "%s", mp->name);
913 ip4_address_t ip4;
914 clib_memcpy (&ip4, mp->address, sizeof (ip4));
915
916 error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
917 vec_free (tmp);
918 if (error)
919 rv = VNET_API_ERROR_UNSPECIFIED;
920#else
921 rv = VNET_API_ERROR_UNIMPLEMENTED;
922#endif
923
924 REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
925}
926
927static void
928vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
929 mp)
930{
931 vl_api_ikev2_set_ike_transforms_reply_t *rmp;
932 int rv = 0;
933
934#if WITH_LIBSSL > 0
935 vlib_main_t *vm = vlib_get_main ();
936 clib_error_t *error;
937
938 u8 *tmp = format (0, "%s", mp->name);
939
940 error =
941 ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
942 mp->dh_group, mp->crypto_key_size);
943 vec_free (tmp);
944 if (error)
945 rv = VNET_API_ERROR_UNSPECIFIED;
946#else
947 rv = VNET_API_ERROR_UNIMPLEMENTED;
948#endif
949
950 REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
951}
952
953static void
954vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
955 mp)
956{
957 vl_api_ikev2_set_esp_transforms_reply_t *rmp;
958 int rv = 0;
959
960#if WITH_LIBSSL > 0
961 vlib_main_t *vm = vlib_get_main ();
962 clib_error_t *error;
963
964 u8 *tmp = format (0, "%s", mp->name);
965
966 error =
967 ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
968 mp->dh_group, mp->crypto_key_size);
969 vec_free (tmp);
970 if (error)
971 rv = VNET_API_ERROR_UNSPECIFIED;
972#else
973 rv = VNET_API_ERROR_UNIMPLEMENTED;
974#endif
975
976 REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
977}
978
979static void
980vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
981{
982 vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
983 int rv = 0;
984
985#if WITH_LIBSSL > 0
986 vlib_main_t *vm = vlib_get_main ();
987 clib_error_t *error;
988
989 u8 *tmp = format (0, "%s", mp->name);
990
991 error =
992 ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
993 mp->handover, mp->lifetime_maxdata);
994 vec_free (tmp);
995 if (error)
996 rv = VNET_API_ERROR_UNSPECIFIED;
997#else
998 rv = VNET_API_ERROR_UNIMPLEMENTED;
999#endif
1000
1001 REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
1002}
1003
1004static void
1005vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
1006{
1007 vl_api_ikev2_initiate_sa_init_reply_t *rmp;
1008 int rv = 0;
1009
1010#if WITH_LIBSSL > 0
1011 vlib_main_t *vm = vlib_get_main ();
1012 clib_error_t *error;
1013
1014 u8 *tmp = format (0, "%s", mp->name);
1015
1016 error = ikev2_initiate_sa_init (vm, tmp);
1017 vec_free (tmp);
1018 if (error)
1019 rv = VNET_API_ERROR_UNSPECIFIED;
1020#else
1021 rv = VNET_API_ERROR_UNIMPLEMENTED;
1022#endif
1023
1024 REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
1025}
1026
1027static void
1028vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
1029 * mp)
1030{
1031 vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
1032 int rv = 0;
1033
1034#if WITH_LIBSSL > 0
1035 vlib_main_t *vm = vlib_get_main ();
1036 clib_error_t *error;
1037
1038 error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
1039 if (error)
1040 rv = VNET_API_ERROR_UNSPECIFIED;
1041#else
1042 rv = VNET_API_ERROR_UNIMPLEMENTED;
1043#endif
1044
1045 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
1046}
1047
1048static void
1049 vl_api_ikev2_initiate_del_child_sa_t_handler
1050 (vl_api_ikev2_initiate_del_child_sa_t * mp)
1051{
1052 vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
1053 int rv = 0;
1054
1055#if WITH_LIBSSL > 0
1056 vlib_main_t *vm = vlib_get_main ();
1057 clib_error_t *error;
1058
1059 error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
1060 if (error)
1061 rv = VNET_API_ERROR_UNSPECIFIED;
1062#else
1063 rv = VNET_API_ERROR_UNIMPLEMENTED;
1064#endif
1065
1066 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
1067}
1068
1069static void
1070 vl_api_ikev2_initiate_rekey_child_sa_t_handler
1071 (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
1072{
1073 vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
1074 int rv = 0;
1075
1076#if WITH_LIBSSL > 0
1077 vlib_main_t *vm = vlib_get_main ();
1078 clib_error_t *error;
1079
1080 error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
1081 if (error)
1082 rv = VNET_API_ERROR_UNSPECIFIED;
1083#else
1084 rv = VNET_API_ERROR_UNIMPLEMENTED;
1085#endif
1086
1087 REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
1088}
1089
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001090/*
1091 * ipsec_api_hookup
1092 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001093 * vlib has already mapped shared memory and
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001094 * added the client registration handlers.
1095 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1096 */
1097#define vl_msg_name_crc_list
1098#include <vnet/vnet_all_api_h.h>
1099#undef vl_msg_name_crc_list
1100
1101static void
1102setup_message_id_table (api_main_t * am)
1103{
1104#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1105 foreach_vl_msg_name_crc_ipsec;
1106#undef _
1107}
1108
Klement Sekerab4d30532018-11-08 13:00:02 +01001109static void
1110vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
1111{
1112 vl_api_registration_t *rp;
1113 ipsec_main_t *im = &ipsec_main;
1114 u32 context = mp->context;
1115
1116 rp = vl_api_client_index_to_registration (mp->client_index);
1117
1118 if (rp == 0)
1119 {
1120 clib_warning ("Client %d AWOL", mp->client_index);
1121 return;
1122 }
1123
1124 ipsec_ah_backend_t *ab;
1125 ipsec_esp_backend_t *eb;
1126 /* *INDENT-OFF* */
1127 pool_foreach (ab, im->ah_backends, {
1128 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1129 clib_memset (mp, 0, sizeof (*mp));
1130 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1131 mp->context = context;
1132 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1133 ab->name);
Neale Ranns17dcec02019-01-09 21:22:20 -08001134 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
Klement Sekerab4d30532018-11-08 13:00:02 +01001135 mp->index = ab - im->ah_backends;
1136 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1137 vl_api_send_msg (rp, (u8 *)mp);
1138 });
1139 pool_foreach (eb, im->esp_backends, {
1140 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1141 clib_memset (mp, 0, sizeof (*mp));
1142 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1143 mp->context = context;
1144 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1145 eb->name);
Neale Ranns17dcec02019-01-09 21:22:20 -08001146 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
Klement Sekerab4d30532018-11-08 13:00:02 +01001147 mp->index = eb - im->esp_backends;
1148 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1149 vl_api_send_msg (rp, (u8 *)mp);
1150 });
1151 /* *INDENT-ON* */
1152}
1153
1154static void
1155vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1156{
1157 ipsec_main_t *im = &ipsec_main;
1158 vl_api_ipsec_select_backend_reply_t *rmp;
Neale Ranns17dcec02019-01-09 21:22:20 -08001159 ipsec_protocol_t protocol;
Klement Sekerab4d30532018-11-08 13:00:02 +01001160 int rv = 0;
1161 if (pool_elts (im->sad) > 0)
1162 {
1163 rv = VNET_API_ERROR_INSTANCE_IN_USE;
1164 goto done;
1165 }
Neale Ranns17dcec02019-01-09 21:22:20 -08001166
1167 rv = ipsec_proto_decode (mp->protocol, &protocol);
1168
1169 if (rv)
1170 goto done;
1171
Klement Sekerab4d30532018-11-08 13:00:02 +01001172#if WITH_LIBSSL > 0
Neale Ranns17dcec02019-01-09 21:22:20 -08001173 switch (protocol)
Klement Sekerab4d30532018-11-08 13:00:02 +01001174 {
1175 case IPSEC_PROTOCOL_ESP:
1176 if (pool_is_free_index (im->esp_backends, mp->index))
1177 {
1178 rv = VNET_API_ERROR_INVALID_VALUE;
1179 break;
1180 }
1181 ipsec_select_esp_backend (im, mp->index);
1182 break;
1183 case IPSEC_PROTOCOL_AH:
1184 if (pool_is_free_index (im->ah_backends, mp->index))
1185 {
1186 rv = VNET_API_ERROR_INVALID_VALUE;
1187 break;
1188 }
1189 ipsec_select_ah_backend (im, mp->index);
1190 break;
1191 default:
1192 rv = VNET_API_ERROR_INVALID_VALUE;
1193 break;
1194 }
1195#else
1196 clib_warning ("unimplemented"); /* FIXME */
1197#endif
1198done:
1199 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1200}
1201
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001202static clib_error_t *
1203ipsec_api_hookup (vlib_main_t * vm)
1204{
1205 api_main_t *am = &api_main;
1206
1207#define _(N,n) \
1208 vl_msg_api_set_handlers(VL_API_##N, #n, \
1209 vl_api_##n##_t_handler, \
1210 vl_noop_handler, \
1211 vl_api_##n##_t_endian, \
1212 vl_api_##n##_t_print, \
1213 sizeof(vl_api_##n##_t), 1);
1214 foreach_vpe_api_msg;
1215#undef _
1216
1217 /*
1218 * Set up the (msg_name, crc, message-id) table
1219 */
1220 setup_message_id_table (am);
1221
1222 return 0;
1223}
1224
1225VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1226
1227/*
1228 * fd.io coding-style-patch-verification: ON
1229 *
1230 * Local Variables:
1231 * eval: (c-set-style "gnu")
1232 * End:
1233 */