blob: c4284b914780eb5ec7c0d3089a3d56d1087b8257 [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;
Radu Nicolau717de092018-08-03 10:37:24 +0100222 sa.udp_encap = mp->udp_encap;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100223 if (sa.is_tunnel_ip6)
224 {
225 clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
226 clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
227 }
228 else
229 {
230 clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
231 clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
232 }
Matthew Smithca514fd2017-10-12 12:06:59 -0500233 sa.use_anti_replay = mp->use_anti_replay;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100234
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000235 ASSERT (im->cb.check_support_cb);
236 clib_error_t *err = im->cb.check_support_cb (&sa);
237 if (err)
238 {
239 clib_warning ("%s", err->what);
240 rv = VNET_API_ERROR_UNIMPLEMENTED;
241 goto out;
242 }
243
Radu Nicolau717de092018-08-03 10:37:24 +0100244 rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100245#else
246 rv = VNET_API_ERROR_UNIMPLEMENTED;
247 goto out;
248#endif
249
250out:
251 REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
252}
253
254static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800255send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
256 u32 context)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100257{
258 vl_api_ipsec_spd_details_t *mp;
259
260 mp = vl_msg_api_alloc (sizeof (*mp));
261 memset (mp, 0, sizeof (*mp));
262 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
263 mp->context = context;
264
265 mp->spd_id = htonl (p->id);
266 mp->priority = htonl (p->priority);
267 mp->is_outbound = p->is_outbound;
268 mp->is_ipv6 = p->is_ipv6;
269 if (p->is_ipv6)
270 {
271 memcpy (mp->local_start_addr, &p->laddr.start.ip6, 16);
272 memcpy (mp->local_stop_addr, &p->laddr.stop.ip6, 16);
273 memcpy (mp->remote_start_addr, &p->raddr.start.ip6, 16);
274 memcpy (mp->remote_stop_addr, &p->raddr.stop.ip6, 16);
275 }
276 else
277 {
278 memcpy (mp->local_start_addr, &p->laddr.start.ip4, 4);
279 memcpy (mp->local_stop_addr, &p->laddr.stop.ip4, 4);
280 memcpy (mp->remote_start_addr, &p->raddr.start.ip4, 4);
281 memcpy (mp->remote_stop_addr, &p->raddr.stop.ip4, 4);
282 }
283 mp->local_start_port = htons (p->lport.start);
284 mp->local_stop_port = htons (p->lport.stop);
285 mp->remote_start_port = htons (p->rport.start);
286 mp->remote_stop_port = htons (p->rport.stop);
287 mp->protocol = p->protocol;
288 mp->policy = p->policy;
289 mp->sa_id = htonl (p->sa_id);
290 mp->bytes = clib_host_to_net_u64 (p->counter.bytes);
291 mp->packets = clib_host_to_net_u64 (p->counter.packets);
292
Florin Coras6c4dae22018-01-09 06:39:23 -0800293 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100294}
295
296static void
297vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
298{
Florin Coras6c4dae22018-01-09 06:39:23 -0800299 vl_api_registration_t *reg;
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100300 ipsec_main_t *im = &ipsec_main;
301 ipsec_policy_t *policy;
302 ipsec_spd_t *spd;
303 uword *p;
304 u32 spd_index;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100305#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800306 reg = vl_api_client_index_to_registration (mp->client_index);
307 if (!reg)
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100308 return;
309
310 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
311 if (!p)
312 return;
313
314 spd_index = p[0];
315 spd = pool_elt_at_index (im->spds, spd_index);
316
317 /* *INDENT-OFF* */
318 pool_foreach (policy, spd->policies,
319 ({
320 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
Florin Coras6c4dae22018-01-09 06:39:23 -0800321 send_ipsec_spd_details (policy, reg,
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100322 mp->context);}
323 ));
324 /* *INDENT-ON* */
325#else
326 clib_warning ("unimplemented");
327#endif
328}
329
330static void
331vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
332{
333 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
334 vl_api_ipsec_sa_set_key_reply_t *rmp;
335 int rv;
Damjan Mariona9a951f2017-01-16 22:06:10 +0100336#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100337 ipsec_sa_t sa;
338 sa.id = ntohl (mp->sa_id);
339 sa.crypto_key_len = mp->crypto_key_length;
340 clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
341 sa.integ_key_len = mp->integrity_key_length;
342 clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
343
344 rv = ipsec_set_sa_key (vm, &sa);
345#else
346 rv = VNET_API_ERROR_UNIMPLEMENTED;
347#endif
348
349 REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
350}
351
352static void
Matthew Smithb0972cb2017-05-02 16:20:41 -0500353vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
354 mp)
355{
356 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
Matthew Smithe04d09d2017-05-14 21:47:18 -0500357 ipsec_main_t *im = &ipsec_main;
358 vnet_main_t *vnm = im->vnet_main;
359 u32 sw_if_index = ~0;
Matthew Smithb0972cb2017-05-02 16:20:41 -0500360 int rv;
361
362#if WITH_LIBSSL > 0
363 ipsec_add_del_tunnel_args_t tun;
364
365 memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
366
367 tun.is_add = mp->is_add;
368 tun.esn = mp->esn;
369 tun.anti_replay = mp->anti_replay;
370 tun.local_spi = ntohl (mp->local_spi);
371 tun.remote_spi = ntohl (mp->remote_spi);
372 tun.crypto_alg = mp->crypto_alg;
373 tun.local_crypto_key_len = mp->local_crypto_key_len;
374 tun.remote_crypto_key_len = mp->remote_crypto_key_len;
375 tun.integ_alg = mp->integ_alg;
376 tun.local_integ_key_len = mp->local_integ_key_len;
377 tun.remote_integ_key_len = mp->remote_integ_key_len;
378 memcpy (&tun.local_ip, mp->local_ip, 4);
379 memcpy (&tun.remote_ip, mp->remote_ip, 4);
380 memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
381 mp->local_crypto_key_len);
382 memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
383 mp->remote_crypto_key_len);
384 memcpy (&tun.local_integ_key, &mp->local_integ_key,
385 mp->local_integ_key_len);
386 memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
387 mp->remote_integ_key_len);
Matthew Smith8e1039a2018-04-12 07:32:56 -0500388 tun.renumber = mp->renumber;
389 tun.show_instance = ntohl (mp->show_instance);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500390
Matthew Smithe04d09d2017-05-14 21:47:18 -0500391 rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
Matthew Smithb0972cb2017-05-02 16:20:41 -0500392
393#else
394 rv = VNET_API_ERROR_UNIMPLEMENTED;
395#endif
396
Matthew Smithe04d09d2017-05-14 21:47:18 -0500397 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY, (
398 {
399 rmp->sw_if_index =
400 htonl (sw_if_index);
401 }));
Matthew Smithb0972cb2017-05-02 16:20:41 -0500402}
403
Matthew Smith28029532017-09-26 13:33:44 -0500404static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800405send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
Matthew Smith28029532017-09-26 13:33:44 -0500406 u32 context, u32 sw_if_index)
407{
408 vl_api_ipsec_sa_details_t *mp;
409
410 mp = vl_msg_api_alloc (sizeof (*mp));
411 memset (mp, 0, sizeof (*mp));
412 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
413 mp->context = context;
414
415 mp->sa_id = htonl (sa->id);
416 mp->sw_if_index = htonl (sw_if_index);
417
418 mp->spi = htonl (sa->spi);
419 mp->protocol = sa->protocol;
420
421 mp->crypto_alg = sa->crypto_alg;
422 mp->crypto_key_len = sa->crypto_key_len;
423 memcpy (mp->crypto_key, sa->crypto_key, sa->crypto_key_len);
424
425 mp->integ_alg = sa->integ_alg;
426 mp->integ_key_len = sa->integ_key_len;
427 memcpy (mp->integ_key, sa->integ_key, sa->integ_key_len);
428
429 mp->use_esn = sa->use_esn;
430 mp->use_anti_replay = sa->use_anti_replay;
431
432 mp->is_tunnel = sa->is_tunnel;
433 mp->is_tunnel_ip6 = sa->is_tunnel_ip6;
434
435 if (sa->is_tunnel)
436 {
437 if (sa->is_tunnel_ip6)
438 {
439 memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip6, 16);
440 memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip6, 16);
441 }
442 else
443 {
444 memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip4, 4);
445 memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip4, 4);
446 }
447 }
448
449 mp->salt = clib_host_to_net_u32 (sa->salt);
450 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
451 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
452 if (sa->use_esn)
453 {
454 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
455 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
456 }
457 if (sa->use_anti_replay)
458 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
459 mp->total_data_size = clib_host_to_net_u64 (sa->total_data_size);
Klement Sekera4b089f22018-04-17 18:04:57 +0200460 mp->udp_encap = sa->udp_encap;
Matthew Smith28029532017-09-26 13:33:44 -0500461
Florin Coras6c4dae22018-01-09 06:39:23 -0800462 vl_api_send_msg (reg, (u8 *) mp);
Matthew Smith28029532017-09-26 13:33:44 -0500463}
464
465
466static void
467vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
468{
Florin Coras6c4dae22018-01-09 06:39:23 -0800469 vl_api_registration_t *reg;
Matthew Smith28029532017-09-26 13:33:44 -0500470 ipsec_main_t *im = &ipsec_main;
471 vnet_main_t *vnm = im->vnet_main;
472 ipsec_sa_t *sa;
473 ipsec_tunnel_if_t *t;
474 u32 *sa_index_to_tun_if_index = 0;
475
476#if WITH_LIBSSL > 0
Florin Coras6c4dae22018-01-09 06:39:23 -0800477 reg = vl_api_client_index_to_registration (mp->client_index);
478 if (!reg || pool_elts (im->sad) == 0)
Matthew Smith28029532017-09-26 13:33:44 -0500479 return;
480
481 vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
482 ~0);
483
484 /* *INDENT-OFF* */
485 pool_foreach (t, im->tunnel_interfaces,
486 ({
487 vnet_hw_interface_t *hi;
488 u32 sw_if_index = ~0;
489
490 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
491 sw_if_index = hi->sw_if_index;
492 sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
493 sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
494 }));
495
496 pool_foreach (sa, im->sad,
497 ({
498 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
Florin Coras6c4dae22018-01-09 06:39:23 -0800499 send_ipsec_sa_details (sa, reg, mp->context,
Matthew Smith28029532017-09-26 13:33:44 -0500500 sa_index_to_tun_if_index[sa - im->sad]);
501 }));
502 /* *INDENT-ON* */
503
504 vec_free (sa_index_to_tun_if_index);
505#else
506 clib_warning ("unimplemented");
507#endif
508}
509
Matthew Smithe04d09d2017-05-14 21:47:18 -0500510
Matthew Smithb0972cb2017-05-02 16:20:41 -0500511static void
Matthew Smith75d85602017-10-05 19:03:05 -0500512vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t *
513 mp)
514{
515 vl_api_ipsec_tunnel_if_set_key_reply_t *rmp;
516 ipsec_main_t *im = &ipsec_main;
517 vnet_main_t *vnm = im->vnet_main;
518 vnet_sw_interface_t *sw;
519 u8 *key = 0;
520 int rv;
521
522#if WITH_LIBSSL > 0
523 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
524
525 switch (mp->key_type)
526 {
527 case IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO:
528 case IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO:
529 if (mp->alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
Dave Baracha8d47642018-07-13 11:22:23 -0400530 mp->alg >= IPSEC_CRYPTO_N_ALG)
Matthew Smith75d85602017-10-05 19:03:05 -0500531 {
532 rv = VNET_API_ERROR_UNIMPLEMENTED;
533 goto out;
534 }
535 break;
536 case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG:
537 case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG:
Dave Baracha8d47642018-07-13 11:22:23 -0400538 if (mp->alg >= IPSEC_INTEG_N_ALG)
Matthew Smith75d85602017-10-05 19:03:05 -0500539 {
540 rv = VNET_API_ERROR_UNIMPLEMENTED;
541 goto out;
542 }
543 break;
544 case IPSEC_IF_SET_KEY_TYPE_NONE:
545 default:
546 rv = VNET_API_ERROR_UNIMPLEMENTED;
547 goto out;
548 break;
549 }
550
551 key = vec_new (u8, mp->key_len);
552 clib_memcpy (key, mp->key, mp->key_len);
553
554 rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg,
555 key);
556 vec_free (key);
557#else
558 clib_warning ("unimplemented");
559#endif
560
561out:
562 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY);
563}
564
565
566static void
Matthew Smithca514fd2017-10-12 12:06:59 -0500567vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
568{
569 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
570 ipsec_main_t *im = &ipsec_main;
571 vnet_main_t *vnm = im->vnet_main;
572 vnet_sw_interface_t *sw;
573 int rv;
574
575#if WITH_LIBSSL > 0
576 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
577
578 rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
579 mp->is_outbound);
580#else
581 clib_warning ("unimplemented");
582#endif
583
584 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
585}
586
587
588static void
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100589vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
590{
591 vl_api_ikev2_profile_add_del_reply_t *rmp;
592 int rv = 0;
593
Damjan Mariona9a951f2017-01-16 22:06:10 +0100594#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100595 vlib_main_t *vm = vlib_get_main ();
596 clib_error_t *error;
597 u8 *tmp = format (0, "%s", mp->name);
598 error = ikev2_add_del_profile (vm, tmp, mp->is_add);
599 vec_free (tmp);
600 if (error)
601 rv = VNET_API_ERROR_UNSPECIFIED;
602#else
603 rv = VNET_API_ERROR_UNIMPLEMENTED;
604#endif
605
606 REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
607}
608
609static void
610 vl_api_ikev2_profile_set_auth_t_handler
611 (vl_api_ikev2_profile_set_auth_t * mp)
612{
613 vl_api_ikev2_profile_set_auth_reply_t *rmp;
614 int rv = 0;
615
Damjan Mariona9a951f2017-01-16 22:06:10 +0100616#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100617 vlib_main_t *vm = vlib_get_main ();
618 clib_error_t *error;
619 u8 *tmp = format (0, "%s", mp->name);
620 u8 *data = vec_new (u8, mp->data_len);
621 clib_memcpy (data, mp->data, mp->data_len);
622 error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
623 vec_free (tmp);
624 vec_free (data);
625 if (error)
626 rv = VNET_API_ERROR_UNSPECIFIED;
627#else
628 rv = VNET_API_ERROR_UNIMPLEMENTED;
629#endif
630
631 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
632}
633
634static void
635vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
636{
637 vl_api_ikev2_profile_add_del_reply_t *rmp;
638 int rv = 0;
639
Damjan Mariona9a951f2017-01-16 22:06:10 +0100640#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100641 vlib_main_t *vm = vlib_get_main ();
642 clib_error_t *error;
643 u8 *tmp = format (0, "%s", mp->name);
644 u8 *data = vec_new (u8, mp->data_len);
645 clib_memcpy (data, mp->data, mp->data_len);
646 error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
647 vec_free (tmp);
648 vec_free (data);
649 if (error)
650 rv = VNET_API_ERROR_UNSPECIFIED;
651#else
652 rv = VNET_API_ERROR_UNIMPLEMENTED;
653#endif
654
655 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
656}
657
658static void
659vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
660{
661 vl_api_ikev2_profile_set_ts_reply_t *rmp;
662 int rv = 0;
663
Damjan Mariona9a951f2017-01-16 22:06:10 +0100664#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100665 vlib_main_t *vm = vlib_get_main ();
666 clib_error_t *error;
667 u8 *tmp = format (0, "%s", mp->name);
668 error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
669 mp->end_port, (ip4_address_t) mp->start_addr,
670 (ip4_address_t) mp->end_addr, mp->is_local);
671 vec_free (tmp);
672 if (error)
673 rv = VNET_API_ERROR_UNSPECIFIED;
674#else
675 rv = VNET_API_ERROR_UNIMPLEMENTED;
676#endif
677
678 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
679}
680
681static void
682vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
683{
684 vl_api_ikev2_profile_set_ts_reply_t *rmp;
685 int rv = 0;
686
Damjan Mariona9a951f2017-01-16 22:06:10 +0100687#if WITH_LIBSSL > 0
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100688 vlib_main_t *vm = vlib_get_main ();
689 clib_error_t *error;
690
691 error = ikev2_set_local_key (vm, mp->key_file);
692 if (error)
693 rv = VNET_API_ERROR_UNSPECIFIED;
694#else
695 rv = VNET_API_ERROR_UNIMPLEMENTED;
696#endif
697
698 REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
699}
700
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000701static void
702vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
703{
704 vl_api_ikev2_set_responder_reply_t *rmp;
705 int rv = 0;
706
707#if WITH_LIBSSL > 0
708 vlib_main_t *vm = vlib_get_main ();
709 clib_error_t *error;
710
711 u8 *tmp = format (0, "%s", mp->name);
712 ip4_address_t ip4;
713 clib_memcpy (&ip4, mp->address, sizeof (ip4));
714
715 error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
716 vec_free (tmp);
717 if (error)
718 rv = VNET_API_ERROR_UNSPECIFIED;
719#else
720 rv = VNET_API_ERROR_UNIMPLEMENTED;
721#endif
722
723 REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
724}
725
726static void
727vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
728 mp)
729{
730 vl_api_ikev2_set_ike_transforms_reply_t *rmp;
731 int rv = 0;
732
733#if WITH_LIBSSL > 0
734 vlib_main_t *vm = vlib_get_main ();
735 clib_error_t *error;
736
737 u8 *tmp = format (0, "%s", mp->name);
738
739 error =
740 ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
741 mp->dh_group, mp->crypto_key_size);
742 vec_free (tmp);
743 if (error)
744 rv = VNET_API_ERROR_UNSPECIFIED;
745#else
746 rv = VNET_API_ERROR_UNIMPLEMENTED;
747#endif
748
749 REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
750}
751
752static void
753vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
754 mp)
755{
756 vl_api_ikev2_set_esp_transforms_reply_t *rmp;
757 int rv = 0;
758
759#if WITH_LIBSSL > 0
760 vlib_main_t *vm = vlib_get_main ();
761 clib_error_t *error;
762
763 u8 *tmp = format (0, "%s", mp->name);
764
765 error =
766 ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
767 mp->dh_group, mp->crypto_key_size);
768 vec_free (tmp);
769 if (error)
770 rv = VNET_API_ERROR_UNSPECIFIED;
771#else
772 rv = VNET_API_ERROR_UNIMPLEMENTED;
773#endif
774
775 REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
776}
777
778static void
779vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
780{
781 vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
782 int rv = 0;
783
784#if WITH_LIBSSL > 0
785 vlib_main_t *vm = vlib_get_main ();
786 clib_error_t *error;
787
788 u8 *tmp = format (0, "%s", mp->name);
789
790 error =
791 ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
792 mp->handover, mp->lifetime_maxdata);
793 vec_free (tmp);
794 if (error)
795 rv = VNET_API_ERROR_UNSPECIFIED;
796#else
797 rv = VNET_API_ERROR_UNIMPLEMENTED;
798#endif
799
800 REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
801}
802
803static void
804vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
805{
806 vl_api_ikev2_initiate_sa_init_reply_t *rmp;
807 int rv = 0;
808
809#if WITH_LIBSSL > 0
810 vlib_main_t *vm = vlib_get_main ();
811 clib_error_t *error;
812
813 u8 *tmp = format (0, "%s", mp->name);
814
815 error = ikev2_initiate_sa_init (vm, tmp);
816 vec_free (tmp);
817 if (error)
818 rv = VNET_API_ERROR_UNSPECIFIED;
819#else
820 rv = VNET_API_ERROR_UNIMPLEMENTED;
821#endif
822
823 REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
824}
825
826static void
827vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
828 * mp)
829{
830 vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
831 int rv = 0;
832
833#if WITH_LIBSSL > 0
834 vlib_main_t *vm = vlib_get_main ();
835 clib_error_t *error;
836
837 error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
838 if (error)
839 rv = VNET_API_ERROR_UNSPECIFIED;
840#else
841 rv = VNET_API_ERROR_UNIMPLEMENTED;
842#endif
843
844 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
845}
846
847static void
848 vl_api_ikev2_initiate_del_child_sa_t_handler
849 (vl_api_ikev2_initiate_del_child_sa_t * mp)
850{
851 vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
852 int rv = 0;
853
854#if WITH_LIBSSL > 0
855 vlib_main_t *vm = vlib_get_main ();
856 clib_error_t *error;
857
858 error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
859 if (error)
860 rv = VNET_API_ERROR_UNSPECIFIED;
861#else
862 rv = VNET_API_ERROR_UNIMPLEMENTED;
863#endif
864
865 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
866}
867
868static void
869 vl_api_ikev2_initiate_rekey_child_sa_t_handler
870 (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
871{
872 vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
873 int rv = 0;
874
875#if WITH_LIBSSL > 0
876 vlib_main_t *vm = vlib_get_main ();
877 clib_error_t *error;
878
879 error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
880 if (error)
881 rv = VNET_API_ERROR_UNSPECIFIED;
882#else
883 rv = VNET_API_ERROR_UNIMPLEMENTED;
884#endif
885
886 REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
887}
888
Pavel Kotucek9c7ef032016-12-21 07:46:45 +0100889/*
890 * ipsec_api_hookup
891 * Add vpe's API message handlers to the table.
892 * vlib has alread mapped shared memory and
893 * added the client registration handlers.
894 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
895 */
896#define vl_msg_name_crc_list
897#include <vnet/vnet_all_api_h.h>
898#undef vl_msg_name_crc_list
899
900static void
901setup_message_id_table (api_main_t * am)
902{
903#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
904 foreach_vl_msg_name_crc_ipsec;
905#undef _
906}
907
908static clib_error_t *
909ipsec_api_hookup (vlib_main_t * vm)
910{
911 api_main_t *am = &api_main;
912
913#define _(N,n) \
914 vl_msg_api_set_handlers(VL_API_##N, #n, \
915 vl_api_##n##_t_handler, \
916 vl_noop_handler, \
917 vl_api_##n##_t_endian, \
918 vl_api_##n##_t_print, \
919 sizeof(vl_api_##n##_t), 1);
920 foreach_vpe_api_msg;
921#undef _
922
923 /*
924 * Set up the (msg_name, crc, message-id) table
925 */
926 setup_message_id_table (am);
927
928 return 0;
929}
930
931VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
932
933/*
934 * fd.io coding-style-patch-verification: ON
935 *
936 * Local Variables:
937 * eval: (c-set-style "gnu")
938 * End:
939 */