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;
128 } ipsec_dump_walk_ctx_t;
131 send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
133 ipsec_dump_walk_ctx_t *ctx = arg;
134 vl_api_ipsec_tunnel_protect_details_t *mp;
135 ipsec_tun_protect_t *itp;
139 itp = ipsec_tun_protect_get (itpi);
141 mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
142 clib_memset (mp, 0, sizeof (*mp));
144 ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
145 mp->context = ctx->context;
147 mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
148 ip_address_encode2 (itp->itp_key, &mp->tun.nh);
150 sa = ipsec_sa_get (itp->itp_out_sa);
151 mp->tun.sa_out = htonl (sa->id);
152 mp->tun.n_sa_in = itp->itp_n_sa_in;
154 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
156 mp->tun.sa_in[ii++] = htonl (sa->id);
160 vl_api_send_msg (ctx->reg, (u8 *) mp);
162 return (WALK_CONTINUE);
166 vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
169 vl_api_registration_t *reg;
172 reg = vl_api_client_index_to_registration (mp->client_index);
176 ipsec_dump_walk_ctx_t ctx = {
178 .context = mp->context,
181 sw_if_index = ntohl (mp->sw_if_index);
183 if (~0 == sw_if_index)
185 ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
189 ipsec_tun_protect_walk_itf (sw_if_index,
190 send_ipsec_tunnel_protect_details, &ctx);
195 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
196 ipsec_policy_action_t * out)
198 in = clib_net_to_host_u32 (in);
202 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
203 *out = IPSEC_POLICY_ACTION_##f; \
205 foreach_ipsec_policy_action
208 return (VNET_API_ERROR_UNIMPLEMENTED);
211 static void vl_api_ipsec_spd_entry_add_del_t_handler
212 (vl_api_ipsec_spd_entry_add_del_t * mp)
214 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
215 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
224 clib_memset (&p, 0, sizeof (p));
226 p.id = ntohl (mp->entry.spd_id);
227 p.priority = ntohl (mp->entry.priority);
229 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
230 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
231 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
232 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
234 p.is_ipv6 = (itype == IP46_TYPE_IP6);
236 p.protocol = mp->entry.protocol;
237 p.rport.start = ntohs (mp->entry.remote_port_start);
238 p.rport.stop = ntohs (mp->entry.remote_port_stop);
239 p.lport.start = ntohs (mp->entry.local_port_start);
240 p.lport.stop = ntohs (mp->entry.local_port_stop);
242 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
247 /* policy action resolve unsupported */
248 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
250 clib_warning ("unsupported action: 'resolve'");
251 rv = VNET_API_ERROR_UNIMPLEMENTED;
254 p.sa_id = ntohl (mp->entry.sa_id);
256 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
261 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
267 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
269 rmp->stat_index = ntohl(stat_index);
274 static void vl_api_ipsec_sad_entry_add_del_t_handler
275 (vl_api_ipsec_sad_entry_add_del_t * mp)
277 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
278 ipsec_key_t crypto_key, integ_key;
279 ipsec_crypto_alg_t crypto_alg;
280 ipsec_integ_alg_t integ_alg;
281 ipsec_protocol_t proto;
282 ipsec_sa_flags_t flags;
283 u32 id, spi, sa_index = ~0;
285 .t_flags = TUNNEL_FLAG_NONE,
286 .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
288 .t_mode = TUNNEL_MODE_P2P,
294 id = ntohl (mp->entry.sad_id);
297 rv = ipsec_sa_unlock_id (id);
300 spi = ntohl (mp->entry.spi);
302 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
307 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
312 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
317 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
318 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
320 flags = ipsec_sa_flags_decode (mp->entry.flags);
322 ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
323 ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
325 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &crypto_key,
326 integ_alg, &integ_key, flags, mp->entry.salt,
327 htons (mp->entry.udp_src_port),
328 htons (mp->entry.udp_dst_port), &tun, &sa_index);
332 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
334 rmp->stat_index = htonl (sa_index);
339 static void vl_api_ipsec_sad_entry_add_del_v2_t_handler
340 (vl_api_ipsec_sad_entry_add_del_v2_t * mp)
342 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
343 vl_api_ipsec_sad_entry_add_del_v2_reply_t *rmp;
344 ipsec_key_t crypto_key, integ_key;
345 ipsec_crypto_alg_t crypto_alg;
346 ipsec_integ_alg_t integ_alg;
347 ipsec_protocol_t proto;
348 ipsec_sa_flags_t flags;
349 u32 id, spi, sa_index = ~0;
352 .t_flags = TUNNEL_FLAG_NONE,
353 .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
355 .t_mode = TUNNEL_MODE_P2P,
356 .t_table_id = htonl (mp->entry.tx_table_id),
360 id = ntohl (mp->entry.sad_id);
363 rv = ipsec_sa_unlock_id (id);
367 spi = ntohl (mp->entry.spi);
369 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
374 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
379 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
384 rv = tunnel_encap_decap_flags_decode (mp->entry.tunnel_flags,
385 &tun.t_encap_decap_flags);
390 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
391 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
393 flags = ipsec_sa_flags_decode (mp->entry.flags);
394 tun.t_dscp = ip_dscp_decode (mp->entry.dscp);
396 ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
397 ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
399 rv = ipsec_sa_add_and_lock (
400 id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags,
401 mp->entry.salt, htons (mp->entry.udp_src_port),
402 htons (mp->entry.udp_dst_port), &tun, &sa_index);
406 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V2_REPLY,
408 rmp->stat_index = htonl (sa_index);
414 ipsec_sad_entry_add_v3 (const vl_api_ipsec_sad_entry_v3_t *entry,
417 ipsec_key_t crypto_key, integ_key;
418 ipsec_crypto_alg_t crypto_alg;
419 ipsec_integ_alg_t integ_alg;
420 ipsec_protocol_t proto;
421 ipsec_sa_flags_t flags;
426 id = ntohl (entry->sad_id);
427 spi = ntohl (entry->spi);
429 rv = ipsec_proto_decode (entry->protocol, &proto);
434 rv = ipsec_crypto_algo_decode (entry->crypto_algorithm, &crypto_alg);
439 rv = ipsec_integ_algo_decode (entry->integrity_algorithm, &integ_alg);
444 flags = ipsec_sa_flags_decode (entry->flags);
446 if (flags & IPSEC_SA_FLAG_IS_TUNNEL)
448 rv = tunnel_decode (&entry->tunnel, &tun);
454 ipsec_key_decode (&entry->crypto_key, &crypto_key);
455 ipsec_key_decode (&entry->integrity_key, &integ_key);
457 return ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &crypto_key,
458 integ_alg, &integ_key, flags, entry->salt,
459 htons (entry->udp_src_port),
460 htons (entry->udp_dst_port), &tun, sa_index);
464 vl_api_ipsec_sad_entry_add_del_v3_t_handler (
465 vl_api_ipsec_sad_entry_add_del_v3_t *mp)
467 vl_api_ipsec_sad_entry_add_del_v3_reply_t *rmp;
468 u32 id, sa_index = ~0;
471 id = ntohl (mp->entry.sad_id);
475 rv = ipsec_sa_unlock_id (id);
479 rv = ipsec_sad_entry_add_v3 (&mp->entry, &sa_index);
482 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V3_REPLY,
483 { rmp->stat_index = htonl (sa_index); });
487 vl_api_ipsec_sad_entry_del_t_handler (vl_api_ipsec_sad_entry_del_t *mp)
489 vl_api_ipsec_sad_entry_del_reply_t *rmp;
492 rv = ipsec_sa_unlock_id (ntohl (mp->id));
494 REPLY_MACRO (VL_API_IPSEC_SAD_ENTRY_DEL_REPLY);
498 vl_api_ipsec_sad_entry_add_t_handler (vl_api_ipsec_sad_entry_add_t *mp)
500 vl_api_ipsec_sad_entry_add_reply_t *rmp;
504 rv = ipsec_sad_entry_add_v3 (&mp->entry, &sa_index);
506 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_REPLY,
507 { rmp->stat_index = htonl (sa_index); });
511 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
514 vl_api_ipsec_spds_details_t *mp;
517 mp = vl_msg_api_alloc (sizeof (*mp));
518 clib_memset (mp, 0, sizeof (*mp));
519 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPDS_DETAILS);
520 mp->context = context;
522 mp->spd_id = htonl (spd->id);
523 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
524 foreach_ipsec_spd_policy_type
526 mp->npolicies = htonl (n_policies);
528 vl_api_send_msg (reg, (u8 *) mp);
532 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
534 vl_api_registration_t *reg;
535 ipsec_main_t *im = &ipsec_main;
538 reg = vl_api_client_index_to_registration (mp->client_index);
542 pool_foreach (spd, im->spds) {
543 send_ipsec_spds_details (spd, reg, mp->context);
547 vl_api_ipsec_spd_action_t
548 ipsec_spd_action_encode (ipsec_policy_action_t in)
550 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
554 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
555 out = IPSEC_API_SPD_ACTION_##f; \
557 foreach_ipsec_policy_action
560 return (clib_host_to_net_u32 (out));
564 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
567 vl_api_ipsec_spd_details_t *mp;
569 mp = vl_msg_api_alloc (sizeof (*mp));
570 clib_memset (mp, 0, sizeof (*mp));
571 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_DETAILS);
572 mp->context = context;
574 mp->entry.spd_id = htonl (p->id);
575 mp->entry.priority = htonl (p->priority);
576 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
577 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
579 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
580 &mp->entry.local_address_start);
581 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
582 &mp->entry.local_address_stop);
583 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
584 &mp->entry.remote_address_start);
585 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
586 &mp->entry.remote_address_stop);
587 mp->entry.local_port_start = htons (p->lport.start);
588 mp->entry.local_port_stop = htons (p->lport.stop);
589 mp->entry.remote_port_start = htons (p->rport.start);
590 mp->entry.remote_port_stop = htons (p->rport.stop);
591 mp->entry.protocol = p->protocol;
592 mp->entry.policy = ipsec_spd_action_encode (p->policy);
593 mp->entry.sa_id = htonl (p->sa_id);
595 vl_api_send_msg (reg, (u8 *) mp);
599 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
601 vl_api_registration_t *reg;
602 ipsec_main_t *im = &ipsec_main;
603 ipsec_spd_policy_type_t ptype;
604 ipsec_policy_t *policy;
609 reg = vl_api_client_index_to_registration (mp->client_index);
613 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
618 spd = pool_elt_at_index (im->spds, spd_index);
620 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
621 vec_foreach(ii, spd->policies[ptype])
623 policy = pool_elt_at_index(im->policies, *ii);
625 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
626 send_ipsec_spd_details (policy, reg, mp->context);
632 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
633 u32 sw_if_index, u32 context)
635 vl_api_ipsec_spd_interface_details_t *mp;
637 mp = vl_msg_api_alloc (sizeof (*mp));
638 clib_memset (mp, 0, sizeof (*mp));
640 ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_INTERFACE_DETAILS);
641 mp->context = context;
643 mp->spd_index = htonl (spd_index);
644 mp->sw_if_index = htonl (sw_if_index);
646 vl_api_send_msg (reg, (u8 *) mp);
650 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
653 ipsec_main_t *im = &ipsec_main;
654 vl_api_registration_t *reg;
657 reg = vl_api_client_index_to_registration (mp->client_index);
661 if (mp->spd_index_valid)
663 spd_index = ntohl (mp->spd_index);
665 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
667 send_ipsec_spd_interface_details(reg, v, k, mp->context);
673 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
674 send_ipsec_spd_interface_details(reg, v, k, mp->context);
680 vl_api_ipsec_itf_create_t_handler (vl_api_ipsec_itf_create_t * mp)
682 vl_api_ipsec_itf_create_reply_t *rmp;
684 u32 sw_if_index = ~0;
687 rv = tunnel_mode_decode (mp->itf.mode, &mode);
690 rv = ipsec_itf_create (ntohl (mp->itf.user_instance), mode, &sw_if_index);
693 REPLY_MACRO2 (VL_API_IPSEC_ITF_CREATE_REPLY,
695 rmp->sw_if_index = htonl (sw_if_index);
701 vl_api_ipsec_itf_delete_t_handler (vl_api_ipsec_itf_delete_t * mp)
703 vl_api_ipsec_itf_delete_reply_t *rmp;
706 rv = ipsec_itf_delete (ntohl (mp->sw_if_index));
708 REPLY_MACRO (VL_API_IPSEC_ITF_DELETE_REPLY);
712 send_ipsec_itf_details (ipsec_itf_t *itf, void *arg)
714 ipsec_dump_walk_ctx_t *ctx = arg;
715 vl_api_ipsec_itf_details_t *mp;
717 if (~0 != ctx->sw_if_index && ctx->sw_if_index != itf->ii_sw_if_index)
718 return (WALK_CONTINUE);
720 mp = vl_msg_api_alloc (sizeof (*mp));
721 clib_memset (mp, 0, sizeof (*mp));
722 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_ITF_DETAILS);
723 mp->context = ctx->context;
725 mp->itf.mode = tunnel_mode_encode (itf->ii_mode);
726 mp->itf.user_instance = htonl (itf->ii_user_instance);
727 mp->itf.sw_if_index = htonl (itf->ii_sw_if_index);
728 vl_api_send_msg (ctx->reg, (u8 *) mp);
730 return (WALK_CONTINUE);
734 vl_api_ipsec_itf_dump_t_handler (vl_api_ipsec_itf_dump_t * mp)
736 vl_api_registration_t *reg;
738 reg = vl_api_client_index_to_registration (mp->client_index);
742 ipsec_dump_walk_ctx_t ctx = {
744 .context = mp->context,
745 .sw_if_index = ntohl (mp->sw_if_index),
748 ipsec_itf_walk (send_ipsec_itf_details, &ctx);
751 typedef struct ipsec_sa_dump_match_ctx_t_
755 } ipsec_sa_dump_match_ctx_t;
758 ipsec_sa_dump_match_sa (index_t itpi, void *arg)
760 ipsec_sa_dump_match_ctx_t *ctx = arg;
761 ipsec_tun_protect_t *itp;
764 itp = ipsec_tun_protect_get (itpi);
766 if (itp->itp_out_sa == ctx->sai)
768 ctx->sw_if_index = itp->itp_sw_if_index;
772 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
776 ctx->sw_if_index = itp->itp_sw_if_index;
781 return (WALK_CONTINUE);
785 send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
787 ipsec_dump_walk_ctx_t *ctx = arg;
788 vl_api_ipsec_sa_details_t *mp;
790 mp = vl_msg_api_alloc (sizeof (*mp));
791 clib_memset (mp, 0, sizeof (*mp));
792 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_DETAILS);
793 mp->context = ctx->context;
795 mp->entry.sad_id = htonl (sa->id);
796 mp->entry.spi = htonl (sa->spi);
797 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
798 mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
800 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
801 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
803 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
804 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
806 mp->entry.flags = ipsec_sad_flags_encode (sa);
807 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
809 if (ipsec_sa_is_set_IS_PROTECT (sa))
811 ipsec_sa_dump_match_ctx_t ctx = {
812 .sai = sa - ipsec_sa_pool,
815 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
817 mp->sw_if_index = htonl (ctx.sw_if_index);
820 mp->sw_if_index = ~0;
822 if (ipsec_sa_is_set_IS_TUNNEL (sa))
824 ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
825 ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
827 if (ipsec_sa_is_set_UDP_ENCAP (sa))
829 mp->entry.udp_src_port = sa->udp_hdr.src_port;
830 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
833 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
834 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq));
835 if (ipsec_sa_is_set_USE_ESN (sa))
837 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
838 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
840 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
841 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
843 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
845 vl_api_send_msg (ctx->reg, (u8 *) mp);
847 return (WALK_CONTINUE);
851 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
853 vl_api_registration_t *reg;
855 reg = vl_api_client_index_to_registration (mp->client_index);
859 ipsec_dump_walk_ctx_t ctx = {
861 .context = mp->context,
864 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
868 send_ipsec_sa_v2_details (ipsec_sa_t * sa, void *arg)
870 ipsec_dump_walk_ctx_t *ctx = arg;
871 vl_api_ipsec_sa_v2_details_t *mp;
873 mp = vl_msg_api_alloc (sizeof (*mp));
874 clib_memset (mp, 0, sizeof (*mp));
875 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V2_DETAILS);
876 mp->context = ctx->context;
878 mp->entry.sad_id = htonl (sa->id);
879 mp->entry.spi = htonl (sa->spi);
880 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
881 mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
883 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
884 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
886 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
887 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
889 mp->entry.flags = ipsec_sad_flags_encode (sa);
890 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
892 if (ipsec_sa_is_set_IS_PROTECT (sa))
894 ipsec_sa_dump_match_ctx_t ctx = {
895 .sai = sa - ipsec_sa_pool,
898 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
900 mp->sw_if_index = htonl (ctx.sw_if_index);
903 mp->sw_if_index = ~0;
905 if (ipsec_sa_is_set_IS_TUNNEL (sa))
907 ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
908 ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
910 if (ipsec_sa_is_set_UDP_ENCAP (sa))
912 mp->entry.udp_src_port = sa->udp_hdr.src_port;
913 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
916 mp->entry.tunnel_flags =
917 tunnel_encap_decap_flags_encode (sa->tunnel.t_encap_decap_flags);
918 mp->entry.dscp = ip_dscp_encode (sa->tunnel.t_dscp);
920 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
921 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq));
922 if (ipsec_sa_is_set_USE_ESN (sa))
924 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
925 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
927 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
928 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
930 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
932 vl_api_send_msg (ctx->reg, (u8 *) mp);
934 return (WALK_CONTINUE);
938 vl_api_ipsec_sa_v2_dump_t_handler (vl_api_ipsec_sa_v2_dump_t *mp)
940 vl_api_registration_t *reg;
942 reg = vl_api_client_index_to_registration (mp->client_index);
946 ipsec_dump_walk_ctx_t ctx = {
948 .context = mp->context,
951 ipsec_sa_walk (send_ipsec_sa_v2_details, &ctx);
955 send_ipsec_sa_v3_details (ipsec_sa_t *sa, void *arg)
957 ipsec_dump_walk_ctx_t *ctx = arg;
958 vl_api_ipsec_sa_v3_details_t *mp;
960 mp = vl_msg_api_alloc (sizeof (*mp));
961 clib_memset (mp, 0, sizeof (*mp));
962 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V3_DETAILS);
963 mp->context = ctx->context;
965 mp->entry.sad_id = htonl (sa->id);
966 mp->entry.spi = htonl (sa->spi);
967 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
969 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
970 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
972 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
973 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
975 mp->entry.flags = ipsec_sad_flags_encode (sa);
976 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
978 if (ipsec_sa_is_set_IS_PROTECT (sa))
980 ipsec_sa_dump_match_ctx_t ctx = {
981 .sai = sa - ipsec_sa_pool,
984 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
986 mp->sw_if_index = htonl (ctx.sw_if_index);
989 mp->sw_if_index = ~0;
991 if (ipsec_sa_is_set_IS_TUNNEL (sa))
992 tunnel_encode (&sa->tunnel, &mp->entry.tunnel);
994 if (ipsec_sa_is_set_UDP_ENCAP (sa))
996 mp->entry.udp_src_port = sa->udp_hdr.src_port;
997 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
1000 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
1001 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq));
1002 if (ipsec_sa_is_set_USE_ESN (sa))
1004 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
1005 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
1007 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
1008 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
1010 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
1012 vl_api_send_msg (ctx->reg, (u8 *) mp);
1014 return (WALK_CONTINUE);
1018 vl_api_ipsec_sa_v3_dump_t_handler (vl_api_ipsec_sa_v3_dump_t *mp)
1020 vl_api_registration_t *reg;
1022 reg = vl_api_client_index_to_registration (mp->client_index);
1026 ipsec_dump_walk_ctx_t ctx = {
1028 .context = mp->context,
1031 ipsec_sa_walk (send_ipsec_sa_v3_details, &ctx);
1035 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
1037 vl_api_registration_t *rp;
1038 ipsec_main_t *im = &ipsec_main;
1039 u32 context = mp->context;
1041 rp = vl_api_client_index_to_registration (mp->client_index);
1045 clib_warning ("Client %d AWOL", mp->client_index);
1049 ipsec_ah_backend_t *ab;
1050 ipsec_esp_backend_t *eb;
1052 pool_foreach (ab, im->ah_backends) {
1053 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1054 clib_memset (mp, 0, sizeof (*mp));
1055 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
1056 mp->context = context;
1057 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1059 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
1060 mp->index = ab - im->ah_backends;
1061 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1062 vl_api_send_msg (rp, (u8 *)mp);
1064 pool_foreach (eb, im->esp_backends) {
1065 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1066 clib_memset (mp, 0, sizeof (*mp));
1067 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
1068 mp->context = context;
1069 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1071 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
1072 mp->index = eb - im->esp_backends;
1073 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1074 vl_api_send_msg (rp, (u8 *)mp);
1080 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1082 ipsec_main_t *im = &ipsec_main;
1083 vl_api_ipsec_select_backend_reply_t *rmp;
1084 ipsec_protocol_t protocol;
1086 if (pool_elts (ipsec_sa_pool) > 0)
1088 rv = VNET_API_ERROR_INSTANCE_IN_USE;
1092 rv = ipsec_proto_decode (mp->protocol, &protocol);
1099 case IPSEC_PROTOCOL_ESP:
1100 rv = ipsec_select_esp_backend (im, mp->index);
1102 case IPSEC_PROTOCOL_AH:
1103 rv = ipsec_select_ah_backend (im, mp->index);
1106 rv = VNET_API_ERROR_INVALID_PROTOCOL;
1110 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1114 vl_api_ipsec_set_async_mode_t_handler (vl_api_ipsec_set_async_mode_t * mp)
1116 vl_api_ipsec_set_async_mode_reply_t *rmp;
1119 ipsec_set_async_mode (mp->async_enable);
1121 REPLY_MACRO (VL_API_IPSEC_SET_ASYNC_MODE_REPLY);
1124 #include <vnet/ipsec/ipsec.api.c>
1125 static clib_error_t *
1126 ipsec_api_hookup (vlib_main_t * vm)
1129 * Set up the (msg_name, crc, message-id) table
1131 REPLY_MSG_ID_BASE = setup_message_id_table ();
1136 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1139 * fd.io coding-style-patch-verification: ON
1142 * eval: (c-set-style "gnu")