2 *------------------------------------------------------------------
3 * ipsec_api.c - ipsec api
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/ip/ip_types_api.h>
27 #include <vnet/ipsec/ipsec_types_api.h>
28 #include <vnet/fib/fib.h>
29 #include <vnet/ipip/ipip.h>
31 #include <vnet/vnet_msg_enum.h>
34 #include <vnet/ipsec/ipsec.h>
35 #include <vnet/ipsec/ipsec_tun.h>
38 #define vl_typedefs /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
42 #define vl_endianfun /* define message structures */
43 #include <vnet/vnet_all_api_h.h>
46 /* instantiate all the print functions we know about */
47 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
49 #include <vnet/vnet_all_api_h.h>
52 #include <vlibapi/api_helper_macros.h>
54 #define foreach_vpe_api_msg \
55 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
56 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
57 _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \
58 _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \
59 _(IPSEC_SA_DUMP, ipsec_sa_dump) \
60 _(IPSEC_SPDS_DUMP, ipsec_spds_dump) \
61 _(IPSEC_SPD_DUMP, ipsec_spd_dump) \
62 _(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \
63 _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \
64 _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \
65 _(IPSEC_SELECT_BACKEND, ipsec_select_backend) \
66 _(IPSEC_BACKEND_DUMP, ipsec_backend_dump) \
67 _(IPSEC_TUNNEL_PROTECT_UPDATE, ipsec_tunnel_protect_update) \
68 _(IPSEC_TUNNEL_PROTECT_DEL, ipsec_tunnel_protect_del) \
69 _(IPSEC_TUNNEL_PROTECT_DUMP, ipsec_tunnel_protect_dump)
72 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
75 clib_warning ("unimplemented");
78 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
79 vl_api_ipsec_spd_add_del_reply_t *rmp;
82 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
84 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
88 static void vl_api_ipsec_interface_add_del_spd_t_handler
89 (vl_api_ipsec_interface_add_del_spd_t * mp)
91 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
92 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
94 u32 sw_if_index __attribute__ ((unused));
95 u32 spd_id __attribute__ ((unused));
97 sw_if_index = ntohl (mp->sw_if_index);
98 spd_id = ntohl (mp->spd_id);
100 VALIDATE_SW_IF_INDEX (mp);
103 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
105 rv = VNET_API_ERROR_UNIMPLEMENTED;
108 BAD_SW_IF_INDEX_LABEL;
110 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
113 static void vl_api_ipsec_tunnel_protect_update_t_handler
114 (vl_api_ipsec_tunnel_protect_update_t * mp)
116 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
117 vl_api_ipsec_tunnel_protect_update_reply_t *rmp;
118 u32 sw_if_index, ii, *sa_ins = NULL;
122 sw_if_index = ntohl (mp->tunnel.sw_if_index);
124 VALIDATE_SW_IF_INDEX (&(mp->tunnel));
128 for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
129 vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
131 ip_address_decode2 (&mp->tunnel.nh, &nh);
133 rv = ipsec_tun_protect_update (sw_if_index, &nh,
134 ntohl (mp->tunnel.sa_out), sa_ins);
136 rv = VNET_API_ERROR_UNIMPLEMENTED;
139 BAD_SW_IF_INDEX_LABEL;
141 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
144 static void vl_api_ipsec_tunnel_protect_del_t_handler
145 (vl_api_ipsec_tunnel_protect_del_t * mp)
147 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
148 vl_api_ipsec_tunnel_protect_del_reply_t *rmp;
153 sw_if_index = ntohl (mp->sw_if_index);
155 VALIDATE_SW_IF_INDEX (mp);
158 ip_address_decode2 (&mp->nh, &nh);
159 rv = ipsec_tun_protect_del (sw_if_index, &nh);
161 rv = VNET_API_ERROR_UNIMPLEMENTED;
164 BAD_SW_IF_INDEX_LABEL;
166 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
169 typedef struct ipsec_dump_walk_ctx_t_
171 vl_api_registration_t *reg;
173 } ipsec_dump_walk_ctx_t;
176 send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
178 ipsec_dump_walk_ctx_t *ctx = arg;
179 vl_api_ipsec_tunnel_protect_details_t *mp;
180 ipsec_tun_protect_t *itp;
184 itp = ipsec_tun_protect_get (itpi);
186 mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
187 clib_memset (mp, 0, sizeof (*mp));
188 mp->_vl_msg_id = ntohs (VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
189 mp->context = ctx->context;
191 mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
192 ip_address_encode2 (itp->itp_key, &mp->tun.nh);
194 sa = ipsec_sa_get (itp->itp_out_sa);
195 mp->tun.sa_out = htonl (sa->id);
196 mp->tun.n_sa_in = itp->itp_n_sa_in;
198 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
200 mp->tun.sa_in[ii++] = htonl (sa->id);
204 vl_api_send_msg (ctx->reg, (u8 *) mp);
206 return (WALK_CONTINUE);
210 vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
213 vl_api_registration_t *reg;
217 reg = vl_api_client_index_to_registration (mp->client_index);
221 ipsec_dump_walk_ctx_t ctx = {
223 .context = mp->context,
226 sw_if_index = ntohl (mp->sw_if_index);
228 if (~0 == sw_if_index)
230 ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
234 ipsec_tun_protect_walk_itf (sw_if_index,
235 send_ipsec_tunnel_protect_details, &ctx);
238 clib_warning ("unimplemented");
243 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
244 ipsec_policy_action_t * out)
246 in = clib_net_to_host_u32 (in);
250 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
251 *out = IPSEC_POLICY_ACTION_##f; \
253 foreach_ipsec_policy_action
256 return (VNET_API_ERROR_UNIMPLEMENTED);
259 static void vl_api_ipsec_spd_entry_add_del_t_handler
260 (vl_api_ipsec_spd_entry_add_del_t * mp)
262 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
263 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
273 clib_memset (&p, 0, sizeof (p));
275 p.id = ntohl (mp->entry.spd_id);
276 p.priority = ntohl (mp->entry.priority);
278 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
279 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
280 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
281 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
283 p.is_ipv6 = (itype == IP46_TYPE_IP6);
285 p.protocol = mp->entry.protocol;
286 p.rport.start = ntohs (mp->entry.remote_port_start);
287 p.rport.stop = ntohs (mp->entry.remote_port_stop);
288 p.lport.start = ntohs (mp->entry.local_port_start);
289 p.lport.stop = ntohs (mp->entry.local_port_stop);
291 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
296 /* policy action resolve unsupported */
297 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
299 clib_warning ("unsupported action: 'resolve'");
300 rv = VNET_API_ERROR_UNIMPLEMENTED;
303 p.sa_id = ntohl (mp->entry.sa_id);
305 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
310 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
315 rv = VNET_API_ERROR_UNIMPLEMENTED;
321 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
323 rmp->stat_index = ntohl(stat_index);
328 static void vl_api_ipsec_sad_entry_add_del_t_handler
329 (vl_api_ipsec_sad_entry_add_del_t * mp)
331 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
332 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
333 ip46_address_t tun_src = { }, tun_dst =
336 ipsec_key_t crypto_key, integ_key;
337 ipsec_crypto_alg_t crypto_alg;
338 ipsec_integ_alg_t integ_alg;
339 ipsec_protocol_t proto;
340 ipsec_sa_flags_t flags;
341 u32 id, spi, sa_index = ~0;
346 id = ntohl (mp->entry.sad_id);
347 spi = ntohl (mp->entry.spi);
349 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
354 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
359 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
364 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
365 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
367 flags = ipsec_sa_flags_decode (mp->entry.flags);
369 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
370 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
373 rv = ipsec_sa_add_and_lock (id, spi, proto,
374 crypto_alg, &crypto_key,
375 integ_alg, &integ_key, flags,
376 0, mp->entry.salt, &tun_src, &tun_dst,
377 &sa_index, htons (mp->entry.udp_src_port),
378 htons (mp->entry.udp_dst_port));
380 rv = ipsec_sa_unlock_id (id);
383 rv = VNET_API_ERROR_UNIMPLEMENTED;
388 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
390 rmp->stat_index = htonl (sa_index);
396 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
399 vl_api_ipsec_spds_details_t *mp;
402 mp = vl_msg_api_alloc (sizeof (*mp));
403 clib_memset (mp, 0, sizeof (*mp));
404 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
405 mp->context = context;
407 mp->spd_id = htonl (spd->id);
408 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
409 foreach_ipsec_spd_policy_type
411 mp->npolicies = htonl (n_policies);
413 vl_api_send_msg (reg, (u8 *) mp);
417 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
419 vl_api_registration_t *reg;
420 ipsec_main_t *im = &ipsec_main;
423 reg = vl_api_client_index_to_registration (mp->client_index);
428 pool_foreach (spd, im->spds, ({
429 send_ipsec_spds_details (spd, reg, mp->context);
433 clib_warning ("unimplemented");
437 vl_api_ipsec_spd_action_t
438 ipsec_spd_action_encode (ipsec_policy_action_t in)
440 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
444 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
445 out = IPSEC_API_SPD_ACTION_##f; \
447 foreach_ipsec_policy_action
450 return (clib_host_to_net_u32 (out));
454 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
457 vl_api_ipsec_spd_details_t *mp;
459 mp = vl_msg_api_alloc (sizeof (*mp));
460 clib_memset (mp, 0, sizeof (*mp));
461 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
462 mp->context = context;
464 mp->entry.spd_id = htonl (p->id);
465 mp->entry.priority = htonl (p->priority);
466 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
467 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
469 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
470 &mp->entry.local_address_start);
471 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
472 &mp->entry.local_address_stop);
473 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
474 &mp->entry.remote_address_start);
475 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
476 &mp->entry.remote_address_stop);
477 mp->entry.local_port_start = htons (p->lport.start);
478 mp->entry.local_port_stop = htons (p->lport.stop);
479 mp->entry.remote_port_start = htons (p->rport.start);
480 mp->entry.remote_port_stop = htons (p->rport.stop);
481 mp->entry.protocol = p->protocol;
482 mp->entry.policy = ipsec_spd_action_encode (p->policy);
483 mp->entry.sa_id = htonl (p->sa_id);
485 vl_api_send_msg (reg, (u8 *) mp);
489 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
491 vl_api_registration_t *reg;
492 ipsec_main_t *im = &ipsec_main;
493 ipsec_spd_policy_type_t ptype;
494 ipsec_policy_t *policy;
499 reg = vl_api_client_index_to_registration (mp->client_index);
503 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
508 spd = pool_elt_at_index (im->spds, spd_index);
511 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
512 vec_foreach(ii, spd->policies[ptype])
514 policy = pool_elt_at_index(im->policies, *ii);
516 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
517 send_ipsec_spd_details (policy, reg, mp->context);
522 clib_warning ("unimplemented");
527 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
528 u32 sw_if_index, u32 context)
530 vl_api_ipsec_spd_interface_details_t *mp;
532 mp = vl_msg_api_alloc (sizeof (*mp));
533 clib_memset (mp, 0, sizeof (*mp));
534 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
535 mp->context = context;
537 mp->spd_index = htonl (spd_index);
538 mp->sw_if_index = htonl (sw_if_index);
540 vl_api_send_msg (reg, (u8 *) mp);
544 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
547 ipsec_main_t *im = &ipsec_main;
548 vl_api_registration_t *reg;
552 reg = vl_api_client_index_to_registration (mp->client_index);
556 if (mp->spd_index_valid)
558 spd_index = ntohl (mp->spd_index);
560 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
562 send_ipsec_spd_interface_details(reg, v, k, mp->context);
569 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
570 send_ipsec_spd_interface_details(reg, v, k, mp->context);
576 clib_warning ("unimplemented");
581 ipsec_tun_mk_input_sa_id (u32 ti)
583 return (0x80000000 | ti);
587 ipsec_tun_mk_output_sa_id (u32 ti)
589 return (0xc0000000 | ti);
593 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
596 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
597 u32 sw_if_index = ~0;
601 ip46_address_t local_ip = ip46_address_initializer;
602 ip46_address_t remote_ip = ip46_address_initializer;
603 ipsec_key_t crypto_key, integ_key;
604 ipsec_sa_flags_t flags;
605 ip46_type_t local_ip_type, remote_ip_type;
606 ipip_transport_t transport;
609 local_ip_type = ip_address_decode (&mp->local_ip, &local_ip);
610 remote_ip_type = ip_address_decode (&mp->remote_ip, &remote_ip);
611 transport = (IP46_TYPE_IP6 == local_ip_type ?
612 IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4);
614 if (local_ip_type != remote_ip_type)
616 rv = VNET_API_ERROR_INVALID_VALUE;
620 flags = IPSEC_SA_FLAG_NONE;
623 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
625 flags |= IPSEC_SA_FLAG_USE_ESN;
627 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
629 ipsec_mk_key (&crypto_key, mp->remote_crypto_key,
630 mp->remote_crypto_key_len);
631 ipsec_mk_key (&integ_key, mp->remote_integ_key, mp->remote_integ_key_len);
632 ipsec_mk_key (&crypto_key, mp->local_crypto_key, mp->local_crypto_key_len);
633 ipsec_mk_key (&integ_key, mp->local_integ_key, mp->local_integ_key_len);
636 fib_table_find (fib_proto_from_ip46 (local_ip_type),
637 ntohl (mp->tx_table_id));
641 rv = VNET_API_ERROR_NO_SUCH_FIB;
647 // remote = input, local = output
648 /* create an ip-ip tunnel, then the two SA, then bind them */
649 rv = ipip_add_tunnel (transport,
650 (mp->renumber ? ntohl (mp->show_instance) : ~0),
652 &remote_ip, fib_index,
653 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
654 TUNNEL_MODE_P2P, &sw_if_index);
659 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_input_sa_id (sw_if_index),
660 ntohl (mp->remote_spi),
666 (flags | IPSEC_SA_FLAG_IS_INBOUND),
667 ntohl (mp->tx_table_id),
668 mp->salt, &remote_ip, &local_ip, NULL,
669 IPSEC_UDP_PORT_NONE, IPSEC_UDP_PORT_NONE);
674 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_output_sa_id (sw_if_index),
675 ntohl (mp->local_spi),
682 ntohl (mp->tx_table_id),
683 mp->salt, &local_ip, &remote_ip, NULL,
684 IPSEC_UDP_PORT_NONE, IPSEC_UDP_PORT_NONE);
689 rv = ipsec_tun_protect_update_one (sw_if_index, NULL,
690 ipsec_tun_mk_output_sa_id
692 ipsec_tun_mk_input_sa_id
697 /* the SAs are locked as a result of being used for proection,
698 * they cannot be removed from the API, since they cannot be refered
699 * to by the API. unlock them now, so that if the tunnel is rekeyed
702 ipsec_sa_unlock_id (ipsec_tun_mk_input_sa_id (sw_if_index));
703 ipsec_sa_unlock_id (ipsec_tun_mk_output_sa_id (sw_if_index));
708 ipip_tunnel_key_t key = {
709 .transport = transport,
710 .fib_index = fib_index,
716 ipip_tunnel_t *t = ipip_tunnel_db_find (&key);
720 rv = ipsec_tun_protect_del (t->sw_if_index, NULL);
721 ipip_del_tunnel (t->sw_if_index);
724 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
728 rv = VNET_API_ERROR_UNIMPLEMENTED;
732 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
734 rmp->sw_if_index = htonl (sw_if_index);
739 typedef struct ipsec_sa_dump_match_ctx_t_
743 } ipsec_sa_dump_match_ctx_t;
746 ipsec_sa_dump_match_sa (index_t itpi, void *arg)
748 ipsec_sa_dump_match_ctx_t *ctx = arg;
749 ipsec_tun_protect_t *itp;
752 itp = ipsec_tun_protect_get (itpi);
754 if (itp->itp_out_sa == ctx->sai)
756 ctx->sw_if_index = itp->itp_sw_if_index;
760 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
764 ctx->sw_if_index = itp->itp_sw_if_index;
770 return (WALK_CONTINUE);
774 send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
776 ipsec_dump_walk_ctx_t *ctx = arg;
777 vl_api_ipsec_sa_details_t *mp;
778 ipsec_main_t *im = &ipsec_main;
780 mp = vl_msg_api_alloc (sizeof (*mp));
781 clib_memset (mp, 0, sizeof (*mp));
782 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
783 mp->context = ctx->context;
785 mp->entry.sad_id = htonl (sa->id);
786 mp->entry.spi = htonl (sa->spi);
787 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
788 mp->entry.tx_table_id =
789 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
791 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
792 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
794 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
795 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
797 mp->entry.flags = ipsec_sad_flags_encode (sa);
798 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
800 if (ipsec_sa_is_set_IS_PROTECT (sa))
802 ipsec_sa_dump_match_ctx_t ctx = {
806 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
808 mp->sw_if_index = htonl (ctx.sw_if_index);
811 mp->sw_if_index = ~0;
813 if (ipsec_sa_is_set_IS_TUNNEL (sa))
815 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
816 &mp->entry.tunnel_src);
817 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
818 &mp->entry.tunnel_dst);
820 if (ipsec_sa_is_set_UDP_ENCAP (sa))
822 mp->entry.udp_src_port = sa->udp_hdr.src_port;
823 mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
826 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
827 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
828 if (ipsec_sa_is_set_USE_ESN (sa))
830 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
831 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
833 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
834 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
836 mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
838 vl_api_send_msg (ctx->reg, (u8 *) mp);
840 return (WALK_CONTINUE);
844 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
846 vl_api_registration_t *reg;
849 reg = vl_api_client_index_to_registration (mp->client_index);
853 ipsec_dump_walk_ctx_t ctx = {
855 .context = mp->context,
858 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
861 clib_warning ("unimplemented");
866 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
868 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
872 VALIDATE_SW_IF_INDEX(mp);
875 rv = ipsec_tun_protect_update_out (ntohl (mp->sw_if_index), NULL,
878 rv = ipsec_tun_protect_update_in (ntohl (mp->sw_if_index), NULL,
882 clib_warning ("unimplemented");
885 BAD_SW_IF_INDEX_LABEL;
887 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
891 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
893 vl_api_registration_t *rp;
894 ipsec_main_t *im = &ipsec_main;
895 u32 context = mp->context;
897 rp = vl_api_client_index_to_registration (mp->client_index);
901 clib_warning ("Client %d AWOL", mp->client_index);
905 ipsec_ah_backend_t *ab;
906 ipsec_esp_backend_t *eb;
908 pool_foreach (ab, im->ah_backends, {
909 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
910 clib_memset (mp, 0, sizeof (*mp));
911 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
912 mp->context = context;
913 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
915 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
916 mp->index = ab - im->ah_backends;
917 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
918 vl_api_send_msg (rp, (u8 *)mp);
920 pool_foreach (eb, im->esp_backends, {
921 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
922 clib_memset (mp, 0, sizeof (*mp));
923 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
924 mp->context = context;
925 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
927 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
928 mp->index = eb - im->esp_backends;
929 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
930 vl_api_send_msg (rp, (u8 *)mp);
936 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
938 ipsec_main_t *im = &ipsec_main;
939 vl_api_ipsec_select_backend_reply_t *rmp;
940 ipsec_protocol_t protocol;
942 if (pool_elts (im->sad) > 0)
944 rv = VNET_API_ERROR_INSTANCE_IN_USE;
948 rv = ipsec_proto_decode (mp->protocol, &protocol);
956 case IPSEC_PROTOCOL_ESP:
957 rv = ipsec_select_esp_backend (im, mp->index);
959 case IPSEC_PROTOCOL_AH:
960 rv = ipsec_select_ah_backend (im, mp->index);
963 rv = VNET_API_ERROR_INVALID_PROTOCOL;
967 clib_warning ("unimplemented"); /* FIXME */
970 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
975 * Add vpe's API message handlers to the table.
976 * vlib has already mapped shared memory and
977 * added the client registration handlers.
978 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
980 #define vl_msg_name_crc_list
981 #include <vnet/vnet_all_api_h.h>
982 #undef vl_msg_name_crc_list
985 setup_message_id_table (api_main_t * am)
987 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
988 foreach_vl_msg_name_crc_ipsec;
992 static clib_error_t *
993 ipsec_api_hookup (vlib_main_t * vm)
995 api_main_t *am = vlibapi_get_main ();
998 vl_msg_api_set_handlers(VL_API_##N, #n, \
999 vl_api_##n##_t_handler, \
1001 vl_api_##n##_t_endian, \
1002 vl_api_##n##_t_print, \
1003 sizeof(vl_api_##n##_t), 1);
1004 foreach_vpe_api_msg;
1008 * Adding and deleting SAs is MP safe since when they are added/delete
1009 * no traffic is using them
1011 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL] = 1;
1012 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY] = 1;
1015 * Set up the (msg_name, crc, message-id) table
1017 setup_message_id_table (am);
1022 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1025 * fd.io coding-style-patch-verification: ON
1028 * eval: (c-set-style "gnu")