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_SET_KEY, ipsec_sa_set_key) \
57 _(IPSEC_SA_DUMP, ipsec_sa_dump) \
58 _(IPSEC_SPDS_DUMP, ipsec_spds_dump) \
59 _(IPSEC_SPD_DUMP, ipsec_spd_dump) \
60 _(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump) \
61 _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del) \
62 _(IPSEC_TUNNEL_IF_SET_KEY, ipsec_tunnel_if_set_key) \
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)
68 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
71 clib_warning ("unimplemented");
74 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
75 vl_api_ipsec_spd_add_del_reply_t *rmp;
78 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
80 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
84 static void vl_api_ipsec_interface_add_del_spd_t_handler
85 (vl_api_ipsec_interface_add_del_spd_t * mp)
87 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
88 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
90 u32 sw_if_index __attribute__ ((unused));
91 u32 spd_id __attribute__ ((unused));
93 sw_if_index = ntohl (mp->sw_if_index);
94 spd_id = ntohl (mp->spd_id);
96 VALIDATE_SW_IF_INDEX (mp);
99 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
101 rv = VNET_API_ERROR_UNIMPLEMENTED;
104 BAD_SW_IF_INDEX_LABEL;
106 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
110 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
111 ipsec_policy_action_t * out)
113 in = clib_net_to_host_u32 (in);
117 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
118 *out = IPSEC_POLICY_ACTION_##f; \
120 foreach_ipsec_policy_action
123 return (VNET_API_ERROR_UNIMPLEMENTED);
126 static void vl_api_ipsec_spd_entry_add_del_t_handler
127 (vl_api_ipsec_spd_entry_add_del_t * mp)
129 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
130 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
140 clib_memset (&p, 0, sizeof (p));
142 p.id = ntohl (mp->entry.spd_id);
143 p.priority = ntohl (mp->entry.priority);
144 p.is_outbound = mp->entry.is_outbound;
146 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
147 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
148 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
149 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
151 p.is_ipv6 = (itype == IP46_TYPE_IP6);
153 p.protocol = mp->entry.protocol;
154 p.rport.start = ntohs (mp->entry.remote_port_start);
155 p.rport.stop = ntohs (mp->entry.remote_port_stop);
156 p.lport.start = ntohs (mp->entry.local_port_start);
157 p.lport.stop = ntohs (mp->entry.local_port_stop);
159 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
164 /* policy action resolve unsupported */
165 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
167 clib_warning ("unsupported action: 'resolve'");
168 rv = VNET_API_ERROR_UNIMPLEMENTED;
171 p.sa_id = ntohl (mp->entry.sa_id);
173 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
178 rv = VNET_API_ERROR_UNIMPLEMENTED;
184 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
186 rmp->stat_index = ntohl(stat_index);
192 ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
194 in = clib_net_to_host_u32 (in);
198 case IPSEC_API_PROTO_ESP:
199 *out = IPSEC_PROTOCOL_ESP;
201 case IPSEC_API_PROTO_AH:
202 *out = IPSEC_PROTOCOL_AH;
205 return (VNET_API_ERROR_UNIMPLEMENTED);
208 static vl_api_ipsec_proto_t
209 ipsec_proto_encode (ipsec_protocol_t p)
213 case IPSEC_PROTOCOL_ESP:
214 return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP);
215 case IPSEC_PROTOCOL_AH:
216 return clib_host_to_net_u32 (IPSEC_API_PROTO_AH);
218 return (VNET_API_ERROR_UNIMPLEMENTED);
222 ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
223 ipsec_crypto_alg_t * out)
225 in = clib_net_to_host_u32 (in);
229 #define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
230 *out = IPSEC_CRYPTO_ALG_##f; \
232 foreach_ipsec_crypto_alg
235 return (VNET_API_ERROR_UNIMPLEMENTED);
238 static vl_api_ipsec_crypto_alg_t
239 ipsec_crypto_algo_encode (ipsec_crypto_alg_t c)
243 #define _(v,f,s) case IPSEC_CRYPTO_ALG_##f: \
244 return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f);
245 foreach_ipsec_crypto_alg
247 case IPSEC_CRYPTO_N_ALG:
251 return (VNET_API_ERROR_UNIMPLEMENTED);
256 ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
258 in = clib_net_to_host_u32 (in);
262 #define _(v,f,s) case IPSEC_API_INTEG_ALG_##f: \
263 *out = IPSEC_INTEG_ALG_##f; \
265 foreach_ipsec_integ_alg
268 return (VNET_API_ERROR_UNIMPLEMENTED);
271 static vl_api_ipsec_integ_alg_t
272 ipsec_integ_algo_encode (ipsec_integ_alg_t i)
276 #define _(v,f,s) case IPSEC_INTEG_ALG_##f: \
277 return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f));
278 foreach_ipsec_integ_alg
280 case IPSEC_INTEG_N_ALG:
284 return (VNET_API_ERROR_UNIMPLEMENTED);
288 ipsec_key_decode (const vl_api_key_t * key, ipsec_key_t * out)
290 ipsec_mk_key (out, key->data, key->length);
294 ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out)
296 out->length = in->len;
297 clib_memcpy (out->data, in->data, out->length);
300 static ipsec_sa_flags_t
301 ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in)
303 ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE;
304 in = clib_net_to_host_u32 (in);
306 #define _(v,f,s) if (in & IPSEC_API_SAD_FLAG_##f) \
307 flags |= IPSEC_SA_FLAG_##f;
308 foreach_ipsec_sa_flags
313 static vl_api_ipsec_sad_flags_t
314 ipsec_sad_flags_encode (const ipsec_sa_t * sa)
316 vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE;
319 flags |= IPSEC_API_SAD_FLAG_USE_EXTENDED_SEQ_NUM;
320 if (sa->use_anti_replay)
321 flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
323 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
324 if (sa->is_tunnel_ip6)
325 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
327 flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
329 return clib_host_to_net_u32 (flags);
332 static void vl_api_ipsec_sad_entry_add_del_t_handler
333 (vl_api_ipsec_sad_entry_add_del_t * mp)
335 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
336 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
337 ip46_address_t tun_src = { }, tun_dst =
340 ipsec_key_t crypto_key, integ_key;
341 ipsec_crypto_alg_t crypto_alg;
342 ipsec_integ_alg_t integ_alg;
343 ipsec_protocol_t proto;
344 ipsec_sa_flags_t flags;
345 u32 id, spi, sa_index = ~0;
350 id = ntohl (mp->entry.sad_id);
351 spi = ntohl (mp->entry.spi);
353 rv = ipsec_proto_decode (mp->entry.protocol, &proto);
358 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
363 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
368 ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
369 ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
371 flags = ipsec_sa_flags_decode (mp->entry.flags);
373 ip_address_decode (&mp->entry.tunnel_src, &tun_src);
374 ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
378 rv = ipsec_sa_add (id, spi, proto,
379 crypto_alg, &crypto_key,
380 integ_alg, &integ_key, flags,
381 0, &tun_src, &tun_dst, &sa_index);
383 rv = ipsec_sa_del (id);
386 rv = VNET_API_ERROR_UNIMPLEMENTED;
391 REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
393 rmp->stat_index = htonl (sa_index);
399 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
402 vl_api_ipsec_spds_details_t *mp;
405 mp = vl_msg_api_alloc (sizeof (*mp));
406 clib_memset (mp, 0, sizeof (*mp));
407 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
408 mp->context = context;
410 mp->spd_id = htonl (spd->id);
411 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
412 foreach_ipsec_spd_policy_type
414 mp->npolicies = htonl (n_policies);
416 vl_api_send_msg (reg, (u8 *) mp);
420 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
422 vl_api_registration_t *reg;
423 ipsec_main_t *im = &ipsec_main;
426 reg = vl_api_client_index_to_registration (mp->client_index);
431 pool_foreach (spd, im->spds, ({
432 send_ipsec_spds_details (spd, reg, mp->context);
436 clib_warning ("unimplemented");
440 vl_api_ipsec_spd_action_t
441 ipsec_spd_action_encode (ipsec_policy_action_t in)
443 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
447 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
448 out = IPSEC_API_SPD_ACTION_##f; \
450 foreach_ipsec_policy_action
453 return (clib_host_to_net_u32 (out));
457 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
460 vl_api_ipsec_spd_details_t *mp;
462 mp = vl_msg_api_alloc (sizeof (*mp));
463 clib_memset (mp, 0, sizeof (*mp));
464 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
465 mp->context = context;
467 mp->entry.spd_id = htonl (p->id);
468 mp->entry.priority = htonl (p->priority);
469 mp->entry.is_outbound = p->is_outbound;
471 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
472 &mp->entry.local_address_start);
473 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
474 &mp->entry.local_address_stop);
475 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
476 &mp->entry.remote_address_start);
477 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
478 &mp->entry.remote_address_stop);
479 mp->entry.local_port_start = htons (p->lport.start);
480 mp->entry.local_port_stop = htons (p->lport.stop);
481 mp->entry.remote_port_start = htons (p->rport.start);
482 mp->entry.remote_port_stop = htons (p->rport.stop);
483 mp->entry.protocol = p->protocol;
484 mp->entry.policy = ipsec_spd_action_encode (p->policy);
485 mp->entry.sa_id = htonl (p->sa_id);
487 vl_api_send_msg (reg, (u8 *) mp);
491 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
493 vl_api_registration_t *reg;
494 ipsec_main_t *im = &ipsec_main;
495 ipsec_spd_policy_t ptype;
496 ipsec_policy_t *policy;
501 reg = vl_api_client_index_to_registration (mp->client_index);
505 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
510 spd = pool_elt_at_index (im->spds, spd_index);
513 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
514 vec_foreach(ii, spd->policies[ptype])
516 policy = pool_elt_at_index(im->policies, *ii);
518 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
519 send_ipsec_spd_details (policy, reg, mp->context);
524 clib_warning ("unimplemented");
529 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
530 u32 sw_if_index, u32 context)
532 vl_api_ipsec_spd_interface_details_t *mp;
534 mp = vl_msg_api_alloc (sizeof (*mp));
535 clib_memset (mp, 0, sizeof (*mp));
536 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
537 mp->context = context;
539 mp->spd_index = htonl (spd_index);
540 mp->sw_if_index = htonl (sw_if_index);
542 vl_api_send_msg (reg, (u8 *) mp);
546 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
549 ipsec_main_t *im = &ipsec_main;
550 vl_api_registration_t *reg;
554 reg = vl_api_client_index_to_registration (mp->client_index);
558 if (mp->spd_index_valid)
560 spd_index = ntohl (mp->spd_index);
562 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
564 send_ipsec_spd_interface_details(reg, v, k, mp->context);
571 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
572 send_ipsec_spd_interface_details(reg, v, k, mp->context);
578 clib_warning ("unimplemented");
583 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
585 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
586 vl_api_ipsec_sa_set_key_reply_t *rmp;
592 id = ntohl (mp->sa_id);
594 ipsec_key_decode (&mp->crypto_key, &ck);
595 ipsec_key_decode (&mp->integrity_key, &ik);
597 rv = ipsec_set_sa_key (id, &ck, &ik);
599 rv = VNET_API_ERROR_UNIMPLEMENTED;
602 REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
606 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
609 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
610 ipsec_main_t *im = &ipsec_main;
611 vnet_main_t *vnm = im->vnet_main;
612 u32 sw_if_index = ~0;
616 ipsec_add_del_tunnel_args_t tun;
618 clib_memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
620 tun.is_add = mp->is_add;
622 tun.anti_replay = mp->anti_replay;
623 tun.local_spi = ntohl (mp->local_spi);
624 tun.remote_spi = ntohl (mp->remote_spi);
625 tun.crypto_alg = mp->crypto_alg;
626 tun.local_crypto_key_len = mp->local_crypto_key_len;
627 tun.remote_crypto_key_len = mp->remote_crypto_key_len;
628 tun.integ_alg = mp->integ_alg;
629 tun.local_integ_key_len = mp->local_integ_key_len;
630 tun.remote_integ_key_len = mp->remote_integ_key_len;
631 tun.udp_encap = mp->udp_encap;
632 tun.tx_table_id = ntohl (mp->tx_table_id);
633 memcpy (&tun.local_ip.ip4, mp->local_ip, 4);
634 memcpy (&tun.remote_ip.ip4, mp->remote_ip, 4);
635 memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
636 mp->local_crypto_key_len);
637 memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
638 mp->remote_crypto_key_len);
639 memcpy (&tun.local_integ_key, &mp->local_integ_key,
640 mp->local_integ_key_len);
641 memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
642 mp->remote_integ_key_len);
643 tun.renumber = mp->renumber;
644 tun.show_instance = ntohl (mp->show_instance);
646 rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
649 rv = VNET_API_ERROR_UNIMPLEMENTED;
653 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
655 rmp->sw_if_index = htonl (sw_if_index);
661 send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
662 u32 context, u32 sw_if_index)
664 vl_api_ipsec_sa_details_t *mp;
666 mp = vl_msg_api_alloc (sizeof (*mp));
667 clib_memset (mp, 0, sizeof (*mp));
668 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
669 mp->context = context;
671 mp->entry.sad_id = htonl (sa->id);
672 mp->entry.spi = htonl (sa->spi);
673 mp->entry.protocol = ipsec_proto_encode (sa->protocol);
674 mp->entry.tx_table_id =
675 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
677 mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
678 ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
680 mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
681 ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
683 mp->entry.flags = ipsec_sad_flags_encode (sa);
687 ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
688 &mp->entry.tunnel_src);
689 ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
690 &mp->entry.tunnel_dst);
693 mp->sw_if_index = htonl (sw_if_index);
694 mp->salt = clib_host_to_net_u32 (sa->salt);
695 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
696 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
699 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
700 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
702 if (sa->use_anti_replay)
703 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
705 vl_api_send_msg (reg, (u8 *) mp);
710 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
712 vl_api_registration_t *reg;
713 ipsec_main_t *im = &ipsec_main;
714 vnet_main_t *vnm = im->vnet_main;
716 ipsec_tunnel_if_t *t;
717 u32 *sa_index_to_tun_if_index = 0;
720 reg = vl_api_client_index_to_registration (mp->client_index);
721 if (!reg || pool_elts (im->sad) == 0)
724 vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
728 pool_foreach (t, im->tunnel_interfaces,
730 vnet_hw_interface_t *hi;
731 u32 sw_if_index = ~0;
733 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
734 sw_if_index = hi->sw_if_index;
735 sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
736 sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
739 pool_foreach (sa, im->sad,
741 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
742 send_ipsec_sa_details (sa, reg, mp->context,
743 sa_index_to_tun_if_index[sa - im->sad]);
747 vec_free (sa_index_to_tun_if_index);
749 clib_warning ("unimplemented");
755 vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t *
758 vl_api_ipsec_tunnel_if_set_key_reply_t *rmp;
759 ipsec_main_t *im = &ipsec_main;
760 vnet_main_t *vnm = im->vnet_main;
761 vnet_sw_interface_t *sw;
766 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
768 switch (mp->key_type)
770 case IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO:
771 case IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO:
772 if (mp->alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
773 mp->alg >= IPSEC_CRYPTO_N_ALG)
775 rv = VNET_API_ERROR_UNIMPLEMENTED;
779 case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG:
780 case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG:
781 if (mp->alg >= IPSEC_INTEG_N_ALG)
783 rv = VNET_API_ERROR_UNIMPLEMENTED;
787 case IPSEC_IF_SET_KEY_TYPE_NONE:
789 rv = VNET_API_ERROR_UNIMPLEMENTED;
794 key = vec_new (u8, mp->key_len);
795 clib_memcpy (key, mp->key, mp->key_len);
797 rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg,
801 clib_warning ("unimplemented");
805 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY);
810 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
812 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
813 ipsec_main_t *im = &ipsec_main;
814 vnet_main_t *vnm = im->vnet_main;
815 vnet_sw_interface_t *sw;
819 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
821 rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
824 clib_warning ("unimplemented");
827 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
831 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
833 vl_api_registration_t *rp;
834 ipsec_main_t *im = &ipsec_main;
835 u32 context = mp->context;
837 rp = vl_api_client_index_to_registration (mp->client_index);
841 clib_warning ("Client %d AWOL", mp->client_index);
845 ipsec_ah_backend_t *ab;
846 ipsec_esp_backend_t *eb;
848 pool_foreach (ab, im->ah_backends, {
849 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
850 clib_memset (mp, 0, sizeof (*mp));
851 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
852 mp->context = context;
853 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
855 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
856 mp->index = ab - im->ah_backends;
857 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
858 vl_api_send_msg (rp, (u8 *)mp);
860 pool_foreach (eb, im->esp_backends, {
861 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
862 clib_memset (mp, 0, sizeof (*mp));
863 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
864 mp->context = context;
865 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
867 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
868 mp->index = eb - im->esp_backends;
869 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
870 vl_api_send_msg (rp, (u8 *)mp);
876 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
878 ipsec_main_t *im = &ipsec_main;
879 vl_api_ipsec_select_backend_reply_t *rmp;
880 ipsec_protocol_t protocol;
882 if (pool_elts (im->sad) > 0)
884 rv = VNET_API_ERROR_INSTANCE_IN_USE;
888 rv = ipsec_proto_decode (mp->protocol, &protocol);
896 case IPSEC_PROTOCOL_ESP:
897 if (pool_is_free_index (im->esp_backends, mp->index))
899 rv = VNET_API_ERROR_INVALID_VALUE;
902 ipsec_select_esp_backend (im, mp->index);
904 case IPSEC_PROTOCOL_AH:
905 if (pool_is_free_index (im->ah_backends, mp->index))
907 rv = VNET_API_ERROR_INVALID_VALUE;
910 ipsec_select_ah_backend (im, mp->index);
913 rv = VNET_API_ERROR_INVALID_VALUE;
917 clib_warning ("unimplemented"); /* FIXME */
920 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
925 * Add vpe's API message handlers to the table.
926 * vlib has already mapped shared memory and
927 * added the client registration handlers.
928 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
930 #define vl_msg_name_crc_list
931 #include <vnet/vnet_all_api_h.h>
932 #undef vl_msg_name_crc_list
935 setup_message_id_table (api_main_t * am)
937 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
938 foreach_vl_msg_name_crc_ipsec;
942 static clib_error_t *
943 ipsec_api_hookup (vlib_main_t * vm)
945 api_main_t *am = &api_main;
948 vl_msg_api_set_handlers(VL_API_##N, #n, \
949 vl_api_##n##_t_handler, \
951 vl_api_##n##_t_endian, \
952 vl_api_##n##_t_print, \
953 sizeof(vl_api_##n##_t), 1);
958 * Set up the (msg_name, crc, message-id) table
960 setup_message_id_table (am);
965 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
968 * fd.io coding-style-patch-verification: ON
971 * eval: (c-set-style "gnu")