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>
33 #include <vnet/ipsec/ikev2.h>
36 #define vl_typedefs /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
40 #define vl_endianfun /* define message structures */
41 #include <vnet/vnet_all_api_h.h>
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #include <vnet/vnet_all_api_h.h>
50 #include <vlibapi/api_helper_macros.h>
52 #define foreach_vpe_api_msg \
53 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
54 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
55 _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del) \
56 _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del) \
57 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key) \
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_KEY, ipsec_tunnel_if_set_key) \
64 _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa) \
65 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del) \
66 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth) \
67 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id) \
68 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts) \
69 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key) \
70 _(IKEV2_SET_RESPONDER, ikev2_set_responder) \
71 _(IKEV2_SET_IKE_TRANSFORMS, ikev2_set_ike_transforms) \
72 _(IKEV2_SET_ESP_TRANSFORMS, ikev2_set_esp_transforms) \
73 _(IKEV2_SET_SA_LIFETIME, ikev2_set_sa_lifetime) \
74 _(IKEV2_INITIATE_SA_INIT, ikev2_initiate_sa_init) \
75 _(IKEV2_INITIATE_DEL_IKE_SA, ikev2_initiate_del_ike_sa) \
76 _(IKEV2_INITIATE_DEL_CHILD_SA, ikev2_initiate_del_child_sa) \
77 _(IKEV2_INITIATE_REKEY_CHILD_SA, ikev2_initiate_rekey_child_sa) \
78 _(IPSEC_SELECT_BACKEND, ipsec_select_backend) \
79 _(IPSEC_BACKEND_DUMP, ipsec_backend_dump)
82 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
85 clib_warning ("unimplemented");
88 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
89 vl_api_ipsec_spd_add_del_reply_t *rmp;
92 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
94 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
98 static void vl_api_ipsec_interface_add_del_spd_t_handler
99 (vl_api_ipsec_interface_add_del_spd_t * mp)
101 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
102 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
104 u32 sw_if_index __attribute__ ((unused));
105 u32 spd_id __attribute__ ((unused));
107 sw_if_index = ntohl (mp->sw_if_index);
108 spd_id = ntohl (mp->spd_id);
110 VALIDATE_SW_IF_INDEX (mp);
113 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
115 rv = VNET_API_ERROR_UNIMPLEMENTED;
118 BAD_SW_IF_INDEX_LABEL;
120 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
124 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
125 ipsec_policy_action_t * out)
127 in = clib_net_to_host_u32 (in);
131 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
132 *out = IPSEC_POLICY_ACTION_##f; \
134 foreach_ipsec_policy_action
137 return (VNET_API_ERROR_UNIMPLEMENTED);
140 static void vl_api_ipsec_spd_entry_add_del_t_handler
141 (vl_api_ipsec_spd_entry_add_del_t * mp)
143 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
144 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
152 clib_memset (&p, 0, sizeof (p));
154 p.id = ntohl (mp->entry.spd_id);
155 p.priority = ntohl (mp->entry.priority);
156 p.is_outbound = mp->entry.is_outbound;
158 itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
159 ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
160 ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
161 ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
163 p.is_ipv6 = (itype == IP46_TYPE_IP6);
165 p.protocol = mp->entry.protocol;
166 p.rport.start = ntohs (mp->entry.remote_port_start);
167 p.rport.stop = ntohs (mp->entry.remote_port_stop);
168 p.lport.start = ntohs (mp->entry.local_port_start);
169 p.lport.stop = ntohs (mp->entry.local_port_stop);
171 rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
176 /* policy action resolve unsupported */
177 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
179 clib_warning ("unsupported action: 'resolve'");
180 rv = VNET_API_ERROR_UNIMPLEMENTED;
183 p.sa_id = ntohl (mp->entry.sa_id);
185 rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
190 rv = VNET_API_ERROR_UNIMPLEMENTED;
196 REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
198 rmp->stat_index = ntohl(stat_index);
204 ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
206 in = clib_net_to_host_u32 (in);
210 case IPSEC_API_PROTO_ESP:
211 *out = IPSEC_PROTOCOL_ESP;
213 case IPSEC_API_PROTO_AH:
214 *out = IPSEC_PROTOCOL_AH;
217 return (VNET_API_ERROR_UNIMPLEMENTED);
221 ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
222 ipsec_crypto_alg_t * out)
224 in = clib_net_to_host_u32 (in);
228 #define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
229 *out = IPSEC_CRYPTO_ALG_##f; \
231 foreach_ipsec_crypto_alg
234 return (VNET_API_ERROR_UNIMPLEMENTED);
238 ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
240 in = clib_net_to_host_u32 (in);
244 #define _(v,f,s) case IPSEC_API_INTEG_ALG_##f: \
245 *out = IPSEC_INTEG_ALG_##f; \
247 foreach_ipsec_integ_alg
250 return (VNET_API_ERROR_UNIMPLEMENTED);
254 vl_api_ipsec_key_decode (const vl_api_key_t * key, u8 * len, u8 out[128])
257 clib_memcpy (out, key->data, key->length);
261 vl_api_ipsec_sad_flags_decode (vl_api_ipsec_sad_flags_t in, ipsec_sa_t * sa)
263 in = clib_net_to_host_u32 (in);
265 if (in & IPSEC_API_SAD_FLAG_USE_EXTENDED_SEQ_NUM)
267 if (in & IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY)
268 sa->use_anti_replay = 1;
269 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL)
271 if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL_V6)
272 sa->is_tunnel_ip6 = 1;
273 if (in & IPSEC_API_SAD_FLAG_UDP_ENCAP)
278 static void vl_api_ipsec_sad_entry_add_del_t_handler
279 (vl_api_ipsec_sad_entry_add_del_t * mp)
281 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
282 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
285 ipsec_main_t *im = &ipsec_main;
288 clib_memset (&sa, 0, sizeof (sa));
290 sa.id = ntohl (mp->entry.sad_id);
291 sa.spi = ntohl (mp->entry.spi);
293 rv = ipsec_proto_decode (mp->entry.protocol, &sa.protocol);
298 rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &sa.crypto_alg);
303 rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &sa.integ_alg);
308 vl_api_ipsec_key_decode (&mp->entry.crypto_key,
309 &sa.crypto_key_len, sa.crypto_key);
310 vl_api_ipsec_key_decode (&mp->entry.integrity_key,
311 &sa.integ_key_len, sa.integ_key);
313 vl_api_ipsec_sad_flags_decode (mp->entry.flags, &sa);
315 if (sa.is_tunnel_ip6 || sa.is_tunnel)
317 ip_address_decode (&mp->entry.tunnel_src, &sa.tunnel_src_addr);
318 ip_address_decode (&mp->entry.tunnel_dst, &sa.tunnel_dst_addr);
321 clib_error_t *err = ipsec_check_support_cb (im, &sa);
324 clib_warning ("%s", err->what);
325 rv = VNET_API_ERROR_UNIMPLEMENTED;
329 rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
331 rv = VNET_API_ERROR_UNIMPLEMENTED;
336 REPLY_MACRO (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY);
340 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
343 vl_api_ipsec_spds_details_t *mp;
346 mp = vl_msg_api_alloc (sizeof (*mp));
347 clib_memset (mp, 0, sizeof (*mp));
348 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
349 mp->context = context;
351 mp->spd_id = htonl (spd->id);
352 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
353 foreach_ipsec_spd_policy_type
355 mp->npolicies = htonl (n_policies);
357 vl_api_send_msg (reg, (u8 *) mp);
361 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
363 vl_api_registration_t *reg;
364 ipsec_main_t *im = &ipsec_main;
367 reg = vl_api_client_index_to_registration (mp->client_index);
372 pool_foreach (spd, im->spds, ({
373 send_ipsec_spds_details (spd, reg, mp->context);
377 clib_warning ("unimplemented");
381 vl_api_ipsec_spd_action_t
382 ipsec_spd_action_encode (ipsec_policy_action_t in)
384 vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
388 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
389 out = IPSEC_API_SPD_ACTION_##f; \
391 foreach_ipsec_policy_action
394 return (clib_host_to_net_u32 (out));
398 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
401 vl_api_ipsec_spd_details_t *mp;
403 mp = vl_msg_api_alloc (sizeof (*mp));
404 clib_memset (mp, 0, sizeof (*mp));
405 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
406 mp->context = context;
408 mp->entry.spd_id = htonl (p->id);
409 mp->entry.priority = htonl (p->priority);
410 mp->entry.is_outbound = p->is_outbound;
412 ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
413 &mp->entry.local_address_start);
414 ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
415 &mp->entry.local_address_stop);
416 ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
417 &mp->entry.remote_address_start);
418 ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
419 &mp->entry.remote_address_stop);
420 mp->entry.local_port_start = htons (p->lport.start);
421 mp->entry.local_port_stop = htons (p->lport.stop);
422 mp->entry.remote_port_start = htons (p->rport.start);
423 mp->entry.remote_port_stop = htons (p->rport.stop);
424 mp->entry.protocol = p->protocol;
425 mp->entry.policy = ipsec_spd_action_encode (p->policy);
426 mp->entry.sa_id = htonl (p->sa_id);
428 vl_api_send_msg (reg, (u8 *) mp);
432 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
434 vl_api_registration_t *reg;
435 ipsec_main_t *im = &ipsec_main;
436 ipsec_spd_policy_t ptype;
437 ipsec_policy_t *policy;
442 reg = vl_api_client_index_to_registration (mp->client_index);
446 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
451 spd = pool_elt_at_index (im->spds, spd_index);
454 FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
455 vec_foreach(ii, spd->policies[ptype])
457 policy = pool_elt_at_index(im->policies, *ii);
459 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
460 send_ipsec_spd_details (policy, reg, mp->context);
465 clib_warning ("unimplemented");
470 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
471 u32 sw_if_index, u32 context)
473 vl_api_ipsec_spd_interface_details_t *mp;
475 mp = vl_msg_api_alloc (sizeof (*mp));
476 clib_memset (mp, 0, sizeof (*mp));
477 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
478 mp->context = context;
480 mp->spd_index = htonl (spd_index);
481 mp->sw_if_index = htonl (sw_if_index);
483 vl_api_send_msg (reg, (u8 *) mp);
487 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
490 ipsec_main_t *im = &ipsec_main;
491 vl_api_registration_t *reg;
495 reg = vl_api_client_index_to_registration (mp->client_index);
499 if (mp->spd_index_valid)
501 spd_index = ntohl (mp->spd_index);
503 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
505 send_ipsec_spd_interface_details(reg, v, k, mp->context);
512 hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
513 send_ipsec_spd_interface_details(reg, v, k, mp->context);
519 clib_warning ("unimplemented");
524 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
526 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
527 vl_api_ipsec_sa_set_key_reply_t *rmp;
531 sa.id = ntohl (mp->sa_id);
533 vl_api_ipsec_key_decode (&mp->crypto_key,
534 &sa.crypto_key_len, sa.crypto_key);
535 vl_api_ipsec_key_decode (&mp->integrity_key,
536 &sa.integ_key_len, sa.integ_key);
538 rv = ipsec_set_sa_key (vm, &sa);
540 rv = VNET_API_ERROR_UNIMPLEMENTED;
543 REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
547 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
550 vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
551 ipsec_main_t *im = &ipsec_main;
552 vnet_main_t *vnm = im->vnet_main;
553 u32 sw_if_index = ~0;
557 ipsec_add_del_tunnel_args_t tun;
559 clib_memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
561 tun.is_add = mp->is_add;
563 tun.anti_replay = mp->anti_replay;
564 tun.local_spi = ntohl (mp->local_spi);
565 tun.remote_spi = ntohl (mp->remote_spi);
566 tun.crypto_alg = mp->crypto_alg;
567 tun.local_crypto_key_len = mp->local_crypto_key_len;
568 tun.remote_crypto_key_len = mp->remote_crypto_key_len;
569 tun.integ_alg = mp->integ_alg;
570 tun.local_integ_key_len = mp->local_integ_key_len;
571 tun.remote_integ_key_len = mp->remote_integ_key_len;
572 tun.udp_encap = mp->udp_encap;
573 tun.tx_table_id = ntohl (mp->tx_table_id);
574 memcpy (&tun.local_ip, mp->local_ip, 4);
575 memcpy (&tun.remote_ip, mp->remote_ip, 4);
576 memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
577 mp->local_crypto_key_len);
578 memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
579 mp->remote_crypto_key_len);
580 memcpy (&tun.local_integ_key, &mp->local_integ_key,
581 mp->local_integ_key_len);
582 memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
583 mp->remote_integ_key_len);
584 tun.renumber = mp->renumber;
585 tun.show_instance = ntohl (mp->show_instance);
587 rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
590 rv = VNET_API_ERROR_UNIMPLEMENTED;
593 REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY, (
601 send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
602 u32 context, u32 sw_if_index)
604 vl_api_ipsec_sa_details_t *mp;
606 mp = vl_msg_api_alloc (sizeof (*mp));
607 clib_memset (mp, 0, sizeof (*mp));
608 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
609 mp->context = context;
611 mp->sa_id = htonl (sa->id);
612 mp->sw_if_index = htonl (sw_if_index);
614 mp->spi = htonl (sa->spi);
615 mp->protocol = sa->protocol;
617 mp->crypto_alg = sa->crypto_alg;
618 mp->crypto_key_len = sa->crypto_key_len;
619 memcpy (mp->crypto_key, sa->crypto_key, sa->crypto_key_len);
621 mp->integ_alg = sa->integ_alg;
622 mp->integ_key_len = sa->integ_key_len;
623 memcpy (mp->integ_key, sa->integ_key, sa->integ_key_len);
625 mp->use_esn = sa->use_esn;
626 mp->use_anti_replay = sa->use_anti_replay;
628 mp->is_tunnel = sa->is_tunnel;
629 mp->is_tunnel_ip6 = sa->is_tunnel_ip6;
633 if (sa->is_tunnel_ip6)
635 memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip6, 16);
636 memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip6, 16);
640 memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip4, 4);
641 memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip4, 4);
645 mp->salt = clib_host_to_net_u32 (sa->salt);
646 mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
647 mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
650 mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
651 mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
653 if (sa->use_anti_replay)
654 mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
655 mp->total_data_size = clib_host_to_net_u64 (sa->total_data_size);
656 mp->udp_encap = sa->udp_encap;
659 htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
661 vl_api_send_msg (reg, (u8 *) mp);
666 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
668 vl_api_registration_t *reg;
669 ipsec_main_t *im = &ipsec_main;
670 vnet_main_t *vnm = im->vnet_main;
672 ipsec_tunnel_if_t *t;
673 u32 *sa_index_to_tun_if_index = 0;
676 reg = vl_api_client_index_to_registration (mp->client_index);
677 if (!reg || pool_elts (im->sad) == 0)
680 vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
684 pool_foreach (t, im->tunnel_interfaces,
686 vnet_hw_interface_t *hi;
687 u32 sw_if_index = ~0;
689 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
690 sw_if_index = hi->sw_if_index;
691 sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
692 sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
695 pool_foreach (sa, im->sad,
697 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
698 send_ipsec_sa_details (sa, reg, mp->context,
699 sa_index_to_tun_if_index[sa - im->sad]);
703 vec_free (sa_index_to_tun_if_index);
705 clib_warning ("unimplemented");
711 vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t *
714 vl_api_ipsec_tunnel_if_set_key_reply_t *rmp;
715 ipsec_main_t *im = &ipsec_main;
716 vnet_main_t *vnm = im->vnet_main;
717 vnet_sw_interface_t *sw;
722 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
724 switch (mp->key_type)
726 case IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO:
727 case IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO:
728 if (mp->alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
729 mp->alg >= IPSEC_CRYPTO_N_ALG)
731 rv = VNET_API_ERROR_UNIMPLEMENTED;
735 case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG:
736 case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG:
737 if (mp->alg >= IPSEC_INTEG_N_ALG)
739 rv = VNET_API_ERROR_UNIMPLEMENTED;
743 case IPSEC_IF_SET_KEY_TYPE_NONE:
745 rv = VNET_API_ERROR_UNIMPLEMENTED;
750 key = vec_new (u8, mp->key_len);
751 clib_memcpy (key, mp->key, mp->key_len);
753 rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg,
757 clib_warning ("unimplemented");
761 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY);
766 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
768 vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
769 ipsec_main_t *im = &ipsec_main;
770 vnet_main_t *vnm = im->vnet_main;
771 vnet_sw_interface_t *sw;
775 sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
777 rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
780 clib_warning ("unimplemented");
783 REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
788 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
790 vl_api_ikev2_profile_add_del_reply_t *rmp;
794 vlib_main_t *vm = vlib_get_main ();
796 u8 *tmp = format (0, "%s", mp->name);
797 error = ikev2_add_del_profile (vm, tmp, mp->is_add);
800 rv = VNET_API_ERROR_UNSPECIFIED;
802 rv = VNET_API_ERROR_UNIMPLEMENTED;
805 REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
809 vl_api_ikev2_profile_set_auth_t_handler
810 (vl_api_ikev2_profile_set_auth_t * mp)
812 vl_api_ikev2_profile_set_auth_reply_t *rmp;
816 vlib_main_t *vm = vlib_get_main ();
818 int data_len = ntohl (mp->data_len);
819 u8 *tmp = format (0, "%s", mp->name);
820 u8 *data = vec_new (u8, data_len);
821 clib_memcpy (data, mp->data, data_len);
822 error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
826 rv = VNET_API_ERROR_UNSPECIFIED;
828 rv = VNET_API_ERROR_UNIMPLEMENTED;
831 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
835 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
837 vl_api_ikev2_profile_add_del_reply_t *rmp;
841 vlib_main_t *vm = vlib_get_main ();
843 u8 *tmp = format (0, "%s", mp->name);
844 int data_len = ntohl (mp->data_len);
845 u8 *data = vec_new (u8, data_len);
846 clib_memcpy (data, mp->data, data_len);
847 error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
851 rv = VNET_API_ERROR_UNSPECIFIED;
853 rv = VNET_API_ERROR_UNIMPLEMENTED;
856 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
860 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
862 vl_api_ikev2_profile_set_ts_reply_t *rmp;
866 vlib_main_t *vm = vlib_get_main ();
868 u8 *tmp = format (0, "%s", mp->name);
869 error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
870 mp->end_port, (ip4_address_t) mp->start_addr,
871 (ip4_address_t) mp->end_addr, mp->is_local);
874 rv = VNET_API_ERROR_UNSPECIFIED;
876 rv = VNET_API_ERROR_UNIMPLEMENTED;
879 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
883 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
885 vl_api_ikev2_profile_set_ts_reply_t *rmp;
889 vlib_main_t *vm = vlib_get_main ();
892 error = ikev2_set_local_key (vm, mp->key_file);
894 rv = VNET_API_ERROR_UNSPECIFIED;
896 rv = VNET_API_ERROR_UNIMPLEMENTED;
899 REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
903 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
905 vl_api_ikev2_set_responder_reply_t *rmp;
909 vlib_main_t *vm = vlib_get_main ();
912 u8 *tmp = format (0, "%s", mp->name);
914 clib_memcpy (&ip4, mp->address, sizeof (ip4));
916 error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
919 rv = VNET_API_ERROR_UNSPECIFIED;
921 rv = VNET_API_ERROR_UNIMPLEMENTED;
924 REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
928 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
931 vl_api_ikev2_set_ike_transforms_reply_t *rmp;
935 vlib_main_t *vm = vlib_get_main ();
938 u8 *tmp = format (0, "%s", mp->name);
941 ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
942 mp->dh_group, mp->crypto_key_size);
945 rv = VNET_API_ERROR_UNSPECIFIED;
947 rv = VNET_API_ERROR_UNIMPLEMENTED;
950 REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
954 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
957 vl_api_ikev2_set_esp_transforms_reply_t *rmp;
961 vlib_main_t *vm = vlib_get_main ();
964 u8 *tmp = format (0, "%s", mp->name);
967 ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
968 mp->dh_group, mp->crypto_key_size);
971 rv = VNET_API_ERROR_UNSPECIFIED;
973 rv = VNET_API_ERROR_UNIMPLEMENTED;
976 REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
980 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
982 vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
986 vlib_main_t *vm = vlib_get_main ();
989 u8 *tmp = format (0, "%s", mp->name);
992 ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
993 mp->handover, mp->lifetime_maxdata);
996 rv = VNET_API_ERROR_UNSPECIFIED;
998 rv = VNET_API_ERROR_UNIMPLEMENTED;
1001 REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
1005 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
1007 vl_api_ikev2_initiate_sa_init_reply_t *rmp;
1011 vlib_main_t *vm = vlib_get_main ();
1012 clib_error_t *error;
1014 u8 *tmp = format (0, "%s", mp->name);
1016 error = ikev2_initiate_sa_init (vm, tmp);
1019 rv = VNET_API_ERROR_UNSPECIFIED;
1021 rv = VNET_API_ERROR_UNIMPLEMENTED;
1024 REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
1028 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
1031 vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
1035 vlib_main_t *vm = vlib_get_main ();
1036 clib_error_t *error;
1038 error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
1040 rv = VNET_API_ERROR_UNSPECIFIED;
1042 rv = VNET_API_ERROR_UNIMPLEMENTED;
1045 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
1049 vl_api_ikev2_initiate_del_child_sa_t_handler
1050 (vl_api_ikev2_initiate_del_child_sa_t * mp)
1052 vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
1056 vlib_main_t *vm = vlib_get_main ();
1057 clib_error_t *error;
1059 error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
1061 rv = VNET_API_ERROR_UNSPECIFIED;
1063 rv = VNET_API_ERROR_UNIMPLEMENTED;
1066 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
1070 vl_api_ikev2_initiate_rekey_child_sa_t_handler
1071 (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
1073 vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
1077 vlib_main_t *vm = vlib_get_main ();
1078 clib_error_t *error;
1080 error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
1082 rv = VNET_API_ERROR_UNSPECIFIED;
1084 rv = VNET_API_ERROR_UNIMPLEMENTED;
1087 REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
1092 * Add vpe's API message handlers to the table.
1093 * vlib has already mapped shared memory and
1094 * added the client registration handlers.
1095 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1097 #define vl_msg_name_crc_list
1098 #include <vnet/vnet_all_api_h.h>
1099 #undef vl_msg_name_crc_list
1102 setup_message_id_table (api_main_t * am)
1104 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1105 foreach_vl_msg_name_crc_ipsec;
1110 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
1112 vl_api_registration_t *rp;
1113 ipsec_main_t *im = &ipsec_main;
1114 u32 context = mp->context;
1116 rp = vl_api_client_index_to_registration (mp->client_index);
1120 clib_warning ("Client %d AWOL", mp->client_index);
1124 ipsec_ah_backend_t *ab;
1125 ipsec_esp_backend_t *eb;
1127 pool_foreach (ab, im->ah_backends, {
1128 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1129 clib_memset (mp, 0, sizeof (*mp));
1130 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1131 mp->context = context;
1132 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1134 mp->protocol = ntohl (IPSEC_API_PROTO_AH);
1135 mp->index = ab - im->ah_backends;
1136 mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1137 vl_api_send_msg (rp, (u8 *)mp);
1139 pool_foreach (eb, im->esp_backends, {
1140 vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1141 clib_memset (mp, 0, sizeof (*mp));
1142 mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1143 mp->context = context;
1144 snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1146 mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
1147 mp->index = eb - im->esp_backends;
1148 mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1149 vl_api_send_msg (rp, (u8 *)mp);
1155 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1157 ipsec_main_t *im = &ipsec_main;
1158 vl_api_ipsec_select_backend_reply_t *rmp;
1159 ipsec_protocol_t protocol;
1161 if (pool_elts (im->sad) > 0)
1163 rv = VNET_API_ERROR_INSTANCE_IN_USE;
1167 rv = ipsec_proto_decode (mp->protocol, &protocol);
1175 case IPSEC_PROTOCOL_ESP:
1176 if (pool_is_free_index (im->esp_backends, mp->index))
1178 rv = VNET_API_ERROR_INVALID_VALUE;
1181 ipsec_select_esp_backend (im, mp->index);
1183 case IPSEC_PROTOCOL_AH:
1184 if (pool_is_free_index (im->ah_backends, mp->index))
1186 rv = VNET_API_ERROR_INVALID_VALUE;
1189 ipsec_select_ah_backend (im, mp->index);
1192 rv = VNET_API_ERROR_INVALID_VALUE;
1196 clib_warning ("unimplemented"); /* FIXME */
1199 REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1202 static clib_error_t *
1203 ipsec_api_hookup (vlib_main_t * vm)
1205 api_main_t *am = &api_main;
1208 vl_msg_api_set_handlers(VL_API_##N, #n, \
1209 vl_api_##n##_t_handler, \
1211 vl_api_##n##_t_endian, \
1212 vl_api_##n##_t_print, \
1213 sizeof(vl_api_##n##_t), 1);
1214 foreach_vpe_api_msg;
1218 * Set up the (msg_name, crc, message-id) table
1220 setup_message_id_table (am);
1225 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1228 * fd.io coding-style-patch-verification: ON
1231 * eval: (c-set-style "gnu")