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>
29 #include <vnet/vnet_msg_enum.h>
32 #include <vnet/ipsec/ipsec.h>
35 #define vl_typedefs /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
39 #define vl_endianfun /* define message structures */
40 #include <vnet/vnet_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <vnet/vnet_all_api_h.h>
49 #include <vlibapi/api_helper_macros.h>
51 #define foreach_vpe_api_msg \
52 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
53 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
54 _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \
55 _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \
56 _(IPSEC_SA_DUMP, ipsec_sa_dump) \
57 _(IPSEC_SPDS_DUMP, ipsec_spds_dump) \
58 _(IPSEC_SPD_DUMP, ipsec_spd_dump) \
59 _(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \
60 _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \
61 _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \
62 _(IPSEC_SELECT_BACKEND, ipsec_select_backend) \
63 _(IPSEC_BACKEND_DUMP, ipsec_backend_dump)
66 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
69 clib_warning ("unimplemented");
72 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
73 vl_api_ipsec_spd_add_del_reply_t *rmp;
76 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
78 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
82 static void vl_api_ipsec_interface_add_del_spd_t_handler
83 (vl_api_ipsec_interface_add_del_spd_t * mp)
85 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
86 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
88 u32 sw_if_index __attribute__ ((unused));
89 u32 spd_id __attribute__ ((unused));
91 sw_if_index = ntohl (mp->sw_if_index);
92 spd_id = ntohl (mp->spd_id);
94 VALIDATE_SW_IF_INDEX (mp);
97 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
99 rv = VNET_API_ERROR_UNIMPLEMENTED;
102 BAD_SW_IF_INDEX_LABEL;
104 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
108 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
109 ipsec_policy_action_t * out)
111 in = clib_net_to_host_u32 (in);
115 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
116 *out = IPSEC_POLICY_ACTION_##f; \
118 foreach_ipsec_policy_action
121 return (VNET_API_ERROR_UNIMPLEMENTED);
124 static void vl_api_ipsec_spd_entry_add_del_t_handler
125 (vl_api_ipsec_spd_entry_add_del_t * mp)
127 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
128 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
138 clib_memset (&p, 0, sizeof (p));
140 p.id = ntohl (mp->entry.spd_id);
141 p.priority = ntohl (mp->entry.priority);
143 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
144 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
145 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
146 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
148 p.is_ipv6 = (itype == IP46_TYPE_IP6);
150 p.protocol = mp->entry.protocol;
151 /* leave the ports in network order */
152 p.rport.start = mp->entry.remote_port_start;
153 p.rport.stop = mp->entry.remote_port_stop;
154 p.lport.start = mp->entry.local_port_start;
155 p.lport.stop = mp->entry.local_port_stop;
157 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
162 /* policy action resolve unsupported */
163 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
165 clib_warning ("unsupported action: 'resolve'");
166 rv = VNET_API_ERROR_UNIMPLEMENTED;
169 p.sa_id = ntohl (mp->entry.sa_id);
171 ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
176 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
181 rv = VNET_API_ERROR_UNIMPLEMENTED;
187 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
189 rmp->stat_index = ntohl(stat_index);
195 ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
197 in = clib_net_to_host_u32 (in);
201 case IPSEC_API_PROTO_ESP:
202 *out = IPSEC_PROTOCOL_ESP;
204 case IPSEC_API_PROTO_AH:
205 *out = IPSEC_PROTOCOL_AH;
208 return (VNET_API_ERROR_INVALID_PROTOCOL);
211 static vl_api_ipsec_proto_t
212 ipsec_proto_encode (ipsec_protocol_t p)
216 case IPSEC_PROTOCOL_ESP:
217 return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP);
218 case IPSEC_PROTOCOL_AH:
219 return clib_host_to_net_u32 (IPSEC_API_PROTO_AH);
221 return (VNET_API_ERROR_UNIMPLEMENTED);
225 ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
226 ipsec_crypto_alg_t * out)
228 in = clib_net_to_host_u32 (in);
232 #define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
233 *out = IPSEC_CRYPTO_ALG_##f; \
235 foreach_ipsec_crypto_alg
238 return (VNET_API_ERROR_INVALID_ALGORITHM);
241 static vl_api_ipsec_crypto_alg_t
242 ipsec_crypto_algo_encode (ipsec_crypto_alg_t c)
246 #define _(v,f,s) case IPSEC_CRYPTO_ALG_##f: \
247 return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f);
248 foreach_ipsec_crypto_alg
250 case IPSEC_CRYPTO_N_ALG:
254 return (VNET_API_ERROR_UNIMPLEMENTED);
259 ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
261 in = clib_net_to_host_u32 (in);
265 #define _(v,f,s) case IPSEC_API_INTEG_ALG_##f: \
266 *out = IPSEC_INTEG_ALG_##f; \
268 foreach_ipsec_integ_alg
271 return (VNET_API_ERROR_INVALID_ALGORITHM);
274 static vl_api_ipsec_integ_alg_t
275 ipsec_integ_algo_encode (ipsec_integ_alg_t i)
279 #define _(v,f,s) case IPSEC_INTEG_ALG_##f: \
280 return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f));
281 foreach_ipsec_integ_alg
283 case IPSEC_INTEG_N_ALG:
287 return (VNET_API_ERROR_UNIMPLEMENTED);
291 ipsec_key_decode (const vl_api_key_t * key, ipsec_key_t * out)
293 ipsec_mk_key (out, key->data, key->length);
297 ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out)
299 out->length = in->len;
300 clib_memcpy (out->data, in->data, out->length);
303 static ipsec_sa_flags_t
304 ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in)
306 ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE;
307 in = clib_net_to_host_u32 (in);
309 if (in & IPSEC_API_SAD_FLAG_USE_ESN)
310 flags |= IPSEC_SA_FLAG_USE_ESN;
311 if (in & IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY)
312 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
313 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL)
314 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
315 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL_V6)
316 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
317 if (in & IPSEC_API_SAD_FLAG_UDP_ENCAP)
318 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
323 static vl_api_ipsec_sad_flags_t
324 ipsec_sad_flags_encode (const ipsec_sa_t * sa)
326 vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE;
328 if (ipsec_sa_is_set_USE_ESN (sa))
329 flags |= IPSEC_API_SAD_FLAG_USE_ESN;
330 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
331 flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
332 if (ipsec_sa_is_set_IS_TUNNEL (sa))
333 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
334 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
335 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
336 if (ipsec_sa_is_set_UDP_ENCAP (sa))
337 flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
339 return clib_host_to_net_u32 (flags);
342 static void vl_api_ipsec_sad_entry_add_del_t_handler
343 (vl_api_ipsec_sad_entry_add_del_t * mp)
345 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
346 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
347 ip46_address_t tun_src = { }, tun_dst =
350 ipsec_key_t crypto_key, integ_key;
351 ipsec_crypto_alg_t crypto_alg;
352 ipsec_integ_alg_t integ_alg;
353 ipsec_protocol_t proto;
354 ipsec_sa_flags_t flags;
355 u32 id, spi, sa_index = ~0;
360 id = ntohl (mp->entry.sad_id);
361 spi = ntohl (mp->entry.spi);
363 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
368 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
373 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
378 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
379 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
381 flags = ipsec_sa_flags_decode (mp->entry.flags);
383 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
384 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
387 rv = ipsec_sa_add (id, spi, proto,
388 crypto_alg, &crypto_key,
389 integ_alg, &integ_key, flags,
390 0, mp->entry.salt, &tun_src, &tun_dst, &sa_index);
392 rv = ipsec_sa_del (id);
395 rv = VNET_API_ERROR_UNIMPLEMENTED;
400 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
402 rmp->stat_index = htonl (sa_index);
408 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
411 vl_api_ipsec_spds_details_t *mp;
414 mp = vl_msg_api_alloc (sizeof (*mp));
415 clib_memset (mp, 0, sizeof (*mp));
416 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
417 mp->context = context;
419 mp->spd_id = htonl (spd->id);
420 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
421 foreach_ipsec_spd_policy_type
423 mp->npolicies = htonl (n_policies);
425 vl_api_send_msg (reg, (u8 *) mp);
429 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
431 vl_api_registration_t *reg;
432 ipsec_main_t *im = &ipsec_main;
435 reg = vl_api_client_index_to_registration (mp->client_index);
440 pool_foreach (spd, im->spds, ({
441 send_ipsec_spds_details (spd, reg, mp->context);
445 clib_warning ("unimplemented");
449 vl_api_ipsec_spd_action_t
450 ipsec_spd_action_encode (ipsec_policy_action_t in)
452 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
456 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
457 out = IPSEC_API_SPD_ACTION_##f; \
459 foreach_ipsec_policy_action
462 return (clib_host_to_net_u32 (out));
466 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
469 vl_api_ipsec_spd_details_t *mp;
471 mp = vl_msg_api_alloc (sizeof (*mp));
472 clib_memset (mp, 0, sizeof (*mp));
473 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
474 mp->context = context;
476 mp->entry.spd_id = htonl (p->id);
477 mp->entry.priority = htonl (p->priority);
478 mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
479 (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
481 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
482 &mp->entry.local_address_start);
483 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
484 &mp->entry.local_address_stop);
485 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
486 &mp->entry.remote_address_start);
487 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
488 &mp->entry.remote_address_stop);
489 mp->entry.local_port_start = p->lport.start;
490 mp->entry.local_port_stop = p->lport.stop;
491 mp->entry.remote_port_start = p->rport.start;
492 mp->entry.remote_port_stop = p->rport.stop;
493 mp->entry.protocol = p->protocol;
494 mp->entry.policy = ipsec_spd_action_encode (p->policy);
495 mp->entry.sa_id = htonl (p->sa_id);
497 vl_api_send_msg (reg, (u8 *) mp);
501 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
503 vl_api_registration_t *reg;
504 ipsec_main_t *im = &ipsec_main;
505 ipsec_spd_policy_type_t ptype;
506 ipsec_policy_t *policy;
511 reg = vl_api_client_index_to_registration (mp->client_index);
515 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
520 spd = pool_elt_at_index (im->spds, spd_index);
523 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
524 vec_foreach(ii, spd->policies[ptype])
526 policy = pool_elt_at_index(im->policies, *ii);
528 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
529 send_ipsec_spd_details (policy, reg, mp->context);
534 clib_warning ("unimplemented");
539 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
540 u32 sw_if_index, u32 context)
542 vl_api_ipsec_spd_interface_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_SPD_INTERFACE_DETAILS);
547 mp->context = context;
549 mp->spd_index = htonl (spd_index);
550 mp->sw_if_index = htonl (sw_if_index);
552 vl_api_send_msg (reg, (u8 *) mp);
556 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
559 ipsec_main_t *im = &ipsec_main;
560 vl_api_registration_t *reg;
564 reg = vl_api_client_index_to_registration (mp->client_index);
568 if (mp->spd_index_valid)
570 spd_index = ntohl (mp->spd_index);
572 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
574 send_ipsec_spd_interface_details(reg, v, k, mp->context);
581 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
582 send_ipsec_spd_interface_details(reg, v, k, mp->context);
588 clib_warning ("unimplemented");
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 ipsec_main_t *im = &ipsec_main;
598 vnet_main_t *vnm = im->vnet_main;
599 u32 sw_if_index = ~0;
604 ipsec_add_del_tunnel_args_t tun;
606 clib_memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
608 tun.is_add = mp->is_add;
610 tun.anti_replay = mp->anti_replay;
611 tun.local_spi = ntohl (mp->local_spi);
612 tun.remote_spi = ntohl (mp->remote_spi);
613 tun.crypto_alg = mp->crypto_alg;
614 tun.local_crypto_key_len = mp->local_crypto_key_len;
615 tun.remote_crypto_key_len = mp->remote_crypto_key_len;
616 tun.integ_alg = mp->integ_alg;
617 tun.local_integ_key_len = mp->local_integ_key_len;
618 tun.remote_integ_key_len = mp->remote_integ_key_len;
619 tun.udp_encap = mp->udp_encap;
620 tun.tx_table_id = ntohl (mp->tx_table_id);
622 itype = ip_address_decode (&mp->local_ip, &tun.local_ip);
623 itype = ip_address_decode (&mp->remote_ip, &tun.remote_ip);
624 tun.is_ip6 = (IP46_TYPE_IP6 == itype);
625 memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
626 mp->local_crypto_key_len);
627 memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
628 mp->remote_crypto_key_len);
629 memcpy (&tun.local_integ_key, &mp->local_integ_key,
630 mp->local_integ_key_len);
631 memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
632 mp->remote_integ_key_len);
633 tun.renumber = mp->renumber;
634 tun.show_instance = ntohl (mp->show_instance);
636 rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
639 rv = VNET_API_ERROR_UNIMPLEMENTED;
643 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
645 rmp->sw_if_index = htonl (sw_if_index);
651 send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
652 u32 context, u32 sw_if_index)
654 vl_api_ipsec_sa_details_t *mp;
656 mp = vl_msg_api_alloc (sizeof (*mp));
657 clib_memset (mp, 0, sizeof (*mp));
658 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
659 mp->context = context;
661 mp->entry.sad_id = htonl (sa->id);
662 mp->entry.spi = htonl (sa->spi);
663 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
664 mp->entry.tx_table_id =
665 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
667 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
668 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
670 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
671 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
673 mp->entry.flags = ipsec_sad_flags_encode (sa);
675 if (ipsec_sa_is_set_IS_TUNNEL (sa))
677 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
678 &mp->entry.tunnel_src);
679 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
680 &mp->entry.tunnel_dst);
683 mp->sw_if_index = htonl (sw_if_index);
684 mp->salt = clib_host_to_net_u32 (sa->salt);
685 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
686 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
687 if (ipsec_sa_is_set_USE_ESN (sa))
689 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
690 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
692 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
693 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
695 vl_api_send_msg (reg, (u8 *) mp);
700 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
702 vl_api_registration_t *reg;
703 ipsec_main_t *im = &ipsec_main;
704 vnet_main_t *vnm = im->vnet_main;
706 ipsec_tunnel_if_t *t;
707 u32 *sa_index_to_tun_if_index = 0;
710 reg = vl_api_client_index_to_registration (mp->client_index);
711 if (!reg || pool_elts (im->sad) == 0)
714 vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
718 pool_foreach (t, im->tunnel_interfaces,
720 vnet_hw_interface_t *hi;
721 u32 sw_if_index = ~0;
723 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
724 sw_if_index = hi->sw_if_index;
725 sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
726 sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
729 pool_foreach (sa, im->sad,
731 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
732 send_ipsec_sa_details (sa, reg, mp->context,
733 sa_index_to_tun_if_index[sa - im->sad]);
737 vec_free (sa_index_to_tun_if_index);
739 clib_warning ("unimplemented");
744 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
746 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
747 ipsec_main_t *im = &ipsec_main;
748 vnet_main_t *vnm = im->vnet_main;
749 vnet_sw_interface_t *sw;
753 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
755 rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
758 clib_warning ("unimplemented");
761 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
765 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
767 vl_api_registration_t *rp;
768 ipsec_main_t *im = &ipsec_main;
769 u32 context = mp->context;
771 rp = vl_api_client_index_to_registration (mp->client_index);
775 clib_warning ("Client %d AWOL", mp->client_index);
779 ipsec_ah_backend_t *ab;
780 ipsec_esp_backend_t *eb;
782 pool_foreach (ab, im->ah_backends, {
783 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
784 clib_memset (mp, 0, sizeof (*mp));
785 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
786 mp->context = context;
787 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
789 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
790 mp->index = ab - im->ah_backends;
791 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
792 vl_api_send_msg (rp, (u8 *)mp);
794 pool_foreach (eb, im->esp_backends, {
795 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
796 clib_memset (mp, 0, sizeof (*mp));
797 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
798 mp->context = context;
799 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
801 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
802 mp->index = eb - im->esp_backends;
803 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
804 vl_api_send_msg (rp, (u8 *)mp);
810 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
812 ipsec_main_t *im = &ipsec_main;
813 vl_api_ipsec_select_backend_reply_t *rmp;
814 ipsec_protocol_t protocol;
816 if (pool_elts (im->sad) > 0)
818 rv = VNET_API_ERROR_INSTANCE_IN_USE;
822 rv = ipsec_proto_decode (mp->protocol, &protocol);
830 case IPSEC_PROTOCOL_ESP:
831 rv = ipsec_select_esp_backend (im, mp->index);
833 case IPSEC_PROTOCOL_AH:
834 rv = ipsec_select_ah_backend (im, mp->index);
837 rv = VNET_API_ERROR_INVALID_PROTOCOL;
841 clib_warning ("unimplemented"); /* FIXME */
844 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
849 * Add vpe's API message handlers to the table.
850 * vlib has already mapped shared memory and
851 * added the client registration handlers.
852 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
854 #define vl_msg_name_crc_list
855 #include <vnet/vnet_all_api_h.h>
856 #undef vl_msg_name_crc_list
859 setup_message_id_table (api_main_t * am)
861 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
862 foreach_vl_msg_name_crc_ipsec;
866 static clib_error_t *
867 ipsec_api_hookup (vlib_main_t * vm)
869 api_main_t *am = &api_main;
872 vl_msg_api_set_handlers(VL_API_##N, #n, \
873 vl_api_##n##_t_handler, \
875 vl_api_##n##_t_endian, \
876 vl_api_##n##_t_print, \
877 sizeof(vl_api_##n##_t), 1);
882 * Set up the (msg_name, crc, message-id) table
884 setup_message_id_table (am);
889 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
892 * fd.io coding-style-patch-verification: ON
895 * eval: (c-set-style "gnu")