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;
121 sw_if_index = ntohl (mp->tunnel.sw_if_index);
123 VALIDATE_SW_IF_INDEX (&(mp->tunnel));
127 for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
128 vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
130 rv = ipsec_tun_protect_update (sw_if_index,
131 ntohl (mp->tunnel.sa_out), sa_ins);
133 rv = VNET_API_ERROR_UNIMPLEMENTED;
136 BAD_SW_IF_INDEX_LABEL;
138 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
141 static void vl_api_ipsec_tunnel_protect_del_t_handler
142 (vl_api_ipsec_tunnel_protect_del_t * mp)
144 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
145 vl_api_ipsec_tunnel_protect_del_reply_t *rmp;
149 sw_if_index = ntohl (mp->sw_if_index);
151 VALIDATE_SW_IF_INDEX (mp);
154 rv = ipsec_tun_protect_del (sw_if_index);
156 rv = VNET_API_ERROR_UNIMPLEMENTED;
159 BAD_SW_IF_INDEX_LABEL;
161 REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
164 typedef struct ipsec_dump_walk_ctx_t_
166 vl_api_registration_t *reg;
168 } ipsec_dump_walk_ctx_t;
171 send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
173 ipsec_dump_walk_ctx_t *ctx = arg;
174 vl_api_ipsec_tunnel_protect_details_t *mp;
175 ipsec_tun_protect_t *itp;
178 itp = ipsec_tun_protect_get (itpi);
181 mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
182 clib_memset (mp, 0, sizeof (*mp));
183 mp->_vl_msg_id = ntohs (VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
184 mp->context = ctx->context;
186 mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
188 mp->tun.sa_out = htonl (itp->itp_out_sa);
189 mp->tun.n_sa_in = itp->itp_n_sa_in;
191 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
193 mp->tun.sa_in[ii++] = htonl (sai);
197 vl_api_send_msg (ctx->reg, (u8 *) mp);
199 return (WALK_CONTINUE);
203 vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
206 vl_api_registration_t *reg;
210 reg = vl_api_client_index_to_registration (mp->client_index);
214 ipsec_dump_walk_ctx_t ctx = {
216 .context = mp->context,
219 sw_if_index = ntohl (mp->sw_if_index);
221 if (~0 == sw_if_index)
223 ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
229 itpi = ipsec_tun_protect_find (sw_if_index);
231 if (INDEX_INVALID != itpi)
232 send_ipsec_tunnel_protect_details (itpi, &ctx);
235 clib_warning ("unimplemented");
240 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
241 ipsec_policy_action_t * out)
243 in = clib_net_to_host_u32 (in);
247 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
248 *out = IPSEC_POLICY_ACTION_##f; \
250 foreach_ipsec_policy_action
253 return (VNET_API_ERROR_UNIMPLEMENTED);
256 static void vl_api_ipsec_spd_entry_add_del_t_handler
257 (vl_api_ipsec_spd_entry_add_del_t * mp)
259 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
260 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
270 clib_memset (&p, 0, sizeof (p));
272 p.id = ntohl (mp->entry.spd_id);
273 p.priority = ntohl (mp->entry.priority);
275 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
276 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
277 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
278 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
280 p.is_ipv6 = (itype == IP46_TYPE_IP6);
282 p.protocol = mp->entry.protocol;
283 p.rport.start = ntohs (mp->entry.remote_port_start);
284 p.rport.stop = ntohs (mp->entry.remote_port_stop);
285 p.lport.start = ntohs (mp->entry.local_port_start);
286 p.lport.stop = ntohs (mp->entry.local_port_stop);
288 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
293 /* policy action resolve unsupported */
294 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
296 clib_warning ("unsupported action: 'resolve'");
297 rv = VNET_API_ERROR_UNIMPLEMENTED;
300 p.sa_id = ntohl (mp->entry.sa_id);
302 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
307 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
312 rv = VNET_API_ERROR_UNIMPLEMENTED;
318 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
320 rmp->stat_index = ntohl(stat_index);
325 static void vl_api_ipsec_sad_entry_add_del_t_handler
326 (vl_api_ipsec_sad_entry_add_del_t * mp)
328 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
329 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
330 ip46_address_t tun_src = { }, tun_dst =
333 ipsec_key_t crypto_key, integ_key;
334 ipsec_crypto_alg_t crypto_alg;
335 ipsec_integ_alg_t integ_alg;
336 ipsec_protocol_t proto;
337 ipsec_sa_flags_t flags;
338 u32 id, spi, sa_index = ~0;
343 id = ntohl (mp->entry.sad_id);
344 spi = ntohl (mp->entry.spi);
346 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
351 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
356 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
361 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
362 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
364 flags = ipsec_sa_flags_decode (mp->entry.flags);
366 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
367 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
370 rv = ipsec_sa_add_and_lock (id, spi, proto,
371 crypto_alg, &crypto_key,
372 integ_alg, &integ_key, flags,
373 0, mp->entry.salt, &tun_src, &tun_dst,
376 rv = ipsec_sa_unlock_id (id);
379 rv = VNET_API_ERROR_UNIMPLEMENTED;
384 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
386 rmp->stat_index = htonl (sa_index);
392 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
395 vl_api_ipsec_spds_details_t *mp;
398 mp = vl_msg_api_alloc (sizeof (*mp));
399 clib_memset (mp, 0, sizeof (*mp));
400 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
401 mp->context = context;
403 mp->spd_id = htonl (spd->id);
404 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
405 foreach_ipsec_spd_policy_type
407 mp->npolicies = htonl (n_policies);
409 vl_api_send_msg (reg, (u8 *) mp);
413 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
415 vl_api_registration_t *reg;
416 ipsec_main_t *im = &ipsec_main;
419 reg = vl_api_client_index_to_registration (mp->client_index);
424 pool_foreach (spd, im->spds, ({
425 send_ipsec_spds_details (spd, reg, mp->context);
429 clib_warning ("unimplemented");
433 vl_api_ipsec_spd_action_t
434 ipsec_spd_action_encode (ipsec_policy_action_t in)
436 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
440 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
441 out = IPSEC_API_SPD_ACTION_##f; \
443 foreach_ipsec_policy_action
446 return (clib_host_to_net_u32 (out));
450 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
453 vl_api_ipsec_spd_details_t *mp;
455 mp = vl_msg_api_alloc (sizeof (*mp));
456 clib_memset (mp, 0, sizeof (*mp));
457 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
458 mp->context = context;
460 mp->entry.spd_id = htonl (p->id);
461 mp->entry.priority = htonl (p->priority);
462 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
463 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
465 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
466 &mp->entry.local_address_start);
467 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
468 &mp->entry.local_address_stop);
469 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
470 &mp->entry.remote_address_start);
471 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
472 &mp->entry.remote_address_stop);
473 mp->entry.local_port_start = htons (p->lport.start);
474 mp->entry.local_port_stop = htons (p->lport.stop);
475 mp->entry.remote_port_start = htons (p->rport.start);
476 mp->entry.remote_port_stop = htons (p->rport.stop);
477 mp->entry.protocol = p->protocol;
478 mp->entry.policy = ipsec_spd_action_encode (p->policy);
479 mp->entry.sa_id = htonl (p->sa_id);
481 vl_api_send_msg (reg, (u8 *) mp);
485 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
487 vl_api_registration_t *reg;
488 ipsec_main_t *im = &ipsec_main;
489 ipsec_spd_policy_type_t ptype;
490 ipsec_policy_t *policy;
495 reg = vl_api_client_index_to_registration (mp->client_index);
499 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
504 spd = pool_elt_at_index (im->spds, spd_index);
507 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
508 vec_foreach(ii, spd->policies[ptype])
510 policy = pool_elt_at_index(im->policies, *ii);
512 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
513 send_ipsec_spd_details (policy, reg, mp->context);
518 clib_warning ("unimplemented");
523 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
524 u32 sw_if_index, u32 context)
526 vl_api_ipsec_spd_interface_details_t *mp;
528 mp = vl_msg_api_alloc (sizeof (*mp));
529 clib_memset (mp, 0, sizeof (*mp));
530 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
531 mp->context = context;
533 mp->spd_index = htonl (spd_index);
534 mp->sw_if_index = htonl (sw_if_index);
536 vl_api_send_msg (reg, (u8 *) mp);
540 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
543 ipsec_main_t *im = &ipsec_main;
544 vl_api_registration_t *reg;
548 reg = vl_api_client_index_to_registration (mp->client_index);
552 if (mp->spd_index_valid)
554 spd_index = ntohl (mp->spd_index);
556 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
558 send_ipsec_spd_interface_details(reg, v, k, mp->context);
565 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
566 send_ipsec_spd_interface_details(reg, v, k, mp->context);
572 clib_warning ("unimplemented");
577 ipsec_tun_mk_input_sa_id (u32 ti)
579 return (0x80000000 | ti);
583 ipsec_tun_mk_output_sa_id (u32 ti)
585 return (0xc0000000 | ti);
589 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
592 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
593 u32 sw_if_index = ~0;
597 ip46_address_t local_ip = ip46_address_initializer;
598 ip46_address_t remote_ip = ip46_address_initializer;
599 ipsec_key_t crypto_key, integ_key;
600 ipsec_sa_flags_t flags;
601 ip46_type_t local_ip_type, remote_ip_type;
602 ipip_transport_t transport;
605 local_ip_type = ip_address_decode (&mp->local_ip, &local_ip);
606 remote_ip_type = ip_address_decode (&mp->remote_ip, &remote_ip);
607 transport = (IP46_TYPE_IP6 == local_ip_type ?
608 IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4);
610 if (local_ip_type != remote_ip_type)
612 rv = VNET_API_ERROR_INVALID_VALUE;
616 flags = IPSEC_SA_FLAG_NONE;
619 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
621 flags |= IPSEC_SA_FLAG_USE_ESN;
623 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
625 ipsec_mk_key (&crypto_key, mp->remote_crypto_key,
626 mp->remote_crypto_key_len);
627 ipsec_mk_key (&integ_key, mp->remote_integ_key, mp->remote_integ_key_len);
628 ipsec_mk_key (&crypto_key, mp->local_crypto_key, mp->local_crypto_key_len);
629 ipsec_mk_key (&integ_key, mp->local_integ_key, mp->local_integ_key_len);
632 fib_table_find (fib_proto_from_ip46 (local_ip_type),
633 ntohl (mp->tx_table_id));
637 rv = VNET_API_ERROR_NO_SUCH_FIB;
643 // remote = input, local = output
644 /* create an ip-ip tunnel, then the two SA, then bind them */
645 rv = ipip_add_tunnel (transport,
646 (mp->renumber ? ntohl (mp->show_instance) : ~0),
648 &remote_ip, fib_index,
649 IPIP_TUNNEL_FLAG_NONE, IP_DSCP_CS0, &sw_if_index);
654 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_input_sa_id (sw_if_index),
655 ntohl (mp->remote_spi),
661 (flags | IPSEC_SA_FLAG_IS_INBOUND),
662 ntohl (mp->tx_table_id),
663 mp->salt, &remote_ip, &local_ip, NULL);
668 rv = ipsec_sa_add_and_lock (ipsec_tun_mk_output_sa_id (sw_if_index),
669 ntohl (mp->local_spi),
676 ntohl (mp->tx_table_id),
677 mp->salt, &local_ip, &remote_ip, NULL);
682 rv = ipsec_tun_protect_update_one (sw_if_index,
683 ipsec_tun_mk_output_sa_id
685 ipsec_tun_mk_input_sa_id
690 /* the SAs are locked as a result of being used for proection,
691 * they cannot be removed from the API, since they cannot be refered
692 * to by the API. unlock them now, so that if the tunnel is rekeyed
695 ipsec_sa_unlock_id (ipsec_tun_mk_input_sa_id (sw_if_index));
696 ipsec_sa_unlock_id (ipsec_tun_mk_output_sa_id (sw_if_index));
701 ipip_tunnel_key_t key = {
702 .transport = transport,
703 .fib_index = fib_index,
709 ipip_tunnel_t *t = ipip_tunnel_db_find (&key);
713 rv = ipsec_tun_protect_del (t->sw_if_index);
714 ipip_del_tunnel (t->sw_if_index);
717 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
721 rv = VNET_API_ERROR_UNIMPLEMENTED;
725 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
727 rmp->sw_if_index = htonl (sw_if_index);
732 typedef struct ipsec_sa_dump_match_ctx_t_
736 } ipsec_sa_dump_match_ctx_t;
739 ipsec_sa_dump_match_sa (index_t itpi, void *arg)
741 ipsec_sa_dump_match_ctx_t *ctx = arg;
742 ipsec_tun_protect_t *itp;
745 itp = ipsec_tun_protect_get (itpi);
747 if (itp->itp_out_sa == ctx->sai)
749 ctx->sw_if_index = itp->itp_sw_if_index;
753 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
757 ctx->sw_if_index = itp->itp_sw_if_index;
763 return (WALK_CONTINUE);
767 send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
769 ipsec_dump_walk_ctx_t *ctx = arg;
770 vl_api_ipsec_sa_details_t *mp;
771 ipsec_main_t *im = &ipsec_main;
773 mp = vl_msg_api_alloc (sizeof (*mp));
774 clib_memset (mp, 0, sizeof (*mp));
775 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
776 mp->context = ctx->context;
778 mp->entry.sad_id = htonl (sa->id);
779 mp->entry.spi = htonl (sa->spi);
780 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
781 mp->entry.tx_table_id =
782 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
784 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
785 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
787 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
788 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
790 mp->entry.flags = ipsec_sad_flags_encode (sa);
791 mp->entry.salt = clib_host_to_net_u32 (sa->salt);
793 if (ipsec_sa_is_set_IS_PROTECT (sa))
795 ipsec_sa_dump_match_ctx_t ctx = {
799 ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
801 mp->sw_if_index = htonl (ctx.sw_if_index);
804 mp->sw_if_index = ~0;
806 if (ipsec_sa_is_set_IS_TUNNEL (sa))
808 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
809 &mp->entry.tunnel_src);
810 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
811 &mp->entry.tunnel_dst);
814 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
815 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
816 if (ipsec_sa_is_set_USE_ESN (sa))
818 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
819 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
821 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
822 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
824 vl_api_send_msg (ctx->reg, (u8 *) mp);
826 return (WALK_CONTINUE);
830 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
832 vl_api_registration_t *reg;
835 reg = vl_api_client_index_to_registration (mp->client_index);
839 ipsec_dump_walk_ctx_t ctx = {
841 .context = mp->context,
844 ipsec_sa_walk (send_ipsec_sa_details, &ctx);
847 clib_warning ("unimplemented");
852 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
854 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
858 VALIDATE_SW_IF_INDEX(mp);
861 rv = ipsec_tun_protect_update_out (ntohl (mp->sw_if_index),
864 rv = ipsec_tun_protect_update_in (ntohl (mp->sw_if_index),
868 clib_warning ("unimplemented");
871 BAD_SW_IF_INDEX_LABEL;
873 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
877 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
879 vl_api_registration_t *rp;
880 ipsec_main_t *im = &ipsec_main;
881 u32 context = mp->context;
883 rp = vl_api_client_index_to_registration (mp->client_index);
887 clib_warning ("Client %d AWOL", mp->client_index);
891 ipsec_ah_backend_t *ab;
892 ipsec_esp_backend_t *eb;
894 pool_foreach (ab, im->ah_backends, {
895 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
896 clib_memset (mp, 0, sizeof (*mp));
897 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
898 mp->context = context;
899 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
901 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
902 mp->index = ab - im->ah_backends;
903 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
904 vl_api_send_msg (rp, (u8 *)mp);
906 pool_foreach (eb, im->esp_backends, {
907 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
908 clib_memset (mp, 0, sizeof (*mp));
909 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
910 mp->context = context;
911 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
913 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
914 mp->index = eb - im->esp_backends;
915 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
916 vl_api_send_msg (rp, (u8 *)mp);
922 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
924 ipsec_main_t *im = &ipsec_main;
925 vl_api_ipsec_select_backend_reply_t *rmp;
926 ipsec_protocol_t protocol;
928 if (pool_elts (im->sad) > 0)
930 rv = VNET_API_ERROR_INSTANCE_IN_USE;
934 rv = ipsec_proto_decode (mp->protocol, &protocol);
942 case IPSEC_PROTOCOL_ESP:
943 rv = ipsec_select_esp_backend (im, mp->index);
945 case IPSEC_PROTOCOL_AH:
946 rv = ipsec_select_ah_backend (im, mp->index);
949 rv = VNET_API_ERROR_INVALID_PROTOCOL;
953 clib_warning ("unimplemented"); /* FIXME */
956 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
961 * Add vpe's API message handlers to the table.
962 * vlib has already mapped shared memory and
963 * added the client registration handlers.
964 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
966 #define vl_msg_name_crc_list
967 #include <vnet/vnet_all_api_h.h>
968 #undef vl_msg_name_crc_list
971 setup_message_id_table (api_main_t * am)
973 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
974 foreach_vl_msg_name_crc_ipsec;
978 static clib_error_t *
979 ipsec_api_hookup (vlib_main_t * vm)
981 api_main_t *am = vlibapi_get_main ();
984 vl_msg_api_set_handlers(VL_API_##N, #n, \
985 vl_api_##n##_t_handler, \
987 vl_api_##n##_t_endian, \
988 vl_api_##n##_t_print, \
989 sizeof(vl_api_##n##_t), 1);
994 * Adding and deleting SAs is MP safe since when they are added/delete
995 * no traffic is using them
997 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL] = 1;
998 am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY] = 1;
1001 * Set up the (msg_name, crc, message-id) table
1003 setup_message_id_table (am);
1008 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1011 * fd.io coding-style-patch-verification: ON
1014 * eval: (c-set-style "gnu")