blob: 4b4ce4e1770cf1037b8483ad3bcff601351ea2b7 [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
Neale Ranns8c2dd1b2019-02-19 08:27:34 -0800149 stat_index = ~0;
150
Damjan Mariona9a951f2017-01-16 22:06:10 +0100151#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100152 ipsec_policy_t p;
153
Dave Barachb7b92992018-10-17 10:38:51 -0400154 clib_memset (&p, 0, sizeof (p));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100155
Neale Ranns17dcec02019-01-09 21:22:20 -0800156 p.id = ntohl (mp->entry.spd_id);
157 p.priority = ntohl (mp->entry.priority);
158 p.is_outbound = mp->entry.is_outbound;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100159
Neale Ranns17dcec02019-01-09 21:22:20 -0800160 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
161 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
162 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
163 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
164
165 p.is_ipv6 = (itype == IP46_TYPE_IP6);
166
167 p.protocol = mp->entry.protocol;
168 p.rport.start = ntohs (mp->entry.remote_port_start);
169 p.rport.stop = ntohs (mp->entry.remote_port_stop);
170 p.lport.start = ntohs (mp->entry.local_port_start);
171 p.lport.stop = ntohs (mp->entry.local_port_stop);
172
173 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
174
175 if (rv)
176 goto out;
177
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100178 /* policy action resolve unsupported */
Neale Ranns17dcec02019-01-09 21:22:20 -0800179 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100180 {
181 clib_warning ("unsupported action: 'resolve'");
182 rv = VNET_API_ERROR_UNIMPLEMENTED;
183 goto out;
184 }
Neale Ranns17dcec02019-01-09 21:22:20 -0800185 p.sa_id = ntohl (mp->entry.sa_id);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100186
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800187 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100188 if (rv)
189 goto out;
190
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100191#else
192 rv = VNET_API_ERROR_UNIMPLEMENTED;
193 goto out;
194#endif
195
196out:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800197 /* *INDENT-OFF* */
198 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
199 ({
200 rmp->stat_index = ntohl(stat_index);
201 }));
202 /* *INDENT-ON* */
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100203}
204
Neale Ranns17dcec02019-01-09 21:22:20 -0800205static int
206ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
207{
208 in = clib_net_to_host_u32 (in);
209
210 switch (in)
211 {
212 case IPSEC_API_PROTO_ESP:
213 *out = IPSEC_PROTOCOL_ESP;
214 return (0);
215 case IPSEC_API_PROTO_AH:
216 *out = IPSEC_PROTOCOL_AH;
217 return (0);
218 }
219 return (VNET_API_ERROR_UNIMPLEMENTED);
220}
221
Neale Ranns8d7c5022019-02-06 01:41:05 -0800222static vl_api_ipsec_proto_t
223ipsec_proto_encode (ipsec_protocol_t p)
224{
225 switch (p)
226 {
227 case IPSEC_PROTOCOL_ESP:
228 return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP);
229 case IPSEC_PROTOCOL_AH:
230 return clib_host_to_net_u32 (IPSEC_API_PROTO_AH);
231 }
232 return (VNET_API_ERROR_UNIMPLEMENTED);
233}
234
Neale Ranns17dcec02019-01-09 21:22:20 -0800235static int
236ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
237 ipsec_crypto_alg_t * out)
238{
239 in = clib_net_to_host_u32 (in);
240
241 switch (in)
242 {
243#define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
244 *out = IPSEC_CRYPTO_ALG_##f; \
245 return (0);
246 foreach_ipsec_crypto_alg
247#undef _
248 }
249 return (VNET_API_ERROR_UNIMPLEMENTED);
250}
251
Neale Ranns8d7c5022019-02-06 01:41:05 -0800252static vl_api_ipsec_crypto_alg_t
253ipsec_crypto_algo_encode (ipsec_crypto_alg_t c)
254{
255 switch (c)
256 {
257#define _(v,f,s) case IPSEC_CRYPTO_ALG_##f: \
258 return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f);
259 foreach_ipsec_crypto_alg
260#undef _
261 case IPSEC_CRYPTO_N_ALG:
262 break;
263 }
264 ASSERT (0);
265 return (VNET_API_ERROR_UNIMPLEMENTED);
266}
267
268
Neale Ranns17dcec02019-01-09 21:22:20 -0800269static int
270ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
271{
272 in = clib_net_to_host_u32 (in);
273
274 switch (in)
275 {
276#define _(v,f,s) case IPSEC_API_INTEG_ALG_##f: \
277 *out = IPSEC_INTEG_ALG_##f; \
278 return (0);
279 foreach_ipsec_integ_alg
280#undef _
281 }
282 return (VNET_API_ERROR_UNIMPLEMENTED);
283}
284
Neale Ranns8d7c5022019-02-06 01:41:05 -0800285static vl_api_ipsec_integ_alg_t
286ipsec_integ_algo_encode (ipsec_integ_alg_t i)
Neale Ranns17dcec02019-01-09 21:22:20 -0800287{
Neale Ranns8d7c5022019-02-06 01:41:05 -0800288 switch (i)
289 {
290#define _(v,f,s) case IPSEC_INTEG_ALG_##f: \
291 return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f));
292 foreach_ipsec_integ_alg
293#undef _
294 case IPSEC_INTEG_N_ALG:
295 break;
296 }
297 ASSERT (0);
298 return (VNET_API_ERROR_UNIMPLEMENTED);
Neale Ranns17dcec02019-01-09 21:22:20 -0800299}
300
301static void
Neale Ranns8d7c5022019-02-06 01:41:05 -0800302ipsec_key_decode (const vl_api_key_t * key, ipsec_key_t * out)
Neale Ranns17dcec02019-01-09 21:22:20 -0800303{
Neale Ranns8d7c5022019-02-06 01:41:05 -0800304 ipsec_mk_key (out, key->data, key->length);
305}
306
307static void
308ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out)
309{
310 out->length = in->len;
311 clib_memcpy (out->data, in->data, out->length);
312}
313
314static ipsec_sa_flags_t
315ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in)
316{
317 ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE;
Neale Ranns17dcec02019-01-09 21:22:20 -0800318 in = clib_net_to_host_u32 (in);
319
Neale Ranns8d7c5022019-02-06 01:41:05 -0800320#define _(v,f,s) if (in & IPSEC_API_SAD_FLAG_##f) \
321 flags |= IPSEC_SA_FLAG_##f;
322 foreach_ipsec_sa_flags
323#undef _
324 return (flags);
Neale Ranns17dcec02019-01-09 21:22:20 -0800325}
326
Neale Ranns8d7c5022019-02-06 01:41:05 -0800327static vl_api_ipsec_sad_flags_t
328ipsec_sad_flags_encode (const ipsec_sa_t * sa)
329{
330 vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE;
331
332 if (sa->use_esn)
333 flags |= IPSEC_API_SAD_FLAG_USE_EXTENDED_SEQ_NUM;
334 if (sa->use_anti_replay)
335 flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
336 if (sa->is_tunnel)
337 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
338 if (sa->is_tunnel_ip6)
339 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
340 if (sa->udp_encap)
341 flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
342
343 return clib_host_to_net_u32 (flags);
344}
Neale Ranns17dcec02019-01-09 21:22:20 -0800345
346static void vl_api_ipsec_sad_entry_add_del_t_handler
347 (vl_api_ipsec_sad_entry_add_del_t * mp)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100348{
349 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
Neale Ranns17dcec02019-01-09 21:22:20 -0800350 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800351 ip46_address_t tun_src = { }, tun_dst =
352 {
353 };
354 ipsec_key_t crypto_key, integ_key;
355 ipsec_crypto_alg_t crypto_alg;
356 ipsec_integ_alg_t integ_alg;
357 ipsec_protocol_t proto;
358 ipsec_sa_flags_t flags;
Neale Rannseba31ec2019-02-17 18:04:27 +0000359 u32 id, spi, sa_index;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100360 int rv;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800361
Damjan Mariona9a951f2017-01-16 22:06:10 +0100362#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100363
Neale Ranns8d7c5022019-02-06 01:41:05 -0800364 id = ntohl (mp->entry.sad_id);
365 spi = ntohl (mp->entry.spi);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100366
Neale Ranns8d7c5022019-02-06 01:41:05 -0800367 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
Neale Ranns17dcec02019-01-09 21:22:20 -0800368
369 if (rv)
370 goto out;
371
Neale Ranns8d7c5022019-02-06 01:41:05 -0800372 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800373
374 if (rv)
375 goto out;
376
Neale Ranns8d7c5022019-02-06 01:41:05 -0800377 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
Neale Ranns17dcec02019-01-09 21:22:20 -0800378
379 if (rv)
380 goto out;
381
Neale Ranns8d7c5022019-02-06 01:41:05 -0800382 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
383 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
Neale Ranns17dcec02019-01-09 21:22:20 -0800384
Neale Ranns8d7c5022019-02-06 01:41:05 -0800385 flags = ipsec_sa_flags_decode (mp->entry.flags);
Neale Ranns17dcec02019-01-09 21:22:20 -0800386
Neale Ranns8d7c5022019-02-06 01:41:05 -0800387 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
388 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100389
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000390
Neale Ranns8d7c5022019-02-06 01:41:05 -0800391 if (mp->is_add)
392 rv = ipsec_sa_add (id, spi, proto,
393 crypto_alg, &crypto_key,
394 integ_alg, &integ_key, flags,
Neale Rannseba31ec2019-02-17 18:04:27 +0000395 0, &tun_src, &tun_dst, &sa_index);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800396 else
397 rv = ipsec_sa_del (id);
398
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100399#else
400 rv = VNET_API_ERROR_UNIMPLEMENTED;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100401#endif
402
403out:
Neale Rannseba31ec2019-02-17 18:04:27 +0000404 /* *INDENT-OFF* */
405 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
406 {
407 rmp->stat_index = htonl (sa_index);
408 });
409 /* *INDENT-ON* */
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100410}
411
412static void
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700413send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
414 u32 context)
415{
416 vl_api_ipsec_spds_details_t *mp;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800417 u32 n_policies = 0;
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700418
419 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400420 clib_memset (mp, 0, sizeof (*mp));
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700421 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
422 mp->context = context;
423
424 mp->spd_id = htonl (spd->id);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800425#define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
426 foreach_ipsec_spd_policy_type
427#undef _
428 mp->npolicies = htonl (n_policies);
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700429
430 vl_api_send_msg (reg, (u8 *) mp);
431}
432
433static void
434vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
435{
436 vl_api_registration_t *reg;
437 ipsec_main_t *im = &ipsec_main;
438 ipsec_spd_t *spd;
439#if WITH_LIBSSL > 0
440 reg = vl_api_client_index_to_registration (mp->client_index);
441 if (!reg)
442 return;
443
444 /* *INDENT-OFF* */
445 pool_foreach (spd, im->spds, ({
446 send_ipsec_spds_details (spd, reg, mp->context);
447 }));
448 /* *INDENT-ON* */
449#else
450 clib_warning ("unimplemented");
451#endif
452}
453
Neale Ranns17dcec02019-01-09 21:22:20 -0800454vl_api_ipsec_spd_action_t
455ipsec_spd_action_encode (ipsec_policy_action_t in)
456{
457 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
458
459 switch (in)
460 {
461#define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
462 out = IPSEC_API_SPD_ACTION_##f; \
463 break;
464 foreach_ipsec_policy_action
465#undef _
466 }
467 return (clib_host_to_net_u32 (out));
468}
469
Matus Fabiana9a0b2c2018-10-02 01:13:25 -0700470static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800471send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
472 u32 context)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100473{
474 vl_api_ipsec_spd_details_t *mp;
475
476 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400477 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100478 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
479 mp->context = context;
480
Neale Ranns17dcec02019-01-09 21:22:20 -0800481 mp->entry.spd_id = htonl (p->id);
482 mp->entry.priority = htonl (p->priority);
483 mp->entry.is_outbound = p->is_outbound;
484
485 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
486 &mp->entry.local_address_start);
487 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
488 &mp->entry.local_address_stop);
489 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
490 &mp->entry.remote_address_start);
491 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
492 &mp->entry.remote_address_stop);
493 mp->entry.local_port_start = htons (p->lport.start);
494 mp->entry.local_port_stop = htons (p->lport.stop);
495 mp->entry.remote_port_start = htons (p->rport.start);
496 mp->entry.remote_port_stop = htons (p->rport.stop);
497 mp->entry.protocol = p->protocol;
498 mp->entry.policy = ipsec_spd_action_encode (p->policy);
499 mp->entry.sa_id = htonl (p->sa_id);
500
Florin Coras6c4dae22018-01-09 06:39:23 -0800501 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100502}
503
504static void
505vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
506{
Florin Coras6c4dae22018-01-09 06:39:23 -0800507 vl_api_registration_t *reg;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100508 ipsec_main_t *im = &ipsec_main;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800509 ipsec_spd_policy_t ptype;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100510 ipsec_policy_t *policy;
511 ipsec_spd_t *spd;
512 uword *p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800513 u32 spd_index, *ii;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100514#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800515 reg = vl_api_client_index_to_registration (mp->client_index);
516 if (!reg)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100517 return;
518
519 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
520 if (!p)
521 return;
522
523 spd_index = p[0];
524 spd = pool_elt_at_index (im->spds, spd_index);
525
526 /* *INDENT-OFF* */
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800527 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
528 vec_foreach(ii, spd->policies[ptype])
529 {
530 policy = pool_elt_at_index(im->policies, *ii);
531
532 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
533 send_ipsec_spd_details (policy, reg, mp->context);
534 }
535 }
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100536 /* *INDENT-ON* */
537#else
538 clib_warning ("unimplemented");
539#endif
540}
541
542static void
Filip Varga871bca92018-11-02 13:51:44 +0100543send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
544 u32 sw_if_index, u32 context)
545{
546 vl_api_ipsec_spd_interface_details_t *mp;
547
548 mp = vl_msg_api_alloc (sizeof (*mp));
549 clib_memset (mp, 0, sizeof (*mp));
550 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
551 mp->context = context;
552
553 mp->spd_index = htonl (spd_index);
554 mp->sw_if_index = htonl (sw_if_index);
555
556 vl_api_send_msg (reg, (u8 *) mp);
557}
558
559static void
560vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
561 mp)
562{
563 ipsec_main_t *im = &ipsec_main;
564 vl_api_registration_t *reg;
565 u32 k, v, spd_index;
566
567#if WITH_LIBSSL > 0
568 reg = vl_api_client_index_to_registration (mp->client_index);
569 if (!reg)
570 return;
571
572 if (mp->spd_index_valid)
573 {
574 spd_index = ntohl (mp->spd_index);
575 /* *INDENT-OFF* */
576 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
577 if (v == spd_index)
578 send_ipsec_spd_interface_details(reg, v, k, mp->context);
579 }));
580 /* *INDENT-ON* */
581 }
582 else
583 {
584 /* *INDENT-OFF* */
585 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
586 send_ipsec_spd_interface_details(reg, v, k, mp->context);
587 }));
588 /* *INDENT-ON* */
589 }
590
591#else
592 clib_warning ("unimplemented");
593#endif
594}
595
596static void
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100597vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
598{
599 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
600 vl_api_ipsec_sa_set_key_reply_t *rmp;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800601 ipsec_key_t ck, ik;
602 u32 id;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100603 int rv;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100604#if WITH_LIBSSL > 0
Neale Ranns17dcec02019-01-09 21:22:20 -0800605
Neale Ranns8d7c5022019-02-06 01:41:05 -0800606 id = ntohl (mp->sa_id);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100607
Neale Ranns8d7c5022019-02-06 01:41:05 -0800608 ipsec_key_decode (&mp->crypto_key, &ck);
609 ipsec_key_decode (&mp->integrity_key, &ik);
610
611 rv = ipsec_set_sa_key (id, &ck, &ik);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100612#else
613 rv = VNET_API_ERROR_UNIMPLEMENTED;
614#endif
615
616 REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
617}
618
619static void
Matthew Smithb0972cb2017-05-02 16:20:41 -0500620vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
621 mp)
622{
623 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500624 ipsec_main_t *im = &ipsec_main;
625 vnet_main_t *vnm = im->vnet_main;
626 u32 sw_if_index = ~0;
Matthew Smithb0972cb2017-05-02 16:20:41 -0500627 int rv;
628
629#if WITH_LIBSSL > 0
630 ipsec_add_del_tunnel_args_t tun;
631
Dave Barachb7b92992018-10-17 10:38:51 -0400632 clib_memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
Matthew Smithb0972cb2017-05-02 16:20:41 -0500633
634 tun.is_add = mp->is_add;
635 tun.esn = mp->esn;
636 tun.anti_replay = mp->anti_replay;
637 tun.local_spi = ntohl (mp->local_spi);
638 tun.remote_spi = ntohl (mp->remote_spi);
639 tun.crypto_alg = mp->crypto_alg;
640 tun.local_crypto_key_len = mp->local_crypto_key_len;
641 tun.remote_crypto_key_len = mp->remote_crypto_key_len;
642 tun.integ_alg = mp->integ_alg;
643 tun.local_integ_key_len = mp->local_integ_key_len;
644 tun.remote_integ_key_len = mp->remote_integ_key_len;
Filip Tehlarb4a7a7d2018-11-30 07:27:27 -0800645 tun.udp_encap = mp->udp_encap;
Pierre Pfister4c422f92018-12-10 11:19:08 +0100646 tun.tx_table_id = ntohl (mp->tx_table_id);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800647 memcpy (&tun.local_ip.ip4, mp->local_ip, 4);
648 memcpy (&tun.remote_ip.ip4, mp->remote_ip, 4);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500649 memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
650 mp->local_crypto_key_len);
651 memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
652 mp->remote_crypto_key_len);
653 memcpy (&tun.local_integ_key, &mp->local_integ_key,
654 mp->local_integ_key_len);
655 memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
656 mp->remote_integ_key_len);
Matthew Smith8e1039a2018-04-12 07:32:56 -0500657 tun.renumber = mp->renumber;
658 tun.show_instance = ntohl (mp->show_instance);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500659
Matthew Smithe04d09d2017-05-14 21:47:18 -0500660 rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500661
662#else
663 rv = VNET_API_ERROR_UNIMPLEMENTED;
664#endif
665
Neale Ranns8d7c5022019-02-06 01:41:05 -0800666 /* *INDENT-OFF* */
667 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
668 ({
669 rmp->sw_if_index = htonl (sw_if_index);
670 }));
671 /* *INDENT-ON* */
Matthew Smithb0972cb2017-05-02 16:20:41 -0500672}
673
Matthew Smith28029532017-09-26 13:33:44 -0500674static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800675send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
Matthew Smith28029532017-09-26 13:33:44 -0500676 u32 context, u32 sw_if_index)
677{
678 vl_api_ipsec_sa_details_t *mp;
679
680 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400681 clib_memset (mp, 0, sizeof (*mp));
Matthew Smith28029532017-09-26 13:33:44 -0500682 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
683 mp->context = context;
684
Neale Ranns8d7c5022019-02-06 01:41:05 -0800685 mp->entry.sad_id = htonl (sa->id);
686 mp->entry.spi = htonl (sa->spi);
687 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
688 mp->entry.tx_table_id =
689 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
Matthew Smith28029532017-09-26 13:33:44 -0500690
Neale Ranns8d7c5022019-02-06 01:41:05 -0800691 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
692 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
Matthew Smith28029532017-09-26 13:33:44 -0500693
Neale Ranns8d7c5022019-02-06 01:41:05 -0800694 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
695 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
Matthew Smith28029532017-09-26 13:33:44 -0500696
Neale Ranns8d7c5022019-02-06 01:41:05 -0800697 mp->entry.flags = ipsec_sad_flags_encode (sa);
Matthew Smith28029532017-09-26 13:33:44 -0500698
699 if (sa->is_tunnel)
700 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800701 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
702 &mp->entry.tunnel_src);
703 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
704 &mp->entry.tunnel_dst);
Matthew Smith28029532017-09-26 13:33:44 -0500705 }
706
Neale Ranns8d7c5022019-02-06 01:41:05 -0800707 mp->sw_if_index = htonl (sw_if_index);
Matthew Smith28029532017-09-26 13:33:44 -0500708 mp->salt = clib_host_to_net_u32 (sa->salt);
709 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
710 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
711 if (sa->use_esn)
712 {
713 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
714 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
715 }
716 if (sa->use_anti_replay)
717 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
Pierre Pfister4c422f92018-12-10 11:19:08 +0100718
Florin Coras6c4dae22018-01-09 06:39:23 -0800719 vl_api_send_msg (reg, (u8 *) mp);
Matthew Smith28029532017-09-26 13:33:44 -0500720}
721
722
723static void
724vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
725{
Florin Coras6c4dae22018-01-09 06:39:23 -0800726 vl_api_registration_t *reg;
Matthew Smith28029532017-09-26 13:33:44 -0500727 ipsec_main_t *im = &ipsec_main;
728 vnet_main_t *vnm = im->vnet_main;
729 ipsec_sa_t *sa;
730 ipsec_tunnel_if_t *t;
731 u32 *sa_index_to_tun_if_index = 0;
732
733#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800734 reg = vl_api_client_index_to_registration (mp->client_index);
735 if (!reg || pool_elts (im->sad) == 0)
Matthew Smith28029532017-09-26 13:33:44 -0500736 return;
737
738 vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
739 ~0);
740
741 /* *INDENT-OFF* */
742 pool_foreach (t, im->tunnel_interfaces,
743 ({
744 vnet_hw_interface_t *hi;
745 u32 sw_if_index = ~0;
746
747 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
748 sw_if_index = hi->sw_if_index;
749 sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
750 sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
751 }));
752
753 pool_foreach (sa, im->sad,
754 ({
755 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
Florin Coras6c4dae22018-01-09 06:39:23 -0800756 send_ipsec_sa_details (sa, reg, mp->context,
Matthew Smith28029532017-09-26 13:33:44 -0500757 sa_index_to_tun_if_index[sa - im->sad]);
758 }));
759 /* *INDENT-ON* */
760
761 vec_free (sa_index_to_tun_if_index);
762#else
763 clib_warning ("unimplemented");
764#endif
765}
766
Matthew Smithe04d09d2017-05-14 21:47:18 -0500767
Matthew Smithb0972cb2017-05-02 16:20:41 -0500768static void
Matthew Smith75d85602017-10-05 19:03:05 -0500769vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t *
770 mp)
771{
772 vl_api_ipsec_tunnel_if_set_key_reply_t *rmp;
773 ipsec_main_t *im = &ipsec_main;
774 vnet_main_t *vnm = im->vnet_main;
775 vnet_sw_interface_t *sw;
776 u8 *key = 0;
777 int rv;
778
779#if WITH_LIBSSL > 0
780 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
781
782 switch (mp->key_type)
783 {
784 case IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO:
785 case IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO:
786 if (mp->alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
Dave Baracha8d47642018-07-13 11:22:23 -0400787 mp->alg >= IPSEC_CRYPTO_N_ALG)
Matthew Smith75d85602017-10-05 19:03:05 -0500788 {
789 rv = VNET_API_ERROR_UNIMPLEMENTED;
790 goto out;
791 }
792 break;
793 case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG:
794 case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG:
Dave Baracha8d47642018-07-13 11:22:23 -0400795 if (mp->alg >= IPSEC_INTEG_N_ALG)
Matthew Smith75d85602017-10-05 19:03:05 -0500796 {
797 rv = VNET_API_ERROR_UNIMPLEMENTED;
798 goto out;
799 }
800 break;
801 case IPSEC_IF_SET_KEY_TYPE_NONE:
802 default:
803 rv = VNET_API_ERROR_UNIMPLEMENTED;
804 goto out;
805 break;
806 }
807
808 key = vec_new (u8, mp->key_len);
809 clib_memcpy (key, mp->key, mp->key_len);
810
811 rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg,
812 key);
813 vec_free (key);
814#else
815 clib_warning ("unimplemented");
816#endif
817
818out:
819 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY);
820}
821
822
823static void
Matthew Smithca514fd2017-10-12 12:06:59 -0500824vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
825{
826 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
827 ipsec_main_t *im = &ipsec_main;
828 vnet_main_t *vnm = im->vnet_main;
829 vnet_sw_interface_t *sw;
830 int rv;
831
832#if WITH_LIBSSL > 0
833 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
834
835 rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
836 mp->is_outbound);
837#else
838 clib_warning ("unimplemented");
839#endif
840
841 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
842}
843
844
845static void
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100846vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
847{
848 vl_api_ikev2_profile_add_del_reply_t *rmp;
849 int rv = 0;
850
Damjan Mariona9a951f2017-01-16 22:06:10 +0100851#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100852 vlib_main_t *vm = vlib_get_main ();
853 clib_error_t *error;
854 u8 *tmp = format (0, "%s", mp->name);
855 error = ikev2_add_del_profile (vm, tmp, mp->is_add);
856 vec_free (tmp);
857 if (error)
858 rv = VNET_API_ERROR_UNSPECIFIED;
859#else
860 rv = VNET_API_ERROR_UNIMPLEMENTED;
861#endif
862
863 REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
864}
865
866static void
867 vl_api_ikev2_profile_set_auth_t_handler
868 (vl_api_ikev2_profile_set_auth_t * mp)
869{
870 vl_api_ikev2_profile_set_auth_reply_t *rmp;
871 int rv = 0;
872
Damjan Mariona9a951f2017-01-16 22:06:10 +0100873#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100874 vlib_main_t *vm = vlib_get_main ();
875 clib_error_t *error;
Michal Cmaradae67c1d82019-01-18 12:51:08 +0100876 int data_len = ntohl (mp->data_len);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100877 u8 *tmp = format (0, "%s", mp->name);
Michal Cmaradae67c1d82019-01-18 12:51:08 +0100878 u8 *data = vec_new (u8, data_len);
879 clib_memcpy (data, mp->data, data_len);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100880 error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
881 vec_free (tmp);
882 vec_free (data);
883 if (error)
884 rv = VNET_API_ERROR_UNSPECIFIED;
885#else
886 rv = VNET_API_ERROR_UNIMPLEMENTED;
887#endif
888
889 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
890}
891
892static void
893vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
894{
895 vl_api_ikev2_profile_add_del_reply_t *rmp;
896 int rv = 0;
897
Damjan Mariona9a951f2017-01-16 22:06:10 +0100898#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100899 vlib_main_t *vm = vlib_get_main ();
900 clib_error_t *error;
901 u8 *tmp = format (0, "%s", mp->name);
Michal Cmaradae67c1d82019-01-18 12:51:08 +0100902 int data_len = ntohl (mp->data_len);
903 u8 *data = vec_new (u8, data_len);
904 clib_memcpy (data, mp->data, data_len);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100905 error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
906 vec_free (tmp);
907 vec_free (data);
908 if (error)
909 rv = VNET_API_ERROR_UNSPECIFIED;
910#else
911 rv = VNET_API_ERROR_UNIMPLEMENTED;
912#endif
913
914 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
915}
916
917static void
918vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
919{
920 vl_api_ikev2_profile_set_ts_reply_t *rmp;
921 int rv = 0;
922
Damjan Mariona9a951f2017-01-16 22:06:10 +0100923#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100924 vlib_main_t *vm = vlib_get_main ();
925 clib_error_t *error;
926 u8 *tmp = format (0, "%s", mp->name);
927 error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
928 mp->end_port, (ip4_address_t) mp->start_addr,
929 (ip4_address_t) mp->end_addr, mp->is_local);
930 vec_free (tmp);
931 if (error)
932 rv = VNET_API_ERROR_UNSPECIFIED;
933#else
934 rv = VNET_API_ERROR_UNIMPLEMENTED;
935#endif
936
937 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
938}
939
940static void
941vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
942{
943 vl_api_ikev2_profile_set_ts_reply_t *rmp;
944 int rv = 0;
945
Damjan Mariona9a951f2017-01-16 22:06:10 +0100946#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100947 vlib_main_t *vm = vlib_get_main ();
948 clib_error_t *error;
949
950 error = ikev2_set_local_key (vm, mp->key_file);
951 if (error)
952 rv = VNET_API_ERROR_UNSPECIFIED;
953#else
954 rv = VNET_API_ERROR_UNIMPLEMENTED;
955#endif
956
957 REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
958}
959
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000960static void
961vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
962{
963 vl_api_ikev2_set_responder_reply_t *rmp;
964 int rv = 0;
965
966#if WITH_LIBSSL > 0
967 vlib_main_t *vm = vlib_get_main ();
968 clib_error_t *error;
969
970 u8 *tmp = format (0, "%s", mp->name);
971 ip4_address_t ip4;
972 clib_memcpy (&ip4, mp->address, sizeof (ip4));
973
974 error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
975 vec_free (tmp);
976 if (error)
977 rv = VNET_API_ERROR_UNSPECIFIED;
978#else
979 rv = VNET_API_ERROR_UNIMPLEMENTED;
980#endif
981
982 REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
983}
984
985static void
986vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
987 mp)
988{
989 vl_api_ikev2_set_ike_transforms_reply_t *rmp;
990 int rv = 0;
991
992#if WITH_LIBSSL > 0
993 vlib_main_t *vm = vlib_get_main ();
994 clib_error_t *error;
995
996 u8 *tmp = format (0, "%s", mp->name);
997
998 error =
999 ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
1000 mp->dh_group, mp->crypto_key_size);
1001 vec_free (tmp);
1002 if (error)
1003 rv = VNET_API_ERROR_UNSPECIFIED;
1004#else
1005 rv = VNET_API_ERROR_UNIMPLEMENTED;
1006#endif
1007
1008 REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
1009}
1010
1011static void
1012vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
1013 mp)
1014{
1015 vl_api_ikev2_set_esp_transforms_reply_t *rmp;
1016 int rv = 0;
1017
1018#if WITH_LIBSSL > 0
1019 vlib_main_t *vm = vlib_get_main ();
1020 clib_error_t *error;
1021
1022 u8 *tmp = format (0, "%s", mp->name);
1023
1024 error =
1025 ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
1026 mp->dh_group, mp->crypto_key_size);
1027 vec_free (tmp);
1028 if (error)
1029 rv = VNET_API_ERROR_UNSPECIFIED;
1030#else
1031 rv = VNET_API_ERROR_UNIMPLEMENTED;
1032#endif
1033
1034 REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
1035}
1036
1037static void
1038vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
1039{
1040 vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
1041 int rv = 0;
1042
1043#if WITH_LIBSSL > 0
1044 vlib_main_t *vm = vlib_get_main ();
1045 clib_error_t *error;
1046
1047 u8 *tmp = format (0, "%s", mp->name);
1048
1049 error =
1050 ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
1051 mp->handover, mp->lifetime_maxdata);
1052 vec_free (tmp);
1053 if (error)
1054 rv = VNET_API_ERROR_UNSPECIFIED;
1055#else
1056 rv = VNET_API_ERROR_UNIMPLEMENTED;
1057#endif
1058
1059 REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
1060}
1061
1062static void
1063vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
1064{
1065 vl_api_ikev2_initiate_sa_init_reply_t *rmp;
1066 int rv = 0;
1067
1068#if WITH_LIBSSL > 0
1069 vlib_main_t *vm = vlib_get_main ();
1070 clib_error_t *error;
1071
1072 u8 *tmp = format (0, "%s", mp->name);
1073
1074 error = ikev2_initiate_sa_init (vm, tmp);
1075 vec_free (tmp);
1076 if (error)
1077 rv = VNET_API_ERROR_UNSPECIFIED;
1078#else
1079 rv = VNET_API_ERROR_UNIMPLEMENTED;
1080#endif
1081
1082 REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
1083}
1084
1085static void
1086vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
1087 * mp)
1088{
1089 vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
1090 int rv = 0;
1091
1092#if WITH_LIBSSL > 0
1093 vlib_main_t *vm = vlib_get_main ();
1094 clib_error_t *error;
1095
1096 error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
1097 if (error)
1098 rv = VNET_API_ERROR_UNSPECIFIED;
1099#else
1100 rv = VNET_API_ERROR_UNIMPLEMENTED;
1101#endif
1102
1103 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
1104}
1105
1106static void
1107 vl_api_ikev2_initiate_del_child_sa_t_handler
1108 (vl_api_ikev2_initiate_del_child_sa_t * mp)
1109{
1110 vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
1111 int rv = 0;
1112
1113#if WITH_LIBSSL > 0
1114 vlib_main_t *vm = vlib_get_main ();
1115 clib_error_t *error;
1116
1117 error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
1118 if (error)
1119 rv = VNET_API_ERROR_UNSPECIFIED;
1120#else
1121 rv = VNET_API_ERROR_UNIMPLEMENTED;
1122#endif
1123
1124 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
1125}
1126
1127static void
1128 vl_api_ikev2_initiate_rekey_child_sa_t_handler
1129 (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
1130{
1131 vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
1132 int rv = 0;
1133
1134#if WITH_LIBSSL > 0
1135 vlib_main_t *vm = vlib_get_main ();
1136 clib_error_t *error;
1137
1138 error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
1139 if (error)
1140 rv = VNET_API_ERROR_UNSPECIFIED;
1141#else
1142 rv = VNET_API_ERROR_UNIMPLEMENTED;
1143#endif
1144
1145 REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
1146}
1147
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001148/*
1149 * ipsec_api_hookup
1150 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001151 * vlib has already mapped shared memory and
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001152 * added the client registration handlers.
1153 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1154 */
1155#define vl_msg_name_crc_list
1156#include <vnet/vnet_all_api_h.h>
1157#undef vl_msg_name_crc_list
1158
1159static void
1160setup_message_id_table (api_main_t * am)
1161{
1162#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1163 foreach_vl_msg_name_crc_ipsec;
1164#undef _
1165}
1166
Klement Sekerab4d30532018-11-08 13:00:02 +01001167static void
1168vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
1169{
1170 vl_api_registration_t *rp;
1171 ipsec_main_t *im = &ipsec_main;
1172 u32 context = mp->context;
1173
1174 rp = vl_api_client_index_to_registration (mp->client_index);
1175
1176 if (rp == 0)
1177 {
1178 clib_warning ("Client %d AWOL", mp->client_index);
1179 return;
1180 }
1181
1182 ipsec_ah_backend_t *ab;
1183 ipsec_esp_backend_t *eb;
1184 /* *INDENT-OFF* */
1185 pool_foreach (ab, im->ah_backends, {
1186 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1187 clib_memset (mp, 0, sizeof (*mp));
1188 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1189 mp->context = context;
1190 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1191 ab->name);
Neale Ranns17dcec02019-01-09 21:22:20 -08001192 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
Klement Sekerab4d30532018-11-08 13:00:02 +01001193 mp->index = ab - im->ah_backends;
1194 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1195 vl_api_send_msg (rp, (u8 *)mp);
1196 });
1197 pool_foreach (eb, im->esp_backends, {
1198 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1199 clib_memset (mp, 0, sizeof (*mp));
1200 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1201 mp->context = context;
1202 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1203 eb->name);
Neale Ranns17dcec02019-01-09 21:22:20 -08001204 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
Klement Sekerab4d30532018-11-08 13:00:02 +01001205 mp->index = eb - im->esp_backends;
1206 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1207 vl_api_send_msg (rp, (u8 *)mp);
1208 });
1209 /* *INDENT-ON* */
1210}
1211
1212static void
1213vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1214{
1215 ipsec_main_t *im = &ipsec_main;
1216 vl_api_ipsec_select_backend_reply_t *rmp;
Neale Ranns17dcec02019-01-09 21:22:20 -08001217 ipsec_protocol_t protocol;
Klement Sekerab4d30532018-11-08 13:00:02 +01001218 int rv = 0;
1219 if (pool_elts (im->sad) > 0)
1220 {
1221 rv = VNET_API_ERROR_INSTANCE_IN_USE;
1222 goto done;
1223 }
Neale Ranns17dcec02019-01-09 21:22:20 -08001224
1225 rv = ipsec_proto_decode (mp->protocol, &protocol);
1226
1227 if (rv)
1228 goto done;
1229
Klement Sekerab4d30532018-11-08 13:00:02 +01001230#if WITH_LIBSSL > 0
Neale Ranns17dcec02019-01-09 21:22:20 -08001231 switch (protocol)
Klement Sekerab4d30532018-11-08 13:00:02 +01001232 {
1233 case IPSEC_PROTOCOL_ESP:
1234 if (pool_is_free_index (im->esp_backends, mp->index))
1235 {
1236 rv = VNET_API_ERROR_INVALID_VALUE;
1237 break;
1238 }
1239 ipsec_select_esp_backend (im, mp->index);
1240 break;
1241 case IPSEC_PROTOCOL_AH:
1242 if (pool_is_free_index (im->ah_backends, mp->index))
1243 {
1244 rv = VNET_API_ERROR_INVALID_VALUE;
1245 break;
1246 }
1247 ipsec_select_ah_backend (im, mp->index);
1248 break;
1249 default:
1250 rv = VNET_API_ERROR_INVALID_VALUE;
1251 break;
1252 }
1253#else
1254 clib_warning ("unimplemented"); /* FIXME */
1255#endif
1256done:
1257 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1258}
1259
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001260static clib_error_t *
1261ipsec_api_hookup (vlib_main_t * vm)
1262{
1263 api_main_t *am = &api_main;
1264
1265#define _(N,n) \
1266 vl_msg_api_set_handlers(VL_API_##N, #n, \
1267 vl_api_##n##_t_handler, \
1268 vl_noop_handler, \
1269 vl_api_##n##_t_endian, \
1270 vl_api_##n##_t_print, \
1271 sizeof(vl_api_##n##_t), 1);
1272 foreach_vpe_api_msg;
1273#undef _
1274
1275 /*
1276 * Set up the (msg_name, crc, message-id) table
1277 */
1278 setup_message_id_table (am);
1279
1280 return 0;
1281}
1282
1283VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1284
1285/*
1286 * fd.io coding-style-patch-verification: ON
1287 *
1288 * Local Variables:
1289 * eval: (c-set-style "gnu")
1290 * End:
1291 */