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/tunnel/tunnel_types_api.h>
29 #include <vnet/fib/fib.h>
30 #include <vnet/ipip/ipip.h>
31 #include <vnet/tunnel/tunnel_types_api.h>
32 #include <vnet/ipsec/ipsec.h>
33 #include <vnet/ipsec/ipsec_tun.h>
34 #include <vnet/ipsec/ipsec_itf.h>
36 #include <vnet/format_fns.h>
37 #include <vnet/ipsec/ipsec.api_enum.h>
38 #include <vnet/ipsec/ipsec.api_types.h>
40 #define REPLY_MSG_ID_BASE ipsec_main.msg_id_base
41 #include <vlibapi/api_helper_macros.h>
44 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
46 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
47 vl_api_ipsec_spd_add_del_reply_t *rmp;
50 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
52 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
55 static void vl_api_ipsec_interface_add_del_spd_t_handler
56 (vl_api_ipsec_interface_add_del_spd_t * mp)
58 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
59 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
61 u32 sw_if_index __attribute__ ((unused));
62 u32 spd_id __attribute__ ((unused));
64 sw_if_index = ntohl (mp->sw_if_index);
65 spd_id = ntohl (mp->spd_id);
67 VALIDATE_SW_IF_INDEX (mp);
69 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
71 BAD_SW_IF_INDEX_LABEL;
73 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
76 static void vl_api_ipsec_tunnel_protect_update_t_handler
77 (vl_api_ipsec_tunnel_protect_update_t * mp)
79 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
80 vl_api_ipsec_tunnel_protect_update_reply_t *rmp;
81 u32 sw_if_index, ii, *sa_ins = NULL;
85 sw_if_index = ntohl (mp->tunnel.sw_if_index);
87 VALIDATE_SW_IF_INDEX (&(mp->tunnel));
89 for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
90 vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
92 ip_address_decode2 (&mp->tunnel.nh, &nh);
94 rv = ipsec_tun_protect_update (sw_if_index, &nh,
95 ntohl (mp->tunnel.sa_out), sa_ins);
97 BAD_SW_IF_INDEX_LABEL;
99 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
102 static void vl_api_ipsec_tunnel_protect_del_t_handler
103 (vl_api_ipsec_tunnel_protect_del_t * mp)
105 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
106 vl_api_ipsec_tunnel_protect_del_reply_t *rmp;
111 sw_if_index = ntohl (mp->sw_if_index);
113 VALIDATE_SW_IF_INDEX (mp);
115 ip_address_decode2 (&mp->nh, &nh);
116 rv = ipsec_tun_protect_del (sw_if_index, &nh);
118 BAD_SW_IF_INDEX_LABEL;
120 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
123 typedef struct ipsec_dump_walk_ctx_t_
125 vl_api_registration_t *reg;
127 } ipsec_dump_walk_ctx_t;
130 send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
132 ipsec_dump_walk_ctx_t *ctx = arg;
133 vl_api_ipsec_tunnel_protect_details_t *mp;
134 ipsec_tun_protect_t *itp;
138 itp = ipsec_tun_protect_get (itpi);
140 mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
141 clib_memset (mp, 0, sizeof (*mp));
143 ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
144 mp->context = ctx->context;
146 mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
147 ip_address_encode2 (itp->itp_key, &mp->tun.nh);
149 sa = ipsec_sa_get (itp->itp_out_sa);
150 mp->tun.sa_out = htonl (sa->id);
151 mp->tun.n_sa_in = itp->itp_n_sa_in;
153 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
155 mp->tun.sa_in[ii++] = htonl (sa->id);
159 vl_api_send_msg (ctx->reg, (u8 *) mp);
161 return (WALK_CONTINUE);
165 vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
168 vl_api_registration_t *reg;
171 reg = vl_api_client_index_to_registration (mp->client_index);
175 ipsec_dump_walk_ctx_t ctx = {
177 .context = mp->context,
180 sw_if_index = ntohl (mp->sw_if_index);
182 if (~0 == sw_if_index)
184 ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
188 ipsec_tun_protect_walk_itf (sw_if_index,
189 send_ipsec_tunnel_protect_details, &ctx);
194 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
195 ipsec_policy_action_t * out)
197 in = clib_net_to_host_u32 (in);
201 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
202 *out = IPSEC_POLICY_ACTION_##f; \
204 foreach_ipsec_policy_action
207 return (VNET_API_ERROR_UNIMPLEMENTED);
210 static void vl_api_ipsec_spd_entry_add_del_t_handler
211 (vl_api_ipsec_spd_entry_add_del_t * mp)
213 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
214 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
223 clib_memset (&p, 0, sizeof (p));
225 p.id = ntohl (mp->entry.spd_id);
226 p.priority = ntohl (mp->entry.priority);
228 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
229 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
230 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
231 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
233 p.is_ipv6 = (itype == IP46_TYPE_IP6);
235 p.protocol = mp->entry.protocol;
236 p.rport.start = ntohs (mp->entry.remote_port_start);
237 p.rport.stop = ntohs (mp->entry.remote_port_stop);
238 p.lport.start = ntohs (mp->entry.local_port_start);
239 p.lport.stop = ntohs (mp->entry.local_port_stop);
241 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
246 /* policy action resolve unsupported */
247 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
249 clib_warning ("unsupported action: 'resolve'");
250 rv = VNET_API_ERROR_UNIMPLEMENTED;
253 p.sa_id = ntohl (mp->entry.sa_id);
255 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
260 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
266 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
268 rmp->stat_index = ntohl(stat_index);
273 static void vl_api_ipsec_sad_entry_add_del_t_handler
274 (vl_api_ipsec_sad_entry_add_del_t * mp)
276 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
277 ipsec_key_t crypto_key, integ_key;
278 ipsec_crypto_alg_t crypto_alg;
279 ipsec_integ_alg_t integ_alg;
280 ipsec_protocol_t proto;
281 ipsec_sa_flags_t flags;
282 u32 id, spi, sa_index = ~0;
284 .t_flags = TUNNEL_FLAG_NONE,
285 .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
287 .t_mode = TUNNEL_MODE_P2P,
293 id = ntohl (mp->entry.sad_id);
296 rv = ipsec_sa_unlock_id (id);
299 spi = ntohl (mp->entry.spi);
301 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
306 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
311 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
316 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
317 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
319 flags = ipsec_sa_flags_decode (mp->entry.flags);
321 ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
322 ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
324 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &crypto_key,
325 integ_alg, &integ_key, flags, mp->entry.salt,
326 htons (mp->entry.udp_src_port),
327 htons (mp->entry.udp_dst_port), &tun, &sa_index);
331 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
333 rmp->stat_index = htonl (sa_index);
338 static void vl_api_ipsec_sad_entry_add_del_v2_t_handler
339 (vl_api_ipsec_sad_entry_add_del_v2_t * mp)
341 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
342 vl_api_ipsec_sad_entry_add_del_v2_reply_t *rmp;
343 ipsec_key_t crypto_key, integ_key;
344 ipsec_crypto_alg_t crypto_alg;
345 ipsec_integ_alg_t integ_alg;
346 ipsec_protocol_t proto;
347 ipsec_sa_flags_t flags;
348 u32 id, spi, sa_index = ~0;
351 .t_flags = TUNNEL_FLAG_NONE,
352 .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
354 .t_mode = TUNNEL_MODE_P2P,
355 .t_table_id = htonl (mp->entry.tx_table_id),
359 id = ntohl (mp->entry.sad_id);
362 rv = ipsec_sa_unlock_id (id);
366 spi = ntohl (mp->entry.spi);
368 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
373 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
378 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
383 rv = tunnel_encap_decap_flags_decode (mp->entry.tunnel_flags,
384 &tun.t_encap_decap_flags);
389 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
390 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
392 flags = ipsec_sa_flags_decode (mp->entry.flags);
393 tun.t_dscp = ip_dscp_decode (mp->entry.dscp);
395 ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
396 ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
398 rv = ipsec_sa_add_and_lock (
399 id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags,
400 mp->entry.salt, htons (mp->entry.udp_src_port),
401 htons (mp->entry.udp_dst_port), &tun, &sa_index);
405 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V2_REPLY,
407 rmp->stat_index = htonl (sa_index);
413 ipsec_sad_entry_add_v3 (const vl_api_ipsec_sad_entry_v3_t *entry,
416 ipsec_key_t crypto_key, integ_key;
417 ipsec_crypto_alg_t crypto_alg;
418 ipsec_integ_alg_t integ_alg;
419 ipsec_protocol_t proto;
420 ipsec_sa_flags_t flags;
425 id = ntohl (entry->sad_id);
426 spi = ntohl (entry->spi);
428 rv = ipsec_proto_decode (entry->protocol, &proto);
433 rv = ipsec_crypto_algo_decode (entry->crypto_algorithm, &crypto_alg);
438 rv = ipsec_integ_algo_decode (entry->integrity_algorithm, &integ_alg);
443 flags = ipsec_sa_flags_decode (entry->flags);
445 if (flags & IPSEC_SA_FLAG_IS_TUNNEL)
447 rv = tunnel_decode (&entry->tunnel, &tun);
453 ipsec_key_decode (&entry->crypto_key, &crypto_key);
454 ipsec_key_decode (&entry->integrity_key, &integ_key);
456 return ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &crypto_key,
457 integ_alg, &integ_key, flags, entry->salt,
458 htons (entry->udp_src_port),
459 htons (entry->udp_dst_port), &tun, sa_index);
463 vl_api_ipsec_sad_entry_add_del_v3_t_handler (
464 vl_api_ipsec_sad_entry_add_del_v3_t *mp)
466 vl_api_ipsec_sad_entry_add_del_v3_reply_t *rmp;
467 u32 id, sa_index = ~0;
470 id = ntohl (mp->entry.sad_id);
474 rv = ipsec_sa_unlock_id (id);
478 rv = ipsec_sad_entry_add_v3 (&mp->entry, &sa_index);
481 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V3_REPLY,
482 { rmp->stat_index = htonl (sa_index); });
486 vl_api_ipsec_sad_entry_del_t_handler (vl_api_ipsec_sad_entry_del_t *mp)
488 vl_api_ipsec_sad_entry_del_reply_t *rmp;
491 rv = ipsec_sa_unlock_id (ntohl (mp->id));
493 REPLY_MACRO (VL_API_IPSEC_SAD_ENTRY_DEL_REPLY);
497 vl_api_ipsec_sad_entry_add_t_handler (vl_api_ipsec_sad_entry_add_t *mp)
499 vl_api_ipsec_sad_entry_add_reply_t *rmp;
503 rv = ipsec_sad_entry_add_v3 (&mp->entry, &sa_index);
505 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_REPLY,
506 { rmp->stat_index = htonl (sa_index); });
510 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
513 vl_api_ipsec_spds_details_t *mp;
516 mp = vl_msg_api_alloc (sizeof (*mp));
517 clib_memset (mp, 0, sizeof (*mp));
518 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPDS_DETAILS);
519 mp->context = context;
521 mp->spd_id = htonl (spd->id);
522 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
523 foreach_ipsec_spd_policy_type
525 mp->npolicies = htonl (n_policies);
527 vl_api_send_msg (reg, (u8 *) mp);
531 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
533 vl_api_registration_t *reg;
534 ipsec_main_t *im = &ipsec_main;
537 reg = vl_api_client_index_to_registration (mp->client_index);
541 pool_foreach (spd, im->spds) {
542 send_ipsec_spds_details (spd, reg, mp->context);
546 vl_api_ipsec_spd_action_t
547 ipsec_spd_action_encode (ipsec_policy_action_t in)
549 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
553 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
554 out = IPSEC_API_SPD_ACTION_##f; \
556 foreach_ipsec_policy_action
559 return (clib_host_to_net_u32 (out));
563 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
566 vl_api_ipsec_spd_details_t *mp;
568 mp = vl_msg_api_alloc (sizeof (*mp));
569 clib_memset (mp, 0, sizeof (*mp));
570 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_DETAILS);
571 mp->context = context;
573 mp->entry.spd_id = htonl (p->id);
574 mp->entry.priority = htonl (p->priority);
575 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
576 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
578 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
579 &mp->entry.local_address_start);
580 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
581 &mp->entry.local_address_stop);
582 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
583 &mp->entry.remote_address_start);
584 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
585 &mp->entry.remote_address_stop);
586 mp->entry.local_port_start = htons (p->lport.start);
587 mp->entry.local_port_stop = htons (p->lport.stop);
588 mp->entry.remote_port_start = htons (p->rport.start);
589 mp->entry.remote_port_stop = htons (p->rport.stop);
590 mp->entry.protocol = p->protocol;
591 mp->entry.policy = ipsec_spd_action_encode (p->policy);
592 mp->entry.sa_id = htonl (p->sa_id);
594 vl_api_send_msg (reg, (u8 *) mp);
598 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
600 vl_api_registration_t *reg;
601 ipsec_main_t *im = &ipsec_main;
602 ipsec_spd_policy_type_t ptype;
603 ipsec_policy_t *policy;
608 reg = vl_api_client_index_to_registration (mp->client_index);
612 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
617 spd = pool_elt_at_index (im->spds, spd_index);
619 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
620 vec_foreach(ii, spd->policies[ptype])
622 policy = pool_elt_at_index(im->policies, *ii);
624 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
625 send_ipsec_spd_details (policy, reg, mp->context);
631 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
632 u32 sw_if_index, u32 context)
634 vl_api_ipsec_spd_interface_details_t *mp;
636 mp = vl_msg_api_alloc (sizeof (*mp));
637 clib_memset (mp, 0, sizeof (*mp));
639 ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_INTERFACE_DETAILS);
640 mp->context = context;
642 mp->spd_index = htonl (spd_index);
643 mp->sw_if_index = htonl (sw_if_index);
645 vl_api_send_msg (reg, (u8 *) mp);
649 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
652 ipsec_main_t *im = &ipsec_main;
653 vl_api_registration_t *reg;
656 reg = vl_api_client_index_to_registration (mp->client_index);
660 if (mp->spd_index_valid)
662 spd_index = ntohl (mp->spd_index);
664 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
666 send_ipsec_spd_interface_details(reg, v, k, mp->context);
672 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
673 send_ipsec_spd_interface_details(reg, v, k, mp->context);
679 vl_api_ipsec_itf_create_t_handler (vl_api_ipsec_itf_create_t * mp)
681 vl_api_ipsec_itf_create_reply_t *rmp;
683 u32 sw_if_index = ~0;
686 rv = tunnel_mode_decode (mp->itf.mode, &mode);
689 rv = ipsec_itf_create (ntohl (mp->itf.user_instance), mode, &sw_if_index);
692 REPLY_MACRO2 (VL_API_IPSEC_ITF_CREATE_REPLY,
694 rmp->sw_if_index = htonl (sw_if_index);
700 vl_api_ipsec_itf_delete_t_handler (vl_api_ipsec_itf_delete_t * mp)
702 vl_api_ipsec_itf_delete_reply_t *rmp;
705 rv = ipsec_itf_delete (ntohl (mp->sw_if_index));
707 REPLY_MACRO (VL_API_IPSEC_ITF_DELETE_REPLY);
711 send_ipsec_itf_details (ipsec_itf_t *itf, void *arg)
713 ipsec_dump_walk_ctx_t *ctx = arg;
714 vl_api_ipsec_itf_details_t *mp;
716 mp = vl_msg_api_alloc (sizeof (*mp));
717 clib_memset (mp, 0, sizeof (*mp));
718 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_ITF_DETAILS);
719 mp->context = ctx->context;
721 mp->itf.mode = tunnel_mode_encode (itf->ii_mode);
722 mp->itf.user_instance = htonl (itf->ii_user_instance);
723 mp->itf.sw_if_index = htonl (itf->ii_sw_if_index);
724 vl_api_send_msg (ctx->reg, (u8 *) mp);
726 return (WALK_CONTINUE);
730 vl_api_ipsec_itf_dump_t_handler (vl_api_ipsec_itf_dump_t * mp)
732 vl_api_registration_t *reg;
734 reg = vl_api_client_index_to_registration (mp->client_index);
738 ipsec_dump_walk_ctx_t ctx = {
740 .context = mp->context,
743 ipsec_itf_walk (send_ipsec_itf_details, &ctx);
746 typedef struct ipsec_sa_dump_match_ctx_t_
750 } ipsec_sa_dump_match_ctx_t;
753 ipsec_sa_dump_match_sa (index_t itpi, void *arg)
755 ipsec_sa_dump_match_ctx_t *ctx = arg;
756 ipsec_tun_protect_t *itp;
759 itp = ipsec_tun_protect_get (itpi);
761 if (itp->itp_out_sa == ctx->sai)
763 ctx->sw_if_index = itp->itp_sw_if_index;
767 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
771 ctx->sw_if_index = itp->itp_sw_if_index;
776 return (WALK_CONTINUE);
780 send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
782 ipsec_dump_walk_ctx_t *ctx = arg;
783 vl_api_ipsec_sa_details_t *mp;
785 mp = vl_msg_api_alloc (sizeof (*mp));
786 clib_memset (mp, 0, sizeof (*mp));
787 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_DETAILS);
788 mp->context = ctx->context;
790 mp->entry.sad_id = htonl (sa->id);
791 mp->entry.spi = htonl (sa->spi);
792 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
793 mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
795 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
796 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
798 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
799 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
801 mp->entry.flags = ipsec_sad_flags_encode (sa);
802 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
804 if (ipsec_sa_is_set_IS_PROTECT (sa))
806 ipsec_sa_dump_match_ctx_t ctx = {
807 .sai = sa - ipsec_sa_pool,
810 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
812 mp->sw_if_index = htonl (ctx.sw_if_index);
815 mp->sw_if_index = ~0;
817 if (ipsec_sa_is_set_IS_TUNNEL (sa))
819 ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
820 ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
822 if (ipsec_sa_is_set_UDP_ENCAP (sa))
824 mp->entry.udp_src_port = sa->udp_hdr.src_port;
825 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
828 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
829 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
830 if (ipsec_sa_is_set_USE_ESN (sa))
832 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
833 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
835 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
836 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
838 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
840 vl_api_send_msg (ctx->reg, (u8 *) mp);
842 return (WALK_CONTINUE);
846 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
848 vl_api_registration_t *reg;
850 reg = vl_api_client_index_to_registration (mp->client_index);
854 ipsec_dump_walk_ctx_t ctx = {
856 .context = mp->context,
859 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
863 send_ipsec_sa_v2_details (ipsec_sa_t * sa, void *arg)
865 ipsec_dump_walk_ctx_t *ctx = arg;
866 vl_api_ipsec_sa_v2_details_t *mp;
868 mp = vl_msg_api_alloc (sizeof (*mp));
869 clib_memset (mp, 0, sizeof (*mp));
870 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V2_DETAILS);
871 mp->context = ctx->context;
873 mp->entry.sad_id = htonl (sa->id);
874 mp->entry.spi = htonl (sa->spi);
875 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
876 mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
878 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
879 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
881 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
882 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
884 mp->entry.flags = ipsec_sad_flags_encode (sa);
885 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
887 if (ipsec_sa_is_set_IS_PROTECT (sa))
889 ipsec_sa_dump_match_ctx_t ctx = {
890 .sai = sa - ipsec_sa_pool,
893 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
895 mp->sw_if_index = htonl (ctx.sw_if_index);
898 mp->sw_if_index = ~0;
900 if (ipsec_sa_is_set_IS_TUNNEL (sa))
902 ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
903 ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
905 if (ipsec_sa_is_set_UDP_ENCAP (sa))
907 mp->entry.udp_src_port = sa->udp_hdr.src_port;
908 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
911 mp->entry.tunnel_flags =
912 tunnel_encap_decap_flags_encode (sa->tunnel.t_encap_decap_flags);
913 mp->entry.dscp = ip_dscp_encode (sa->tunnel.t_dscp);
915 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
916 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
917 if (ipsec_sa_is_set_USE_ESN (sa))
919 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
920 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
922 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
923 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
925 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
927 vl_api_send_msg (ctx->reg, (u8 *) mp);
929 return (WALK_CONTINUE);
933 vl_api_ipsec_sa_v2_dump_t_handler (vl_api_ipsec_sa_v2_dump_t *mp)
935 vl_api_registration_t *reg;
937 reg = vl_api_client_index_to_registration (mp->client_index);
941 ipsec_dump_walk_ctx_t ctx = {
943 .context = mp->context,
946 ipsec_sa_walk (send_ipsec_sa_v2_details, &ctx);
950 send_ipsec_sa_v3_details (ipsec_sa_t *sa, void *arg)
952 ipsec_dump_walk_ctx_t *ctx = arg;
953 vl_api_ipsec_sa_v3_details_t *mp;
955 mp = vl_msg_api_alloc (sizeof (*mp));
956 clib_memset (mp, 0, sizeof (*mp));
957 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V3_DETAILS);
958 mp->context = ctx->context;
960 mp->entry.sad_id = htonl (sa->id);
961 mp->entry.spi = htonl (sa->spi);
962 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
964 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
965 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
967 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
968 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
970 mp->entry.flags = ipsec_sad_flags_encode (sa);
971 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
973 if (ipsec_sa_is_set_IS_PROTECT (sa))
975 ipsec_sa_dump_match_ctx_t ctx = {
976 .sai = sa - ipsec_sa_pool,
979 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
981 mp->sw_if_index = htonl (ctx.sw_if_index);
984 mp->sw_if_index = ~0;
986 if (ipsec_sa_is_set_IS_TUNNEL (sa))
987 tunnel_encode (&sa->tunnel, &mp->entry.tunnel);
989 if (ipsec_sa_is_set_UDP_ENCAP (sa))
991 mp->entry.udp_src_port = sa->udp_hdr.src_port;
992 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
995 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
996 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
997 if (ipsec_sa_is_set_USE_ESN (sa))
999 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
1000 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
1002 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
1003 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
1005 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
1007 vl_api_send_msg (ctx->reg, (u8 *) mp);
1009 return (WALK_CONTINUE);
1013 vl_api_ipsec_sa_v3_dump_t_handler (vl_api_ipsec_sa_v3_dump_t *mp)
1015 vl_api_registration_t *reg;
1017 reg = vl_api_client_index_to_registration (mp->client_index);
1021 ipsec_dump_walk_ctx_t ctx = {
1023 .context = mp->context,
1026 ipsec_sa_walk (send_ipsec_sa_v3_details, &ctx);
1030 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
1032 vl_api_registration_t *rp;
1033 ipsec_main_t *im = &ipsec_main;
1034 u32 context = mp->context;
1036 rp = vl_api_client_index_to_registration (mp->client_index);
1040 clib_warning ("Client %d AWOL", mp->client_index);
1044 ipsec_ah_backend_t *ab;
1045 ipsec_esp_backend_t *eb;
1047 pool_foreach (ab, im->ah_backends) {
1048 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1049 clib_memset (mp, 0, sizeof (*mp));
1050 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
1051 mp->context = context;
1052 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1054 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
1055 mp->index = ab - im->ah_backends;
1056 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1057 vl_api_send_msg (rp, (u8 *)mp);
1059 pool_foreach (eb, im->esp_backends) {
1060 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1061 clib_memset (mp, 0, sizeof (*mp));
1062 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
1063 mp->context = context;
1064 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1066 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
1067 mp->index = eb - im->esp_backends;
1068 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1069 vl_api_send_msg (rp, (u8 *)mp);
1075 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1077 ipsec_main_t *im = &ipsec_main;
1078 vl_api_ipsec_select_backend_reply_t *rmp;
1079 ipsec_protocol_t protocol;
1081 if (pool_elts (ipsec_sa_pool) > 0)
1083 rv = VNET_API_ERROR_INSTANCE_IN_USE;
1087 rv = ipsec_proto_decode (mp->protocol, &protocol);
1094 case IPSEC_PROTOCOL_ESP:
1095 rv = ipsec_select_esp_backend (im, mp->index);
1097 case IPSEC_PROTOCOL_AH:
1098 rv = ipsec_select_ah_backend (im, mp->index);
1101 rv = VNET_API_ERROR_INVALID_PROTOCOL;
1105 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1109 vl_api_ipsec_set_async_mode_t_handler (vl_api_ipsec_set_async_mode_t * mp)
1111 vl_api_ipsec_set_async_mode_reply_t *rmp;
1114 ipsec_set_async_mode (mp->async_enable);
1116 REPLY_MACRO (VL_API_IPSEC_SET_ASYNC_MODE_REPLY);
1119 #include <vnet/ipsec/ipsec.api.c>
1120 static clib_error_t *
1121 ipsec_api_hookup (vlib_main_t * vm)
1124 * Set up the (msg_name, crc, message-id) table
1126 REPLY_MSG_ID_BASE = setup_message_id_table ();
1131 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1134 * fd.io coding-style-patch-verification: ON
1137 * eval: (c-set-style "gnu")