2 *------------------------------------------------------------------
3 * ipsec_api.c - ipsec api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/ip/ip_types_api.h>
27 #include <vnet/fib/fib.h>
29 #include <vnet/vnet_msg_enum.h>
32 #include <vnet/ipsec/ipsec.h>
35 #define vl_typedefs /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
39 #define vl_endianfun /* define message structures */
40 #include <vnet/vnet_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <vnet/vnet_all_api_h.h>
49 #include <vlibapi/api_helper_macros.h>
51 #define foreach_vpe_api_msg \
52 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
53 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
54 _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \
55 _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \
56 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key) \
57 _(IPSEC_SA_DUMP, ipsec_sa_dump) \
58 _(IPSEC_SPDS_DUMP, ipsec_spds_dump) \
59 _(IPSEC_SPD_DUMP, ipsec_spd_dump) \
60 _(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \
61 _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \
62 _(IPSEC_TUNNEL_IF_SET_KEY, ipsec_tunnel_if_set_key) \
63 _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \
64 _(IPSEC_SELECT_BACKEND, ipsec_select_backend) \
65 _(IPSEC_BACKEND_DUMP, ipsec_backend_dump)
68 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
71 clib_warning ("unimplemented");
74 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
75 vl_api_ipsec_spd_add_del_reply_t *rmp;
78 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
80 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
84 static void vl_api_ipsec_interface_add_del_spd_t_handler
85 (vl_api_ipsec_interface_add_del_spd_t * mp)
87 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
88 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
90 u32 sw_if_index __attribute__ ((unused));
91 u32 spd_id __attribute__ ((unused));
93 sw_if_index = ntohl (mp->sw_if_index);
94 spd_id = ntohl (mp->spd_id);
96 VALIDATE_SW_IF_INDEX (mp);
99 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
101 rv = VNET_API_ERROR_UNIMPLEMENTED;
104 BAD_SW_IF_INDEX_LABEL;
106 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
110 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
111 ipsec_policy_action_t * out)
113 in = clib_net_to_host_u32 (in);
117 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
118 *out = IPSEC_POLICY_ACTION_##f; \
120 foreach_ipsec_policy_action
123 return (VNET_API_ERROR_UNIMPLEMENTED);
126 static void vl_api_ipsec_spd_entry_add_del_t_handler
127 (vl_api_ipsec_spd_entry_add_del_t * mp)
129 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
130 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
140 clib_memset (&p, 0, sizeof (p));
142 p.id = ntohl (mp->entry.spd_id);
143 p.priority = ntohl (mp->entry.priority);
145 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
146 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
147 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
148 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
150 p.is_ipv6 = (itype == IP46_TYPE_IP6);
152 p.protocol = mp->entry.protocol;
153 p.rport.start = ntohs (mp->entry.remote_port_start);
154 p.rport.stop = ntohs (mp->entry.remote_port_stop);
155 p.lport.start = ntohs (mp->entry.local_port_start);
156 p.lport.stop = ntohs (mp->entry.local_port_stop);
158 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
163 /* policy action resolve unsupported */
164 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
166 clib_warning ("unsupported action: 'resolve'");
167 rv = VNET_API_ERROR_UNIMPLEMENTED;
170 p.sa_id = ntohl (mp->entry.sa_id);
172 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
177 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
182 rv = VNET_API_ERROR_UNIMPLEMENTED;
188 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
190 rmp->stat_index = ntohl(stat_index);
196 ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
198 in = clib_net_to_host_u32 (in);
202 case IPSEC_API_PROTO_ESP:
203 *out = IPSEC_PROTOCOL_ESP;
205 case IPSEC_API_PROTO_AH:
206 *out = IPSEC_PROTOCOL_AH;
209 return (VNET_API_ERROR_INVALID_PROTOCOL);
212 static vl_api_ipsec_proto_t
213 ipsec_proto_encode (ipsec_protocol_t p)
217 case IPSEC_PROTOCOL_ESP:
218 return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP);
219 case IPSEC_PROTOCOL_AH:
220 return clib_host_to_net_u32 (IPSEC_API_PROTO_AH);
222 return (VNET_API_ERROR_UNIMPLEMENTED);
226 ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
227 ipsec_crypto_alg_t * out)
229 in = clib_net_to_host_u32 (in);
233 #define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
234 *out = IPSEC_CRYPTO_ALG_##f; \
236 foreach_ipsec_crypto_alg
239 return (VNET_API_ERROR_INVALID_ALGORITHM);
242 static vl_api_ipsec_crypto_alg_t
243 ipsec_crypto_algo_encode (ipsec_crypto_alg_t c)
247 #define _(v,f,s) case IPSEC_CRYPTO_ALG_##f: \
248 return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f);
249 foreach_ipsec_crypto_alg
251 case IPSEC_CRYPTO_N_ALG:
255 return (VNET_API_ERROR_UNIMPLEMENTED);
260 ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
262 in = clib_net_to_host_u32 (in);
266 #define _(v,f,s) case IPSEC_API_INTEG_ALG_##f: \
267 *out = IPSEC_INTEG_ALG_##f; \
269 foreach_ipsec_integ_alg
272 return (VNET_API_ERROR_INVALID_ALGORITHM);
275 static vl_api_ipsec_integ_alg_t
276 ipsec_integ_algo_encode (ipsec_integ_alg_t i)
280 #define _(v,f,s) case IPSEC_INTEG_ALG_##f: \
281 return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f));
282 foreach_ipsec_integ_alg
284 case IPSEC_INTEG_N_ALG:
288 return (VNET_API_ERROR_UNIMPLEMENTED);
292 ipsec_key_decode (const vl_api_key_t * key, ipsec_key_t * out)
294 ipsec_mk_key (out, key->data, key->length);
298 ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out)
300 out->length = in->len;
301 clib_memcpy (out->data, in->data, out->length);
304 static ipsec_sa_flags_t
305 ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in)
307 ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE;
308 in = clib_net_to_host_u32 (in);
310 if (in & IPSEC_API_SAD_FLAG_USE_ESN)
311 flags |= IPSEC_SA_FLAG_USE_ESN;
312 if (in & IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY)
313 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
314 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL)
315 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
316 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL_V6)
317 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
318 if (in & IPSEC_API_SAD_FLAG_UDP_ENCAP)
319 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
324 static vl_api_ipsec_sad_flags_t
325 ipsec_sad_flags_encode (const ipsec_sa_t * sa)
327 vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE;
329 if (ipsec_sa_is_set_USE_ESN (sa))
330 flags |= IPSEC_API_SAD_FLAG_USE_ESN;
331 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
332 flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
333 if (ipsec_sa_is_set_IS_TUNNEL (sa))
334 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
335 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
336 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
337 if (ipsec_sa_is_set_UDP_ENCAP (sa))
338 flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
340 return clib_host_to_net_u32 (flags);
343 static void vl_api_ipsec_sad_entry_add_del_t_handler
344 (vl_api_ipsec_sad_entry_add_del_t * mp)
346 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
347 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
348 ip46_address_t tun_src = { }, tun_dst =
351 ipsec_key_t crypto_key, integ_key;
352 ipsec_crypto_alg_t crypto_alg;
353 ipsec_integ_alg_t integ_alg;
354 ipsec_protocol_t proto;
355 ipsec_sa_flags_t flags;
356 u32 id, spi, sa_index = ~0;
361 id = ntohl (mp->entry.sad_id);
362 spi = ntohl (mp->entry.spi);
364 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
369 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
374 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
379 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
380 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
382 flags = ipsec_sa_flags_decode (mp->entry.flags);
384 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
385 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
389 rv = ipsec_sa_add (id, spi, proto,
390 crypto_alg, &crypto_key,
391 integ_alg, &integ_key, flags,
392 0, 0, &tun_src, &tun_dst, &sa_index);
394 rv = ipsec_sa_del (id);
397 rv = VNET_API_ERROR_UNIMPLEMENTED;
402 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
404 rmp->stat_index = htonl (sa_index);
410 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
413 vl_api_ipsec_spds_details_t *mp;
416 mp = vl_msg_api_alloc (sizeof (*mp));
417 clib_memset (mp, 0, sizeof (*mp));
418 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
419 mp->context = context;
421 mp->spd_id = htonl (spd->id);
422 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
423 foreach_ipsec_spd_policy_type
425 mp->npolicies = htonl (n_policies);
427 vl_api_send_msg (reg, (u8 *) mp);
431 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
433 vl_api_registration_t *reg;
434 ipsec_main_t *im = &ipsec_main;
437 reg = vl_api_client_index_to_registration (mp->client_index);
442 pool_foreach (spd, im->spds, ({
443 send_ipsec_spds_details (spd, reg, mp->context);
447 clib_warning ("unimplemented");
451 vl_api_ipsec_spd_action_t
452 ipsec_spd_action_encode (ipsec_policy_action_t in)
454 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
458 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
459 out = IPSEC_API_SPD_ACTION_##f; \
461 foreach_ipsec_policy_action
464 return (clib_host_to_net_u32 (out));
468 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
471 vl_api_ipsec_spd_details_t *mp;
473 mp = vl_msg_api_alloc (sizeof (*mp));
474 clib_memset (mp, 0, sizeof (*mp));
475 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
476 mp->context = context;
478 mp->entry.spd_id = htonl (p->id);
479 mp->entry.priority = htonl (p->priority);
480 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
481 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
483 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
484 &mp->entry.local_address_start);
485 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
486 &mp->entry.local_address_stop);
487 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
488 &mp->entry.remote_address_start);
489 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
490 &mp->entry.remote_address_stop);
491 mp->entry.local_port_start = htons (p->lport.start);
492 mp->entry.local_port_stop = htons (p->lport.stop);
493 mp->entry.remote_port_start = htons (p->rport.start);
494 mp->entry.remote_port_stop = htons (p->rport.stop);
495 mp->entry.protocol = p->protocol;
496 mp->entry.policy = ipsec_spd_action_encode (p->policy);
497 mp->entry.sa_id = htonl (p->sa_id);
499 vl_api_send_msg (reg, (u8 *) mp);
503 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
505 vl_api_registration_t *reg;
506 ipsec_main_t *im = &ipsec_main;
507 ipsec_spd_policy_type_t ptype;
508 ipsec_policy_t *policy;
513 reg = vl_api_client_index_to_registration (mp->client_index);
517 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
522 spd = pool_elt_at_index (im->spds, spd_index);
525 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
526 vec_foreach(ii, spd->policies[ptype])
528 policy = pool_elt_at_index(im->policies, *ii);
530 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
531 send_ipsec_spd_details (policy, reg, mp->context);
536 clib_warning ("unimplemented");
541 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
542 u32 sw_if_index, u32 context)
544 vl_api_ipsec_spd_interface_details_t *mp;
546 mp = vl_msg_api_alloc (sizeof (*mp));
547 clib_memset (mp, 0, sizeof (*mp));
548 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
549 mp->context = context;
551 mp->spd_index = htonl (spd_index);
552 mp->sw_if_index = htonl (sw_if_index);
554 vl_api_send_msg (reg, (u8 *) mp);
558 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
561 ipsec_main_t *im = &ipsec_main;
562 vl_api_registration_t *reg;
566 reg = vl_api_client_index_to_registration (mp->client_index);
570 if (mp->spd_index_valid)
572 spd_index = ntohl (mp->spd_index);
574 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
576 send_ipsec_spd_interface_details(reg, v, k, mp->context);
583 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
584 send_ipsec_spd_interface_details(reg, v, k, mp->context);
590 clib_warning ("unimplemented");
595 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
597 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
598 vl_api_ipsec_sa_set_key_reply_t *rmp;
604 id = ntohl (mp->sa_id);
606 ipsec_key_decode (&mp->crypto_key, &ck);
607 ipsec_key_decode (&mp->integrity_key, &ik);
609 rv = ipsec_set_sa_key (id, &ck, &ik);
611 rv = VNET_API_ERROR_UNIMPLEMENTED;
614 REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
618 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
621 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
622 ipsec_main_t *im = &ipsec_main;
623 vnet_main_t *vnm = im->vnet_main;
624 u32 sw_if_index = ~0;
629 ipsec_add_del_tunnel_args_t tun;
631 clib_memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
633 tun.is_add = mp->is_add;
635 tun.anti_replay = mp->anti_replay;
636 tun.local_spi = ntohl (mp->local_spi);
637 tun.remote_spi = ntohl (mp->remote_spi);
638 tun.crypto_alg = mp->crypto_alg;
639 tun.local_crypto_key_len = mp->local_crypto_key_len;
640 tun.remote_crypto_key_len = mp->remote_crypto_key_len;
641 tun.integ_alg = mp->integ_alg;
642 tun.local_integ_key_len = mp->local_integ_key_len;
643 tun.remote_integ_key_len = mp->remote_integ_key_len;
644 tun.udp_encap = mp->udp_encap;
645 tun.tx_table_id = ntohl (mp->tx_table_id);
646 itype = ip_address_decode (&mp->local_ip, &tun.local_ip);
647 itype = ip_address_decode (&mp->remote_ip, &tun.remote_ip);
648 tun.is_ip6 = (IP46_TYPE_IP6 == itype);
649 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);
657 tun.renumber = mp->renumber;
658 tun.show_instance = ntohl (mp->show_instance);
660 rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
663 rv = VNET_API_ERROR_UNIMPLEMENTED;
667 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
669 rmp->sw_if_index = htonl (sw_if_index);
675 send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
676 u32 context, u32 sw_if_index)
678 vl_api_ipsec_sa_details_t *mp;
680 mp = vl_msg_api_alloc (sizeof (*mp));
681 clib_memset (mp, 0, sizeof (*mp));
682 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
683 mp->context = context;
685 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));
691 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
692 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
694 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
695 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
697 mp->entry.flags = ipsec_sad_flags_encode (sa);
699 if (ipsec_sa_is_set_IS_TUNNEL (sa))
701 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);
707 mp->sw_if_index = htonl (sw_if_index);
708 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 (ipsec_sa_is_set_USE_ESN (sa))
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));
716 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
717 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
719 vl_api_send_msg (reg, (u8 *) mp);
724 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
726 vl_api_registration_t *reg;
727 ipsec_main_t *im = &ipsec_main;
728 vnet_main_t *vnm = im->vnet_main;
730 ipsec_tunnel_if_t *t;
731 u32 *sa_index_to_tun_if_index = 0;
734 reg = vl_api_client_index_to_registration (mp->client_index);
735 if (!reg || pool_elts (im->sad) == 0)
738 vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
742 pool_foreach (t, im->tunnel_interfaces,
744 vnet_hw_interface_t *hi;
745 u32 sw_if_index = ~0;
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;
753 pool_foreach (sa, im->sad,
755 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
756 send_ipsec_sa_details (sa, reg, mp->context,
757 sa_index_to_tun_if_index[sa - im->sad]);
761 vec_free (sa_index_to_tun_if_index);
763 clib_warning ("unimplemented");
769 vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t *
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;
780 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
782 switch (mp->key_type)
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 ||
787 mp->alg >= IPSEC_CRYPTO_N_ALG)
789 rv = VNET_API_ERROR_INVALID_ALGORITHM;
793 case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG:
794 case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG:
795 if (mp->alg >= IPSEC_INTEG_N_ALG)
797 rv = VNET_API_ERROR_INVALID_ALGORITHM;
801 case IPSEC_IF_SET_KEY_TYPE_NONE:
803 rv = VNET_API_ERROR_UNIMPLEMENTED;
808 key = vec_new (u8, mp->key_len);
809 clib_memcpy (key, mp->key, mp->key_len);
811 rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg,
815 clib_warning ("unimplemented");
819 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY);
824 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
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;
833 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
835 rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
838 clib_warning ("unimplemented");
841 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
845 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
847 vl_api_registration_t *rp;
848 ipsec_main_t *im = &ipsec_main;
849 u32 context = mp->context;
851 rp = vl_api_client_index_to_registration (mp->client_index);
855 clib_warning ("Client %d AWOL", mp->client_index);
859 ipsec_ah_backend_t *ab;
860 ipsec_esp_backend_t *eb;
862 pool_foreach (ab, im->ah_backends, {
863 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
864 clib_memset (mp, 0, sizeof (*mp));
865 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
866 mp->context = context;
867 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
869 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
870 mp->index = ab - im->ah_backends;
871 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
872 vl_api_send_msg (rp, (u8 *)mp);
874 pool_foreach (eb, im->esp_backends, {
875 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
876 clib_memset (mp, 0, sizeof (*mp));
877 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
878 mp->context = context;
879 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
881 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
882 mp->index = eb - im->esp_backends;
883 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
884 vl_api_send_msg (rp, (u8 *)mp);
890 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
892 ipsec_main_t *im = &ipsec_main;
893 vl_api_ipsec_select_backend_reply_t *rmp;
894 ipsec_protocol_t protocol;
896 if (pool_elts (im->sad) > 0)
898 rv = VNET_API_ERROR_INSTANCE_IN_USE;
902 rv = ipsec_proto_decode (mp->protocol, &protocol);
910 case IPSEC_PROTOCOL_ESP:
911 rv = ipsec_select_esp_backend (im, mp->index);
913 case IPSEC_PROTOCOL_AH:
914 rv = ipsec_select_ah_backend (im, mp->index);
917 rv = VNET_API_ERROR_INVALID_PROTOCOL;
921 clib_warning ("unimplemented"); /* FIXME */
924 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
929 * Add vpe's API message handlers to the table.
930 * vlib has already mapped shared memory and
931 * added the client registration handlers.
932 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
934 #define vl_msg_name_crc_list
935 #include <vnet/vnet_all_api_h.h>
936 #undef vl_msg_name_crc_list
939 setup_message_id_table (api_main_t * am)
941 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
942 foreach_vl_msg_name_crc_ipsec;
946 static clib_error_t *
947 ipsec_api_hookup (vlib_main_t * vm)
949 api_main_t *am = &api_main;
952 vl_msg_api_set_handlers(VL_API_##N, #n, \
953 vl_api_##n##_t_handler, \
955 vl_api_##n##_t_endian, \
956 vl_api_##n##_t_print, \
957 sizeof(vl_api_##n##_t), 1);
962 * Set up the (msg_name, crc, message-id) table
964 setup_message_id_table (am);
969 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
972 * fd.io coding-style-patch-verification: ON
975 * eval: (c-set-style "gnu")