2 *------------------------------------------------------------------
3 * ipsec_api.c - ipsec api
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/ip/ip_types_api.h>
27 #include <vnet/fib/fib.h>
28 #include <vnet/ipip/ipip.h>
30 #include <vnet/vnet_msg_enum.h>
33 #include <vnet/ipsec/ipsec.h>
34 #include <vnet/ipsec/ipsec_tun.h>
37 #define vl_typedefs /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
41 #define vl_endianfun /* define message structures */
42 #include <vnet/vnet_all_api_h.h>
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
48 #include <vnet/vnet_all_api_h.h>
51 #include <vlibapi/api_helper_macros.h>
53 #define foreach_vpe_api_msg \
54 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
55 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
56 _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \
57 _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \
58 _(IPSEC_SA_DUMP, ipsec_sa_dump) \
59 _(IPSEC_SPDS_DUMP, ipsec_spds_dump) \
60 _(IPSEC_SPD_DUMP, ipsec_spd_dump) \
61 _(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \
62 _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \
63 _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \
64 _(IPSEC_SELECT_BACKEND, ipsec_select_backend) \
65 _(IPSEC_BACKEND_DUMP, ipsec_backend_dump) \
66 _(IPSEC_TUNNEL_PROTECT_UPDATE, ipsec_tunnel_protect_update) \
67 _(IPSEC_TUNNEL_PROTECT_DEL, ipsec_tunnel_protect_del) \
68 _(IPSEC_TUNNEL_PROTECT_DUMP, ipsec_tunnel_protect_dump)
71 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
74 clib_warning ("unimplemented");
77 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
78 vl_api_ipsec_spd_add_del_reply_t *rmp;
81 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
83 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
87 static void vl_api_ipsec_interface_add_del_spd_t_handler
88 (vl_api_ipsec_interface_add_del_spd_t * mp)
90 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
91 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
93 u32 sw_if_index __attribute__ ((unused));
94 u32 spd_id __attribute__ ((unused));
96 sw_if_index = ntohl (mp->sw_if_index);
97 spd_id = ntohl (mp->spd_id);
99 VALIDATE_SW_IF_INDEX (mp);
102 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
104 rv = VNET_API_ERROR_UNIMPLEMENTED;
107 BAD_SW_IF_INDEX_LABEL;
109 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
112 static void vl_api_ipsec_tunnel_protect_update_t_handler
113 (vl_api_ipsec_tunnel_protect_update_t * mp)
115 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
116 vl_api_ipsec_tunnel_protect_update_reply_t *rmp;
117 u32 sw_if_index, ii, *sa_ins = NULL;
120 sw_if_index = ntohl (mp->tunnel.sw_if_index);
122 VALIDATE_SW_IF_INDEX (&(mp->tunnel));
126 for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
127 vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
129 rv = ipsec_tun_protect_update (sw_if_index,
130 ntohl (mp->tunnel.sa_out), sa_ins);
132 rv = VNET_API_ERROR_UNIMPLEMENTED;
135 BAD_SW_IF_INDEX_LABEL;
137 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
140 static void vl_api_ipsec_tunnel_protect_del_t_handler
141 (vl_api_ipsec_tunnel_protect_del_t * mp)
143 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
144 vl_api_ipsec_tunnel_protect_del_reply_t *rmp;
148 sw_if_index = ntohl (mp->sw_if_index);
150 VALIDATE_SW_IF_INDEX (mp);
153 rv = ipsec_tun_protect_del (sw_if_index);
155 rv = VNET_API_ERROR_UNIMPLEMENTED;
158 BAD_SW_IF_INDEX_LABEL;
160 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
163 typedef struct ipsec_dump_walk_ctx_t_
165 vl_api_registration_t *reg;
167 } ipsec_dump_walk_ctx_t;
170 send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
172 ipsec_dump_walk_ctx_t *ctx = arg;
173 vl_api_ipsec_tunnel_protect_details_t *mp;
174 ipsec_tun_protect_t *itp;
177 itp = ipsec_tun_protect_get (itpi);
180 mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
181 clib_memset (mp, 0, sizeof (*mp));
182 mp->_vl_msg_id = ntohs (VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
183 mp->context = ctx->context;
185 mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
187 mp->tun.sa_out = htonl (itp->itp_out_sa);
188 mp->tun.n_sa_in = itp->itp_n_sa_in;
190 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
192 mp->tun.sa_in[ii++] = htonl (sai);
196 vl_api_send_msg (ctx->reg, (u8 *) mp);
198 return (WALK_CONTINUE);
202 vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
205 vl_api_registration_t *reg;
209 reg = vl_api_client_index_to_registration (mp->client_index);
213 ipsec_dump_walk_ctx_t ctx = {
215 .context = mp->context,
218 sw_if_index = ntohl (mp->sw_if_index);
220 if (~0 == sw_if_index)
222 ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
228 itpi = ipsec_tun_protect_find (sw_if_index);
230 if (INDEX_INVALID != itpi)
231 send_ipsec_tunnel_protect_details (itpi, &ctx);
234 clib_warning ("unimplemented");
239 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
240 ipsec_policy_action_t * out)
242 in = clib_net_to_host_u32 (in);
246 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
247 *out = IPSEC_POLICY_ACTION_##f; \
249 foreach_ipsec_policy_action
252 return (VNET_API_ERROR_UNIMPLEMENTED);
255 static void vl_api_ipsec_spd_entry_add_del_t_handler
256 (vl_api_ipsec_spd_entry_add_del_t * mp)
258 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
259 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
269 clib_memset (&p, 0, sizeof (p));
271 p.id = ntohl (mp->entry.spd_id);
272 p.priority = ntohl (mp->entry.priority);
274 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
275 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
276 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
277 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
279 p.is_ipv6 = (itype == IP46_TYPE_IP6);
281 p.protocol = mp->entry.protocol;
282 p.rport.start = ntohs (mp->entry.remote_port_start);
283 p.rport.stop = ntohs (mp->entry.remote_port_stop);
284 p.lport.start = ntohs (mp->entry.local_port_start);
285 p.lport.stop = ntohs (mp->entry.local_port_stop);
287 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
292 /* policy action resolve unsupported */
293 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
295 clib_warning ("unsupported action: 'resolve'");
296 rv = VNET_API_ERROR_UNIMPLEMENTED;
299 p.sa_id = ntohl (mp->entry.sa_id);
301 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
306 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
311 rv = VNET_API_ERROR_UNIMPLEMENTED;
317 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
319 rmp->stat_index = ntohl(stat_index);
325 ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
327 in = clib_net_to_host_u32 (in);
331 case IPSEC_API_PROTO_ESP:
332 *out = IPSEC_PROTOCOL_ESP;
334 case IPSEC_API_PROTO_AH:
335 *out = IPSEC_PROTOCOL_AH;
338 return (VNET_API_ERROR_INVALID_PROTOCOL);
341 static vl_api_ipsec_proto_t
342 ipsec_proto_encode (ipsec_protocol_t p)
346 case IPSEC_PROTOCOL_ESP:
347 return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP);
348 case IPSEC_PROTOCOL_AH:
349 return clib_host_to_net_u32 (IPSEC_API_PROTO_AH);
351 return (VNET_API_ERROR_UNIMPLEMENTED);
355 ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
356 ipsec_crypto_alg_t * out)
358 in = clib_net_to_host_u32 (in);
362 #define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
363 *out = IPSEC_CRYPTO_ALG_##f; \
365 foreach_ipsec_crypto_alg
368 return (VNET_API_ERROR_INVALID_ALGORITHM);
371 static vl_api_ipsec_crypto_alg_t
372 ipsec_crypto_algo_encode (ipsec_crypto_alg_t c)
376 #define _(v,f,s) case IPSEC_CRYPTO_ALG_##f: \
377 return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f);
378 foreach_ipsec_crypto_alg
380 case IPSEC_CRYPTO_N_ALG:
384 return (VNET_API_ERROR_UNIMPLEMENTED);
388 ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
390 in = clib_net_to_host_u32 (in);
394 #define _(v,f,s) case IPSEC_API_INTEG_ALG_##f: \
395 *out = IPSEC_INTEG_ALG_##f; \
397 foreach_ipsec_integ_alg
400 return (VNET_API_ERROR_INVALID_ALGORITHM);
403 static vl_api_ipsec_integ_alg_t
404 ipsec_integ_algo_encode (ipsec_integ_alg_t i)
408 #define _(v,f,s) case IPSEC_INTEG_ALG_##f: \
409 return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f));
410 foreach_ipsec_integ_alg
412 case IPSEC_INTEG_N_ALG:
416 return (VNET_API_ERROR_UNIMPLEMENTED);
420 ipsec_key_decode (const vl_api_key_t * key, ipsec_key_t * out)
422 ipsec_mk_key (out, key->data, key->length);
426 ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out)
428 out->length = in->len;
429 clib_memcpy (out->data, in->data, out->length);
432 static ipsec_sa_flags_t
433 ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in)
435 ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE;
436 in = clib_net_to_host_u32 (in);
438 if (in & IPSEC_API_SAD_FLAG_USE_ESN)
439 flags |= IPSEC_SA_FLAG_USE_ESN;
440 if (in & IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY)
441 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
442 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL)
443 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
444 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL_V6)
445 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
446 if (in & IPSEC_API_SAD_FLAG_UDP_ENCAP)
447 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
452 static vl_api_ipsec_sad_flags_t
453 ipsec_sad_flags_encode (const ipsec_sa_t * sa)
455 vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE;
457 if (ipsec_sa_is_set_USE_ESN (sa))
458 flags |= IPSEC_API_SAD_FLAG_USE_ESN;
459 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
460 flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
461 if (ipsec_sa_is_set_IS_TUNNEL (sa))
462 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
463 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
464 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
465 if (ipsec_sa_is_set_UDP_ENCAP (sa))
466 flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
468 return clib_host_to_net_u32 (flags);
471 static void vl_api_ipsec_sad_entry_add_del_t_handler
472 (vl_api_ipsec_sad_entry_add_del_t * mp)
474 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
475 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
476 ip46_address_t tun_src = { }, tun_dst =
479 ipsec_key_t crypto_key, integ_key;
480 ipsec_crypto_alg_t crypto_alg;
481 ipsec_integ_alg_t integ_alg;
482 ipsec_protocol_t proto;
483 ipsec_sa_flags_t flags;
484 u32 id, spi, sa_index = ~0;
489 id = ntohl (mp->entry.sad_id);
490 spi = ntohl (mp->entry.spi);
492 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
497 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
502 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
507 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
508 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
510 flags = ipsec_sa_flags_decode (mp->entry.flags);
512 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
513 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
516 rv = ipsec_sa_add_and_lock (id, spi, proto,
517 crypto_alg, &crypto_key,
518 integ_alg, &integ_key, flags,
519 0, mp->entry.salt, &tun_src, &tun_dst,
522 rv = ipsec_sa_unlock_id (id);
525 rv = VNET_API_ERROR_UNIMPLEMENTED;
530 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
532 rmp->stat_index = htonl (sa_index);
538 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
541 vl_api_ipsec_spds_details_t *mp;
544 mp = vl_msg_api_alloc (sizeof (*mp));
545 clib_memset (mp, 0, sizeof (*mp));
546 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
547 mp->context = context;
549 mp->spd_id = htonl (spd->id);
550 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
551 foreach_ipsec_spd_policy_type
553 mp->npolicies = htonl (n_policies);
555 vl_api_send_msg (reg, (u8 *) mp);
559 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
561 vl_api_registration_t *reg;
562 ipsec_main_t *im = &ipsec_main;
565 reg = vl_api_client_index_to_registration (mp->client_index);
570 pool_foreach (spd, im->spds, ({
571 send_ipsec_spds_details (spd, reg, mp->context);
575 clib_warning ("unimplemented");
579 vl_api_ipsec_spd_action_t
580 ipsec_spd_action_encode (ipsec_policy_action_t in)
582 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
586 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
587 out = IPSEC_API_SPD_ACTION_##f; \
589 foreach_ipsec_policy_action
592 return (clib_host_to_net_u32 (out));
596 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
599 vl_api_ipsec_spd_details_t *mp;
601 mp = vl_msg_api_alloc (sizeof (*mp));
602 clib_memset (mp, 0, sizeof (*mp));
603 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
604 mp->context = context;
606 mp->entry.spd_id = htonl (p->id);
607 mp->entry.priority = htonl (p->priority);
608 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
609 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
611 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
612 &mp->entry.local_address_start);
613 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
614 &mp->entry.local_address_stop);
615 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
616 &mp->entry.remote_address_start);
617 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
618 &mp->entry.remote_address_stop);
619 mp->entry.local_port_start = htons (p->lport.start);
620 mp->entry.local_port_stop = htons (p->lport.stop);
621 mp->entry.remote_port_start = htons (p->rport.start);
622 mp->entry.remote_port_stop = htons (p->rport.stop);
623 mp->entry.protocol = p->protocol;
624 mp->entry.policy = ipsec_spd_action_encode (p->policy);
625 mp->entry.sa_id = htonl (p->sa_id);
627 vl_api_send_msg (reg, (u8 *) mp);
631 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
633 vl_api_registration_t *reg;
634 ipsec_main_t *im = &ipsec_main;
635 ipsec_spd_policy_type_t ptype;
636 ipsec_policy_t *policy;
641 reg = vl_api_client_index_to_registration (mp->client_index);
645 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
650 spd = pool_elt_at_index (im->spds, spd_index);
653 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
654 vec_foreach(ii, spd->policies[ptype])
656 policy = pool_elt_at_index(im->policies, *ii);
658 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
659 send_ipsec_spd_details (policy, reg, mp->context);
664 clib_warning ("unimplemented");
669 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
670 u32 sw_if_index, u32 context)
672 vl_api_ipsec_spd_interface_details_t *mp;
674 mp = vl_msg_api_alloc (sizeof (*mp));
675 clib_memset (mp, 0, sizeof (*mp));
676 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
677 mp->context = context;
679 mp->spd_index = htonl (spd_index);
680 mp->sw_if_index = htonl (sw_if_index);
682 vl_api_send_msg (reg, (u8 *) mp);
686 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
689 ipsec_main_t *im = &ipsec_main;
690 vl_api_registration_t *reg;
694 reg = vl_api_client_index_to_registration (mp->client_index);
698 if (mp->spd_index_valid)
700 spd_index = ntohl (mp->spd_index);
702 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
704 send_ipsec_spd_interface_details(reg, v, k, mp->context);
711 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
712 send_ipsec_spd_interface_details(reg, v, k, mp->context);
718 clib_warning ("unimplemented");
723 ipsec_tun_mk_input_sa_id (u32 ti)
725 return (0x80000000 | ti);
729 ipsec_tun_mk_output_sa_id (u32 ti)
731 return (0xc0000000 | ti);
735 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
738 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
739 u32 sw_if_index = ~0;
743 ip46_address_t local_ip = ip46_address_initializer;
744 ip46_address_t remote_ip = ip46_address_initializer;
745 ipsec_key_t crypto_key, integ_key;
746 ipsec_sa_flags_t flags;
747 ip46_type_t local_ip_type, remote_ip_type;
748 ipip_transport_t transport;
751 local_ip_type = ip_address_decode (&mp->local_ip, &local_ip);
752 remote_ip_type = ip_address_decode (&mp->remote_ip, &remote_ip);
753 transport = (IP46_TYPE_IP6 == local_ip_type ?
754 IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4);
756 if (local_ip_type != remote_ip_type)
758 rv = VNET_API_ERROR_INVALID_VALUE;
762 flags = IPSEC_SA_FLAG_NONE;
765 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
767 flags |= IPSEC_SA_FLAG_USE_ESN;
769 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
771 ipsec_mk_key (&crypto_key, mp->remote_crypto_key,
772 mp->remote_crypto_key_len);
773 ipsec_mk_key (&integ_key, mp->remote_integ_key, mp->remote_integ_key_len);
774 ipsec_mk_key (&crypto_key, mp->local_crypto_key, mp->local_crypto_key_len);
775 ipsec_mk_key (&integ_key, mp->local_integ_key, mp->local_integ_key_len);
778 fib_table_find (fib_proto_from_ip46 (local_ip_type),
779 ntohl (mp->tx_table_id));
783 rv = VNET_API_ERROR_NO_SUCH_FIB;
789 // remote = input, local = output
790 /* create an ip-ip tunnel, then the two SA, then bind them */
791 rv = ipip_add_tunnel (transport,
792 ntohl (mp->show_instance),
794 &remote_ip, fib_index, 0, &sw_if_index);
799 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_input_sa_id (sw_if_index),
800 ntohl (mp->remote_spi),
806 (flags | IPSEC_SA_FLAG_IS_INBOUND),
807 ntohl (mp->tx_table_id),
808 mp->salt, &remote_ip, &local_ip, NULL);
813 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_output_sa_id (sw_if_index),
814 ntohl (mp->local_spi),
821 ntohl (mp->tx_table_id),
822 mp->salt, &local_ip, &remote_ip, NULL);
827 rv = ipsec_tun_protect_update_one (sw_if_index,
828 ipsec_tun_mk_output_sa_id
830 ipsec_tun_mk_input_sa_id
835 /* the SAs are locked as a result of being used for proection,
836 * they cannot be removed from the API, since they cannot be refered
837 * to by the API. unlock them now, so that if the tunnel is rekeyed
840 ipsec_sa_unlock_id (ipsec_tun_mk_input_sa_id (sw_if_index));
841 ipsec_sa_unlock_id (ipsec_tun_mk_output_sa_id (sw_if_index));
846 ipip_tunnel_key_t key = {
847 .transport = transport,
848 .fib_index = fib_index,
854 ipip_tunnel_t *t = ipip_tunnel_db_find (&key);
858 rv = ipsec_tun_protect_del (t->sw_if_index);
859 ipip_del_tunnel (t->sw_if_index);
862 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
866 rv = VNET_API_ERROR_UNIMPLEMENTED;
870 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
872 rmp->sw_if_index = htonl (sw_if_index);
877 typedef struct ipsec_sa_dump_match_ctx_t_
881 } ipsec_sa_dump_match_ctx_t;
884 ipsec_sa_dump_match_sa (index_t itpi, void *arg)
886 ipsec_sa_dump_match_ctx_t *ctx = arg;
887 ipsec_tun_protect_t *itp;
890 itp = ipsec_tun_protect_get (itpi);
892 if (itp->itp_out_sa == ctx->sai)
894 ctx->sw_if_index = itp->itp_sw_if_index;
898 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
902 ctx->sw_if_index = itp->itp_sw_if_index;
908 return (WALK_CONTINUE);
912 send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
914 ipsec_dump_walk_ctx_t *ctx = arg;
915 vl_api_ipsec_sa_details_t *mp;
916 ipsec_main_t *im = &ipsec_main;
918 mp = vl_msg_api_alloc (sizeof (*mp));
919 clib_memset (mp, 0, sizeof (*mp));
920 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
921 mp->context = ctx->context;
923 mp->entry.sad_id = htonl (sa->id);
924 mp->entry.spi = htonl (sa->spi);
925 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
926 mp->entry.tx_table_id =
927 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
929 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
930 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
932 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
933 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
935 mp->entry.flags = ipsec_sad_flags_encode (sa);
936 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
938 if (ipsec_sa_is_set_IS_PROTECT (sa))
940 ipsec_sa_dump_match_ctx_t ctx = {
944 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
946 mp->sw_if_index = htonl (ctx.sw_if_index);
949 mp->sw_if_index = ~0;
951 if (ipsec_sa_is_set_IS_TUNNEL (sa))
953 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
954 &mp->entry.tunnel_src);
955 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
956 &mp->entry.tunnel_dst);
959 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
960 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
961 if (ipsec_sa_is_set_USE_ESN (sa))
963 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
964 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
966 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
967 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
969 vl_api_send_msg (ctx->reg, (u8 *) mp);
971 return (WALK_CONTINUE);
975 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
977 vl_api_registration_t *reg;
980 reg = vl_api_client_index_to_registration (mp->client_index);
984 ipsec_dump_walk_ctx_t ctx = {
986 .context = mp->context,
989 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
992 clib_warning ("unimplemented");
997 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
999 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
1003 VALIDATE_SW_IF_INDEX(mp);
1005 if (mp->is_outbound)
1006 rv = ipsec_tun_protect_update_out (ntohl (mp->sw_if_index),
1009 rv = ipsec_tun_protect_update_in (ntohl (mp->sw_if_index),
1013 clib_warning ("unimplemented");
1016 BAD_SW_IF_INDEX_LABEL;
1018 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
1022 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
1024 vl_api_registration_t *rp;
1025 ipsec_main_t *im = &ipsec_main;
1026 u32 context = mp->context;
1028 rp = vl_api_client_index_to_registration (mp->client_index);
1032 clib_warning ("Client %d AWOL", mp->client_index);
1036 ipsec_ah_backend_t *ab;
1037 ipsec_esp_backend_t *eb;
1039 pool_foreach (ab, im->ah_backends, {
1040 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1041 clib_memset (mp, 0, sizeof (*mp));
1042 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1043 mp->context = context;
1044 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1046 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
1047 mp->index = ab - im->ah_backends;
1048 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1049 vl_api_send_msg (rp, (u8 *)mp);
1051 pool_foreach (eb, im->esp_backends, {
1052 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1053 clib_memset (mp, 0, sizeof (*mp));
1054 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1055 mp->context = context;
1056 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1058 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
1059 mp->index = eb - im->esp_backends;
1060 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1061 vl_api_send_msg (rp, (u8 *)mp);
1067 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1069 ipsec_main_t *im = &ipsec_main;
1070 vl_api_ipsec_select_backend_reply_t *rmp;
1071 ipsec_protocol_t protocol;
1073 if (pool_elts (im->sad) > 0)
1075 rv = VNET_API_ERROR_INSTANCE_IN_USE;
1079 rv = ipsec_proto_decode (mp->protocol, &protocol);
1087 case IPSEC_PROTOCOL_ESP:
1088 rv = ipsec_select_esp_backend (im, mp->index);
1090 case IPSEC_PROTOCOL_AH:
1091 rv = ipsec_select_ah_backend (im, mp->index);
1094 rv = VNET_API_ERROR_INVALID_PROTOCOL;
1098 clib_warning ("unimplemented"); /* FIXME */
1101 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1106 * Add vpe's API message handlers to the table.
1107 * vlib has already mapped shared memory and
1108 * added the client registration handlers.
1109 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1111 #define vl_msg_name_crc_list
1112 #include <vnet/vnet_all_api_h.h>
1113 #undef vl_msg_name_crc_list
1116 setup_message_id_table (api_main_t * am)
1118 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1119 foreach_vl_msg_name_crc_ipsec;
1123 static clib_error_t *
1124 ipsec_api_hookup (vlib_main_t * vm)
1126 api_main_t *am = &api_main;
1129 vl_msg_api_set_handlers(VL_API_##N, #n, \
1130 vl_api_##n##_t_handler, \
1132 vl_api_##n##_t_endian, \
1133 vl_api_##n##_t_print, \
1134 sizeof(vl_api_##n##_t), 1);
1135 foreach_vpe_api_msg;
1139 * Adding and deleting SAs is MP safe since when they are added/delete
1140 * no traffic is using them
1142 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL] = 1;
1143 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY] = 1;
1146 * Set up the (msg_name, crc, message-id) table
1148 setup_message_id_table (am);
1153 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1156 * fd.io coding-style-patch-verification: ON
1159 * eval: (c-set-style "gnu")