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>
22 #include <vnet/api_errno.h>
23 #include <vpp/app/version.h>
24 #include <vnet/ip/ip_types_api.h>
25 #include <ikev2/ikev2.h>
26 #include <ikev2/ikev2_priv.h>
28 /* define message IDs */
29 #include <vnet/format_fns.h>
30 #include <plugins/ikev2/ikev2.api_enum.h>
31 #include <plugins/ikev2/ikev2.api_types.h>
34 #define vl_endianfun /* define message structures */
35 #include <plugins/ikev2/ikev2_types.api.h>
38 extern ikev2_main_t ikev2_main;
40 #define IKEV2_PLUGIN_VERSION_MAJOR 1
41 #define IKEV2_PLUGIN_VERSION_MINOR 0
42 #define REPLY_MSG_ID_BASE ikev2_main.msg_id_base
43 #include <vlibapi/api_helper_macros.h>
46 cp_ike_transforms (vl_api_ikev2_ike_transforms_t * vl_api_ts,
47 ikev2_transforms_set * ts)
49 vl_api_ts->crypto_alg = ts->crypto_alg;
50 vl_api_ts->integ_alg = ts->integ_alg;
51 vl_api_ts->dh_group = ts->dh_type;
52 vl_api_ts->crypto_key_size = ts->crypto_key_size;
56 cp_esp_transforms (vl_api_ikev2_esp_transforms_t * vl_api_ts,
57 ikev2_transforms_set * ts)
59 vl_api_ts->crypto_alg = ts->crypto_alg;
60 vl_api_ts->integ_alg = ts->integ_alg;
61 vl_api_ts->crypto_key_size = ts->crypto_key_size;
65 cp_id (vl_api_ikev2_id_t * vl_api_id, ikev2_id_t * id)
71 vl_api_id->type = id->type;
72 size_data = sizeof (vl_api_id->data) - 1; // size without zero ending character
73 if (vec_len (id->data) < size_data)
74 size_data = vec_len (id->data);
76 vl_api_id->data_len = size_data;
77 clib_memcpy (vl_api_id->data, id->data, size_data);
81 cp_ts (vl_api_ikev2_ts_t * vl_api_ts, ikev2_ts_t * ts, u8 is_local)
83 vl_api_ts->is_local = is_local;
84 vl_api_ts->protocol_id = ts->protocol_id;
85 vl_api_ts->start_port = ts->start_port;
86 vl_api_ts->end_port = ts->end_port;
87 ip4_address_encode (&ts->start_addr, vl_api_ts->start_addr);
88 ip4_address_encode (&ts->end_addr, vl_api_ts->end_addr);
92 cp_auth (vl_api_ikev2_auth_t * vl_api_auth, ikev2_auth_t * auth)
94 vl_api_auth->method = auth->method;
95 vl_api_auth->data_len = vec_len (auth->data);
96 vl_api_auth->hex = auth->hex;
97 clib_memcpy (&vl_api_auth->data, auth->data, vec_len (auth->data));
101 cp_responder (vl_api_ikev2_responder_t * vl_api_responder,
102 ikev2_responder_t * responder)
104 vl_api_responder->sw_if_index = responder->sw_if_index;
105 ip4_address_encode (&responder->ip4, vl_api_responder->ip4);
109 send_profile (ikev2_profile_t * profile, vl_api_registration_t * reg,
112 vl_api_ikev2_profile_details_t *rmp = 0;
114 rmp = vl_msg_api_alloc (sizeof (*rmp) + vec_len (profile->auth.data));
115 clib_memset (rmp, 0, sizeof (*rmp) + vec_len (profile->auth.data));
116 ikev2_main_t *im = &ikev2_main;
117 rmp->_vl_msg_id = ntohs (VL_API_IKEV2_PROFILE_DETAILS + im->msg_id_base);
118 rmp->context = context;
120 int size_data = sizeof (rmp->profile.name) - 1;
121 if (vec_len (profile->name) < size_data)
122 size_data = vec_len (profile->name);
123 clib_memcpy (rmp->profile.name, profile->name, size_data);
125 cp_ike_transforms (&rmp->profile.ike_ts, &profile->ike_ts);
126 cp_esp_transforms (&rmp->profile.esp_ts, &profile->esp_ts);
128 cp_id (&rmp->profile.loc_id, &profile->loc_id);
129 cp_id (&rmp->profile.rem_id, &profile->rem_id);
131 cp_ts (&rmp->profile.rem_ts, &profile->rem_ts, 0 /* is_local */ );
132 cp_ts (&rmp->profile.loc_ts, &profile->loc_ts, 1 /* is_local */ );
134 cp_auth (&rmp->profile.auth, &profile->auth);
136 cp_responder (&rmp->profile.responder, &profile->responder);
138 rmp->profile.udp_encap = profile->udp_encap;
139 rmp->profile.tun_itf = profile->tun_itf;
141 rmp->profile.ipsec_over_udp_port = profile->ipsec_over_udp_port;
143 rmp->profile.lifetime = profile->lifetime;
144 rmp->profile.lifetime_maxdata = profile->lifetime_maxdata;
145 rmp->profile.lifetime_jitter = profile->lifetime_jitter;
146 rmp->profile.handover = profile->handover;
148 vl_api_ikev2_profile_t_endian (&rmp->profile);
150 vl_api_send_msg (reg, (u8 *) rmp);
154 vl_api_ikev2_profile_dump_t_handler (vl_api_ikev2_profile_dump_t * mp)
156 ikev2_main_t *im = &ikev2_main;
157 ikev2_profile_t *profile;
158 vl_api_registration_t *reg;
159 reg = vl_api_client_index_to_registration (mp->client_index);
164 pool_foreach (profile, im->profiles,
166 send_profile (profile, reg, mp->context);
172 vl_api_ikev2_plugin_get_version_t_handler (vl_api_ikev2_plugin_get_version_t *
175 ikev2_main_t *im = &ikev2_main;
176 vl_api_ikev2_plugin_get_version_reply_t *rmp;
177 int msg_size = sizeof (*rmp);
178 vl_api_registration_t *reg;
180 reg = vl_api_client_index_to_registration (mp->client_index);
184 rmp = vl_msg_api_alloc (msg_size);
185 clib_memset (rmp, 0, msg_size);
187 ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION_REPLY + im->msg_id_base);
188 rmp->context = mp->context;
189 rmp->major = htonl (IKEV2_PLUGIN_VERSION_MAJOR);
190 rmp->minor = htonl (IKEV2_PLUGIN_VERSION_MINOR);
192 vl_api_send_msg (reg, (u8 *) rmp);
196 vl_api_ikev2_profile_set_liveness_t_handler
197 (vl_api_ikev2_profile_set_liveness_t * mp)
199 vl_api_ikev2_profile_set_liveness_reply_t *rmp;
204 error = ikev2_set_liveness_params (clib_net_to_host_u32 (mp->period),
205 clib_net_to_host_u32 (mp->max_retries));
207 rv = VNET_API_ERROR_UNSPECIFIED;
209 rv = VNET_API_ERROR_UNIMPLEMENTED;
212 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_LIVENESS_REPLY);
216 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
218 vl_api_ikev2_profile_add_del_reply_t *rmp;
222 vlib_main_t *vm = vlib_get_main ();
224 u8 *tmp = format (0, "%s", mp->name);
225 error = ikev2_add_del_profile (vm, tmp, mp->is_add);
228 rv = VNET_API_ERROR_UNSPECIFIED;
230 rv = VNET_API_ERROR_UNIMPLEMENTED;
233 REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
237 vl_api_ikev2_profile_set_auth_t_handler
238 (vl_api_ikev2_profile_set_auth_t * mp)
240 vl_api_ikev2_profile_set_auth_reply_t *rmp;
244 vlib_main_t *vm = vlib_get_main ();
246 int data_len = ntohl (mp->data_len);
247 u8 *tmp = format (0, "%s", mp->name);
248 u8 *data = vec_new (u8, data_len);
249 clib_memcpy (data, mp->data, data_len);
250 error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
254 rv = VNET_API_ERROR_UNSPECIFIED;
256 rv = VNET_API_ERROR_UNIMPLEMENTED;
259 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
263 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
265 vl_api_ikev2_profile_set_id_reply_t *rmp;
269 vlib_main_t *vm = vlib_get_main ();
271 u8 *tmp = format (0, "%s", mp->name);
272 int data_len = ntohl (mp->data_len);
273 u8 *data = vec_new (u8, data_len);
274 clib_memcpy (data, mp->data, data_len);
275 error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
279 rv = VNET_API_ERROR_UNSPECIFIED;
281 rv = VNET_API_ERROR_UNIMPLEMENTED;
284 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
288 vl_api_ikev2_profile_set_udp_encap_t_handler
289 (vl_api_ikev2_profile_set_udp_encap_t * mp)
291 vl_api_ikev2_profile_set_udp_encap_reply_t *rmp;
295 vlib_main_t *vm = vlib_get_main ();
297 u8 *tmp = format (0, "%s", mp->name);
298 error = ikev2_set_profile_udp_encap (vm, tmp);
301 rv = VNET_API_ERROR_UNSPECIFIED;
303 rv = VNET_API_ERROR_UNIMPLEMENTED;
306 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_UDP_ENCAP_REPLY);
310 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
312 vl_api_ikev2_profile_set_ts_reply_t *rmp;
316 vlib_main_t *vm = vlib_get_main ();
318 u8 *tmp = format (0, "%s", mp->name);
319 ip4_address_t start_addr, end_addr;
320 ip4_address_decode (mp->ts.start_addr, &start_addr);
321 ip4_address_decode (mp->ts.end_addr, &end_addr);
323 ikev2_set_profile_ts (vm, tmp, mp->ts.protocol_id,
324 clib_net_to_host_u16 (mp->ts.start_port),
325 clib_net_to_host_u16 (mp->ts.end_port),
326 start_addr, end_addr, mp->ts.is_local);
329 rv = VNET_API_ERROR_UNSPECIFIED;
331 rv = VNET_API_ERROR_UNIMPLEMENTED;
334 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
338 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
340 vl_api_ikev2_set_local_key_reply_t *rmp;
344 vlib_main_t *vm = vlib_get_main ();
347 error = ikev2_set_local_key (vm, mp->key_file);
349 rv = VNET_API_ERROR_UNSPECIFIED;
351 rv = VNET_API_ERROR_UNIMPLEMENTED;
354 REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
358 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
360 vl_api_ikev2_set_responder_reply_t *rmp;
364 vlib_main_t *vm = vlib_get_main ();
367 u8 *tmp = format (0, "%s", mp->name);
369 ip4_address_decode (mp->responder.ip4, &ip4);
370 u32 sw_if_index = clib_net_to_host_u32 (mp->responder.sw_if_index);
372 error = ikev2_set_profile_responder (vm, tmp, sw_if_index, ip4);
375 rv = VNET_API_ERROR_UNSPECIFIED;
377 rv = VNET_API_ERROR_UNIMPLEMENTED;
380 REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
384 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
387 vl_api_ikev2_set_ike_transforms_reply_t *rmp;
391 vlib_main_t *vm = vlib_get_main ();
394 u8 *tmp = format (0, "%s", mp->name);
397 ikev2_set_profile_ike_transforms (vm, tmp, mp->tr.crypto_alg,
400 ntohl (mp->tr.crypto_key_size));
403 rv = VNET_API_ERROR_UNSPECIFIED;
405 rv = VNET_API_ERROR_UNIMPLEMENTED;
408 REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
412 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
415 vl_api_ikev2_set_esp_transforms_reply_t *rmp;
419 vlib_main_t *vm = vlib_get_main ();
422 u8 *tmp = format (0, "%s", mp->name);
425 ikev2_set_profile_esp_transforms (vm, tmp, mp->tr.crypto_alg,
427 ntohl (mp->tr.crypto_key_size));
430 rv = VNET_API_ERROR_UNSPECIFIED;
432 rv = VNET_API_ERROR_UNIMPLEMENTED;
435 REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
439 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
441 vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
445 vlib_main_t *vm = vlib_get_main ();
448 u8 *tmp = format (0, "%s", mp->name);
451 ikev2_set_profile_sa_lifetime (vm, tmp,
452 clib_net_to_host_u64 (mp->lifetime),
453 ntohl (mp->lifetime_jitter),
454 ntohl (mp->handover),
456 (mp->lifetime_maxdata));
459 rv = VNET_API_ERROR_UNSPECIFIED;
461 rv = VNET_API_ERROR_UNIMPLEMENTED;
464 REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
468 vl_api_ikev2_profile_set_ipsec_udp_port_t_handler
469 (vl_api_ikev2_profile_set_ipsec_udp_port_t * mp)
471 vl_api_ikev2_profile_set_ipsec_udp_port_reply_t *rmp;
475 vlib_main_t *vm = vlib_get_main ();
477 u8 *tmp = format (0, "%s", mp->name);
480 ikev2_set_profile_ipsec_udp_port (vm, tmp,
481 clib_net_to_host_u16 (mp->port),
485 rv = VNET_API_ERROR_UNIMPLEMENTED;
488 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_IPSEC_UDP_PORT_REPLY);
492 vl_api_ikev2_set_tunnel_interface_t_handler
493 (vl_api_ikev2_set_tunnel_interface_t * mp)
495 vl_api_ikev2_set_tunnel_interface_reply_t *rmp;
498 VALIDATE_SW_IF_INDEX (mp);
501 u8 *tmp = format (0, "%s", mp->name);
504 error = ikev2_set_profile_tunnel_interface (vlib_get_main (), tmp,
505 ntohl (mp->sw_if_index));
508 rv = VNET_API_ERROR_UNSPECIFIED;
511 rv = VNET_API_ERROR_UNIMPLEMENTED;
514 BAD_SW_IF_INDEX_LABEL;
515 REPLY_MACRO (VL_API_IKEV2_SET_TUNNEL_INTERFACE_REPLY);
519 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
521 vl_api_ikev2_initiate_sa_init_reply_t *rmp;
525 vlib_main_t *vm = vlib_get_main ();
528 u8 *tmp = format (0, "%s", mp->name);
530 error = ikev2_initiate_sa_init (vm, tmp);
533 rv = VNET_API_ERROR_UNSPECIFIED;
535 rv = VNET_API_ERROR_UNIMPLEMENTED;
538 REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
542 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
545 vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
549 vlib_main_t *vm = vlib_get_main ();
552 error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
554 rv = VNET_API_ERROR_UNSPECIFIED;
556 rv = VNET_API_ERROR_UNIMPLEMENTED;
559 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
563 vl_api_ikev2_initiate_del_child_sa_t_handler
564 (vl_api_ikev2_initiate_del_child_sa_t * mp)
566 vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
570 vlib_main_t *vm = vlib_get_main ();
573 error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
575 rv = VNET_API_ERROR_UNSPECIFIED;
577 rv = VNET_API_ERROR_UNIMPLEMENTED;
580 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
584 vl_api_ikev2_initiate_rekey_child_sa_t_handler
585 (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
587 vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
591 vlib_main_t *vm = vlib_get_main ();
594 error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
596 rv = VNET_API_ERROR_UNSPECIFIED;
598 rv = VNET_API_ERROR_UNIMPLEMENTED;
601 REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
604 #include <ikev2/ikev2.api.c>
605 static clib_error_t *
606 ikev2_api_init (vlib_main_t * vm)
608 ikev2_main_t *im = &ikev2_main;
610 /* Ask for a correctly-sized block of API message decode slots */
611 im->msg_id_base = setup_message_id_table ();
616 VLIB_INIT_FUNCTION (ikev2_api_init);
619 * fd.io coding-style-patch-verification: ON
622 * eval: (c-set-style "gnu")