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/ipsec/ipsec_types_api.h>
28 #include <vnet/fib/fib.h>
29 #include <vnet/ipip/ipip.h>
31 #include <vnet/vnet_msg_enum.h>
34 #include <vnet/ipsec/ipsec.h>
35 #include <vnet/ipsec/ipsec_tun.h>
38 #define vl_typedefs /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
42 #define vl_endianfun /* define message structures */
43 #include <vnet/vnet_all_api_h.h>
46 /* instantiate all the print functions we know about */
47 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
49 #include <vnet/vnet_all_api_h.h>
52 #include <vlibapi/api_helper_macros.h>
54 #define foreach_vpe_api_msg \
55 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
56 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
57 _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \
58 _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \
59 _(IPSEC_SA_DUMP, ipsec_sa_dump) \
60 _(IPSEC_SPDS_DUMP, ipsec_spds_dump) \
61 _(IPSEC_SPD_DUMP, ipsec_spd_dump) \
62 _(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \
63 _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \
64 _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \
65 _(IPSEC_SELECT_BACKEND, ipsec_select_backend) \
66 _(IPSEC_BACKEND_DUMP, ipsec_backend_dump) \
67 _(IPSEC_TUNNEL_PROTECT_UPDATE, ipsec_tunnel_protect_update) \
68 _(IPSEC_TUNNEL_PROTECT_DEL, ipsec_tunnel_protect_del) \
69 _(IPSEC_TUNNEL_PROTECT_DUMP, ipsec_tunnel_protect_dump)
72 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
75 clib_warning ("unimplemented");
78 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
79 vl_api_ipsec_spd_add_del_reply_t *rmp;
82 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
84 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
88 static void vl_api_ipsec_interface_add_del_spd_t_handler
89 (vl_api_ipsec_interface_add_del_spd_t * mp)
91 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
92 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
94 u32 sw_if_index __attribute__ ((unused));
95 u32 spd_id __attribute__ ((unused));
97 sw_if_index = ntohl (mp->sw_if_index);
98 spd_id = ntohl (mp->spd_id);
100 VALIDATE_SW_IF_INDEX (mp);
103 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
105 rv = VNET_API_ERROR_UNIMPLEMENTED;
108 BAD_SW_IF_INDEX_LABEL;
110 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
113 static void vl_api_ipsec_tunnel_protect_update_t_handler
114 (vl_api_ipsec_tunnel_protect_update_t * mp)
116 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
117 vl_api_ipsec_tunnel_protect_update_reply_t *rmp;
118 u32 sw_if_index, ii, *sa_ins = NULL;
122 sw_if_index = ntohl (mp->tunnel.sw_if_index);
124 VALIDATE_SW_IF_INDEX (&(mp->tunnel));
128 for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
129 vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
131 ip_address_decode2 (&mp->tunnel.nh, &nh);
133 rv = ipsec_tun_protect_update (sw_if_index, &nh,
134 ntohl (mp->tunnel.sa_out), sa_ins);
136 rv = VNET_API_ERROR_UNIMPLEMENTED;
139 BAD_SW_IF_INDEX_LABEL;
141 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
144 static void vl_api_ipsec_tunnel_protect_del_t_handler
145 (vl_api_ipsec_tunnel_protect_del_t * mp)
147 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
148 vl_api_ipsec_tunnel_protect_del_reply_t *rmp;
153 sw_if_index = ntohl (mp->sw_if_index);
155 VALIDATE_SW_IF_INDEX (mp);
158 ip_address_decode2 (&mp->nh, &nh);
159 rv = ipsec_tun_protect_del (sw_if_index, &nh);
161 rv = VNET_API_ERROR_UNIMPLEMENTED;
164 BAD_SW_IF_INDEX_LABEL;
166 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
169 typedef struct ipsec_dump_walk_ctx_t_
171 vl_api_registration_t *reg;
173 } ipsec_dump_walk_ctx_t;
176 send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
178 ipsec_dump_walk_ctx_t *ctx = arg;
179 vl_api_ipsec_tunnel_protect_details_t *mp;
180 ipsec_tun_protect_t *itp;
183 itp = ipsec_tun_protect_get (itpi);
185 mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
186 clib_memset (mp, 0, sizeof (*mp));
187 mp->_vl_msg_id = ntohs (VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
188 mp->context = ctx->context;
190 mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
191 ip_address_encode2 (itp->itp_key, &mp->tun.nh);
193 mp->tun.sa_out = htonl (itp->itp_out_sa);
194 mp->tun.n_sa_in = itp->itp_n_sa_in;
196 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
198 mp->tun.sa_in[ii++] = htonl (sai);
202 vl_api_send_msg (ctx->reg, (u8 *) mp);
204 return (WALK_CONTINUE);
208 vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
211 vl_api_registration_t *reg;
215 reg = vl_api_client_index_to_registration (mp->client_index);
219 ipsec_dump_walk_ctx_t ctx = {
221 .context = mp->context,
224 sw_if_index = ntohl (mp->sw_if_index);
226 if (~0 == sw_if_index)
228 ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
232 ipsec_tun_protect_walk_itf (sw_if_index,
233 send_ipsec_tunnel_protect_details, &ctx);
236 clib_warning ("unimplemented");
241 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
242 ipsec_policy_action_t * out)
244 in = clib_net_to_host_u32 (in);
248 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
249 *out = IPSEC_POLICY_ACTION_##f; \
251 foreach_ipsec_policy_action
254 return (VNET_API_ERROR_UNIMPLEMENTED);
257 static void vl_api_ipsec_spd_entry_add_del_t_handler
258 (vl_api_ipsec_spd_entry_add_del_t * mp)
260 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
261 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
271 clib_memset (&p, 0, sizeof (p));
273 p.id = ntohl (mp->entry.spd_id);
274 p.priority = ntohl (mp->entry.priority);
276 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
277 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
278 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
279 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
281 p.is_ipv6 = (itype == IP46_TYPE_IP6);
283 p.protocol = mp->entry.protocol;
284 p.rport.start = ntohs (mp->entry.remote_port_start);
285 p.rport.stop = ntohs (mp->entry.remote_port_stop);
286 p.lport.start = ntohs (mp->entry.local_port_start);
287 p.lport.stop = ntohs (mp->entry.local_port_stop);
289 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
294 /* policy action resolve unsupported */
295 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
297 clib_warning ("unsupported action: 'resolve'");
298 rv = VNET_API_ERROR_UNIMPLEMENTED;
301 p.sa_id = ntohl (mp->entry.sa_id);
303 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
308 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
313 rv = VNET_API_ERROR_UNIMPLEMENTED;
319 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
321 rmp->stat_index = ntohl(stat_index);
326 static void vl_api_ipsec_sad_entry_add_del_t_handler
327 (vl_api_ipsec_sad_entry_add_del_t * mp)
329 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
330 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
331 ip46_address_t tun_src = { }, tun_dst =
334 ipsec_key_t crypto_key, integ_key;
335 ipsec_crypto_alg_t crypto_alg;
336 ipsec_integ_alg_t integ_alg;
337 ipsec_protocol_t proto;
338 ipsec_sa_flags_t flags;
339 u32 id, spi, sa_index = ~0;
344 id = ntohl (mp->entry.sad_id);
345 spi = ntohl (mp->entry.spi);
347 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
352 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
357 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
362 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
363 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
365 flags = ipsec_sa_flags_decode (mp->entry.flags);
367 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
368 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
371 rv = ipsec_sa_add_and_lock (id, spi, proto,
372 crypto_alg, &crypto_key,
373 integ_alg, &integ_key, flags,
374 0, mp->entry.salt, &tun_src, &tun_dst,
377 rv = ipsec_sa_unlock_id (id);
380 rv = VNET_API_ERROR_UNIMPLEMENTED;
385 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
387 rmp->stat_index = htonl (sa_index);
393 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
396 vl_api_ipsec_spds_details_t *mp;
399 mp = vl_msg_api_alloc (sizeof (*mp));
400 clib_memset (mp, 0, sizeof (*mp));
401 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
402 mp->context = context;
404 mp->spd_id = htonl (spd->id);
405 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
406 foreach_ipsec_spd_policy_type
408 mp->npolicies = htonl (n_policies);
410 vl_api_send_msg (reg, (u8 *) mp);
414 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
416 vl_api_registration_t *reg;
417 ipsec_main_t *im = &ipsec_main;
420 reg = vl_api_client_index_to_registration (mp->client_index);
425 pool_foreach (spd, im->spds, ({
426 send_ipsec_spds_details (spd, reg, mp->context);
430 clib_warning ("unimplemented");
434 vl_api_ipsec_spd_action_t
435 ipsec_spd_action_encode (ipsec_policy_action_t in)
437 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
441 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
442 out = IPSEC_API_SPD_ACTION_##f; \
444 foreach_ipsec_policy_action
447 return (clib_host_to_net_u32 (out));
451 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
454 vl_api_ipsec_spd_details_t *mp;
456 mp = vl_msg_api_alloc (sizeof (*mp));
457 clib_memset (mp, 0, sizeof (*mp));
458 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
459 mp->context = context;
461 mp->entry.spd_id = htonl (p->id);
462 mp->entry.priority = htonl (p->priority);
463 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
464 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
466 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
467 &mp->entry.local_address_start);
468 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
469 &mp->entry.local_address_stop);
470 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
471 &mp->entry.remote_address_start);
472 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
473 &mp->entry.remote_address_stop);
474 mp->entry.local_port_start = htons (p->lport.start);
475 mp->entry.local_port_stop = htons (p->lport.stop);
476 mp->entry.remote_port_start = htons (p->rport.start);
477 mp->entry.remote_port_stop = htons (p->rport.stop);
478 mp->entry.protocol = p->protocol;
479 mp->entry.policy = ipsec_spd_action_encode (p->policy);
480 mp->entry.sa_id = htonl (p->sa_id);
482 vl_api_send_msg (reg, (u8 *) mp);
486 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
488 vl_api_registration_t *reg;
489 ipsec_main_t *im = &ipsec_main;
490 ipsec_spd_policy_type_t ptype;
491 ipsec_policy_t *policy;
496 reg = vl_api_client_index_to_registration (mp->client_index);
500 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
505 spd = pool_elt_at_index (im->spds, spd_index);
508 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
509 vec_foreach(ii, spd->policies[ptype])
511 policy = pool_elt_at_index(im->policies, *ii);
513 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
514 send_ipsec_spd_details (policy, reg, mp->context);
519 clib_warning ("unimplemented");
524 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
525 u32 sw_if_index, u32 context)
527 vl_api_ipsec_spd_interface_details_t *mp;
529 mp = vl_msg_api_alloc (sizeof (*mp));
530 clib_memset (mp, 0, sizeof (*mp));
531 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
532 mp->context = context;
534 mp->spd_index = htonl (spd_index);
535 mp->sw_if_index = htonl (sw_if_index);
537 vl_api_send_msg (reg, (u8 *) mp);
541 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
544 ipsec_main_t *im = &ipsec_main;
545 vl_api_registration_t *reg;
549 reg = vl_api_client_index_to_registration (mp->client_index);
553 if (mp->spd_index_valid)
555 spd_index = ntohl (mp->spd_index);
557 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
559 send_ipsec_spd_interface_details(reg, v, k, mp->context);
566 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
567 send_ipsec_spd_interface_details(reg, v, k, mp->context);
573 clib_warning ("unimplemented");
578 ipsec_tun_mk_input_sa_id (u32 ti)
580 return (0x80000000 | ti);
584 ipsec_tun_mk_output_sa_id (u32 ti)
586 return (0xc0000000 | ti);
590 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
593 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
594 u32 sw_if_index = ~0;
598 ip46_address_t local_ip = ip46_address_initializer;
599 ip46_address_t remote_ip = ip46_address_initializer;
600 ipsec_key_t crypto_key, integ_key;
601 ipsec_sa_flags_t flags;
602 ip46_type_t local_ip_type, remote_ip_type;
603 ipip_transport_t transport;
606 local_ip_type = ip_address_decode (&mp->local_ip, &local_ip);
607 remote_ip_type = ip_address_decode (&mp->remote_ip, &remote_ip);
608 transport = (IP46_TYPE_IP6 == local_ip_type ?
609 IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4);
611 if (local_ip_type != remote_ip_type)
613 rv = VNET_API_ERROR_INVALID_VALUE;
617 flags = IPSEC_SA_FLAG_NONE;
620 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
622 flags |= IPSEC_SA_FLAG_USE_ESN;
624 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
626 ipsec_mk_key (&crypto_key, mp->remote_crypto_key,
627 mp->remote_crypto_key_len);
628 ipsec_mk_key (&integ_key, mp->remote_integ_key, mp->remote_integ_key_len);
629 ipsec_mk_key (&crypto_key, mp->local_crypto_key, mp->local_crypto_key_len);
630 ipsec_mk_key (&integ_key, mp->local_integ_key, mp->local_integ_key_len);
633 fib_table_find (fib_proto_from_ip46 (local_ip_type),
634 ntohl (mp->tx_table_id));
638 rv = VNET_API_ERROR_NO_SUCH_FIB;
644 // remote = input, local = output
645 /* create an ip-ip tunnel, then the two SA, then bind them */
646 rv = ipip_add_tunnel (transport,
647 (mp->renumber ? ntohl (mp->show_instance) : ~0),
649 &remote_ip, fib_index,
650 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
651 TUNNEL_MODE_P2P, &sw_if_index);
656 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_input_sa_id (sw_if_index),
657 ntohl (mp->remote_spi),
663 (flags | IPSEC_SA_FLAG_IS_INBOUND),
664 ntohl (mp->tx_table_id),
665 mp->salt, &remote_ip, &local_ip, NULL);
670 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_output_sa_id (sw_if_index),
671 ntohl (mp->local_spi),
678 ntohl (mp->tx_table_id),
679 mp->salt, &local_ip, &remote_ip, NULL);
684 rv = ipsec_tun_protect_update_one (sw_if_index, NULL,
685 ipsec_tun_mk_output_sa_id
687 ipsec_tun_mk_input_sa_id
692 /* the SAs are locked as a result of being used for proection,
693 * they cannot be removed from the API, since they cannot be refered
694 * to by the API. unlock them now, so that if the tunnel is rekeyed
697 ipsec_sa_unlock_id (ipsec_tun_mk_input_sa_id (sw_if_index));
698 ipsec_sa_unlock_id (ipsec_tun_mk_output_sa_id (sw_if_index));
703 ipip_tunnel_key_t key = {
704 .transport = transport,
705 .fib_index = fib_index,
711 ipip_tunnel_t *t = ipip_tunnel_db_find (&key);
715 rv = ipsec_tun_protect_del (t->sw_if_index, NULL);
716 ipip_del_tunnel (t->sw_if_index);
719 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
723 rv = VNET_API_ERROR_UNIMPLEMENTED;
727 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
729 rmp->sw_if_index = htonl (sw_if_index);
734 typedef struct ipsec_sa_dump_match_ctx_t_
738 } ipsec_sa_dump_match_ctx_t;
741 ipsec_sa_dump_match_sa (index_t itpi, void *arg)
743 ipsec_sa_dump_match_ctx_t *ctx = arg;
744 ipsec_tun_protect_t *itp;
747 itp = ipsec_tun_protect_get (itpi);
749 if (itp->itp_out_sa == ctx->sai)
751 ctx->sw_if_index = itp->itp_sw_if_index;
755 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
759 ctx->sw_if_index = itp->itp_sw_if_index;
765 return (WALK_CONTINUE);
769 send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
771 ipsec_dump_walk_ctx_t *ctx = arg;
772 vl_api_ipsec_sa_details_t *mp;
773 ipsec_main_t *im = &ipsec_main;
775 mp = vl_msg_api_alloc (sizeof (*mp));
776 clib_memset (mp, 0, sizeof (*mp));
777 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
778 mp->context = ctx->context;
780 mp->entry.sad_id = htonl (sa->id);
781 mp->entry.spi = htonl (sa->spi);
782 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
783 mp->entry.tx_table_id =
784 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
786 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
787 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
789 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
790 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
792 mp->entry.flags = ipsec_sad_flags_encode (sa);
793 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
795 if (ipsec_sa_is_set_IS_PROTECT (sa))
797 ipsec_sa_dump_match_ctx_t ctx = {
801 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
803 mp->sw_if_index = htonl (ctx.sw_if_index);
806 mp->sw_if_index = ~0;
808 if (ipsec_sa_is_set_IS_TUNNEL (sa))
810 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
811 &mp->entry.tunnel_src);
812 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
813 &mp->entry.tunnel_dst);
816 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
817 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
818 if (ipsec_sa_is_set_USE_ESN (sa))
820 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
821 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
823 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
824 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
826 vl_api_send_msg (ctx->reg, (u8 *) mp);
828 return (WALK_CONTINUE);
832 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
834 vl_api_registration_t *reg;
837 reg = vl_api_client_index_to_registration (mp->client_index);
841 ipsec_dump_walk_ctx_t ctx = {
843 .context = mp->context,
846 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
849 clib_warning ("unimplemented");
854 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
856 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
860 VALIDATE_SW_IF_INDEX(mp);
863 rv = ipsec_tun_protect_update_out (ntohl (mp->sw_if_index), NULL,
866 rv = ipsec_tun_protect_update_in (ntohl (mp->sw_if_index), NULL,
870 clib_warning ("unimplemented");
873 BAD_SW_IF_INDEX_LABEL;
875 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
879 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
881 vl_api_registration_t *rp;
882 ipsec_main_t *im = &ipsec_main;
883 u32 context = mp->context;
885 rp = vl_api_client_index_to_registration (mp->client_index);
889 clib_warning ("Client %d AWOL", mp->client_index);
893 ipsec_ah_backend_t *ab;
894 ipsec_esp_backend_t *eb;
896 pool_foreach (ab, im->ah_backends, {
897 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
898 clib_memset (mp, 0, sizeof (*mp));
899 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
900 mp->context = context;
901 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
903 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
904 mp->index = ab - im->ah_backends;
905 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
906 vl_api_send_msg (rp, (u8 *)mp);
908 pool_foreach (eb, im->esp_backends, {
909 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
910 clib_memset (mp, 0, sizeof (*mp));
911 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
912 mp->context = context;
913 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
915 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
916 mp->index = eb - im->esp_backends;
917 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
918 vl_api_send_msg (rp, (u8 *)mp);
924 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
926 ipsec_main_t *im = &ipsec_main;
927 vl_api_ipsec_select_backend_reply_t *rmp;
928 ipsec_protocol_t protocol;
930 if (pool_elts (im->sad) > 0)
932 rv = VNET_API_ERROR_INSTANCE_IN_USE;
936 rv = ipsec_proto_decode (mp->protocol, &protocol);
944 case IPSEC_PROTOCOL_ESP:
945 rv = ipsec_select_esp_backend (im, mp->index);
947 case IPSEC_PROTOCOL_AH:
948 rv = ipsec_select_ah_backend (im, mp->index);
951 rv = VNET_API_ERROR_INVALID_PROTOCOL;
955 clib_warning ("unimplemented"); /* FIXME */
958 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
963 * Add vpe's API message handlers to the table.
964 * vlib has already mapped shared memory and
965 * added the client registration handlers.
966 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
968 #define vl_msg_name_crc_list
969 #include <vnet/vnet_all_api_h.h>
970 #undef vl_msg_name_crc_list
973 setup_message_id_table (api_main_t * am)
975 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
976 foreach_vl_msg_name_crc_ipsec;
980 static clib_error_t *
981 ipsec_api_hookup (vlib_main_t * vm)
983 api_main_t *am = vlibapi_get_main ();
986 vl_msg_api_set_handlers(VL_API_##N, #n, \
987 vl_api_##n##_t_handler, \
989 vl_api_##n##_t_endian, \
990 vl_api_##n##_t_print, \
991 sizeof(vl_api_##n##_t), 1);
996 * Adding and deleting SAs is MP safe since when they are added/delete
997 * no traffic is using them
999 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL] = 1;
1000 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY] = 1;
1003 * Set up the (msg_name, crc, message-id) table
1005 setup_message_id_table (am);
1010 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1013 * fd.io coding-style-patch-verification: ON
1016 * eval: (c-set-style "gnu")