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);
294 spi = ntohl (mp->entry.spi);
296 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
301 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
306 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
311 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
312 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
314 flags = ipsec_sa_flags_decode (mp->entry.flags);
316 ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
317 ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
320 rv = ipsec_sa_add_and_lock (
321 id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags,
322 mp->entry.salt, htons (mp->entry.udp_src_port),
323 htons (mp->entry.udp_dst_port), &tun, &sa_index);
325 rv = ipsec_sa_unlock_id (id);
329 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
331 rmp->stat_index = htonl (sa_index);
336 static void vl_api_ipsec_sad_entry_add_del_v2_t_handler
337 (vl_api_ipsec_sad_entry_add_del_v2_t * mp)
339 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
340 vl_api_ipsec_sad_entry_add_del_v2_reply_t *rmp;
341 ipsec_key_t crypto_key, integ_key;
342 ipsec_crypto_alg_t crypto_alg;
343 ipsec_integ_alg_t integ_alg;
344 ipsec_protocol_t proto;
345 ipsec_sa_flags_t flags;
346 u32 id, spi, sa_index = ~0;
349 .t_flags = TUNNEL_FLAG_NONE,
350 .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
352 .t_mode = TUNNEL_MODE_P2P,
353 .t_table_id = htonl (mp->entry.tx_table_id),
357 id = ntohl (mp->entry.sad_id);
358 spi = ntohl (mp->entry.spi);
360 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
365 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
370 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
375 rv = tunnel_encap_decap_flags_decode (mp->entry.tunnel_flags,
376 &tun.t_encap_decap_flags);
381 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
382 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
384 flags = ipsec_sa_flags_decode (mp->entry.flags);
385 tun.t_dscp = ip_dscp_decode (mp->entry.dscp);
387 ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
388 ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
391 rv = ipsec_sa_add_and_lock (
392 id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags,
393 mp->entry.salt, htons (mp->entry.udp_src_port),
394 htons (mp->entry.udp_dst_port), &tun, &sa_index);
396 rv = ipsec_sa_unlock_id (id);
400 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V2_REPLY,
402 rmp->stat_index = htonl (sa_index);
408 vl_api_ipsec_sad_entry_add_del_v3_t_handler (
409 vl_api_ipsec_sad_entry_add_del_v3_t *mp)
411 vl_api_ipsec_sad_entry_add_del_v3_reply_t *rmp;
412 ipsec_key_t crypto_key, integ_key;
413 ipsec_crypto_alg_t crypto_alg;
414 ipsec_integ_alg_t integ_alg;
415 ipsec_protocol_t proto;
416 ipsec_sa_flags_t flags;
417 u32 id, spi, sa_index = ~0;
421 id = ntohl (mp->entry.sad_id);
422 spi = ntohl (mp->entry.spi);
424 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
429 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
434 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
439 flags = ipsec_sa_flags_decode (mp->entry.flags);
441 if (flags & IPSEC_SA_FLAG_IS_TUNNEL)
443 rv = tunnel_decode (&mp->entry.tunnel, &tun);
449 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
450 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
453 rv = ipsec_sa_add_and_lock (
454 id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags,
455 mp->entry.salt, htons (mp->entry.udp_src_port),
456 htons (mp->entry.udp_dst_port), &tun, &sa_index);
458 rv = ipsec_sa_unlock_id (id);
461 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V3_REPLY,
462 { rmp->stat_index = htonl (sa_index); });
466 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
469 vl_api_ipsec_spds_details_t *mp;
472 mp = vl_msg_api_alloc (sizeof (*mp));
473 clib_memset (mp, 0, sizeof (*mp));
474 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPDS_DETAILS);
475 mp->context = context;
477 mp->spd_id = htonl (spd->id);
478 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
479 foreach_ipsec_spd_policy_type
481 mp->npolicies = htonl (n_policies);
483 vl_api_send_msg (reg, (u8 *) mp);
487 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
489 vl_api_registration_t *reg;
490 ipsec_main_t *im = &ipsec_main;
493 reg = vl_api_client_index_to_registration (mp->client_index);
497 pool_foreach (spd, im->spds) {
498 send_ipsec_spds_details (spd, reg, mp->context);
502 vl_api_ipsec_spd_action_t
503 ipsec_spd_action_encode (ipsec_policy_action_t in)
505 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
509 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
510 out = IPSEC_API_SPD_ACTION_##f; \
512 foreach_ipsec_policy_action
515 return (clib_host_to_net_u32 (out));
519 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
522 vl_api_ipsec_spd_details_t *mp;
524 mp = vl_msg_api_alloc (sizeof (*mp));
525 clib_memset (mp, 0, sizeof (*mp));
526 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_DETAILS);
527 mp->context = context;
529 mp->entry.spd_id = htonl (p->id);
530 mp->entry.priority = htonl (p->priority);
531 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
532 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
534 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
535 &mp->entry.local_address_start);
536 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
537 &mp->entry.local_address_stop);
538 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
539 &mp->entry.remote_address_start);
540 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
541 &mp->entry.remote_address_stop);
542 mp->entry.local_port_start = htons (p->lport.start);
543 mp->entry.local_port_stop = htons (p->lport.stop);
544 mp->entry.remote_port_start = htons (p->rport.start);
545 mp->entry.remote_port_stop = htons (p->rport.stop);
546 mp->entry.protocol = p->protocol;
547 mp->entry.policy = ipsec_spd_action_encode (p->policy);
548 mp->entry.sa_id = htonl (p->sa_id);
550 vl_api_send_msg (reg, (u8 *) mp);
554 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
556 vl_api_registration_t *reg;
557 ipsec_main_t *im = &ipsec_main;
558 ipsec_spd_policy_type_t ptype;
559 ipsec_policy_t *policy;
564 reg = vl_api_client_index_to_registration (mp->client_index);
568 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
573 spd = pool_elt_at_index (im->spds, spd_index);
575 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
576 vec_foreach(ii, spd->policies[ptype])
578 policy = pool_elt_at_index(im->policies, *ii);
580 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
581 send_ipsec_spd_details (policy, reg, mp->context);
587 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
588 u32 sw_if_index, u32 context)
590 vl_api_ipsec_spd_interface_details_t *mp;
592 mp = vl_msg_api_alloc (sizeof (*mp));
593 clib_memset (mp, 0, sizeof (*mp));
595 ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_INTERFACE_DETAILS);
596 mp->context = context;
598 mp->spd_index = htonl (spd_index);
599 mp->sw_if_index = htonl (sw_if_index);
601 vl_api_send_msg (reg, (u8 *) mp);
605 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
608 ipsec_main_t *im = &ipsec_main;
609 vl_api_registration_t *reg;
612 reg = vl_api_client_index_to_registration (mp->client_index);
616 if (mp->spd_index_valid)
618 spd_index = ntohl (mp->spd_index);
620 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
622 send_ipsec_spd_interface_details(reg, v, k, mp->context);
628 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
629 send_ipsec_spd_interface_details(reg, v, k, mp->context);
635 vl_api_ipsec_itf_create_t_handler (vl_api_ipsec_itf_create_t * mp)
637 vl_api_ipsec_itf_create_reply_t *rmp;
639 u32 sw_if_index = ~0;
642 rv = tunnel_mode_decode (mp->itf.mode, &mode);
645 rv = ipsec_itf_create (ntohl (mp->itf.user_instance), mode, &sw_if_index);
648 REPLY_MACRO2 (VL_API_IPSEC_ITF_CREATE_REPLY,
650 rmp->sw_if_index = htonl (sw_if_index);
656 vl_api_ipsec_itf_delete_t_handler (vl_api_ipsec_itf_delete_t * mp)
658 vl_api_ipsec_itf_delete_reply_t *rmp;
661 rv = ipsec_itf_delete (ntohl (mp->sw_if_index));
663 REPLY_MACRO (VL_API_IPSEC_ITF_DELETE_REPLY);
667 send_ipsec_itf_details (ipsec_itf_t *itf, void *arg)
669 ipsec_dump_walk_ctx_t *ctx = arg;
670 vl_api_ipsec_itf_details_t *mp;
672 mp = vl_msg_api_alloc (sizeof (*mp));
673 clib_memset (mp, 0, sizeof (*mp));
674 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_ITF_DETAILS);
675 mp->context = ctx->context;
677 mp->itf.mode = tunnel_mode_encode (itf->ii_mode);
678 mp->itf.user_instance = htonl (itf->ii_user_instance);
679 mp->itf.sw_if_index = htonl (itf->ii_sw_if_index);
680 vl_api_send_msg (ctx->reg, (u8 *) mp);
682 return (WALK_CONTINUE);
686 vl_api_ipsec_itf_dump_t_handler (vl_api_ipsec_itf_dump_t * mp)
688 vl_api_registration_t *reg;
690 reg = vl_api_client_index_to_registration (mp->client_index);
694 ipsec_dump_walk_ctx_t ctx = {
696 .context = mp->context,
699 ipsec_itf_walk (send_ipsec_itf_details, &ctx);
702 typedef struct ipsec_sa_dump_match_ctx_t_
706 } ipsec_sa_dump_match_ctx_t;
709 ipsec_sa_dump_match_sa (index_t itpi, void *arg)
711 ipsec_sa_dump_match_ctx_t *ctx = arg;
712 ipsec_tun_protect_t *itp;
715 itp = ipsec_tun_protect_get (itpi);
717 if (itp->itp_out_sa == ctx->sai)
719 ctx->sw_if_index = itp->itp_sw_if_index;
723 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
727 ctx->sw_if_index = itp->itp_sw_if_index;
732 return (WALK_CONTINUE);
736 send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
738 ipsec_dump_walk_ctx_t *ctx = arg;
739 vl_api_ipsec_sa_details_t *mp;
741 mp = vl_msg_api_alloc (sizeof (*mp));
742 clib_memset (mp, 0, sizeof (*mp));
743 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_DETAILS);
744 mp->context = ctx->context;
746 mp->entry.sad_id = htonl (sa->id);
747 mp->entry.spi = htonl (sa->spi);
748 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
749 mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
751 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
752 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
754 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
755 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
757 mp->entry.flags = ipsec_sad_flags_encode (sa);
758 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
760 if (ipsec_sa_is_set_IS_PROTECT (sa))
762 ipsec_sa_dump_match_ctx_t ctx = {
763 .sai = sa - ipsec_sa_pool,
766 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
768 mp->sw_if_index = htonl (ctx.sw_if_index);
771 mp->sw_if_index = ~0;
773 if (ipsec_sa_is_set_IS_TUNNEL (sa))
775 ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
776 ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
778 if (ipsec_sa_is_set_UDP_ENCAP (sa))
780 mp->entry.udp_src_port = sa->udp_hdr.src_port;
781 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
784 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
785 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
786 if (ipsec_sa_is_set_USE_ESN (sa))
788 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
789 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
791 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
792 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
794 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
796 vl_api_send_msg (ctx->reg, (u8 *) mp);
798 return (WALK_CONTINUE);
802 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
804 vl_api_registration_t *reg;
806 reg = vl_api_client_index_to_registration (mp->client_index);
810 ipsec_dump_walk_ctx_t ctx = {
812 .context = mp->context,
815 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
819 send_ipsec_sa_v2_details (ipsec_sa_t * sa, void *arg)
821 ipsec_dump_walk_ctx_t *ctx = arg;
822 vl_api_ipsec_sa_v2_details_t *mp;
824 mp = vl_msg_api_alloc (sizeof (*mp));
825 clib_memset (mp, 0, sizeof (*mp));
826 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V2_DETAILS);
827 mp->context = ctx->context;
829 mp->entry.sad_id = htonl (sa->id);
830 mp->entry.spi = htonl (sa->spi);
831 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
832 mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
834 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
835 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
837 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
838 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
840 mp->entry.flags = ipsec_sad_flags_encode (sa);
841 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
843 if (ipsec_sa_is_set_IS_PROTECT (sa))
845 ipsec_sa_dump_match_ctx_t ctx = {
846 .sai = sa - ipsec_sa_pool,
849 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
851 mp->sw_if_index = htonl (ctx.sw_if_index);
854 mp->sw_if_index = ~0;
856 if (ipsec_sa_is_set_IS_TUNNEL (sa))
858 ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
859 ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
861 if (ipsec_sa_is_set_UDP_ENCAP (sa))
863 mp->entry.udp_src_port = sa->udp_hdr.src_port;
864 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
867 mp->entry.tunnel_flags =
868 tunnel_encap_decap_flags_encode (sa->tunnel.t_encap_decap_flags);
869 mp->entry.dscp = ip_dscp_encode (sa->tunnel.t_dscp);
871 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
872 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
873 if (ipsec_sa_is_set_USE_ESN (sa))
875 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
876 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
878 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
879 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
881 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
883 vl_api_send_msg (ctx->reg, (u8 *) mp);
885 return (WALK_CONTINUE);
889 vl_api_ipsec_sa_v2_dump_t_handler (vl_api_ipsec_sa_v2_dump_t *mp)
891 vl_api_registration_t *reg;
893 reg = vl_api_client_index_to_registration (mp->client_index);
897 ipsec_dump_walk_ctx_t ctx = {
899 .context = mp->context,
902 ipsec_sa_walk (send_ipsec_sa_v2_details, &ctx);
906 send_ipsec_sa_v3_details (ipsec_sa_t *sa, void *arg)
908 ipsec_dump_walk_ctx_t *ctx = arg;
909 vl_api_ipsec_sa_v3_details_t *mp;
911 mp = vl_msg_api_alloc (sizeof (*mp));
912 clib_memset (mp, 0, sizeof (*mp));
913 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V3_DETAILS);
914 mp->context = ctx->context;
916 mp->entry.sad_id = htonl (sa->id);
917 mp->entry.spi = htonl (sa->spi);
918 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
920 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
921 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
923 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
924 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
926 mp->entry.flags = ipsec_sad_flags_encode (sa);
927 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
929 if (ipsec_sa_is_set_IS_PROTECT (sa))
931 ipsec_sa_dump_match_ctx_t ctx = {
932 .sai = sa - ipsec_sa_pool,
935 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
937 mp->sw_if_index = htonl (ctx.sw_if_index);
940 mp->sw_if_index = ~0;
942 if (ipsec_sa_is_set_IS_TUNNEL (sa))
943 tunnel_encode (&sa->tunnel, &mp->entry.tunnel);
945 if (ipsec_sa_is_set_UDP_ENCAP (sa))
947 mp->entry.udp_src_port = sa->udp_hdr.src_port;
948 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
951 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
952 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
953 if (ipsec_sa_is_set_USE_ESN (sa))
955 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
956 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
958 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
959 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
961 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
963 vl_api_send_msg (ctx->reg, (u8 *) mp);
965 return (WALK_CONTINUE);
969 vl_api_ipsec_sa_v3_dump_t_handler (vl_api_ipsec_sa_v3_dump_t *mp)
971 vl_api_registration_t *reg;
973 reg = vl_api_client_index_to_registration (mp->client_index);
977 ipsec_dump_walk_ctx_t ctx = {
979 .context = mp->context,
982 ipsec_sa_walk (send_ipsec_sa_v3_details, &ctx);
986 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
988 vl_api_registration_t *rp;
989 ipsec_main_t *im = &ipsec_main;
990 u32 context = mp->context;
992 rp = vl_api_client_index_to_registration (mp->client_index);
996 clib_warning ("Client %d AWOL", mp->client_index);
1000 ipsec_ah_backend_t *ab;
1001 ipsec_esp_backend_t *eb;
1003 pool_foreach (ab, im->ah_backends) {
1004 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1005 clib_memset (mp, 0, sizeof (*mp));
1006 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
1007 mp->context = context;
1008 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1010 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
1011 mp->index = ab - im->ah_backends;
1012 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1013 vl_api_send_msg (rp, (u8 *)mp);
1015 pool_foreach (eb, im->esp_backends) {
1016 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1017 clib_memset (mp, 0, sizeof (*mp));
1018 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
1019 mp->context = context;
1020 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1022 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
1023 mp->index = eb - im->esp_backends;
1024 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1025 vl_api_send_msg (rp, (u8 *)mp);
1031 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1033 ipsec_main_t *im = &ipsec_main;
1034 vl_api_ipsec_select_backend_reply_t *rmp;
1035 ipsec_protocol_t protocol;
1037 if (pool_elts (ipsec_sa_pool) > 0)
1039 rv = VNET_API_ERROR_INSTANCE_IN_USE;
1043 rv = ipsec_proto_decode (mp->protocol, &protocol);
1050 case IPSEC_PROTOCOL_ESP:
1051 rv = ipsec_select_esp_backend (im, mp->index);
1053 case IPSEC_PROTOCOL_AH:
1054 rv = ipsec_select_ah_backend (im, mp->index);
1057 rv = VNET_API_ERROR_INVALID_PROTOCOL;
1061 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1065 vl_api_ipsec_set_async_mode_t_handler (vl_api_ipsec_set_async_mode_t * mp)
1067 vl_api_ipsec_set_async_mode_reply_t *rmp;
1070 ipsec_set_async_mode (mp->async_enable);
1072 REPLY_MACRO (VL_API_IPSEC_SET_ASYNC_MODE_REPLY);
1075 #include <vnet/ipsec/ipsec.api.c>
1076 static clib_error_t *
1077 ipsec_api_hookup (vlib_main_t * vm)
1080 * Set up the (msg_name, crc, message-id) table
1082 REPLY_MSG_ID_BASE = setup_message_id_table ();
1087 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1090 * fd.io coding-style-patch-verification: ON
1093 * eval: (c-set-style "gnu")