blob: 8ea47b7ebe135e07fe5e361e5cdc0ad1aca1a012 [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>
26
27#include <vnet/vnet_msg_enum.h>
28
Damjan Mariona9a951f2017-01-16 22:06:10 +010029#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010030#include <vnet/ipsec/ipsec.h>
31#include <vnet/ipsec/ikev2.h>
32#endif /* IPSEC */
33
34#define vl_typedefs /* define message structures */
35#include <vnet/vnet_all_api_h.h>
36#undef vl_typedefs
37
38#define vl_endianfun /* define message structures */
39#include <vnet/vnet_all_api_h.h>
40#undef vl_endianfun
41
42/* instantiate all the print functions we know about */
43#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44#define vl_printfun
45#include <vnet/vnet_all_api_h.h>
46#undef vl_printfun
47
48#include <vlibapi/api_helper_macros.h>
49
50#define foreach_vpe_api_msg \
51_(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
52_(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
53_(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry) \
54_(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry) \
55_(IPSEC_SA_SET_KEY, ipsec_sa_set_key) \
Matthew Smith28029532017-09-26 13:33:44 -050056_(IPSEC_SA_DUMP, ipsec_sa_dump) \
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010057_(IPSEC_SPD_DUMP, ipsec_spd_dump) \
Matthew Smithb0972cb2017-05-02 16:20:41 -050058_(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \
Matthew Smith75d85602017-10-05 19:03:05 -050059_(IPSEC_TUNNEL_IF_SET_KEY, ipsec_tunnel_if_set_key) \
Matthew Smithca514fd2017-10-12 12:06:59 -050060_(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010061_(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del) \
62_(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth) \
63_(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id) \
64_(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts) \
Radu Nicolaucb33dc22017-02-16 16:49:46 +000065_(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key) \
66_(IKEV2_SET_RESPONDER, ikev2_set_responder) \
67_(IKEV2_SET_IKE_TRANSFORMS, ikev2_set_ike_transforms) \
68_(IKEV2_SET_ESP_TRANSFORMS, ikev2_set_esp_transforms) \
69_(IKEV2_SET_SA_LIFETIME, ikev2_set_sa_lifetime) \
70_(IKEV2_INITIATE_SA_INIT, ikev2_initiate_sa_init) \
71_(IKEV2_INITIATE_DEL_IKE_SA, ikev2_initiate_del_ike_sa) \
72_(IKEV2_INITIATE_DEL_CHILD_SA, ikev2_initiate_del_child_sa) \
73_(IKEV2_INITIATE_REKEY_CHILD_SA, ikev2_initiate_rekey_child_sa)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010074
75static void vl_api_ipsec_spd_add_del_t_handler
76 (vl_api_ipsec_spd_add_del_t * mp)
77{
Damjan Mariona9a951f2017-01-16 22:06:10 +010078#if WITH_LIBSSL == 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010079 clib_warning ("unimplemented");
80#else
81
82 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
83 vl_api_ipsec_spd_add_del_reply_t *rmp;
84 int rv;
85
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010086 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +010087
88 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
89#endif
90}
91
92static void vl_api_ipsec_interface_add_del_spd_t_handler
93 (vl_api_ipsec_interface_add_del_spd_t * mp)
94{
95 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
96 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
97 int rv;
98 u32 sw_if_index __attribute__ ((unused));
99 u32 spd_id __attribute__ ((unused));
100
101 sw_if_index = ntohl (mp->sw_if_index);
102 spd_id = ntohl (mp->spd_id);
103
104 VALIDATE_SW_IF_INDEX (mp);
105
Damjan Mariona9a951f2017-01-16 22:06:10 +0100106#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100107 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
108#else
109 rv = VNET_API_ERROR_UNIMPLEMENTED;
110#endif
111
112 BAD_SW_IF_INDEX_LABEL;
113
114 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
115}
116
117static void vl_api_ipsec_spd_add_del_entry_t_handler
118 (vl_api_ipsec_spd_add_del_entry_t * mp)
119{
120 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
121 vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
122 int rv;
123
Damjan Mariona9a951f2017-01-16 22:06:10 +0100124#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100125 ipsec_policy_t p;
126
127 memset (&p, 0, sizeof (p));
128
129 p.id = ntohl (mp->spd_id);
130 p.priority = ntohl (mp->priority);
131 p.is_outbound = mp->is_outbound;
132 p.is_ipv6 = mp->is_ipv6;
133
134 if (mp->is_ipv6 || mp->is_ip_any)
135 {
136 clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
137 clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
138 clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
139 clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
140 }
141 else
142 {
143 clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
144 clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
145 clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
146 clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
147 }
148 p.protocol = mp->protocol;
149 p.rport.start = ntohs (mp->remote_port_start);
150 p.rport.stop = ntohs (mp->remote_port_stop);
151 p.lport.start = ntohs (mp->local_port_start);
152 p.lport.stop = ntohs (mp->local_port_stop);
153 /* policy action resolve unsupported */
154 if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
155 {
156 clib_warning ("unsupported action: 'resolve'");
157 rv = VNET_API_ERROR_UNIMPLEMENTED;
158 goto out;
159 }
160 p.policy = mp->policy;
161 p.sa_id = ntohl (mp->sa_id);
162
163 rv = ipsec_add_del_policy (vm, &p, mp->is_add);
164 if (rv)
165 goto out;
166
167 if (mp->is_ip_any)
168 {
169 p.is_ipv6 = 1;
170 rv = ipsec_add_del_policy (vm, &p, mp->is_add);
171 }
172#else
173 rv = VNET_API_ERROR_UNIMPLEMENTED;
174 goto out;
175#endif
176
177out:
178 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
179}
180
181static void vl_api_ipsec_sad_add_del_entry_t_handler
182 (vl_api_ipsec_sad_add_del_entry_t * mp)
183{
184 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
185 vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
186 int rv;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100187#if WITH_LIBSSL > 0
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000188 ipsec_main_t *im = &ipsec_main;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100189 ipsec_sa_t sa;
190
191 memset (&sa, 0, sizeof (sa));
192
193 sa.id = ntohl (mp->sad_id);
194 sa.spi = ntohl (mp->spi);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100195 sa.protocol = mp->protocol;
196 /* check for unsupported crypto-alg */
Dave Baracha8d47642018-07-13 11:22:23 -0400197 if (mp->crypto_algorithm >= IPSEC_CRYPTO_N_ALG)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100198 {
199 clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
200 mp->crypto_algorithm);
201 rv = VNET_API_ERROR_UNIMPLEMENTED;
202 goto out;
203 }
204 sa.crypto_alg = mp->crypto_algorithm;
205 sa.crypto_key_len = mp->crypto_key_length;
206 clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
207 /* check for unsupported integ-alg */
Pavel Kotucek78053e12017-03-10 10:03:59 +0100208 if (mp->integrity_algorithm >= IPSEC_INTEG_N_ALG)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100209 {
210 clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
211 mp->integrity_algorithm);
212 rv = VNET_API_ERROR_UNIMPLEMENTED;
213 goto out;
214 }
215
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100216 sa.integ_alg = mp->integrity_algorithm;
217 sa.integ_key_len = mp->integrity_key_length;
218 clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
219 sa.use_esn = mp->use_extended_sequence_number;
220 sa.is_tunnel = mp->is_tunnel;
221 sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
222 if (sa.is_tunnel_ip6)
223 {
224 clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
225 clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
226 }
227 else
228 {
229 clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
230 clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
231 }
Matthew Smithca514fd2017-10-12 12:06:59 -0500232 sa.use_anti_replay = mp->use_anti_replay;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100233
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000234 ASSERT (im->cb.check_support_cb);
235 clib_error_t *err = im->cb.check_support_cb (&sa);
236 if (err)
237 {
238 clib_warning ("%s", err->what);
239 rv = VNET_API_ERROR_UNIMPLEMENTED;
240 goto out;
241 }
242
Klement Sekera4b089f22018-04-17 18:04:57 +0200243 rv = ipsec_add_del_sa (vm, &sa, mp->is_add, mp->udp_encap);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100244#else
245 rv = VNET_API_ERROR_UNIMPLEMENTED;
246 goto out;
247#endif
248
249out:
250 REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
251}
252
253static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800254send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
255 u32 context)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100256{
257 vl_api_ipsec_spd_details_t *mp;
258
259 mp = vl_msg_api_alloc (sizeof (*mp));
260 memset (mp, 0, sizeof (*mp));
261 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
262 mp->context = context;
263
264 mp->spd_id = htonl (p->id);
265 mp->priority = htonl (p->priority);
266 mp->is_outbound = p->is_outbound;
267 mp->is_ipv6 = p->is_ipv6;
268 if (p->is_ipv6)
269 {
270 memcpy (mp->local_start_addr, &p->laddr.start.ip6, 16);
271 memcpy (mp->local_stop_addr, &p->laddr.stop.ip6, 16);
272 memcpy (mp->remote_start_addr, &p->raddr.start.ip6, 16);
273 memcpy (mp->remote_stop_addr, &p->raddr.stop.ip6, 16);
274 }
275 else
276 {
277 memcpy (mp->local_start_addr, &p->laddr.start.ip4, 4);
278 memcpy (mp->local_stop_addr, &p->laddr.stop.ip4, 4);
279 memcpy (mp->remote_start_addr, &p->raddr.start.ip4, 4);
280 memcpy (mp->remote_stop_addr, &p->raddr.stop.ip4, 4);
281 }
282 mp->local_start_port = htons (p->lport.start);
283 mp->local_stop_port = htons (p->lport.stop);
284 mp->remote_start_port = htons (p->rport.start);
285 mp->remote_stop_port = htons (p->rport.stop);
286 mp->protocol = p->protocol;
287 mp->policy = p->policy;
288 mp->sa_id = htonl (p->sa_id);
289 mp->bytes = clib_host_to_net_u64 (p->counter.bytes);
290 mp->packets = clib_host_to_net_u64 (p->counter.packets);
291
Florin Coras6c4dae22018-01-09 06:39:23 -0800292 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100293}
294
295static void
296vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
297{
Florin Coras6c4dae22018-01-09 06:39:23 -0800298 vl_api_registration_t *reg;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100299 ipsec_main_t *im = &ipsec_main;
300 ipsec_policy_t *policy;
301 ipsec_spd_t *spd;
302 uword *p;
303 u32 spd_index;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100304#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800305 reg = vl_api_client_index_to_registration (mp->client_index);
306 if (!reg)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100307 return;
308
309 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
310 if (!p)
311 return;
312
313 spd_index = p[0];
314 spd = pool_elt_at_index (im->spds, spd_index);
315
316 /* *INDENT-OFF* */
317 pool_foreach (policy, spd->policies,
318 ({
319 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
Florin Coras6c4dae22018-01-09 06:39:23 -0800320 send_ipsec_spd_details (policy, reg,
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100321 mp->context);}
322 ));
323 /* *INDENT-ON* */
324#else
325 clib_warning ("unimplemented");
326#endif
327}
328
329static void
330vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
331{
332 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
333 vl_api_ipsec_sa_set_key_reply_t *rmp;
334 int rv;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100335#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100336 ipsec_sa_t sa;
337 sa.id = ntohl (mp->sa_id);
338 sa.crypto_key_len = mp->crypto_key_length;
339 clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
340 sa.integ_key_len = mp->integrity_key_length;
341 clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
342
343 rv = ipsec_set_sa_key (vm, &sa);
344#else
345 rv = VNET_API_ERROR_UNIMPLEMENTED;
346#endif
347
348 REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
349}
350
351static void
Matthew Smithb0972cb2017-05-02 16:20:41 -0500352vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
353 mp)
354{
355 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500356 ipsec_main_t *im = &ipsec_main;
357 vnet_main_t *vnm = im->vnet_main;
358 u32 sw_if_index = ~0;
Matthew Smithb0972cb2017-05-02 16:20:41 -0500359 int rv;
360
361#if WITH_LIBSSL > 0
362 ipsec_add_del_tunnel_args_t tun;
363
364 memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
365
366 tun.is_add = mp->is_add;
367 tun.esn = mp->esn;
368 tun.anti_replay = mp->anti_replay;
369 tun.local_spi = ntohl (mp->local_spi);
370 tun.remote_spi = ntohl (mp->remote_spi);
371 tun.crypto_alg = mp->crypto_alg;
372 tun.local_crypto_key_len = mp->local_crypto_key_len;
373 tun.remote_crypto_key_len = mp->remote_crypto_key_len;
374 tun.integ_alg = mp->integ_alg;
375 tun.local_integ_key_len = mp->local_integ_key_len;
376 tun.remote_integ_key_len = mp->remote_integ_key_len;
377 memcpy (&tun.local_ip, mp->local_ip, 4);
378 memcpy (&tun.remote_ip, mp->remote_ip, 4);
379 memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
380 mp->local_crypto_key_len);
381 memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
382 mp->remote_crypto_key_len);
383 memcpy (&tun.local_integ_key, &mp->local_integ_key,
384 mp->local_integ_key_len);
385 memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
386 mp->remote_integ_key_len);
Matthew Smith8e1039a2018-04-12 07:32:56 -0500387 tun.renumber = mp->renumber;
388 tun.show_instance = ntohl (mp->show_instance);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500389
Matthew Smithe04d09d2017-05-14 21:47:18 -0500390 rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500391
392#else
393 rv = VNET_API_ERROR_UNIMPLEMENTED;
394#endif
395
Matthew Smithe04d09d2017-05-14 21:47:18 -0500396 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY, (
397 {
398 rmp->sw_if_index =
399 htonl (sw_if_index);
400 }));
Matthew Smithb0972cb2017-05-02 16:20:41 -0500401}
402
Matthew Smith28029532017-09-26 13:33:44 -0500403static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800404send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
Matthew Smith28029532017-09-26 13:33:44 -0500405 u32 context, u32 sw_if_index)
406{
407 vl_api_ipsec_sa_details_t *mp;
408
409 mp = vl_msg_api_alloc (sizeof (*mp));
410 memset (mp, 0, sizeof (*mp));
411 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
412 mp->context = context;
413
414 mp->sa_id = htonl (sa->id);
415 mp->sw_if_index = htonl (sw_if_index);
416
417 mp->spi = htonl (sa->spi);
418 mp->protocol = sa->protocol;
419
420 mp->crypto_alg = sa->crypto_alg;
421 mp->crypto_key_len = sa->crypto_key_len;
422 memcpy (mp->crypto_key, sa->crypto_key, sa->crypto_key_len);
423
424 mp->integ_alg = sa->integ_alg;
425 mp->integ_key_len = sa->integ_key_len;
426 memcpy (mp->integ_key, sa->integ_key, sa->integ_key_len);
427
428 mp->use_esn = sa->use_esn;
429 mp->use_anti_replay = sa->use_anti_replay;
430
431 mp->is_tunnel = sa->is_tunnel;
432 mp->is_tunnel_ip6 = sa->is_tunnel_ip6;
433
434 if (sa->is_tunnel)
435 {
436 if (sa->is_tunnel_ip6)
437 {
438 memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip6, 16);
439 memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip6, 16);
440 }
441 else
442 {
443 memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip4, 4);
444 memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip4, 4);
445 }
446 }
447
448 mp->salt = clib_host_to_net_u32 (sa->salt);
449 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
450 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
451 if (sa->use_esn)
452 {
453 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
454 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
455 }
456 if (sa->use_anti_replay)
457 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
458 mp->total_data_size = clib_host_to_net_u64 (sa->total_data_size);
Klement Sekera4b089f22018-04-17 18:04:57 +0200459 mp->udp_encap = sa->udp_encap;
Matthew Smith28029532017-09-26 13:33:44 -0500460
Florin Coras6c4dae22018-01-09 06:39:23 -0800461 vl_api_send_msg (reg, (u8 *) mp);
Matthew Smith28029532017-09-26 13:33:44 -0500462}
463
464
465static void
466vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
467{
Florin Coras6c4dae22018-01-09 06:39:23 -0800468 vl_api_registration_t *reg;
Matthew Smith28029532017-09-26 13:33:44 -0500469 ipsec_main_t *im = &ipsec_main;
470 vnet_main_t *vnm = im->vnet_main;
471 ipsec_sa_t *sa;
472 ipsec_tunnel_if_t *t;
473 u32 *sa_index_to_tun_if_index = 0;
474
475#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800476 reg = vl_api_client_index_to_registration (mp->client_index);
477 if (!reg || pool_elts (im->sad) == 0)
Matthew Smith28029532017-09-26 13:33:44 -0500478 return;
479
480 vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
481 ~0);
482
483 /* *INDENT-OFF* */
484 pool_foreach (t, im->tunnel_interfaces,
485 ({
486 vnet_hw_interface_t *hi;
487 u32 sw_if_index = ~0;
488
489 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
490 sw_if_index = hi->sw_if_index;
491 sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
492 sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
493 }));
494
495 pool_foreach (sa, im->sad,
496 ({
497 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
Florin Coras6c4dae22018-01-09 06:39:23 -0800498 send_ipsec_sa_details (sa, reg, mp->context,
Matthew Smith28029532017-09-26 13:33:44 -0500499 sa_index_to_tun_if_index[sa - im->sad]);
500 }));
501 /* *INDENT-ON* */
502
503 vec_free (sa_index_to_tun_if_index);
504#else
505 clib_warning ("unimplemented");
506#endif
507}
508
Matthew Smithe04d09d2017-05-14 21:47:18 -0500509
Matthew Smithb0972cb2017-05-02 16:20:41 -0500510static void
Matthew Smith75d85602017-10-05 19:03:05 -0500511vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t *
512 mp)
513{
514 vl_api_ipsec_tunnel_if_set_key_reply_t *rmp;
515 ipsec_main_t *im = &ipsec_main;
516 vnet_main_t *vnm = im->vnet_main;
517 vnet_sw_interface_t *sw;
518 u8 *key = 0;
519 int rv;
520
521#if WITH_LIBSSL > 0
522 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
523
524 switch (mp->key_type)
525 {
526 case IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO:
527 case IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO:
528 if (mp->alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
Dave Baracha8d47642018-07-13 11:22:23 -0400529 mp->alg >= IPSEC_CRYPTO_N_ALG)
Matthew Smith75d85602017-10-05 19:03:05 -0500530 {
531 rv = VNET_API_ERROR_UNIMPLEMENTED;
532 goto out;
533 }
534 break;
535 case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG:
536 case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG:
Dave Baracha8d47642018-07-13 11:22:23 -0400537 if (mp->alg >= IPSEC_INTEG_N_ALG)
Matthew Smith75d85602017-10-05 19:03:05 -0500538 {
539 rv = VNET_API_ERROR_UNIMPLEMENTED;
540 goto out;
541 }
542 break;
543 case IPSEC_IF_SET_KEY_TYPE_NONE:
544 default:
545 rv = VNET_API_ERROR_UNIMPLEMENTED;
546 goto out;
547 break;
548 }
549
550 key = vec_new (u8, mp->key_len);
551 clib_memcpy (key, mp->key, mp->key_len);
552
553 rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg,
554 key);
555 vec_free (key);
556#else
557 clib_warning ("unimplemented");
558#endif
559
560out:
561 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY);
562}
563
564
565static void
Matthew Smithca514fd2017-10-12 12:06:59 -0500566vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
567{
568 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
569 ipsec_main_t *im = &ipsec_main;
570 vnet_main_t *vnm = im->vnet_main;
571 vnet_sw_interface_t *sw;
572 int rv;
573
574#if WITH_LIBSSL > 0
575 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
576
577 rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
578 mp->is_outbound);
579#else
580 clib_warning ("unimplemented");
581#endif
582
583 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
584}
585
586
587static void
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100588vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
589{
590 vl_api_ikev2_profile_add_del_reply_t *rmp;
591 int rv = 0;
592
Damjan Mariona9a951f2017-01-16 22:06:10 +0100593#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100594 vlib_main_t *vm = vlib_get_main ();
595 clib_error_t *error;
596 u8 *tmp = format (0, "%s", mp->name);
597 error = ikev2_add_del_profile (vm, tmp, mp->is_add);
598 vec_free (tmp);
599 if (error)
600 rv = VNET_API_ERROR_UNSPECIFIED;
601#else
602 rv = VNET_API_ERROR_UNIMPLEMENTED;
603#endif
604
605 REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
606}
607
608static void
609 vl_api_ikev2_profile_set_auth_t_handler
610 (vl_api_ikev2_profile_set_auth_t * mp)
611{
612 vl_api_ikev2_profile_set_auth_reply_t *rmp;
613 int rv = 0;
614
Damjan Mariona9a951f2017-01-16 22:06:10 +0100615#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100616 vlib_main_t *vm = vlib_get_main ();
617 clib_error_t *error;
618 u8 *tmp = format (0, "%s", mp->name);
619 u8 *data = vec_new (u8, mp->data_len);
620 clib_memcpy (data, mp->data, mp->data_len);
621 error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
622 vec_free (tmp);
623 vec_free (data);
624 if (error)
625 rv = VNET_API_ERROR_UNSPECIFIED;
626#else
627 rv = VNET_API_ERROR_UNIMPLEMENTED;
628#endif
629
630 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
631}
632
633static void
634vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
635{
636 vl_api_ikev2_profile_add_del_reply_t *rmp;
637 int rv = 0;
638
Damjan Mariona9a951f2017-01-16 22:06:10 +0100639#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100640 vlib_main_t *vm = vlib_get_main ();
641 clib_error_t *error;
642 u8 *tmp = format (0, "%s", mp->name);
643 u8 *data = vec_new (u8, mp->data_len);
644 clib_memcpy (data, mp->data, mp->data_len);
645 error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
646 vec_free (tmp);
647 vec_free (data);
648 if (error)
649 rv = VNET_API_ERROR_UNSPECIFIED;
650#else
651 rv = VNET_API_ERROR_UNIMPLEMENTED;
652#endif
653
654 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
655}
656
657static void
658vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
659{
660 vl_api_ikev2_profile_set_ts_reply_t *rmp;
661 int rv = 0;
662
Damjan Mariona9a951f2017-01-16 22:06:10 +0100663#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100664 vlib_main_t *vm = vlib_get_main ();
665 clib_error_t *error;
666 u8 *tmp = format (0, "%s", mp->name);
667 error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
668 mp->end_port, (ip4_address_t) mp->start_addr,
669 (ip4_address_t) mp->end_addr, mp->is_local);
670 vec_free (tmp);
671 if (error)
672 rv = VNET_API_ERROR_UNSPECIFIED;
673#else
674 rv = VNET_API_ERROR_UNIMPLEMENTED;
675#endif
676
677 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
678}
679
680static void
681vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
682{
683 vl_api_ikev2_profile_set_ts_reply_t *rmp;
684 int rv = 0;
685
Damjan Mariona9a951f2017-01-16 22:06:10 +0100686#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100687 vlib_main_t *vm = vlib_get_main ();
688 clib_error_t *error;
689
690 error = ikev2_set_local_key (vm, mp->key_file);
691 if (error)
692 rv = VNET_API_ERROR_UNSPECIFIED;
693#else
694 rv = VNET_API_ERROR_UNIMPLEMENTED;
695#endif
696
697 REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
698}
699
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000700static void
701vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
702{
703 vl_api_ikev2_set_responder_reply_t *rmp;
704 int rv = 0;
705
706#if WITH_LIBSSL > 0
707 vlib_main_t *vm = vlib_get_main ();
708 clib_error_t *error;
709
710 u8 *tmp = format (0, "%s", mp->name);
711 ip4_address_t ip4;
712 clib_memcpy (&ip4, mp->address, sizeof (ip4));
713
714 error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
715 vec_free (tmp);
716 if (error)
717 rv = VNET_API_ERROR_UNSPECIFIED;
718#else
719 rv = VNET_API_ERROR_UNIMPLEMENTED;
720#endif
721
722 REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
723}
724
725static void
726vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
727 mp)
728{
729 vl_api_ikev2_set_ike_transforms_reply_t *rmp;
730 int rv = 0;
731
732#if WITH_LIBSSL > 0
733 vlib_main_t *vm = vlib_get_main ();
734 clib_error_t *error;
735
736 u8 *tmp = format (0, "%s", mp->name);
737
738 error =
739 ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
740 mp->dh_group, mp->crypto_key_size);
741 vec_free (tmp);
742 if (error)
743 rv = VNET_API_ERROR_UNSPECIFIED;
744#else
745 rv = VNET_API_ERROR_UNIMPLEMENTED;
746#endif
747
748 REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
749}
750
751static void
752vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
753 mp)
754{
755 vl_api_ikev2_set_esp_transforms_reply_t *rmp;
756 int rv = 0;
757
758#if WITH_LIBSSL > 0
759 vlib_main_t *vm = vlib_get_main ();
760 clib_error_t *error;
761
762 u8 *tmp = format (0, "%s", mp->name);
763
764 error =
765 ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
766 mp->dh_group, mp->crypto_key_size);
767 vec_free (tmp);
768 if (error)
769 rv = VNET_API_ERROR_UNSPECIFIED;
770#else
771 rv = VNET_API_ERROR_UNIMPLEMENTED;
772#endif
773
774 REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
775}
776
777static void
778vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
779{
780 vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
781 int rv = 0;
782
783#if WITH_LIBSSL > 0
784 vlib_main_t *vm = vlib_get_main ();
785 clib_error_t *error;
786
787 u8 *tmp = format (0, "%s", mp->name);
788
789 error =
790 ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
791 mp->handover, mp->lifetime_maxdata);
792 vec_free (tmp);
793 if (error)
794 rv = VNET_API_ERROR_UNSPECIFIED;
795#else
796 rv = VNET_API_ERROR_UNIMPLEMENTED;
797#endif
798
799 REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
800}
801
802static void
803vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
804{
805 vl_api_ikev2_initiate_sa_init_reply_t *rmp;
806 int rv = 0;
807
808#if WITH_LIBSSL > 0
809 vlib_main_t *vm = vlib_get_main ();
810 clib_error_t *error;
811
812 u8 *tmp = format (0, "%s", mp->name);
813
814 error = ikev2_initiate_sa_init (vm, tmp);
815 vec_free (tmp);
816 if (error)
817 rv = VNET_API_ERROR_UNSPECIFIED;
818#else
819 rv = VNET_API_ERROR_UNIMPLEMENTED;
820#endif
821
822 REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
823}
824
825static void
826vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
827 * mp)
828{
829 vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
830 int rv = 0;
831
832#if WITH_LIBSSL > 0
833 vlib_main_t *vm = vlib_get_main ();
834 clib_error_t *error;
835
836 error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
837 if (error)
838 rv = VNET_API_ERROR_UNSPECIFIED;
839#else
840 rv = VNET_API_ERROR_UNIMPLEMENTED;
841#endif
842
843 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
844}
845
846static void
847 vl_api_ikev2_initiate_del_child_sa_t_handler
848 (vl_api_ikev2_initiate_del_child_sa_t * mp)
849{
850 vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
851 int rv = 0;
852
853#if WITH_LIBSSL > 0
854 vlib_main_t *vm = vlib_get_main ();
855 clib_error_t *error;
856
857 error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
858 if (error)
859 rv = VNET_API_ERROR_UNSPECIFIED;
860#else
861 rv = VNET_API_ERROR_UNIMPLEMENTED;
862#endif
863
864 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
865}
866
867static void
868 vl_api_ikev2_initiate_rekey_child_sa_t_handler
869 (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
870{
871 vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
872 int rv = 0;
873
874#if WITH_LIBSSL > 0
875 vlib_main_t *vm = vlib_get_main ();
876 clib_error_t *error;
877
878 error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
879 if (error)
880 rv = VNET_API_ERROR_UNSPECIFIED;
881#else
882 rv = VNET_API_ERROR_UNIMPLEMENTED;
883#endif
884
885 REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
886}
887
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100888/*
889 * ipsec_api_hookup
890 * Add vpe's API message handlers to the table.
891 * vlib has alread mapped shared memory and
892 * added the client registration handlers.
893 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
894 */
895#define vl_msg_name_crc_list
896#include <vnet/vnet_all_api_h.h>
897#undef vl_msg_name_crc_list
898
899static void
900setup_message_id_table (api_main_t * am)
901{
902#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
903 foreach_vl_msg_name_crc_ipsec;
904#undef _
905}
906
907static clib_error_t *
908ipsec_api_hookup (vlib_main_t * vm)
909{
910 api_main_t *am = &api_main;
911
912#define _(N,n) \
913 vl_msg_api_set_handlers(VL_API_##N, #n, \
914 vl_api_##n##_t_handler, \
915 vl_noop_handler, \
916 vl_api_##n##_t_endian, \
917 vl_api_##n##_t_print, \
918 sizeof(vl_api_##n##_t), 1);
919 foreach_vpe_api_msg;
920#undef _
921
922 /*
923 * Set up the (msg_name, crc, message-id) table
924 */
925 setup_message_id_table (am);
926
927 return 0;
928}
929
930VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
931
932/*
933 * fd.io coding-style-patch-verification: ON
934 *
935 * Local Variables:
936 * eval: (c-set-style "gnu")
937 * End:
938 */